diff options
Diffstat (limited to 'cython/afc.pxi')
| -rw-r--r-- | cython/afc.pxi | 165 |
1 files changed, 165 insertions, 0 deletions
diff --git a/cython/afc.pxi b/cython/afc.pxi new file mode 100644 index 0000000..51251c8 --- /dev/null +++ b/cython/afc.pxi | |||
| @@ -0,0 +1,165 @@ | |||
| 1 | cdef extern from "libimobiledevice/afc.h": | ||
| 2 | cdef struct afc_client_int: | ||
| 3 | pass | ||
| 4 | ctypedef afc_client_int *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_ARGUMENT = 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 | |||
| 49 | afc_error_t afc_client_new(idevice_t device, uint16_t port, afc_client_t *client) | ||
| 50 | afc_error_t afc_client_free(afc_client_t client) | ||
| 51 | afc_error_t afc_get_device_info(afc_client_t client, char ***infos) | ||
| 52 | afc_error_t afc_read_directory(afc_client_t client, char *dir, char ***list) | ||
| 53 | afc_error_t afc_get_file_info(afc_client_t client, char *filename, char ***infolist) | ||
| 54 | afc_error_t afc_file_open(afc_client_t client, char *filename, afc_file_mode_t file_mode, uint64_t *handle) | ||
| 55 | afc_error_t afc_file_close(afc_client_t client, uint64_t handle) | ||
| 56 | afc_error_t afc_file_lock(afc_client_t client, uint64_t handle, afc_lock_op_t operation) | ||
| 57 | afc_error_t afc_file_read(afc_client_t client, uint64_t handle, char *data, uint32_t length, uint32_t *bytes_read) | ||
| 58 | afc_error_t afc_file_write(afc_client_t client, uint64_t handle, char *data, uint32_t length, uint32_t *bytes_written) | ||
| 59 | afc_error_t afc_file_seek(afc_client_t client, uint64_t handle, int64_t offset, int whence) | ||
| 60 | afc_error_t afc_file_tell(afc_client_t client, uint64_t handle, uint64_t *position) | ||
| 61 | afc_error_t afc_file_truncate(afc_client_t client, uint64_t handle, uint64_t newsize) | ||
| 62 | afc_error_t afc_remove_path(afc_client_t client, char *path) | ||
| 63 | afc_error_t afc_rename_path(afc_client_t client, char *f, char *to) | ||
| 64 | afc_error_t afc_make_directory(afc_client_t client, char *dir) | ||
| 65 | afc_error_t afc_truncate(afc_client_t client, char *path, uint64_t newsize) | ||
| 66 | afc_error_t afc_make_link(afc_client_t client, afc_link_type_t linktype, char *target, char *linkname) | ||
| 67 | afc_error_t afc_set_file_time(afc_client_t client, char *path, uint64_t mtime) | ||
| 68 | |||
| 69 | cdef extern from *: | ||
| 70 | void free(void *ptr) | ||
| 71 | |||
| 72 | cdef class AfcError(BaseError): | ||
| 73 | def __init__(self, *args, **kwargs): | ||
| 74 | self._lookup_table = { | ||
| 75 | AFC_E_SUCCESS: "Success", | ||
| 76 | AFC_E_UNKNOWN_ERROR: "Unknown error", | ||
| 77 | AFC_E_OP_HEADER_INVALID: "OP header invalid", | ||
| 78 | AFC_E_NO_RESOURCES: "No resources", | ||
| 79 | AFC_E_READ_ERROR: "Read error", | ||
| 80 | AFC_E_WRITE_ERROR: "Write error", | ||
| 81 | AFC_E_UNKNOWN_PACKET_TYPE: "Unknown packet type", | ||
| 82 | AFC_E_INVALID_ARGUMENT: "Invalid argument", | ||
| 83 | AFC_E_OBJECT_NOT_FOUND: "Object not found", | ||
| 84 | AFC_E_OBJECT_IS_DIR: "Object is directory", | ||
| 85 | AFC_E_PERM_DENIED: "Permission denied", | ||
| 86 | AFC_E_SERVICE_NOT_CONNECTED: "Service not connected", | ||
| 87 | AFC_E_OP_TIMEOUT: "OP timeout", | ||
| 88 | AFC_E_TOO_MUCH_DATA: "Too much data", | ||
| 89 | AFC_E_END_OF_DATA: "End of data", | ||
| 90 | AFC_E_OP_NOT_SUPPORTED: "OP not supported", | ||
| 91 | AFC_E_OBJECT_EXISTS: "Object exists", | ||
| 92 | AFC_E_OBJECT_BUSY: "Object busy", | ||
| 93 | AFC_E_NO_SPACE_LEFT: "No space left", | ||
| 94 | AFC_E_OP_WOULD_BLOCK: "OP would block", | ||
| 95 | AFC_E_IO_ERROR: "IO error", | ||
| 96 | AFC_E_OP_INTERRUPTED: "OP interrupted", | ||
| 97 | AFC_E_OP_IN_PROGRESS: "OP in progress", | ||
| 98 | AFC_E_INTERNAL_ERROR: "Internal error", | ||
| 99 | AFC_E_MUX_ERROR: "MUX error", | ||
| 100 | AFC_E_NO_MEM: "No memory", | ||
| 101 | AFC_E_NOT_ENOUGH_DATA: "Not enough data", | ||
| 102 | AFC_E_DIR_NOT_EMPTY: "Directory not empty" | ||
| 103 | } | ||
| 104 | BaseError.__init__(self, *args, **kwargs) | ||
| 105 | |||
| 106 | cdef class AfcClient(Base): | ||
| 107 | cdef afc_client_t _c_client | ||
| 108 | |||
| 109 | def __cinit__(self, iDevice device not None, LockdownClient lockdown=None, *args, **kwargs): | ||
| 110 | cdef: | ||
| 111 | iDevice dev = device | ||
| 112 | LockdownClient lckd | ||
| 113 | afc_error_t err | ||
| 114 | if lockdown is None: | ||
| 115 | lckd = LockdownClient(dev) | ||
| 116 | else: | ||
| 117 | lckd = lockdown | ||
| 118 | port = lckd.start_service("com.apple.afc") | ||
| 119 | err = afc_client_new(dev._c_dev, port, &(self._c_client)) | ||
| 120 | self.handle_error(err) | ||
| 121 | |||
| 122 | def __dealloc__(self): | ||
| 123 | cdef afc_error_t err | ||
| 124 | if self._c_client is not NULL: | ||
| 125 | err = afc_client_free(self._c_client) | ||
| 126 | self.handle_error(err) | ||
| 127 | |||
| 128 | cdef inline BaseError _error(self, int16_t ret): | ||
| 129 | return AfcError(ret) | ||
| 130 | |||
| 131 | cpdef list get_device_info(self): | ||
| 132 | cdef: | ||
| 133 | afc_error_t err | ||
| 134 | char** infos | ||
| 135 | bytes info | ||
| 136 | int i = 0 | ||
| 137 | list result = [] | ||
| 138 | err = afc_get_device_info(self._c_client, &infos) | ||
| 139 | self.handle_error(err) | ||
| 140 | while infos[i]: | ||
| 141 | info = infos[i] | ||
| 142 | result.append(info) | ||
| 143 | free(infos[i]) | ||
| 144 | i = i + 1 | ||
| 145 | free(infos) | ||
| 146 | |||
| 147 | return result | ||
| 148 | |||
| 149 | cpdef list read_directory(self, bytes directory): | ||
| 150 | cdef: | ||
| 151 | afc_error_t err | ||
| 152 | char** dir_list | ||
| 153 | bytes f | ||
| 154 | int i = 0 | ||
| 155 | list result = [] | ||
| 156 | err = afc_read_directory(self._c_client, directory, &dir_list) | ||
| 157 | self.handle_error(err) | ||
| 158 | while dir_list[i]: | ||
| 159 | f = dir_list[i] | ||
| 160 | result.append(f) | ||
| 161 | free(dir_list[i]) | ||
| 162 | i = i + 1 | ||
| 163 | free(dir_list) | ||
| 164 | |||
| 165 | return result | ||
