diff options
| author | 2010-04-09 16:52:30 -0500 | |
|---|---|---|
| committer | 2012-03-20 23:25:55 +0100 | |
| commit | bea5efe442daeab05d5d7a2e9d9e7b934ba6e684 (patch) | |
| tree | 0346eebd799517c0976e640db7241d0c72cf7d95 /cython/afc.pxi | |
| parent | acac4f819ccafa6f6bb945626f2e21ec2b75074b (diff) | |
| download | libimobiledevice-bea5efe442daeab05d5d7a2e9d9e7b934ba6e684.tar.gz libimobiledevice-bea5efe442daeab05d5d7a2e9d9e7b934ba6e684.tar.bz2 | |
Implemented hierarchy suggested by Martin S.
Implemented new BaseService constructors.
Moved LockdownClient to lockdown.pxi.
Implemented more of the afc interface.
Diffstat (limited to 'cython/afc.pxi')
| -rw-r--r-- | cython/afc.pxi | 155 |
1 files changed, 127 insertions, 28 deletions
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": | |||
| 51 | afc_error_t afc_get_device_info(afc_client_t client, char ***infos) | 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) | 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) | 53 | afc_error_t afc_get_file_info(afc_client_t client, char *filename, char ***infolist) |
| 54 | afc_error_t afc_remove_path(afc_client_t client, char *path) | ||
| 55 | afc_error_t afc_rename_path(afc_client_t client, char *f, char *to) | ||
| 56 | afc_error_t afc_make_directory(afc_client_t client, char *dir) | ||
| 57 | afc_error_t afc_truncate(afc_client_t client, char *path, uint64_t newsize) | ||
| 58 | afc_error_t afc_make_link(afc_client_t client, afc_link_type_t linktype, char *target, char *linkname) | ||
| 59 | afc_error_t afc_set_file_time(afc_client_t client, char *path, uint64_t mtime) | ||
| 60 | |||
| 54 | afc_error_t afc_file_open(afc_client_t client, char *filename, afc_file_mode_t file_mode, uint64_t *handle) | 61 | 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) | 62 | 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) | 63 | 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": | |||
| 59 | afc_error_t afc_file_seek(afc_client_t client, uint64_t handle, int64_t offset, int whence) | 66 | 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) | 67 | 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) | 68 | 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 | ||
| 69 | cdef class AfcError(BaseError): | 70 | cdef class AfcError(BaseError): |
| 70 | def __init__(self, *args, **kwargs): | 71 | def __init__(self, *args, **kwargs): |
| @@ -100,19 +101,55 @@ cdef class AfcError(BaseError): | |||
| 100 | } | 101 | } |
| 101 | BaseError.__init__(self, *args, **kwargs) | 102 | BaseError.__init__(self, *args, **kwargs) |
| 102 | 103 | ||
| 103 | cdef class AfcClient(Base): | 104 | # forward declaration of AfcClient |
| 105 | cdef class AfcClient(BaseService) | ||
| 106 | |||
| 107 | cdef class AfcFile(Base): | ||
| 108 | cdef uint64_t _c_handle | ||
| 109 | cdef AfcClient _client | ||
| 110 | cdef bytes _filename | ||
| 111 | |||
| 112 | def __init__(self, *args, **kwargs): | ||
| 113 | raise TypeError("AfcFile cannot be instantiated") | ||
| 114 | |||
| 115 | cpdef close(self): | ||
| 116 | self.handle_error(afc_file_close(self._client._c_client, self._c_handle)) | ||
| 117 | |||
| 118 | cpdef seek(self, int64_t offset, int whence): | ||
| 119 | self.handle_error(afc_file_seek(self._client._c_client, self._c_handle, offset, whence)) | ||
| 120 | |||
| 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 | |||
| 126 | cpdef truncate(self, uint64_t newsize): | ||
| 127 | self.handle_error(afc_file_truncate(self._client._c_client, self._c_handle, newsize)) | ||
| 128 | |||
| 129 | cpdef uint32_t write(self, bytes data): | ||
| 130 | cdef: | ||
| 131 | uint32_t bytes_written | ||
| 132 | char* c_data = data | ||
| 133 | try: | ||
| 134 | self.handle_error(afc_file_write(self._client._c_client, self._c_handle, c_data, len(data), &bytes_written)) | ||
| 135 | except BaseError, e: | ||
| 136 | raise | ||
| 137 | finally: | ||
| 138 | free(c_data) | ||
| 139 | |||
| 140 | return bytes_written | ||
| 141 | |||
| 142 | cdef inline BaseError _error(self, int16_t ret): | ||
| 143 | return AfcError(ret) | ||
| 144 | |||
| 145 | cdef class AfcClient(BaseService): | ||
| 146 | __service_name__ = "com.apple.afc" | ||
| 104 | cdef afc_client_t _c_client | 147 | cdef afc_client_t _c_client |
| 105 | 148 | ||
| 106 | def __cinit__(self, iDevice device not None, LockdownClient lockdown=None, *args, **kwargs): | 149 | def __cinit__(self, iDevice device not None, int port, *args, **kwargs): |
| 107 | cdef: | 150 | cdef: |
| 108 | iDevice dev = device | 151 | iDevice dev = device |
| 109 | LockdownClient lckd | ||
| 110 | afc_error_t err | 152 | afc_error_t err |
| 111 | if lockdown is None: | ||
| 112 | lckd = LockdownClient(dev) | ||
| 113 | else: | ||
| 114 | lckd = lockdown | ||
| 115 | port = lckd.start_service("com.apple.afc") | ||
| 116 | err = afc_client_new(dev._c_dev, port, &(self._c_client)) | 153 | err = afc_client_new(dev._c_dev, port, &(self._c_client)) |
| 117 | self.handle_error(err) | 154 | self.handle_error(err) |
| 118 | 155 | ||
| @@ -133,13 +170,18 @@ cdef class AfcClient(Base): | |||
| 133 | int i = 0 | 170 | int i = 0 |
| 134 | list result = [] | 171 | list result = [] |
| 135 | err = afc_get_device_info(self._c_client, &infos) | 172 | err = afc_get_device_info(self._c_client, &infos) |
| 136 | self.handle_error(err) | 173 | try: |
| 137 | while infos[i]: | 174 | self.handle_error(err) |
| 138 | info = infos[i] | 175 | except BaseError, e: |
| 139 | result.append(info) | 176 | raise |
| 140 | free(infos[i]) | 177 | finally: |
| 141 | i = i + 1 | 178 | if infos != NULL: |
| 142 | free(infos) | 179 | while infos[i]: |
| 180 | info = infos[i] | ||
| 181 | result.append(info) | ||
| 182 | free(infos[i]) | ||
| 183 | i = i + 1 | ||
| 184 | free(infos) | ||
| 143 | 185 | ||
| 144 | return result | 186 | return result |
| 145 | 187 | ||
| @@ -151,12 +193,69 @@ cdef class AfcClient(Base): | |||
| 151 | int i = 0 | 193 | int i = 0 |
| 152 | list result = [] | 194 | list result = [] |
| 153 | err = afc_read_directory(self._c_client, directory, &dir_list) | 195 | err = afc_read_directory(self._c_client, directory, &dir_list) |
| 154 | self.handle_error(err) | 196 | try: |
| 155 | while dir_list[i]: | 197 | self.handle_error(err) |
| 156 | f = dir_list[i] | 198 | except BaseError, e: |
| 157 | result.append(f) | 199 | raise |
| 158 | free(dir_list[i]) | 200 | finally: |
| 159 | i = i + 1 | 201 | if dir_list != NULL: |
| 160 | free(dir_list) | 202 | while dir_list[i]: |
| 203 | f = dir_list[i] | ||
| 204 | result.append(f) | ||
| 205 | free(dir_list[i]) | ||
| 206 | i = i + 1 | ||
| 207 | free(dir_list) | ||
| 208 | |||
| 209 | return result | ||
| 210 | |||
| 211 | cpdef AfcFile open(self, bytes filename): | ||
| 212 | cdef: | ||
| 213 | str mode = 'r' | ||
| 214 | afc_file_mode_t c_mode | ||
| 215 | uint64_t handle | ||
| 216 | AfcFile f | ||
| 217 | if mode == 'r': | ||
| 218 | c_mode = AFC_FOPEN_RDONLY | ||
| 219 | self.handle_error(afc_file_open(self._c_client, filename, c_mode, &handle)) | ||
| 220 | f = AfcFile.__new__(AfcFile) | ||
| 221 | f._c_handle = handle | ||
| 222 | f._client = self | ||
| 223 | f._filename = filename | ||
| 224 | |||
| 225 | return f | ||
| 226 | |||
| 227 | cpdef get_file_info(self, bytes path): | ||
| 228 | cdef: | ||
| 229 | list result | ||
| 230 | char** c_result | ||
| 231 | int i = 0 | ||
| 232 | bytes info | ||
| 233 | try: | ||
| 234 | self.handle_error(afc_get_file_info(self._c_client, path, &c_result)) | ||
| 235 | except BaseError, e: | ||
| 236 | raise | ||
| 237 | finally: | ||
| 238 | if c_result != NULL: | ||
| 239 | while c_result[i]: | ||
| 240 | info = c_result[i] | ||
| 241 | result.append(info) | ||
| 242 | free(c_result[i]) | ||
| 243 | i = i + 1 | ||
| 244 | free(c_result) | ||
| 161 | 245 | ||
| 162 | return result | 246 | return result |
| 247 | |||
| 248 | cpdef remove_path(self, bytes path): | ||
| 249 | self.handle_error(afc_remove_path(self._c_client, path)) | ||
| 250 | |||
| 251 | cpdef rename_path(self, bytes f, bytes t): | ||
| 252 | self.handle_error(afc_rename_path(self._c_client, f, t)) | ||
| 253 | |||
| 254 | cpdef make_directory(self, bytes d): | ||
| 255 | self.handle_error(afc_make_directory(self._c_client, d)) | ||
| 256 | |||
| 257 | cpdef truncate(self, bytes path, uint64_t newsize): | ||
| 258 | self.handle_error(afc_truncate(self._c_client, path, newsize)) | ||
| 259 | |||
| 260 | cpdef set_file_time(self, bytes path, uint64_t mtime): | ||
| 261 | self.handle_error(afc_set_file_time(self._c_client, path, mtime)) | ||
