Class: Idevice::AFCClient

Inherits:
C::ManagedOpaquePointer show all
Includes:
LibHelpers
Defined in:
lib/idevice/afc.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LibHelpers

included

Methods inherited from C::ManagedOpaquePointer

#initialize

Constructor Details

This class inherits a constructor from Idevice::C::ManagedOpaquePointer

Class Method Details

.attach(opts = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/idevice/afc.rb', line 44

def self.attach(opts={})
  if opts[:appid]
    return HouseArrestClient.attach(opts).afc_client_for_container(opts[:appid])

  else
    identifier =
      if opts[:root]
        "com.apple.afc2"
      elsif opts[:afc_identifier]
        opts[:afc_identifier]
      else
        "com.apple.afc"
      end

    _attach_helper(identifier, opts) do |idevice, ldsvc, p_afc|
      err = C.afc_client_new(idevice, ldsvc, p_afc)
      raise AFCError, "AFC error: #{err}" if err != :SUCCESS

      afc = p_afc.read_pointer
      raise AFCError, "afc_client_new returned a NULL afc_client_t pointer" if afc.null?
      return new(afc)
    end
  end
end

.release(ptr) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/idevice/afc.rb', line 36

def self.release(ptr)
  C::Freelock.synchronize do
    unless ptr.null?
      C.afc_client_free(ptr)
    end
  end
end

Instance Method Details

#cat(path, chunksz = nil, &block) ⇒ Object



174
175
176
# File 'lib/idevice/afc.rb', line 174

def cat(path, chunksz=nil, &block)
  AFCFile.open(self, path, 'r') { |f| return f.read_all(chunksz, &block) }
end

#ctime(path) ⇒ Object



188
189
190
191
# File 'lib/idevice/afc.rb', line 188

def ctime(path)
  info = file_info(path)
  return info[:st_birthtime]
end

#device_info(key = nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/idevice/afc.rb', line 69

def device_info(key=nil)
  ret = nil
  if key
    FFI::MemoryPointer.new(:pointer) do |p_value|
      err = C.afc_get_device_info_key(self, key, p_value)
      raise AFCError, "AFC error: #{err}" if err != :SUCCESS

      value = p_value.read_pointer
      unless value.null?
        ret = value.read_string
        C.free(value)
      end
    end
  else
    FFI::MemoryPointer.new(:pointer) do |p_infos|
      err = C.afc_get_device_info(self, p_infos)
      raise AFCError, "AFC error: #{err}" if err != :SUCCESS
      ret = _infolist_to_hash(p_infos)
    end
  end
  return ret
end

#file_info(path) ⇒ Object Also known as: stat

Raises:



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/idevice/afc.rb', line 105

def file_info(path)
  ret = nil
  FFI::MemoryPointer.new(:pointer) do |p_fileinfo|
    err = C.afc_get_file_info(self, path, p_fileinfo)
    raise AFCError, "AFC error: #{err}" if err != :SUCCESS
    ret = _infolist_to_hash(p_fileinfo)

    # convert string values to something more useful
    ret[:st_ifmt] = ret[:st_ifmt].to_sym if ret[:st_ifmt]

    [:st_blocks, :st_nlink, :st_size].each do |k|
      ret[k] = ret[k].to_i if ret[k]
    end

    [:st_birthtime, :st_mtime].each do |k|
      ret[k] = _afctime_to_time(ret[k])
    end
  end
  raise AFCError, "afc_get_file_info returned null info for path: #{path}" if ret.nil?
  return ret
end

#get_path(frompath, topath, chunksz = nil) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/idevice/afc.rb', line 254

def get_path(frompath, topath, chunksz=nil)
  wlen = 0
  AFCFile.open(self, frompath, 'r') do |from|
    File.open(topath, 'w') do |to|
      from.read_all(chunksz) do |chunk|
        to.write(chunk)
        yield chunk.size if block_given?
        wlen += chunk.size
      end
    end
  end
  return wlen
end

Raises:



147
148
149
150
151
# File 'lib/idevice/afc.rb', line 147

def hardlink(from, to)
  err = C.afc_make_link(self, :HARDLINK, from, to)
  raise AFCError, "AFC error: #{err}" if err != :SUCCESS
  return true
end

#make_directory(path) ⇒ Object Also known as: mkdir

Raises:



133
134
135
136
137
# File 'lib/idevice/afc.rb', line 133

def make_directory(path)
  err = C.afc_make_directory(self, path)
  raise AFCError, "AFC error: #{err}" if err != :SUCCESS
  return true
end

#mtime(path) ⇒ Object



183
184
185
186
# File 'lib/idevice/afc.rb', line 183

def mtime(path)
  info = file_info(path)
  return info[:st_mtime]
end

#open(path, mode = nil, &block) ⇒ Object



282
283
284
# File 'lib/idevice/afc.rb', line 282

def open(path, mode=nil, &block)
  AFCFile.open(self,path,mode,&block)
end

#put_path(frompath, topath, opts = {}) ⇒ Object

supported options:

chunksize:   (Integer) chunksize for file transfer
recurse:     (Boolean) if true, will recursively transfer using put_recursively


228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/idevice/afc.rb', line 228

def put_path(frompath, topath, opts={})
  opts = opts.dup
  chunksz = opts[:chunksize] || AFC_DEFAULT_CHUNKSIZE
  recurse = opts.delete(:recurse)

  return put_recursively(frompath, topath, opts) if recurse

  if File.directory?(frompath)
    raise ArgumentError, "#{frompath.inspect} is a directory and can only be put on the device using the 'recurse: true' option)"
  end

  wlen = 0

  File.open(frompath, 'r') do |from|
    AFCFile.open(self, topath, 'w') do |to|
      while chunk = from.read(chunksz)
        to.write(chunk)
        yield chunk.size if block_given?
        wlen+=chunk.size
      end
    end
  end

  return wlen
end

#put_recursively(frompath, topath, opts = {}) ⇒ Object

puts files recursively from the local machine to the remove idevice. topath should be a directory that exists on the remote device or ‘.’ for the top-level directory. opts are passed as-is back to put_path (allowing :chunksize or other options to be set). opts will be ignored.



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/idevice/afc.rb', line 198

def put_recursively(frompath, topath, opts={})
  frompath = Pathname(frompath)
  opts = opts.dup
  opts.delete(:recurse)

  if frompath.directory?
    # remove trailing slash(es)
    while topath[-1] == '/'
      topath = topath[0..-2]
    end

    base_frompath = Pathname(File.basename(frompath.expand_path))
    self.mkdir("#{topath}/#{base_frompath.basename}") rescue nil

    Dir[frompath.join("**", "*")].each do |rpath|
      rtopath = "#{topath}/#{Pathname(rpath).relative_path_from(frompath.dirname)}"
      if File.directory?(rpath)
        self.mkdir(rtopath)
      else
        self.put_path(rpath, rtopath, opts)
      end
    end
  else
    return self.put_path(frompath, topath, opts)
  end
end

#read_directory(path = '.') ⇒ Object Also known as: ls

Raises:



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/idevice/afc.rb', line 92

def read_directory(path='.')
  ret = nil
  FFI::MemoryPointer.new(:pointer) do |p_dirlist|
    err = C.afc_read_directory(self, path, p_dirlist)
    raise AFCError, "AFC error: #{err}" if err != :SUCCESS
    ret = _unbound_list_to_array(p_dirlist)
  end

  raise AFCError, "afc_read_directory returned a null directory list for path: #{path}" if ret.nil?
  return ret
end

#remove_path(path) ⇒ Object Also known as: rm

Raises:



161
162
163
164
165
# File 'lib/idevice/afc.rb', line 161

def remove_path(path)
  err = C.afc_remove_path(self, path)
  raise AFCError, "AFC error: #{err}" if err != :SUCCESS
  return true
end

#rename_path(from, to) ⇒ Object Also known as: mv

Raises:



154
155
156
157
158
# File 'lib/idevice/afc.rb', line 154

def rename_path(from, to)
  err = C.afc_rename_path(self, from, to)
  raise AFCError, "AFC error: #{err}" if err != :SUCCESS
  return true
end

#set_file_time(path, time) ⇒ Object

Raises:



268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/idevice/afc.rb', line 268

def set_file_time(path, time)
  tval = case time
         when Time
           _time_to_afctime(time)
         when DateTime
           _time_to_afctime(time.to_time)
         else
           time
         end
  err = C.afc_set_file_time(self, path, tval)
  raise AFCError, "AFC error: #{err}" if err != :SUCCESS
  return true
end

#size(path) ⇒ Object



178
179
180
181
# File 'lib/idevice/afc.rb', line 178

def size(path)
  res = file_info(path)
  return res[:st_size].to_i
end

Raises:



140
141
142
143
144
# File 'lib/idevice/afc.rb', line 140

def symlink(from, to)
  err = C.afc_make_link(self, :SYMLINK, from, to)
  raise AFCError, "AFC error: #{err}" if err != :SUCCESS
  return true
end

#touch(path) ⇒ Object



128
129
130
131
# File 'lib/idevice/afc.rb', line 128

def touch(path)
  open(path, 'a'){ }
  return true
end

#truncate(path, size) ⇒ Object

Raises:



168
169
170
171
172
# File 'lib/idevice/afc.rb', line 168

def truncate(path, size)
  err = C.afc_truncate(self, path, size)
  raise AFCError, "AFC error: #{err}" if err != :SUCCESS
  return true
end