From bea5efe442daeab05d5d7a2e9d9e7b934ba6e684 Mon Sep 17 00:00:00 2001 From: Bryan Forbes Date: Fri, 9 Apr 2010 16:52:30 -0500 Subject: Implemented hierarchy suggested by Martin S. Implemented new BaseService constructors. Moved LockdownClient to lockdown.pxi. Implemented more of the afc interface. --- cython/afc.pxi | 155 ++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 127 insertions(+), 28 deletions(-) (limited to 'cython/afc.pxi') diff --git a/cython/afc.pxi b/cython/afc.pxi index 1e2617a..ff5c2b0 100644 --- a/cython/afc.pxi +++ b/cython/afc.pxi @@ -51,6 +51,13 @@ cdef extern from "libimobiledevice/afc.h": 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_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) + afc_error_t afc_make_link(afc_client_t client, afc_link_type_t linktype, char *target, char *linkname) + afc_error_t afc_set_file_time(afc_client_t client, char *path, uint64_t mtime) + afc_error_t afc_file_open(afc_client_t client, char *filename, afc_file_mode_t file_mode, uint64_t *handle) afc_error_t afc_file_close(afc_client_t client, uint64_t handle) afc_error_t afc_file_lock(afc_client_t client, uint64_t handle, afc_lock_op_t operation) @@ -59,12 +66,6 @@ cdef extern from "libimobiledevice/afc.h": afc_error_t afc_file_seek(afc_client_t client, uint64_t handle, int64_t offset, int whence) afc_error_t afc_file_tell(afc_client_t client, uint64_t handle, uint64_t *position) afc_error_t afc_file_truncate(afc_client_t client, uint64_t handle, uint64_t newsize) - afc_error_t afc_remove_path(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) - afc_error_t afc_make_link(afc_client_t client, afc_link_type_t linktype, char *target, char *linkname) - afc_error_t afc_set_file_time(afc_client_t client, char *path, uint64_t mtime) cdef class AfcError(BaseError): def __init__(self, *args, **kwargs): @@ -100,19 +101,55 @@ cdef class AfcError(BaseError): } BaseError.__init__(self, *args, **kwargs) -cdef class AfcClient(Base): +# forward declaration of AfcClient +cdef class AfcClient(BaseService) + +cdef class AfcFile(Base): + cdef uint64_t _c_handle + cdef AfcClient _client + cdef bytes _filename + + def __init__(self, *args, **kwargs): + raise TypeError("AfcFile cannot be instantiated") + + cpdef close(self): + self.handle_error(afc_file_close(self._client._c_client, self._c_handle)) + + cpdef seek(self, int64_t offset, int whence): + self.handle_error(afc_file_seek(self._client._c_client, self._c_handle, offset, whence)) + + cpdef uint64_t tell(self): + cdef uint64_t position + self.handle_error(afc_file_tell(self._client._c_client, self._c_handle, &position)) + return position + + cpdef truncate(self, uint64_t newsize): + self.handle_error(afc_file_truncate(self._client._c_client, self._c_handle, newsize)) + + cpdef uint32_t write(self, bytes data): + cdef: + uint32_t bytes_written + char* c_data = data + try: + 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 + + cdef inline BaseError _error(self, int16_t ret): + return AfcError(ret) + +cdef class AfcClient(BaseService): + __service_name__ = "com.apple.afc" cdef afc_client_t _c_client - def __cinit__(self, iDevice device not None, LockdownClient lockdown=None, *args, **kwargs): + def __cinit__(self, iDevice device not None, int port, *args, **kwargs): cdef: iDevice dev = device - LockdownClient lckd afc_error_t err - if lockdown is None: - lckd = LockdownClient(dev) - else: - lckd = lockdown - port = lckd.start_service("com.apple.afc") err = afc_client_new(dev._c_dev, port, &(self._c_client)) self.handle_error(err) @@ -133,13 +170,18 @@ cdef class AfcClient(Base): int i = 0 list result = [] err = afc_get_device_info(self._c_client, &infos) - self.handle_error(err) - while infos[i]: - info = infos[i] - result.append(info) - free(infos[i]) - i = i + 1 - free(infos) + try: + self.handle_error(err) + except BaseError, e: + raise + finally: + if infos != NULL: + while infos[i]: + info = infos[i] + result.append(info) + free(infos[i]) + i = i + 1 + free(infos) return result @@ -151,12 +193,69 @@ cdef class AfcClient(Base): int i = 0 list result = [] err = afc_read_directory(self._c_client, directory, &dir_list) - self.handle_error(err) - while dir_list[i]: - f = dir_list[i] - result.append(f) - free(dir_list[i]) - i = i + 1 - free(dir_list) + try: + self.handle_error(err) + except BaseError, e: + raise + finally: + if dir_list != NULL: + while dir_list[i]: + f = dir_list[i] + result.append(f) + free(dir_list[i]) + i = i + 1 + free(dir_list) + + return result + + cpdef AfcFile open(self, bytes filename): + cdef: + str mode = 'r' + afc_file_mode_t c_mode + uint64_t handle + AfcFile f + if mode == 'r': + c_mode = AFC_FOPEN_RDONLY + self.handle_error(afc_file_open(self._c_client, filename, c_mode, &handle)) + f = AfcFile.__new__(AfcFile) + f._c_handle = handle + f._client = self + f._filename = filename + + return f + + cpdef get_file_info(self, bytes path): + cdef: + list result + char** c_result + int i = 0 + bytes info + try: + self.handle_error(afc_get_file_info(self._c_client, path, &c_result)) + except BaseError, e: + raise + finally: + if c_result != NULL: + while c_result[i]: + info = c_result[i] + result.append(info) + free(c_result[i]) + i = i + 1 + free(c_result) return result + + cpdef remove_path(self, bytes path): + self.handle_error(afc_remove_path(self._c_client, path)) + + cpdef rename_path(self, bytes f, bytes t): + self.handle_error(afc_rename_path(self._c_client, f, t)) + + cpdef make_directory(self, bytes d): + self.handle_error(afc_make_directory(self._c_client, d)) + + cpdef truncate(self, bytes path, uint64_t newsize): + self.handle_error(afc_truncate(self._c_client, path, newsize)) + + cpdef set_file_time(self, bytes path, uint64_t mtime): + self.handle_error(afc_set_file_time(self._c_client, path, mtime)) -- cgit v1.1-32-gdbae