diff options
Diffstat (limited to 'include/libimobiledevice/afc.h')
-rw-r--r-- | include/libimobiledevice/afc.h | 384 |
1 files changed, 323 insertions, 61 deletions
diff --git a/include/libimobiledevice/afc.h b/include/libimobiledevice/afc.h index 8d47696..4ad3dbd 100644 --- a/include/libimobiledevice/afc.h +++ b/include/libimobiledevice/afc.h | |||
@@ -1,9 +1,10 @@ | |||
1 | /** | 1 | /** |
2 | * @file libimobiledevice/afc.h | 2 | * @file libimobiledevice/afc.h |
3 | * @brief Access the filesystem. | 3 | * @brief Access the filesystem on the device. |
4 | * \internal | 4 | * \internal |
5 | * | 5 | * |
6 | * Copyright (c) 2009 Nikias Bassen All Rights Reserved. | 6 | * Copyright (c) 2010-2014 Martin Szulecki All Rights Reserved. |
7 | * Copyright (c) 2009-2010 Nikias Bassen All Rights Reserved. | ||
7 | * | 8 | * |
8 | * This library is free software; you can redistribute it and/or | 9 | * This library is free software; you can redistribute it and/or |
9 | * modify it under the terms of the GNU Lesser General Public | 10 | * modify it under the terms of the GNU Lesser General Public |
@@ -20,50 +21,51 @@ | |||
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
21 | */ | 22 | */ |
22 | 23 | ||
23 | #ifndef AFC_H | 24 | #ifndef IAFC_H |
24 | #define AFC_H | 25 | #define IAFC_H |
25 | 26 | ||
26 | #ifdef __cplusplus | 27 | #ifdef __cplusplus |
27 | extern "C" { | 28 | extern "C" { |
28 | #endif | 29 | #endif |
29 | 30 | ||
30 | #include <libimobiledevice/libimobiledevice.h> | 31 | #include <libimobiledevice/libimobiledevice.h> |
32 | #include <libimobiledevice/lockdown.h> | ||
31 | 33 | ||
32 | /** @name Error Codes */ | 34 | /** Service identifier passed to lockdownd_start_service() to start the AFC service */ |
33 | /*@{*/ | 35 | #define AFC_SERVICE_NAME "com.apple.afc" |
34 | #define AFC_E_SUCCESS 0 | 36 | |
35 | #define AFC_E_UNKNOWN_ERROR 1 | 37 | /** Error Codes */ |
36 | #define AFC_E_OP_HEADER_INVALID 2 | 38 | typedef enum { |
37 | #define AFC_E_NO_RESOURCES 3 | 39 | AFC_E_SUCCESS = 0, |
38 | #define AFC_E_READ_ERROR 4 | 40 | AFC_E_UNKNOWN_ERROR = 1, |
39 | #define AFC_E_WRITE_ERROR 5 | 41 | AFC_E_OP_HEADER_INVALID = 2, |
40 | #define AFC_E_UNKNOWN_PACKET_TYPE 6 | 42 | AFC_E_NO_RESOURCES = 3, |
41 | #define AFC_E_INVALID_ARG 7 | 43 | AFC_E_READ_ERROR = 4, |
42 | #define AFC_E_OBJECT_NOT_FOUND 8 | 44 | AFC_E_WRITE_ERROR = 5, |
43 | #define AFC_E_OBJECT_IS_DIR 9 | 45 | AFC_E_UNKNOWN_PACKET_TYPE = 6, |
44 | #define AFC_E_PERM_DENIED 10 | 46 | AFC_E_INVALID_ARG = 7, |
45 | #define AFC_E_SERVICE_NOT_CONNECTED 11 | 47 | AFC_E_OBJECT_NOT_FOUND = 8, |
46 | #define AFC_E_OP_TIMEOUT 12 | 48 | AFC_E_OBJECT_IS_DIR = 9, |
47 | #define AFC_E_TOO_MUCH_DATA 13 | 49 | AFC_E_PERM_DENIED = 10, |
48 | #define AFC_E_END_OF_DATA 14 | 50 | AFC_E_SERVICE_NOT_CONNECTED = 11, |
49 | #define AFC_E_OP_NOT_SUPPORTED 15 | 51 | AFC_E_OP_TIMEOUT = 12, |
50 | #define AFC_E_OBJECT_EXISTS 16 | 52 | AFC_E_TOO_MUCH_DATA = 13, |
51 | #define AFC_E_OBJECT_BUSY 17 | 53 | AFC_E_END_OF_DATA = 14, |
52 | #define AFC_E_NO_SPACE_LEFT 18 | 54 | AFC_E_OP_NOT_SUPPORTED = 15, |
53 | #define AFC_E_OP_WOULD_BLOCK 19 | 55 | AFC_E_OBJECT_EXISTS = 16, |
54 | #define AFC_E_IO_ERROR 20 | 56 | AFC_E_OBJECT_BUSY = 17, |
55 | #define AFC_E_OP_INTERRUPTED 21 | 57 | AFC_E_NO_SPACE_LEFT = 18, |
56 | #define AFC_E_OP_IN_PROGRESS 22 | 58 | AFC_E_OP_WOULD_BLOCK = 19, |
57 | #define AFC_E_INTERNAL_ERROR 23 | 59 | AFC_E_IO_ERROR = 20, |
58 | 60 | AFC_E_OP_INTERRUPTED = 21, | |
59 | #define AFC_E_MUX_ERROR 30 | 61 | AFC_E_OP_IN_PROGRESS = 22, |
60 | #define AFC_E_NO_MEM 31 | 62 | AFC_E_INTERNAL_ERROR = 23, |
61 | #define AFC_E_NOT_ENOUGH_DATA 32 | 63 | AFC_E_MUX_ERROR = 30, |
62 | #define AFC_E_DIR_NOT_EMPTY 33 | 64 | AFC_E_NO_MEM = 31, |
63 | /*@}*/ | 65 | AFC_E_NOT_ENOUGH_DATA = 32, |
64 | 66 | AFC_E_DIR_NOT_EMPTY = 33, | |
65 | /** Represents an error code. */ | 67 | AFC_E_FORCE_SIGNED_TYPE = -1 |
66 | typedef int16_t afc_error_t; | 68 | } afc_error_t; |
67 | 69 | ||
68 | /** Flags for afc_file_open */ | 70 | /** Flags for afc_file_open */ |
69 | typedef enum { | 71 | typedef enum { |
@@ -88,33 +90,293 @@ typedef enum { | |||
88 | AFC_LOCK_UN = 8 | 4 /**< unlock */ | 90 | AFC_LOCK_UN = 8 | 4 /**< unlock */ |
89 | } afc_lock_op_t; | 91 | } afc_lock_op_t; |
90 | 92 | ||
91 | typedef struct afc_client_private afc_client_private; | 93 | typedef struct afc_client_private afc_client_private; /**< \private */ |
92 | typedef afc_client_private *afc_client_t; /**< The client handle. */ | 94 | typedef afc_client_private *afc_client_t; /**< The client handle. */ |
93 | 95 | ||
94 | /* Interface */ | 96 | /* Interface */ |
95 | afc_error_t afc_client_new_from_connection(idevice_connection_t connection, afc_client_t *client); | 97 | |
96 | afc_error_t afc_client_new(idevice_t device, uint16_t port, afc_client_t *client); | 98 | /** |
97 | afc_error_t afc_client_free(afc_client_t client); | 99 | * Makes a connection to the AFC service on the device. |
98 | afc_error_t afc_get_device_info(afc_client_t client, char ***infos); | 100 | * |
99 | afc_error_t afc_read_directory(afc_client_t client, const char *dir, char ***list); | 101 | * @param device The device to connect to. |
100 | afc_error_t afc_get_file_info(afc_client_t client, const char *filename, char ***infolist); | 102 | * @param service The service descriptor returned by lockdownd_start_service. |
101 | afc_error_t afc_file_open(afc_client_t client, const char *filename, afc_file_mode_t file_mode, uint64_t *handle); | 103 | * @param client Pointer that will be set to a newly allocated afc_client_t |
102 | afc_error_t afc_file_close(afc_client_t client, uint64_t handle); | 104 | * upon successful return. |
103 | afc_error_t afc_file_lock(afc_client_t client, uint64_t handle, afc_lock_op_t operation); | 105 | * |
104 | afc_error_t afc_file_read(afc_client_t client, uint64_t handle, char *data, uint32_t length, uint32_t *bytes_read); | 106 | * @return AFC_E_SUCCESS on success, AFC_E_INVALID_ARG if device or service is |
105 | afc_error_t afc_file_write(afc_client_t client, uint64_t handle, const char *data, uint32_t length, uint32_t *bytes_written); | 107 | * invalid, AFC_E_MUX_ERROR if the connection cannot be established, |
106 | afc_error_t afc_file_seek(afc_client_t client, uint64_t handle, int64_t offset, int whence); | 108 | * or AFC_E_NO_MEM if there is a memory allocation problem. |
107 | afc_error_t afc_file_tell(afc_client_t client, uint64_t handle, uint64_t *position); | 109 | */ |
108 | afc_error_t afc_file_truncate(afc_client_t client, uint64_t handle, uint64_t newsize); | 110 | LIBIMOBILEDEVICE_API afc_error_t afc_client_new(idevice_t device, lockdownd_service_descriptor_t service, afc_client_t *client); |
109 | afc_error_t afc_remove_path(afc_client_t client, const char *path); | 111 | |
110 | afc_error_t afc_rename_path(afc_client_t client, const char *from, const char *to); | 112 | /** |
111 | afc_error_t afc_make_directory(afc_client_t client, const char *dir); | 113 | * Starts a new AFC service on the specified device and connects to it. |
112 | afc_error_t afc_truncate(afc_client_t client, const char *path, uint64_t newsize); | 114 | * |
113 | afc_error_t afc_make_link(afc_client_t client, afc_link_type_t linktype, const char *target, const char *linkname); | 115 | * @param device The device to connect to. |
114 | afc_error_t afc_set_file_time(afc_client_t client, const char *path, uint64_t mtime); | 116 | * @param client Pointer that will point to a newly allocated afc_client_t upon |
117 | * successful return. Must be freed using afc_client_free() after use. | ||
118 | * @param label The label to use for communication. Usually the program name. | ||
119 | * Pass NULL to disable sending the label in requests to lockdownd. | ||
120 | * | ||
121 | * @return AFC_E_SUCCESS on success, or an AFC_E_* error code otherwise. | ||
122 | */ | ||
123 | LIBIMOBILEDEVICE_API afc_error_t afc_client_start_service(idevice_t device, afc_client_t* client, const char* label); | ||
124 | |||
125 | /** | ||
126 | * Frees up an AFC client. If the connection was created by the client itself, | ||
127 | * the connection will be closed. | ||
128 | * | ||
129 | * @param client The client to free. | ||
130 | */ | ||
131 | LIBIMOBILEDEVICE_API afc_error_t afc_client_free(afc_client_t client); | ||
132 | |||
133 | /** | ||
134 | * Get device information for a connected client. The device information | ||
135 | * returned is the device model as well as the free space, the total capacity | ||
136 | * and blocksize on the accessed disk partition. | ||
137 | * | ||
138 | * @param client The client to get device info for. | ||
139 | * @param device_information A char list of device information terminated by an | ||
140 | * empty string or NULL if there was an error. Free with | ||
141 | * afc_dictionary_free(). | ||
142 | * | ||
143 | * @return AFC_E_SUCCESS on success or an AFC_E_* error value. | ||
144 | */ | ||
145 | LIBIMOBILEDEVICE_API afc_error_t afc_get_device_info(afc_client_t client, char ***device_information); | ||
146 | |||
147 | /** | ||
148 | * Gets a directory listing of the directory requested. | ||
149 | * | ||
150 | * @param client The client to get a directory listing from. | ||
151 | * @param path The directory for listing. (must be a fully-qualified path) | ||
152 | * @param directory_information A char list of files in the directory | ||
153 | * terminated by an empty string or NULL if there was an error. Free with | ||
154 | * afc_dictionary_free(). | ||
155 | * | ||
156 | * @return AFC_E_SUCCESS on success or an AFC_E_* error value. | ||
157 | */ | ||
158 | LIBIMOBILEDEVICE_API afc_error_t afc_read_directory(afc_client_t client, const char *path, char ***directory_information); | ||
159 | |||
160 | /** | ||
161 | * Gets information about a specific file. | ||
162 | * | ||
163 | * @param client The client to use to get the information of the file. | ||
164 | * @param path The fully-qualified path to the file. | ||
165 | * @param file_information Pointer to a buffer that will be filled with a | ||
166 | * NULL-terminated list of strings with the file information. Set to NULL | ||
167 | * before calling this function. Free with afc_dictionary_free(). | ||
168 | * | ||
169 | * @return AFC_E_SUCCESS on success or an AFC_E_* error value. | ||
170 | */ | ||
171 | LIBIMOBILEDEVICE_API afc_error_t afc_get_file_info(afc_client_t client, const char *path, char ***file_information); | ||
172 | |||
173 | /** | ||
174 | * Opens a file on the device. | ||
175 | * | ||
176 | * @param client The client to use to open the file. | ||
177 | * @param filename The file to open. (must be a fully-qualified path) | ||
178 | * @param file_mode The mode to use to open the file. | ||
179 | * @param handle Pointer to a uint64_t that will hold the handle of the file | ||
180 | * | ||
181 | * @return AFC_E_SUCCESS on success or an AFC_E_* error value. | ||
182 | */ | ||
183 | LIBIMOBILEDEVICE_API afc_error_t afc_file_open(afc_client_t client, const char *filename, afc_file_mode_t file_mode, uint64_t *handle); | ||
184 | |||
185 | /** | ||
186 | * Closes a file on the device. | ||
187 | * | ||
188 | * @param client The client to close the file with. | ||
189 | * @param handle File handle of a previously opened file. | ||
190 | */ | ||
191 | LIBIMOBILEDEVICE_API afc_error_t afc_file_close(afc_client_t client, uint64_t handle); | ||
192 | |||
193 | /** | ||
194 | * Locks or unlocks a file on the device. | ||
195 | * | ||
196 | * Makes use of flock on the device. | ||
197 | * @see http://developer.apple.com/documentation/Darwin/Reference/ManPages/man2/flock.2.html | ||
198 | * | ||
199 | * @param client The client to lock the file with. | ||
200 | * @param handle File handle of a previously opened file. | ||
201 | * @param operation the lock or unlock operation to perform, this is one of | ||
202 | * AFC_LOCK_SH (shared lock), AFC_LOCK_EX (exclusive lock), or | ||
203 | * AFC_LOCK_UN (unlock). | ||
204 | */ | ||
205 | LIBIMOBILEDEVICE_API afc_error_t afc_file_lock(afc_client_t client, uint64_t handle, afc_lock_op_t operation); | ||
206 | |||
207 | /** | ||
208 | * Attempts to the read the given number of bytes from the given file. | ||
209 | * | ||
210 | * @param client The relevant AFC client | ||
211 | * @param handle File handle of a previously opened file | ||
212 | * @param data The pointer to the memory region to store the read data | ||
213 | * @param length The number of bytes to read | ||
214 | * @param bytes_read The number of bytes actually read. | ||
215 | * | ||
216 | * @return AFC_E_SUCCESS on success or an AFC_E_* error value. | ||
217 | */ | ||
218 | LIBIMOBILEDEVICE_API afc_error_t afc_file_read(afc_client_t client, uint64_t handle, char *data, uint32_t length, uint32_t *bytes_read); | ||
219 | |||
220 | /** | ||
221 | * Writes a given number of bytes to a file. | ||
222 | * | ||
223 | * @param client The client to use to write to the file. | ||
224 | * @param handle File handle of previously opened file. | ||
225 | * @param data The data to write to the file. | ||
226 | * @param length How much data to write. | ||
227 | * @param bytes_written The number of bytes actually written to the file. | ||
228 | * | ||
229 | * @return AFC_E_SUCCESS on success or an AFC_E_* error value. | ||
230 | */ | ||
231 | LIBIMOBILEDEVICE_API afc_error_t afc_file_write(afc_client_t client, uint64_t handle, const char *data, uint32_t length, uint32_t *bytes_written); | ||
232 | |||
233 | /** | ||
234 | * Seeks to a given position of a pre-opened file on the device. | ||
235 | * | ||
236 | * @param client The client to use to seek to the position. | ||
237 | * @param handle File handle of a previously opened. | ||
238 | * @param offset Seek offset. | ||
239 | * @param whence Seeking direction, one of SEEK_SET, SEEK_CUR, or SEEK_END. | ||
240 | * | ||
241 | * @return AFC_E_SUCCESS on success or an AFC_E_* error value. | ||
242 | */ | ||
243 | LIBIMOBILEDEVICE_API afc_error_t afc_file_seek(afc_client_t client, uint64_t handle, int64_t offset, int whence); | ||
244 | |||
245 | /** | ||
246 | * Returns current position in a pre-opened file on the device. | ||
247 | * | ||
248 | * @param client The client to use. | ||
249 | * @param handle File handle of a previously opened file. | ||
250 | * @param position Position in bytes of indicator | ||
251 | * | ||
252 | * @return AFC_E_SUCCESS on success or an AFC_E_* error value. | ||
253 | */ | ||
254 | LIBIMOBILEDEVICE_API afc_error_t afc_file_tell(afc_client_t client, uint64_t handle, uint64_t *position); | ||
255 | |||
256 | /** | ||
257 | * Sets the size of a file on the device. | ||
258 | * | ||
259 | * @param client The client to use to set the file size. | ||
260 | * @param handle File handle of a previously opened file. | ||
261 | * @param newsize The size to set the file to. | ||
262 | * | ||
263 | * @return AFC_E_SUCCESS on success or an AFC_E_* error value. | ||
264 | * | ||
265 | * @note This function is more akin to ftruncate than truncate, and truncate | ||
266 | * calls would have to open the file before calling this, sadly. | ||
267 | */ | ||
268 | LIBIMOBILEDEVICE_API afc_error_t afc_file_truncate(afc_client_t client, uint64_t handle, uint64_t newsize); | ||
269 | |||
270 | /** | ||
271 | * Deletes a file or directory. | ||
272 | * | ||
273 | * @param client The client to use. | ||
274 | * @param path The path to delete. (must be a fully-qualified path) | ||
275 | * | ||
276 | * @return AFC_E_SUCCESS on success or an AFC_E_* error value. | ||
277 | */ | ||
278 | LIBIMOBILEDEVICE_API afc_error_t afc_remove_path(afc_client_t client, const char *path); | ||
279 | |||
280 | /** | ||
281 | * Renames a file or directory on the device. | ||
282 | * | ||
283 | * @param client The client to have rename. | ||
284 | * @param from The name to rename from. (must be a fully-qualified path) | ||
285 | * @param to The new name. (must also be a fully-qualified path) | ||
286 | * | ||
287 | * @return AFC_E_SUCCESS on success or an AFC_E_* error value. | ||
288 | */ | ||
289 | LIBIMOBILEDEVICE_API afc_error_t afc_rename_path(afc_client_t client, const char *from, const char *to); | ||
290 | |||
291 | /** | ||
292 | * Creates a directory on the device. | ||
293 | * | ||
294 | * @param client The client to use to make a directory. | ||
295 | * @param path The directory's path. (must be a fully-qualified path, I assume | ||
296 | * all other mkdir restrictions apply as well) | ||
297 | * | ||
298 | * @return AFC_E_SUCCESS on success or an AFC_E_* error value. | ||
299 | */ | ||
300 | LIBIMOBILEDEVICE_API afc_error_t afc_make_directory(afc_client_t client, const char *path); | ||
301 | |||
302 | /** | ||
303 | * Sets the size of a file on the device without prior opening it. | ||
304 | * | ||
305 | * @param client The client to use to set the file size. | ||
306 | * @param path The path of the file to be truncated. | ||
307 | * @param newsize The size to set the file to. | ||
308 | * | ||
309 | * @return AFC_E_SUCCESS on success or an AFC_E_* error value. | ||
310 | */ | ||
311 | LIBIMOBILEDEVICE_API afc_error_t afc_truncate(afc_client_t client, const char *path, uint64_t newsize); | ||
312 | |||
313 | /** | ||
314 | * Creates a hard link or symbolic link on the device. | ||
315 | * | ||
316 | * @param client The client to use for making a link | ||
317 | * @param linktype 1 = hard link, 2 = symlink | ||
318 | * @param target The file to be linked. | ||
319 | * @param linkname The name of link. | ||
320 | * | ||
321 | * @return AFC_E_SUCCESS on success or an AFC_E_* error value. | ||
322 | */ | ||
323 | LIBIMOBILEDEVICE_API afc_error_t afc_make_link(afc_client_t client, afc_link_type_t linktype, const char *target, const char *linkname); | ||
324 | |||
325 | /** | ||
326 | * Sets the modification time of a file on the device. | ||
327 | * | ||
328 | * @param client The client to use to set the file size. | ||
329 | * @param path Path of the file for which the modification time should be set. | ||
330 | * @param mtime The modification time to set in nanoseconds since epoch. | ||
331 | * | ||
332 | * @return AFC_E_SUCCESS on success or an AFC_E_* error value. | ||
333 | */ | ||
334 | LIBIMOBILEDEVICE_API afc_error_t afc_set_file_time(afc_client_t client, const char *path, uint64_t mtime); | ||
335 | |||
336 | /** | ||
337 | * Deletes a file or directory including possible contents. | ||
338 | * | ||
339 | * @param client The client to use. | ||
340 | * @param path The path to delete. (must be a fully-qualified path) | ||
341 | * @since libimobiledevice 1.1.7 | ||
342 | * @note Only available in iOS 6 and later. | ||
343 | * | ||
344 | * @return AFC_E_SUCCESS on success or an AFC_E_* error value. | ||
345 | */ | ||
346 | LIBIMOBILEDEVICE_API afc_error_t afc_remove_path_and_contents(afc_client_t client, const char *path); | ||
115 | 347 | ||
116 | /* Helper functions */ | 348 | /* Helper functions */ |
117 | afc_error_t afc_get_device_info_key(afc_client_t client, const char *key, char **value); | 349 | |
350 | /** | ||
351 | * Get a specific key of the device info list for a client connection. | ||
352 | * Known key values are: Model, FSTotalBytes, FSFreeBytes and FSBlockSize. | ||
353 | * This is a helper function for afc_get_device_info(). | ||
354 | * | ||
355 | * @param client The client to get device info for. | ||
356 | * @param key The key to get the value of. | ||
357 | * @param value The value for the key if successful or NULL otherwise. | ||
358 | * | ||
359 | * @return AFC_E_SUCCESS on success or an AFC_E_* error value. | ||
360 | */ | ||
361 | LIBIMOBILEDEVICE_API afc_error_t afc_get_device_info_key(afc_client_t client, const char *key, char **value); | ||
362 | |||
363 | /** | ||
364 | * Frees up a char dictionary as returned by some AFC functions. | ||
365 | * | ||
366 | * @param dictionary The char array terminated by an empty string. | ||
367 | * | ||
368 | * @return AFC_E_SUCCESS on success or an AFC_E_* error value. | ||
369 | */ | ||
370 | LIBIMOBILEDEVICE_API afc_error_t afc_dictionary_free(char **dictionary); | ||
371 | |||
372 | /** | ||
373 | * Gets a readable error string for a given AFC error code. | ||
374 | * | ||
375 | * @param err An AFC error code | ||
376 | * | ||
377 | * @returns A readable error string | ||
378 | */ | ||
379 | LIBIMOBILEDEVICE_API const char* afc_strerror(afc_error_t err); | ||
118 | 380 | ||
119 | #ifdef __cplusplus | 381 | #ifdef __cplusplus |
120 | } | 382 | } |