diff options
Diffstat (limited to 'cython/afc.pxi')
| -rw-r--r-- | cython/afc.pxi | 85 |
1 files changed, 70 insertions, 15 deletions
diff --git a/cython/afc.pxi b/cython/afc.pxi index 0383471..6bd8182 100644 --- a/cython/afc.pxi +++ b/cython/afc.pxi @@ -46,12 +46,13 @@ cdef extern from "libimobiledevice/afc.h": AFC_LOCK_EX = 2 | 4 AFC_LOCK_UN = 8 | 4 - afc_error_t afc_client_new(idevice_t device, uint16_t port, afc_client_t *client) + afc_error_t afc_client_new(idevice_t device, lockdownd_service_descriptor_t descriptor, afc_client_t *client) afc_error_t afc_client_free(afc_client_t client) afc_error_t afc_get_device_info(afc_client_t client, char ***infos) afc_error_t afc_read_directory(afc_client_t client, char *dir, char ***list) afc_error_t afc_get_file_info(afc_client_t client, char *filename, char ***infolist) afc_error_t afc_remove_path(afc_client_t client, char *path) + afc_error_t afc_remove_path_and_contents(afc_client_t client, char *path) afc_error_t afc_rename_path(afc_client_t client, char *f, char *to) afc_error_t afc_make_directory(afc_client_t client, char *dir) afc_error_t afc_truncate(afc_client_t client, char *path, uint64_t newsize) @@ -116,6 +117,12 @@ cdef class AfcFile(Base): def __init__(self, *args, **kwargs): raise TypeError("AfcFile cannot be instantiated") + def __enter__(self): + return self + + def __exit__(self, type, value, traceback): + self.close() + cpdef close(self): self.handle_error(afc_file_close(self._client._c_client, self._c_handle)) @@ -133,6 +140,20 @@ cdef class AfcFile(Base): cpdef truncate(self, uint64_t newsize): self.handle_error(afc_file_truncate(self._client._c_client, self._c_handle, newsize)) + cpdef bytes read(self, uint32_t size): + cdef: + uint32_t bytes_read + char* c_data = <char *>malloc(size) + bytes result + try: + self.handle_error(afc_file_read(self._client._c_client, self._c_handle, c_data, size, &bytes_read)) + result = c_data[:bytes_read] + return result + except BaseError, e: + raise + finally: + free(c_data) + cpdef uint32_t write(self, bytes data): cdef: uint32_t bytes_written @@ -141,8 +162,6 @@ cdef class AfcFile(Base): self.handle_error(afc_file_write(self._client._c_client, self._c_handle, c_data, len(data), &bytes_written)) except BaseError, e: raise - finally: - free(c_data) return bytes_written @@ -153,8 +172,9 @@ cdef class AfcClient(BaseService): __service_name__ = "com.apple.afc" cdef afc_client_t _c_client - def __cinit__(self, iDevice device not None, int port, *args, **kwargs): - self.handle_error(afc_client_new(device._c_dev, port, &(self._c_client))) + def __cinit__(self, iDevice device = None, LockdownServiceDescriptor descriptor = None, *args, **kwargs): + if (device is not None and descriptor is not None): + self.handle_error(afc_client_new(device._c_dev, descriptor._c_service_descriptor, &(self._c_client))) def __dealloc__(self): cdef afc_error_t err @@ -168,7 +188,7 @@ cdef class AfcClient(BaseService): cpdef list get_device_info(self): cdef: afc_error_t err - char** infos + char** infos = NULL bytes info int i = 0 list result = [] @@ -191,7 +211,7 @@ cdef class AfcClient(BaseService): cpdef list read_directory(self, bytes directory): cdef: afc_error_t err - char** dir_list + char** dir_list = NULL bytes f int i = 0 list result = [] @@ -216,17 +236,17 @@ cdef class AfcClient(BaseService): afc_file_mode_t c_mode uint64_t handle AfcFile f - if mode == <bytes>'r': + if mode == b'r': c_mode = AFC_FOPEN_RDONLY - elif mode == <bytes>'r+': + elif mode == b'r+': c_mode = AFC_FOPEN_RW - elif mode == <bytes>'w': + elif mode == b'w': c_mode = AFC_FOPEN_WRONLY - elif mode == <bytes>'w+': + elif mode == b'w+': c_mode = AFC_FOPEN_WR - elif mode == <bytes>'a': + elif mode == b'a': c_mode = AFC_FOPEN_APPEND - elif mode == <bytes>'a+': + elif mode == b'a+': c_mode = AFC_FOPEN_RDAPPEND else: raise ValueError("mode string must be 'r', 'r+', 'w', 'w+', 'a', or 'a+'") @@ -239,10 +259,10 @@ cdef class AfcClient(BaseService): return f - cpdef get_file_info(self, bytes path): + cpdef list get_file_info(self, bytes path): cdef: list result = [] - char** c_result + char** c_result = NULL int i = 0 bytes info try: @@ -263,6 +283,9 @@ cdef class AfcClient(BaseService): cpdef remove_path(self, bytes path): self.handle_error(afc_remove_path(self._c_client, path)) + cpdef remove_path_and_contents(self, bytes path): + self.handle_error(afc_remove_path_and_contents(self._c_client, path)) + cpdef rename_path(self, bytes f, bytes t): self.handle_error(afc_rename_path(self._c_client, f, t)) @@ -280,3 +303,35 @@ cdef class AfcClient(BaseService): cpdef set_file_time(self, bytes path, uint64_t mtime): self.handle_error(afc_set_file_time(self._c_client, path, mtime)) + +cdef class Afc2Client(AfcClient): + __service_name__ = "com.apple.afc2" + + cpdef AfcFile open(self, bytes filename, bytes mode=b'r'): + cdef: + afc_file_mode_t c_mode + uint64_t handle + AfcFile f + if mode == b'r': + c_mode = AFC_FOPEN_RDONLY + elif mode == b'r+': + c_mode = AFC_FOPEN_RW + elif mode == b'w': + c_mode = AFC_FOPEN_WRONLY + elif mode == b'w+': + c_mode = AFC_FOPEN_WR + elif mode == b'a': + c_mode = AFC_FOPEN_APPEND + elif mode == b'a+': + c_mode = AFC_FOPEN_RDAPPEND + else: + raise ValueError("mode string must be 'r', 'r+', 'w', 'w+', 'a', or 'a+'") + + self.handle_error(afc_file_open(self._c_client, filename, c_mode, &handle)) + f = AfcFile.__new__(AfcFile) + f._c_handle = handle + f._client = <AfcClient>self + f._filename = filename + + return f + |
