afc.pxi 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. cdef extern from "libimobiledevice/afc.h":
  2. cdef struct afc_client_private:
  3. pass
  4. ctypedef afc_client_private *afc_client_t
  5. ctypedef enum afc_error_t:
  6. AFC_E_SUCCESS = 0
  7. AFC_E_UNKNOWN_ERROR = 1
  8. AFC_E_OP_HEADER_INVALID = 2
  9. AFC_E_NO_RESOURCES = 3
  10. AFC_E_READ_ERROR = 4
  11. AFC_E_WRITE_ERROR = 5
  12. AFC_E_UNKNOWN_PACKET_TYPE = 6
  13. AFC_E_INVALID_ARG = 7
  14. AFC_E_OBJECT_NOT_FOUND = 8
  15. AFC_E_OBJECT_IS_DIR = 9
  16. AFC_E_PERM_DENIED = 10
  17. AFC_E_SERVICE_NOT_CONNECTED = 11
  18. AFC_E_OP_TIMEOUT = 12
  19. AFC_E_TOO_MUCH_DATA = 13
  20. AFC_E_END_OF_DATA = 14
  21. AFC_E_OP_NOT_SUPPORTED = 15
  22. AFC_E_OBJECT_EXISTS = 16
  23. AFC_E_OBJECT_BUSY = 17
  24. AFC_E_NO_SPACE_LEFT = 18
  25. AFC_E_OP_WOULD_BLOCK = 19
  26. AFC_E_IO_ERROR = 20
  27. AFC_E_OP_INTERRUPTED = 21
  28. AFC_E_OP_IN_PROGRESS = 22
  29. AFC_E_INTERNAL_ERROR = 23
  30. AFC_E_MUX_ERROR = 30
  31. AFC_E_NO_MEM = 31
  32. AFC_E_NOT_ENOUGH_DATA = 32
  33. AFC_E_DIR_NOT_EMPTY = 33
  34. ctypedef enum afc_file_mode_t:
  35. AFC_FOPEN_RDONLY = 0x00000001
  36. AFC_FOPEN_RW = 0x00000002
  37. AFC_FOPEN_WRONLY = 0x00000003
  38. AFC_FOPEN_WR = 0x00000004
  39. AFC_FOPEN_APPEND = 0x00000005
  40. AFC_FOPEN_RDAPPEND = 0x00000006
  41. ctypedef enum afc_link_type_t:
  42. AFC_HARDLINK = 1
  43. AFC_SYMLINK = 2
  44. ctypedef enum afc_lock_op_t:
  45. AFC_LOCK_SH = 1 | 4
  46. AFC_LOCK_EX = 2 | 4
  47. AFC_LOCK_UN = 8 | 4
  48. afc_error_t afc_client_new(idevice_t device, lockdownd_service_descriptor_t descriptor, afc_client_t *client)
  49. afc_error_t afc_client_free(afc_client_t client)
  50. afc_error_t afc_get_device_info(afc_client_t client, char ***infos)
  51. afc_error_t afc_read_directory(afc_client_t client, char *dir, char ***list)
  52. afc_error_t afc_get_file_info(afc_client_t client, char *filename, char ***infolist)
  53. afc_error_t afc_remove_path(afc_client_t client, char *path)
  54. afc_error_t afc_rename_path(afc_client_t client, char *f, char *to)
  55. afc_error_t afc_make_directory(afc_client_t client, char *dir)
  56. afc_error_t afc_truncate(afc_client_t client, char *path, uint64_t newsize)
  57. afc_error_t afc_make_link(afc_client_t client, afc_link_type_t linktype, char *target, char *linkname)
  58. afc_error_t afc_set_file_time(afc_client_t client, char *path, uint64_t mtime)
  59. afc_error_t afc_file_open(afc_client_t client, char *filename, afc_file_mode_t file_mode, uint64_t *handle)
  60. afc_error_t afc_file_close(afc_client_t client, uint64_t handle)
  61. afc_error_t afc_file_lock(afc_client_t client, uint64_t handle, afc_lock_op_t operation)
  62. afc_error_t afc_file_read(afc_client_t client, uint64_t handle, char *data, uint32_t length, uint32_t *bytes_read)
  63. afc_error_t afc_file_write(afc_client_t client, uint64_t handle, char *data, uint32_t length, uint32_t *bytes_written)
  64. afc_error_t afc_file_seek(afc_client_t client, uint64_t handle, int64_t offset, int whence)
  65. afc_error_t afc_file_tell(afc_client_t client, uint64_t handle, uint64_t *position)
  66. afc_error_t afc_file_truncate(afc_client_t client, uint64_t handle, uint64_t newsize)
  67. LOCK_SH = AFC_LOCK_SH
  68. LOCK_EX = AFC_LOCK_EX
  69. LOCK_UN = AFC_LOCK_UN
  70. cdef class AfcError(BaseError):
  71. def __init__(self, *args, **kwargs):
  72. self._lookup_table = {
  73. AFC_E_SUCCESS: "Success",
  74. AFC_E_UNKNOWN_ERROR: "Unknown error",
  75. AFC_E_OP_HEADER_INVALID: "OP header invalid",
  76. AFC_E_NO_RESOURCES: "No resources",
  77. AFC_E_READ_ERROR: "Read error",
  78. AFC_E_WRITE_ERROR: "Write error",
  79. AFC_E_UNKNOWN_PACKET_TYPE: "Unknown packet type",
  80. AFC_E_INVALID_ARG: "Invalid argument",
  81. AFC_E_OBJECT_NOT_FOUND: "Object not found",
  82. AFC_E_OBJECT_IS_DIR: "Object is directory",
  83. AFC_E_PERM_DENIED: "Permission denied",
  84. AFC_E_SERVICE_NOT_CONNECTED: "Service not connected",
  85. AFC_E_OP_TIMEOUT: "OP timeout",
  86. AFC_E_TOO_MUCH_DATA: "Too much data",
  87. AFC_E_END_OF_DATA: "End of data",
  88. AFC_E_OP_NOT_SUPPORTED: "OP not supported",
  89. AFC_E_OBJECT_EXISTS: "Object exists",
  90. AFC_E_OBJECT_BUSY: "Object busy",
  91. AFC_E_NO_SPACE_LEFT: "No space left",
  92. AFC_E_OP_WOULD_BLOCK: "OP would block",
  93. AFC_E_IO_ERROR: "IO error",
  94. AFC_E_OP_INTERRUPTED: "OP interrupted",
  95. AFC_E_OP_IN_PROGRESS: "OP in progress",
  96. AFC_E_INTERNAL_ERROR: "Internal error",
  97. AFC_E_MUX_ERROR: "MUX error",
  98. AFC_E_NO_MEM: "No memory",
  99. AFC_E_NOT_ENOUGH_DATA: "Not enough data",
  100. AFC_E_DIR_NOT_EMPTY: "Directory not empty"
  101. }
  102. BaseError.__init__(self, *args, **kwargs)
  103. # forward declaration of AfcClient
  104. cdef class AfcClient(BaseService)
  105. cdef class AfcFile(Base):
  106. cdef uint64_t _c_handle
  107. cdef AfcClient _client
  108. cdef bytes _filename
  109. def __init__(self, *args, **kwargs):
  110. raise TypeError("AfcFile cannot be instantiated")
  111. def __enter__(self):
  112. return self
  113. def __exit__(self, type, value, traceback):
  114. self.close()
  115. cpdef close(self):
  116. self.handle_error(afc_file_close(self._client._c_client, self._c_handle))
  117. cpdef lock(self, int operation):
  118. self.handle_error(afc_file_lock(self._client._c_client, self._c_handle, <afc_lock_op_t>operation))
  119. cpdef seek(self, int64_t offset, int whence):
  120. self.handle_error(afc_file_seek(self._client._c_client, self._c_handle, offset, whence))
  121. cpdef uint64_t tell(self):
  122. cdef uint64_t position
  123. self.handle_error(afc_file_tell(self._client._c_client, self._c_handle, &position))
  124. return position
  125. cpdef truncate(self, uint64_t newsize):
  126. self.handle_error(afc_file_truncate(self._client._c_client, self._c_handle, newsize))
  127. cpdef bytes read(self, uint32_t size):
  128. cdef:
  129. uint32_t bytes_read
  130. char* c_data = <char *>malloc(size)
  131. bytes result
  132. try:
  133. self.handle_error(afc_file_read(self._client._c_client, self._c_handle, c_data, size, &bytes_read))
  134. result = c_data[:bytes_read]
  135. return result
  136. except BaseError, e:
  137. raise
  138. finally:
  139. free(c_data)
  140. cpdef uint32_t write(self, bytes data):
  141. cdef:
  142. uint32_t bytes_written
  143. char* c_data = data
  144. try:
  145. self.handle_error(afc_file_write(self._client._c_client, self._c_handle, c_data, len(data), &bytes_written))
  146. except BaseError, e:
  147. raise
  148. return bytes_written
  149. cdef inline BaseError _error(self, int16_t ret):
  150. return AfcError(ret)
  151. cdef class AfcClient(BaseService):
  152. __service_name__ = "com.apple.afc"
  153. cdef afc_client_t _c_client
  154. def __cinit__(self, iDevice device = None, LockdownServiceDescriptor descriptor = None, *args, **kwargs):
  155. if (device is not None and descriptor is not None):
  156. self.handle_error(afc_client_new(device._c_dev, descriptor._c_service_descriptor, &(self._c_client)))
  157. def __dealloc__(self):
  158. cdef afc_error_t err
  159. if self._c_client is not NULL:
  160. err = afc_client_free(self._c_client)
  161. self.handle_error(err)
  162. cdef BaseError _error(self, int16_t ret):
  163. return AfcError(ret)
  164. cpdef list get_device_info(self):
  165. cdef:
  166. afc_error_t err
  167. char** infos = NULL
  168. bytes info
  169. int i = 0
  170. list result = []
  171. err = afc_get_device_info(self._c_client, &infos)
  172. try:
  173. self.handle_error(err)
  174. except BaseError, e:
  175. raise
  176. finally:
  177. if infos != NULL:
  178. while infos[i]:
  179. info = infos[i]
  180. result.append(info)
  181. free(infos[i])
  182. i = i + 1
  183. free(infos)
  184. return result
  185. cpdef list read_directory(self, bytes directory):
  186. cdef:
  187. afc_error_t err
  188. char** dir_list = NULL
  189. bytes f
  190. int i = 0
  191. list result = []
  192. err = afc_read_directory(self._c_client, directory, &dir_list)
  193. try:
  194. self.handle_error(err)
  195. except BaseError, e:
  196. raise
  197. finally:
  198. if dir_list != NULL:
  199. while dir_list[i]:
  200. f = dir_list[i]
  201. result.append(f)
  202. free(dir_list[i])
  203. i = i + 1
  204. free(dir_list)
  205. return result
  206. cpdef AfcFile open(self, bytes filename, bytes mode=b'r'):
  207. cdef:
  208. afc_file_mode_t c_mode
  209. uint64_t handle
  210. AfcFile f
  211. if mode == <bytes>'r':
  212. c_mode = AFC_FOPEN_RDONLY
  213. elif mode == <bytes>'r+':
  214. c_mode = AFC_FOPEN_RW
  215. elif mode == <bytes>'w':
  216. c_mode = AFC_FOPEN_WRONLY
  217. elif mode == <bytes>'w+':
  218. c_mode = AFC_FOPEN_WR
  219. elif mode == <bytes>'a':
  220. c_mode = AFC_FOPEN_APPEND
  221. elif mode == <bytes>'a+':
  222. c_mode = AFC_FOPEN_RDAPPEND
  223. else:
  224. raise ValueError("mode string must be 'r', 'r+', 'w', 'w+', 'a', or 'a+'")
  225. self.handle_error(afc_file_open(self._c_client, filename, c_mode, &handle))
  226. f = AfcFile.__new__(AfcFile)
  227. f._c_handle = handle
  228. f._client = self
  229. f._filename = filename
  230. return f
  231. cpdef list get_file_info(self, bytes path):
  232. cdef:
  233. list result = []
  234. char** c_result = NULL
  235. int i = 0
  236. bytes info
  237. try:
  238. self.handle_error(afc_get_file_info(self._c_client, path, &c_result))
  239. except BaseError, e:
  240. raise
  241. finally:
  242. if c_result != NULL:
  243. while c_result[i]:
  244. info = c_result[i]
  245. result.append(info)
  246. free(c_result[i])
  247. i = i + 1
  248. free(c_result)
  249. return result
  250. cpdef remove_path(self, bytes path):
  251. self.handle_error(afc_remove_path(self._c_client, path))
  252. cpdef rename_path(self, bytes f, bytes t):
  253. self.handle_error(afc_rename_path(self._c_client, f, t))
  254. cpdef make_directory(self, bytes d):
  255. self.handle_error(afc_make_directory(self._c_client, d))
  256. cpdef truncate(self, bytes path, uint64_t newsize):
  257. self.handle_error(afc_truncate(self._c_client, path, newsize))
  258. cpdef link(self, bytes source, bytes link_name):
  259. self.handle_error(afc_make_link(self._c_client, AFC_HARDLINK, source, link_name))
  260. cpdef symlink(self, bytes source, bytes link_name):
  261. self.handle_error(afc_make_link(self._c_client, AFC_SYMLINK, source, link_name))
  262. cpdef set_file_time(self, bytes path, uint64_t mtime):
  263. self.handle_error(afc_set_file_time(self._c_client, path, mtime))
  264. cdef class Afc2Client(AfcClient):
  265. __service_name__ = "com.apple.afc2"
  266. cpdef AfcFile open(self, bytes filename, bytes mode=b'r'):
  267. cdef:
  268. afc_file_mode_t c_mode
  269. uint64_t handle
  270. AfcFile f
  271. if mode == <bytes>'r':
  272. c_mode = AFC_FOPEN_RDONLY
  273. elif mode == <bytes>'r+':
  274. c_mode = AFC_FOPEN_RW
  275. elif mode == <bytes>'w':
  276. c_mode = AFC_FOPEN_WRONLY
  277. elif mode == <bytes>'w+':
  278. c_mode = AFC_FOPEN_WR
  279. elif mode == <bytes>'a':
  280. c_mode = AFC_FOPEN_APPEND
  281. elif mode == <bytes>'a+':
  282. c_mode = AFC_FOPEN_RDAPPEND
  283. else:
  284. raise ValueError("mode string must be 'r', 'r+', 'w', 'w+', 'a', or 'a+'")
  285. self.handle_error(afc_file_open(self._c_client, filename, c_mode, &handle))
  286. f = AfcFile.__new__(AfcFile)
  287. f._c_handle = handle
  288. f._client = <AfcClient>self
  289. f._filename = filename
  290. return f