From 50be30047dbb0d38fd8d61763c13ec75d2b52543 Mon Sep 17 00:00:00 2001 From: Martin Szulecki Date: Sat, 25 Jul 2009 03:26:39 +0200 Subject: [PATCH] Update AFC API and use error codes from the STATUS operation response --- dev/afccheck.c | 16 +- dev/main.c | 46 +++--- include/libiphone/afc.h | 72 +++++++--- include/libiphone/libiphone.h | 8 +- src/AFC.c | 306 ++++++++++++++++++++--------------------- src/AFC.h | 2 + 6 files changed, 238 insertions(+), 212 deletions(-) diff --git a/dev/afccheck.c b/dev/afccheck.c index 067dd5b..d06a147 100644 --- a/dev/afccheck.c +++ b/dev/afccheck.c @@ -55,18 +55,18 @@ void check_afc(gpointer data) uint64_t file = 0; char path[50]; sprintf(path, "/Buf%i", ((param *) data)->id); - afc_open_file(((param *) data)->afc, path, AFC_FOPEN_RW, &file); - afc_write_file(((param *) data)->afc, file, (char *) buf, buffersize, &bytes); - afc_close_file(((param *) data)->afc, file); + afc_file_open(((param *) data)->afc, path, AFC_FOPEN_RW, &file); + afc_file_write(((param *) data)->afc, file, (char *) buf, buffersize, &bytes); + afc_file_close(((param *) data)->afc, file); file = 0; if (bytes != buffersize) printf("Write operation failed\n"); //now read it bytes = 0; - afc_open_file(((param *) data)->afc, path, AFC_FOPEN_RDONLY, &file); - afc_read_file(((param *) data)->afc, file, (char *) buf2, buffersize, &bytes); - afc_close_file(((param *) data)->afc, file); + afc_file_open(((param *) data)->afc, path, AFC_FOPEN_RDONLY, &file); + afc_file_read(((param *) data)->afc, file, (char *) buf2, buffersize, &bytes); + afc_file_close(((param *) data)->afc, file); if (bytes != buffersize) printf("Read operation failed\n"); @@ -79,7 +79,7 @@ void check_afc(gpointer data) } //cleanup - afc_delete_file(((param *) data)->afc, path); + afc_remove_path(((param *) data)->afc, path); g_thread_exit(0); } @@ -116,7 +116,7 @@ int main(int argc, char *argv[]) return 1; } - afc_new_client(phone, port, &afc); + afc_client_new(phone, port, &afc); //makes sure thread environment is available if (!g_thread_supported()) diff --git a/dev/main.c b/dev/main.c index 5551ed9..3e5860e 100644 --- a/dev/main.c +++ b/dev/main.c @@ -103,7 +103,7 @@ int main(int argc, char *argv[]) if (port) { afc_client_t afc = NULL; - afc_new_client(phone, port, &afc); + afc_client_new(phone, port, &afc); if (afc) { lockdownd_start_service(client, "com.apple.mobile.notification_proxy", &npp); if (npp) { @@ -126,18 +126,18 @@ int main(int argc, char *argv[]) perform_notification(phone, client, NP_SYNC_WILL_START); - afc_open_file(afc, "/com.apple.itunes.lock_sync", AFC_FOPEN_RW, &lockfile); + afc_file_open(afc, "/com.apple.itunes.lock_sync", AFC_FOPEN_RW, &lockfile); if (lockfile) { printf("locking file\n"); - afc_lock_file(afc, lockfile, AFC_LOCK_EX); + afc_file_lock(afc, lockfile, AFC_LOCK_EX); perform_notification(phone, client, NP_SYNC_DID_START); } char **dirs = NULL; - afc_get_dir_list(afc, "/eafaedf", &dirs); + afc_read_directory(afc, "/eafaedf", &dirs); if (!dirs) - afc_get_dir_list(afc, "/", &dirs); + afc_read_directory(afc, "/", &dirs); printf("Directory time.\n"); for (i = 0; dirs[i]; i++) { printf("/%s\n", dirs[i]); @@ -146,7 +146,7 @@ int main(int argc, char *argv[]) g_strfreev(dirs); dirs = NULL; - afc_get_devinfo(afc, &dirs); + afc_get_device_info(afc, &dirs); if (dirs) { for (i = 0; dirs[i]; i += 2) { printf("%s: %s\n", dirs[i], dirs[i + 1]); @@ -157,7 +157,7 @@ int main(int argc, char *argv[]) uint64_t my_file = 0; char **info = NULL; uint64_t fsize = 0; - if (IPHONE_E_SUCCESS == afc_get_file_info(afc, "/readme.libiphone.fx", &info) && info) { + if (AFC_E_SUCCESS == afc_get_file_info(afc, "/readme.libiphone.fx", &info) && info) { for (i = 0; info[i]; i += 2) { printf("%s: %s\n", info[i], info[i+1]); if (!strcmp(info[i], "st_size")) { @@ -166,59 +166,59 @@ int main(int argc, char *argv[]) } } - if (IPHONE_E_SUCCESS == - afc_open_file(afc, "/readme.libiphone.fx", AFC_FOPEN_RDONLY, &my_file) && my_file) { + if (AFC_E_SUCCESS == + afc_file_open(afc, "/readme.libiphone.fx", AFC_FOPEN_RDONLY, &my_file) && my_file) { printf("A file size: %llu\n", fsize); char *file_data = (char *) malloc(sizeof(char) * fsize); - afc_read_file(afc, my_file, file_data, fsize, &bytes); + afc_file_read(afc, my_file, file_data, fsize, &bytes); if (bytes > 0) { printf("The file's data:\n"); fwrite(file_data, 1, bytes, stdout); } printf("\nClosing my file.\n"); - afc_close_file(afc, my_file); + afc_file_close(afc, my_file); free(file_data); } else printf("couldn't open a file\n"); - afc_open_file(afc, "/readme.libiphone.fx", AFC_FOPEN_WR, &my_file); + afc_file_open(afc, "/readme.libiphone.fx", AFC_FOPEN_WR, &my_file); if (my_file) { char *outdatafile = strdup("this is a bitchin text file\n"); - afc_write_file(afc, my_file, outdatafile, strlen(outdatafile), &bytes); + afc_file_write(afc, my_file, outdatafile, strlen(outdatafile), &bytes); free(outdatafile); if (bytes > 0) printf("Wrote a surprise. ;)\n"); else printf("I wanted to write a surprise, but... :(\n"); - afc_close_file(afc, my_file); + afc_file_close(afc, my_file); } printf("Deleting a file...\n"); - bytes = afc_delete_file(afc, "/delme"); + bytes = afc_remove_path(afc, "/delme"); if (bytes) printf("Success.\n"); else printf("Failure. (expected unless you have a /delme file on your phone)\n"); printf("Renaming a file...\n"); - bytes = afc_rename_file(afc, "/renme", "/renme2"); + bytes = afc_rename_path(afc, "/renme", "/renme2"); if (bytes > 0) printf("Success.\n"); else printf("Failure. (expected unless you have a /renme file on your phone)\n"); printf("Seek & read\n"); - afc_open_file(afc, "/readme.libiphone.fx", AFC_FOPEN_RDONLY, &my_file); - if (IPHONE_E_SUCCESS != afc_seek_file(afc, my_file, 5, SEEK_CUR)) + afc_file_open(afc, "/readme.libiphone.fx", AFC_FOPEN_RDONLY, &my_file); + if (AFC_E_SUCCESS != afc_file_seek(afc, my_file, 5, SEEK_CUR)) printf("WARN: SEEK DID NOT WORK\n"); char *threeletterword = (char *) malloc(sizeof(char) * 5); - afc_read_file(afc, my_file, threeletterword, 3, &bytes); + afc_file_read(afc, my_file, threeletterword, 3, &bytes); threeletterword[3] = '\0'; if (bytes > 0) printf("Result: %s\n", threeletterword); else printf("Couldn't read!\n"); free(threeletterword); - afc_close_file(afc, my_file); + afc_file_close(afc, my_file); } if (gnp && lockfile) { @@ -226,10 +226,10 @@ int main(int argc, char *argv[]) sleep(5); printf("XXX unlocking file\n"); - afc_lock_file(afc, lockfile, AFC_LOCK_UN); + afc_file_lock(afc, lockfile, AFC_LOCK_UN); printf("XXX closing file\n"); - afc_close_file(afc, lockfile); + afc_file_close(afc, lockfile); printf("XXX sleeping\n"); sleep(5); @@ -241,7 +241,7 @@ int main(int argc, char *argv[]) gnp = NULL; } - afc_free_client(afc); + afc_client_free(afc); } else { printf("Start service failure.\n"); } diff --git a/include/libiphone/afc.h b/include/libiphone/afc.h index af801b1..4b024a1 100644 --- a/include/libiphone/afc.h +++ b/include/libiphone/afc.h @@ -27,6 +27,39 @@ extern "C" { #include +/* Error Codes */ +#define AFC_E_SUCCESS 0 +#define AFC_E_UNKNOWN_ERROR 1 +#define AFC_E_OP_HEADER_INVALID 2 +#define AFC_E_NO_RESOURCES 3 +#define AFC_E_READ_ERROR 4 +#define AFC_E_WRITE_ERROR 5 +#define AFC_E_UNKNOWN_PACKET_TYPE 6 +#define AFC_E_INVALID_ARGUMENT 7 +#define AFC_E_OBJECT_NOT_FOUND 8 +#define AFC_E_OBJECT_IS_DIR 9 +#define AFC_E_PERM_DENIED 10 +#define AFC_E_SERVICE_NOT_CONNECTED 11 +#define AFC_E_OP_TIMEOUT 12 +#define AFC_E_TOO_MUCH_DATA 13 +#define AFC_E_END_OF_DATA 14 +#define AFC_E_OP_NOT_SUPPORTED 15 +#define AFC_E_OBJECT_EXISTS 16 +#define AFC_E_OBJECT_BUSY 17 +#define AFC_E_NO_SPACE_LEFT 18 +#define AFC_E_OP_WOULD_BLOCK 19 +#define AFC_E_IO_ERROR 20 +#define AFC_E_OP_INTERRUPTED 21 +#define AFC_E_OP_IN_PROGRESS 22 +#define AFC_E_INTERNAL_ERROR 23 + +#define AFC_E_MUX_ERROR 30 +#define AFC_E_NO_MEM 31 +#define AFC_E_NOT_ENOUGH_DATA 32 + +typedef int16_t afc_error_t; + +/* Flags */ typedef enum { AFC_FOPEN_RDONLY = 0x00000001, // r O_RDONLY AFC_FOPEN_RW = 0x00000002, // r+ O_RDWR | O_CREAT @@ -50,28 +83,27 @@ typedef enum { struct afc_client_int; typedef struct afc_client_int *afc_client_t; -//afc related functions -iphone_error_t afc_new_client ( iphone_device_t device, int dst_port, afc_client_t *client ); -iphone_error_t afc_free_client ( afc_client_t client ); +/* Interface */ +afc_error_t afc_client_new(iphone_device_t device, int dst_port, afc_client_t *client); +afc_error_t afc_client_free(afc_client_t client); int afc_get_afcerror ( afc_client_t client ); int afc_get_errno ( afc_client_t client ); - -iphone_error_t afc_get_devinfo ( afc_client_t client, char ***infos ); -iphone_error_t afc_get_dir_list ( afc_client_t client, const char *dir, char ***list); - -iphone_error_t afc_get_file_info ( afc_client_t client, const char *filename, char ***infolist ); -iphone_error_t afc_open_file ( afc_client_t client, const char *filename, afc_file_mode_t file_mode, uint64_t *handle ); -iphone_error_t afc_close_file ( afc_client_t client, uint64_t handle); -iphone_error_t afc_lock_file ( afc_client_t client, uint64_t handle, afc_lock_op_t operation); -iphone_error_t afc_read_file ( afc_client_t client, uint64_t handle, char *data, int length, uint32_t *bytes); -iphone_error_t afc_write_file ( afc_client_t client, uint64_t handle, const char *data, int length, uint32_t *bytes); -iphone_error_t afc_seek_file ( afc_client_t client, uint64_t handle, int64_t offset, int whence); -iphone_error_t afc_truncate_file ( afc_client_t client, uint64_t handle, uint64_t newsize); -iphone_error_t afc_delete_file ( afc_client_t client, const char *path); -iphone_error_t afc_rename_file ( afc_client_t client, const char *from, const char *to); -iphone_error_t afc_mkdir ( afc_client_t client, const char *dir); -iphone_error_t afc_truncate ( afc_client_t client, const char *path, off_t newsize); -iphone_error_t afc_make_link ( afc_client_t client, afc_link_type_t linktype, const char *target, const char *linkname); +afc_error_t afc_get_device_info(afc_client_t client, char ***infos); +afc_error_t afc_read_directory(afc_client_t client, const char *dir, char ***list); +afc_error_t afc_get_file_info(afc_client_t client, const char *filename, char ***infolist); +afc_error_t afc_get_connection_info(afc_client_t client, char ***infolist); +afc_error_t afc_file_open(afc_client_t client, const 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); +afc_error_t afc_file_read(afc_client_t client, uint64_t handle, char *data, int length, uint32_t *bytes); +afc_error_t afc_file_write(afc_client_t client, uint64_t handle, const char *data, int length, uint32_t *bytes); +afc_error_t afc_file_seek(afc_client_t client, uint64_t handle, int64_t offset, int whence); +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, const char *path); +afc_error_t afc_rename_path(afc_client_t client, const char *from, const char *to); +afc_error_t afc_make_directory(afc_client_t client, const char *dir); +afc_error_t afc_truncate(afc_client_t client, const char *path, off_t newsize); +afc_error_t afc_make_link(afc_client_t client, afc_link_type_t linktype, const char *target, const char *linkname); #ifdef __cplusplus } diff --git a/include/libiphone/libiphone.h b/include/libiphone/libiphone.h index 61820e0..014c8bf 100644 --- a/include/libiphone/libiphone.h +++ b/include/libiphone/libiphone.h @@ -37,12 +37,8 @@ extern "C" { #define IPHONE_E_INVALID_ARG -1 #define IPHONE_E_UNKNOWN_ERROR -2 #define IPHONE_E_NO_DEVICE -3 -#define IPHONE_E_TIMEOUT -4 -#define IPHONE_E_NOT_ENOUGH_DATA -5 -#define IPHONE_E_BAD_HEADER -6 - -//afc specific error -#define IPHONE_E_AFC_ERROR -13 +#define IPHONE_E_NOT_ENOUGH_DATA -4 +#define IPHONE_E_BAD_HEADER -5 typedef int16_t iphone_error_t; diff --git a/src/AFC.c b/src/AFC.c index 1151f23..95ce0a1 100644 --- a/src/AFC.c +++ b/src/AFC.c @@ -23,13 +23,11 @@ #include #include #include + #include "AFC.h" #include "iphone.h" #include "utils.h" -#include - - // This is the maximum size an AFC data packet can be const int MAXIMUM_PACKET_SIZE = (2 << 15); @@ -61,30 +59,30 @@ static void afc_unlock(afc_client_t client) * * @return A handle to the newly-connected client or NULL upon error. */ -iphone_error_t afc_new_client(iphone_device_t device, int dst_port, afc_client_t * client) +afc_error_t afc_client_new(iphone_device_t device, int dst_port, afc_client_t * client) { - //makes sure thread environment is available + /* makes sure thread environment is available */ if (!g_thread_supported()) g_thread_init(NULL); if (!device) - return IPHONE_E_INVALID_ARG; + return AFC_E_INVALID_ARGUMENT; - // Attempt connection + /* attempt connection */ int sfd = usbmuxd_connect(device->handle, dst_port); if (sfd < 0) { - return IPHONE_E_UNKNOWN_ERROR; // ret; + return AFC_E_MUX_ERROR; } afc_client_t client_loc = (afc_client_t) malloc(sizeof(struct afc_client_int)); client_loc->sfd = sfd; - // Allocate a packet + /* allocate a packet */ client_loc->afc_packet = (AFCPacket *) malloc(sizeof(AFCPacket)); if (!client_loc->afc_packet) { usbmuxd_disconnect(client_loc->sfd); free(client_loc); - return IPHONE_E_UNKNOWN_ERROR; + return AFC_E_NO_MEM; } client_loc->afc_packet->packet_num = 0; @@ -96,17 +94,17 @@ iphone_error_t afc_new_client(iphone_device_t device, int dst_port, afc_client_t client_loc->mutex = g_mutex_new(); *client = client_loc; - return IPHONE_E_SUCCESS; + return AFC_E_SUCCESS; } /** Disconnects an AFC client from the phone. * * @param client The client to disconnect. */ -iphone_error_t afc_free_client(afc_client_t client) +afc_error_t afc_client_free(afc_client_t client) { if (!client || client->sfd < 0 || !client->afc_packet) - return IPHONE_E_INVALID_ARG; + return AFC_E_INVALID_ARGUMENT; usbmuxd_disconnect(client->sfd); free(client->afc_packet); @@ -214,13 +212,14 @@ int afc_get_errno(afc_client_t client) * reason is that if you set them to different values, it indicates * you want to send the data as two packets. */ -static int dispatch_AFC_packet(afc_client_t client, const char *data, uint64_t length) +static int afc_dispatch_packet(afc_client_t client, const char *data, uint64_t length) { int bytes = 0, offset = 0; char *buffer; if (!client || client->sfd < 0 || !client->afc_packet) return 0; + if (!data || !length) length = 0; @@ -319,20 +318,20 @@ static int receive_AFC_data(afc_client_t client, char **dump_here) return -1; } - // check if it's a valid AFC header + /* check if it's a valid AFC header */ if (strncmp(header.magic, AFC_MAGIC, AFC_MAGIC_LEN)) { log_debug_msg("%s: Invalid AFC packet received (magic != " AFC_MAGIC ")!\n", __func__); } - // check if it has the correct packet number + /* check if it has the correct packet number */ if (header.packet_num != client->afc_packet->packet_num) { - // otherwise print a warning but do not abort + /* otherwise print a warning but do not abort */ log_debug_msg("%s: ERROR: Unexpected packet number (%lld != %lld) aborting.\n", __func__, header.packet_num, client->afc_packet->packet_num); *dump_here = NULL; return -1; } - // then, read the attached packet + /* then, read the attached packet */ if (header.this_length < sizeof(AFCPacket)) { log_debug_msg("%s: Invalid AFCPacket header received!\n", __func__); *dump_here = NULL; @@ -354,7 +353,7 @@ static int receive_AFC_data(afc_client_t client, char **dump_here) entire_len = (uint32_t)header.entire_length - sizeof(AFCPacket); this_len = (uint32_t)header.this_length - sizeof(AFCPacket); - // this is here as a check (perhaps a different upper limit is good?) + /* this is here as a check (perhaps a different upper limit is good?) */ if (entire_len > (uint32_t)MAXIMUM_PACKET_SIZE) { fprintf(stderr, "%s: entire_len is larger than MAXIMUM_PACKET_SIZE, (%d > %d)!\n", __func__, entire_len, MAXIMUM_PACKET_SIZE); } @@ -469,14 +468,14 @@ static char **make_strings_list(char *tokens, int true_length) * @return A char ** list of files in that directory, terminated by an empty * string for now or NULL if there was an error. */ -iphone_error_t afc_get_dir_list(afc_client_t client, const char *dir, char ***list) +afc_error_t afc_read_directory(afc_client_t client, const char *dir, char ***list) { int bytes = 0; char *data = NULL, **list_loc = NULL; - iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; + afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || !dir || !list || (list && *list)) - return IPHONE_E_INVALID_ARG; + return AFC_E_INVALID_ARGUMENT; afc_lock(client); @@ -484,10 +483,10 @@ iphone_error_t afc_get_dir_list(afc_client_t client, const char *dir, char ***li client->afc_packet->operation = AFC_OP_READ_DIR; client->afc_packet->entire_length = 0; client->afc_packet->this_length = 0; - bytes = dispatch_AFC_packet(client, dir, strlen(dir)+1); + bytes = afc_dispatch_packet(client, dir, strlen(dir)+1); if (bytes <= 0) { afc_unlock(client); - return IPHONE_E_NOT_ENOUGH_DATA; + return AFC_E_NOT_ENOUGH_DATA; } // Receive the data bytes = receive_AFC_data(client, &data); @@ -515,23 +514,24 @@ iphone_error_t afc_get_dir_list(afc_client_t client, const char *dir, char ***li * @return A char ** list of parameters as given by AFC or NULL if there was an * error. */ -iphone_error_t afc_get_devinfo(afc_client_t client, char ***infos) +afc_error_t afc_get_device_info(afc_client_t client, char ***infos) { int bytes = 0; char *data = NULL, **list = NULL; + afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || !infos) - return IPHONE_E_INVALID_ARG; + return AFC_E_INVALID_ARGUMENT; afc_lock(client); // Send the command client->afc_packet->operation = AFC_OP_GET_DEVINFO; client->afc_packet->entire_length = client->afc_packet->this_length = 0; - bytes = dispatch_AFC_packet(client, NULL, 0); + bytes = afc_dispatch_packet(client, NULL, 0); if (bytes < 0) { afc_unlock(client); - return IPHONE_E_NOT_ENOUGH_DATA; + return AFC_E_NOT_ENOUGH_DATA; } // Receive the data bytes = receive_AFC_data(client, &data); @@ -547,34 +547,36 @@ iphone_error_t afc_get_devinfo(afc_client_t client, char ***infos) afc_unlock(client); *infos = list; - return IPHONE_E_SUCCESS; + + return ret; } -/** Deletes a file. +/** Deletes a file or directory. * - * @param client The client to have delete the file. - * @param path The file to delete. (must be a fully-qualified path) + * @param client The client to use. + * @param path The path to delete. (must be a fully-qualified path) * - * @return IPHONE_E_SUCCESS if everythong went well, IPHONE_E_INVALID_ARG - * if arguments are NULL or invalid, IPHONE_E_NOT_ENOUGH_DATA otherwise. + * @return AFC_E_SUCCESS if everythong went well, AFC_E_INVALID_ARGUMENT + * if arguments are NULL or invalid, AFC_E_NOT_ENOUGH_DATA otherwise. */ -iphone_error_t afc_delete_file(afc_client_t client, const char *path) +afc_error_t afc_remove_path(afc_client_t client, const char *path) { char *response = NULL; int bytes; + afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || !path || !client->afc_packet || client->sfd < 0) - return IPHONE_E_INVALID_ARG; + return AFC_E_INVALID_ARGUMENT; afc_lock(client); // Send command client->afc_packet->this_length = client->afc_packet->entire_length = 0; client->afc_packet->operation = AFC_OP_REMOVE_PATH; - bytes = dispatch_AFC_packet(client, path, strlen(path)+1); + bytes = afc_dispatch_packet(client, path, strlen(path)+1); if (bytes <= 0) { afc_unlock(client); - return IPHONE_E_NOT_ENOUGH_DATA; + return AFC_E_NOT_ENOUGH_DATA; } // Receive response bytes = receive_AFC_data(client, &response); @@ -583,29 +585,27 @@ iphone_error_t afc_delete_file(afc_client_t client, const char *path) afc_unlock(client); - if (bytes < 0) { - return IPHONE_E_AFC_ERROR; - } - return IPHONE_E_SUCCESS; + return ret; } -/** Renames a file on the phone. +/** Renames a file or directory on the phone. * - * @param client The client to have rename the file. - * @param from The file to rename. (must be a fully-qualified path) - * @param to The new name of the file. (must also be a fully-qualified path) + * @param client The client to have rename. + * @param from The name to rename from. (must be a fully-qualified path) + * @param to The new name. (must also be a fully-qualified path) * - * @return IPHONE_E_SUCCESS if everythong went well, IPHONE_E_INVALID_ARG - * if arguments are NULL or invalid, IPHONE_E_NOT_ENOUGH_DATA otherwise. + * @return AFC_E_SUCCESS if everythong went well, AFC_E_INVALID_ARGUMENT + * if arguments are NULL or invalid, AFC_E_NOT_ENOUGH_DATA otherwise. */ -iphone_error_t afc_rename_file(afc_client_t client, const char *from, const char *to) +afc_error_t afc_rename_path(afc_client_t client, const char *from, const char *to) { char *response = NULL; char *send = (char *) malloc(sizeof(char) * (strlen(from) + strlen(to) + 1 + sizeof(uint32_t))); int bytes = 0; + afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || !from || !to || !client->afc_packet || client->sfd < 0) - return IPHONE_E_INVALID_ARG; + return AFC_E_INVALID_ARGUMENT; afc_lock(client); @@ -614,11 +614,11 @@ iphone_error_t afc_rename_file(afc_client_t client, const char *from, const char memcpy(send + strlen(from) + 1, to, strlen(to) + 1); client->afc_packet->entire_length = client->afc_packet->this_length = 0; client->afc_packet->operation = AFC_OP_RENAME_PATH; - bytes = dispatch_AFC_packet(client, send, strlen(to)+1 + strlen(from)+1); + bytes = afc_dispatch_packet(client, send, strlen(to)+1 + strlen(from)+1); free(send); if (bytes <= 0) { afc_unlock(client); - return IPHONE_E_NOT_ENOUGH_DATA; + return AFC_E_NOT_ENOUGH_DATA; } // Receive response bytes = receive_AFC_data(client, &response); @@ -627,10 +627,7 @@ iphone_error_t afc_rename_file(afc_client_t client, const char *from, const char afc_unlock(client); - if (bytes < 0) { - return IPHONE_E_AFC_ERROR; - } - return IPHONE_E_SUCCESS; + return ret; } /** Creates a directory on the phone. @@ -639,26 +636,27 @@ iphone_error_t afc_rename_file(afc_client_t client, const char *from, const char * @param dir The directory's path. (must be a fully-qualified path, I assume * all other mkdir restrictions apply as well) * - * @return IPHONE_E_SUCCESS if everythong went well, IPHONE_E_INVALID_ARG - * if arguments are NULL or invalid, IPHONE_E_NOT_ENOUGH_DATA otherwise. + * @return AFC_E_SUCCESS if everythong went well, AFC_E_INVALID_ARGUMENT + * if arguments are NULL or invalid, AFC_E_NOT_ENOUGH_DATA otherwise. */ -iphone_error_t afc_mkdir(afc_client_t client, const char *dir) +afc_error_t afc_make_directory(afc_client_t client, const char *dir) { int bytes = 0; char *response = NULL; + afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client) - return IPHONE_E_INVALID_ARG; + return AFC_E_INVALID_ARGUMENT; afc_lock(client); // Send command client->afc_packet->operation = AFC_OP_MAKE_DIR; client->afc_packet->this_length = client->afc_packet->entire_length = 0; - bytes = dispatch_AFC_packet(client, dir, strlen(dir)+1); + bytes = afc_dispatch_packet(client, dir, strlen(dir)+1); if (bytes <= 0) { afc_unlock(client); - return IPHONE_E_NOT_ENOUGH_DATA; + return AFC_E_NOT_ENOUGH_DATA; } // Receive response bytes = receive_AFC_data(client, &response); @@ -667,10 +665,7 @@ iphone_error_t afc_mkdir(afc_client_t client, const char *dir) afc_unlock(client); - if (bytes < 0) { - return IPHONE_E_AFC_ERROR; - } - return IPHONE_E_SUCCESS; + return ret; } /** Gets information about a specific file. @@ -681,38 +676,37 @@ iphone_error_t afc_mkdir(afc_client_t client, const char *dir) * list of strings with the file information. * Set to NULL before calling this function. * - * @return IPHONE_E_SUCCESS on success or an IPHONE_E_* error value + * @return AFC_E_SUCCESS on success or an AFC_E_* error value * when something went wrong. */ -iphone_error_t afc_get_file_info(afc_client_t client, const char *path, char ***infolist) +afc_error_t afc_get_file_info(afc_client_t client, const char *path, char ***infolist) { char *received = NULL; - int length; + int bytes; + afc_error_t ret = AFC_E_UNKNOWN_ERROR; - if (!client || !path || !infolist) { - return IPHONE_E_INVALID_ARG; - } + if (!client || !path || !infolist) + return AFC_E_INVALID_ARGUMENT; afc_lock(client); // Send command client->afc_packet->operation = AFC_OP_GET_FILE_INFO; client->afc_packet->entire_length = client->afc_packet->this_length = 0; - dispatch_AFC_packet(client, path, strlen(path)+1); + afc_dispatch_packet(client, path, strlen(path)+1); // Receive data length = receive_AFC_data(client, &received); if (received) { - *infolist = make_strings_list(received, length); + *infolist = make_strings_list(received, bytes); free(received); - } else { - afc_unlock(client); - return IPHONE_E_AFC_ERROR; } + log_debug_msg("%s: Didn't get any further data\n", __func__); + afc_unlock(client); - return IPHONE_E_SUCCESS; + return ret; } /** Opens a file on the phone. @@ -725,21 +719,22 @@ iphone_error_t afc_get_file_info(afc_client_t client, const char *path, char *** * destroying anything previously there. * @param handle Pointer to a uint64_t that will hold the handle of the file * - * @return IPHONE_E_SUCCESS on success or an IPHONE_E_* error on failure. + * @return AFC_E_SUCCESS on success or an AFC_E_* error on failure. */ iphone_error_t -afc_open_file(afc_client_t client, const char *filename, +afc_file_open(afc_client_t client, const char *filename, afc_file_mode_t file_mode, uint64_t *handle) { uint32_t ag = 0; - int bytes = 0, length = 0; + int bytes = 0; char *data = (char *) malloc(sizeof(char) * (8 + strlen(filename) + 1)); + afc_error_t ret = AFC_E_UNKNOWN_ERROR; // set handle to 0 so in case an error occurs, the handle is invalid *handle = 0; if (!client || client->sfd < 0|| !client->afc_packet) - return IPHONE_E_INVALID_ARG; + return AFC_E_INVALID_ARGUMENT; afc_lock(client); @@ -750,13 +745,13 @@ afc_open_file(afc_client_t client, const char *filename, data[8 + strlen(filename)] = '\0'; client->afc_packet->operation = AFC_OP_FILE_OPEN; client->afc_packet->entire_length = client->afc_packet->this_length = 0; - bytes = dispatch_AFC_packet(client, data, 8 + strlen(filename) + 1); + bytes = afc_dispatch_packet(client, data, 8 + strlen(filename) + 1); free(data); if (bytes <= 0) { log_debug_msg("%s: Didn't receive a response to the command\n", __func__); afc_unlock(client); - return IPHONE_E_NOT_ENOUGH_DATA; + return AFC_E_NOT_ENOUGH_DATA; } // Receive the data length = receive_AFC_data(client, &data); @@ -766,16 +761,12 @@ afc_open_file(afc_client_t client, const char *filename, // Get the file handle memcpy(handle, data, sizeof(uint64_t)); free(data); - return IPHONE_E_SUCCESS; - } else { - log_debug_msg("afc_open_file: Didn't get any further data\n"); - afc_unlock(client); - return IPHONE_E_AFC_ERROR; + return ret; } afc_unlock(client); - return IPHONE_E_UNKNOWN_ERROR; + return ret; } /** Attempts to the read the given number of bytes from the given file. @@ -788,20 +779,21 @@ afc_open_file(afc_client_t client, const char *filename, * @return The number of bytes read if successful. If there was an error -1. */ iphone_error_t -afc_read_file(afc_client_t client, uint64_t handle, char *data, int length, uint32_t * bytes) +afc_file_read(afc_client_t client, uint64_t handle, char *data, int length, uint32_t * bytes) { char *input = NULL; int current_count = 0, bytes_loc = 0; const int MAXIMUM_READ_SIZE = 1 << 16; + afc_error_t ret = AFC_E_SUCCESS; if (!client || !client->afc_packet || client->sfd < 0 || handle == 0) - return IPHONE_E_INVALID_ARG; + return AFC_E_INVALID_ARGUMENT; log_debug_msg("%s: called for length %i\n", __func__, length); afc_lock(client); // Looping here to get around the maximum amount of data that - // recieve_AFC_data can handle + // afc_receive_data can handle while (current_count < length) { log_debug_msg("%s: current count is %i but length is %i\n", __func__, current_count, length); @@ -811,26 +803,26 @@ afc_read_file(afc_client_t client, uint64_t handle, char *data, int length, uint packet->size = ((length - current_count) < MAXIMUM_READ_SIZE) ? (length - current_count) : MAXIMUM_READ_SIZE; client->afc_packet->operation = AFC_OP_READ; client->afc_packet->entire_length = client->afc_packet->this_length = 0; - bytes_loc = dispatch_AFC_packet(client, (char *) packet, sizeof(AFCFilePacket)); + bytes_loc = afc_dispatch_packet(client, (char *) packet, sizeof(AFCFilePacket)); free(packet); if (bytes_loc <= 0) { afc_unlock(client); - return IPHONE_E_NOT_ENOUGH_DATA; + return AFC_E_NOT_ENOUGH_DATA; } // Receive the data bytes_loc = receive_AFC_data(client, &input); log_debug_msg("%s: bytes returned: %i\n", __func__, bytes_loc); if (bytes_loc < 0) { afc_unlock(client); - return IPHONE_E_AFC_ERROR; + return ret; } else if (bytes_loc == 0) { if (input) free(input); afc_unlock(client); *bytes = current_count; - return IPHONE_E_SUCCESS; // FIXME check that's actually a - // success + /* FIXME: check that's actually a success */ + return ret; } else { if (input) { log_debug_msg("%s: %d\n", __func__, bytes_loc); @@ -845,7 +837,7 @@ afc_read_file(afc_client_t client, uint64_t handle, char *data, int length, uint afc_unlock(client); *bytes = current_count; - return IPHONE_E_SUCCESS; + return ret; } /** Writes a given number of bytes to a file. @@ -859,7 +851,7 @@ afc_read_file(afc_client_t client, uint64_t handle, char *data, int length, uint * none were written... */ iphone_error_t -afc_write_file(afc_client_t client, uint64_t handle, +afc_file_write(afc_client_t client, uint64_t handle, const char *data, int length, uint32_t * bytes) { char *acknowledgement = NULL; @@ -868,9 +860,10 @@ afc_write_file(afc_client_t client, uint64_t handle, uint32_t segments = (length / MAXIMUM_WRITE_SIZE); int bytes_loc = 0; char *out_buffer = NULL; + afc_error_t ret = AFC_E_SUCCESS; if (!client || !client->afc_packet || client->sfd < 0 || !bytes || (handle == 0)) - return IPHONE_E_INVALID_ARG; + return AFC_E_INVALID_ARGUMENT; afc_lock(client); @@ -885,10 +878,10 @@ afc_write_file(afc_client_t client, uint64_t handle, out_buffer = (char *) malloc(sizeof(char) * client->afc_packet->entire_length - sizeof(AFCPacket)); memcpy(out_buffer, (char *)&handle, sizeof(uint64_t)); memcpy(out_buffer + 8, data + current_count, MAXIMUM_WRITE_SIZE); - bytes_loc = dispatch_AFC_packet(client, out_buffer, MAXIMUM_WRITE_SIZE + 8); + bytes_loc = afc_dispatch_packet(client, out_buffer, MAXIMUM_WRITE_SIZE + 8); if (bytes_loc < 0) { afc_unlock(client); - return IPHONE_E_NOT_ENOUGH_DATA; + return AFC_E_NOT_ENOUGH_DATA; } free(out_buffer); out_buffer = NULL; @@ -897,7 +890,7 @@ afc_write_file(afc_client_t client, uint64_t handle, bytes_loc = receive_AFC_data(client, &acknowledgement); if (bytes_loc < 0) { afc_unlock(client); - return IPHONE_E_AFC_ERROR; + return ret; } else { free(acknowledgement); } @@ -910,7 +903,7 @@ afc_write_file(afc_client_t client, uint64_t handle, if (current_count == (uint32_t)length) { afc_unlock(client); *bytes = current_count; - return IPHONE_E_SUCCESS; + return ret; } client->afc_packet->this_length = sizeof(AFCPacket) + 8; @@ -919,7 +912,7 @@ afc_write_file(afc_client_t client, uint64_t handle, out_buffer = (char *) malloc(sizeof(char) * client->afc_packet->entire_length - sizeof(AFCPacket)); memcpy(out_buffer, (char *) &handle, sizeof(uint64_t)); memcpy(out_buffer + 8, data + current_count, (length - current_count)); - bytes_loc = dispatch_AFC_packet(client, out_buffer, (length - current_count) + 8); + bytes_loc = afc_dispatch_packet(client, out_buffer, (length - current_count) + 8); free(out_buffer); out_buffer = NULL; @@ -928,7 +921,7 @@ afc_write_file(afc_client_t client, uint64_t handle, if (bytes_loc <= 0) { afc_unlock(client); *bytes = current_count; - return IPHONE_E_SUCCESS; + return AFC_E_SUCCESS; } zero = bytes_loc; @@ -940,7 +933,7 @@ afc_write_file(afc_client_t client, uint64_t handle, free(acknowledgement); } *bytes = current_count; - return IPHONE_E_SUCCESS; + return ret; } /** Closes a file on the phone. @@ -948,12 +941,14 @@ afc_write_file(afc_client_t client, uint64_t handle, * @param client The client to close the file with. * @param handle File handle of a previously opened file. */ -iphone_error_t afc_close_file(afc_client_t client, uint64_t handle) +afc_error_t afc_file_close(afc_client_t client, uint64_t handle) { - if (!client || (handle == 0)) - return IPHONE_E_INVALID_ARG; char *buffer = malloc(sizeof(char) * 8); int bytes = 0; + afc_error_t ret = AFC_E_UNKNOWN_ERROR; + + if (!client || (handle == 0)) + return AFC_E_INVALID_ARGUMENT; afc_lock(client); @@ -963,13 +958,13 @@ iphone_error_t afc_close_file(afc_client_t client, uint64_t handle) memcpy(buffer, &handle, sizeof(uint64_t)); client->afc_packet->operation = AFC_OP_FILE_CLOSE; client->afc_packet->entire_length = client->afc_packet->this_length = 0; - bytes = dispatch_AFC_packet(client, buffer, 8); + bytes = afc_dispatch_packet(client, buffer, 8); free(buffer); buffer = NULL; if (bytes <= 0) { afc_unlock(client); - return IPHONE_E_UNKNOWN_ERROR; + return AFC_E_UNKNOWN_ERROR; } // Receive the response @@ -978,7 +973,8 @@ iphone_error_t afc_close_file(afc_client_t client, uint64_t handle) free(buffer); afc_unlock(client); - return IPHONE_E_SUCCESS; + + return ret; } /** Locks or unlocks a file on the phone. @@ -992,13 +988,15 @@ iphone_error_t afc_close_file(afc_client_t client, uint64_t handle) * AFC_LOCK_SH (shared lock), AFC_LOCK_EX (exclusive lock), * or AFC_LOCK_UN (unlock). */ -iphone_error_t afc_lock_file(afc_client_t client, uint64_t handle, afc_lock_op_t operation) +afc_error_t afc_file_lock(afc_client_t client, uint64_t handle, afc_lock_op_t operation) { - if (!client || (handle == 0)) - return IPHONE_E_INVALID_ARG; char *buffer = malloc(16); int bytes = 0; uint64_t op = operation; + afc_error_t ret = AFC_E_UNKNOWN_ERROR; + + if (!client || (handle == 0)) + return AFC_E_INVALID_ARGUMENT; afc_lock(client); @@ -1010,14 +1008,14 @@ iphone_error_t afc_lock_file(afc_client_t client, uint64_t handle, afc_lock_op_t client->afc_packet->operation = AFC_OP_FILE_LOCK; client->afc_packet->entire_length = client->afc_packet->this_length = 0; - bytes = dispatch_AFC_packet(client, buffer, 16); + bytes = afc_dispatch_packet(client, buffer, 16); free(buffer); buffer = NULL; if (bytes <= 0) { afc_unlock(client); log_debug_msg("%s: could not send lock command\n", __func__); - return IPHONE_E_UNKNOWN_ERROR; + return AFC_E_UNKNOWN_ERROR; } // Receive the response bytes = receive_AFC_data(client, &buffer); @@ -1026,10 +1024,8 @@ iphone_error_t afc_lock_file(afc_client_t client, uint64_t handle, afc_lock_op_t free(buffer); } afc_unlock(client); - if (bytes < 0) { - return IPHONE_E_AFC_ERROR; - } - return IPHONE_E_SUCCESS; + + return ret; } /** Seeks to a given position of a pre-opened file on the phone. @@ -1039,13 +1035,17 @@ iphone_error_t afc_lock_file(afc_client_t client, uint64_t handle, afc_lock_op_t * @param offset Seek offset. * @param whence Seeking direction, one of SEEK_SET, SEEK_CUR, or SEEK_END. * - * @return IPHONE_E_SUCCESS on success, IPHONE_E_NOT_ENOUGH_DATA on failure. + * @return AFC_E_SUCCESS on success, AFC_E_NOT_ENOUGH_DATA on failure. */ -iphone_error_t afc_seek_file(afc_client_t client, uint64_t handle, int64_t offset, int whence) +afc_error_t afc_file_seek(afc_client_t client, uint64_t handle, int64_t offset, int whence) { char *buffer = (char *) malloc(sizeof(char) * 24); uint32_t zero = 0; int bytes = 0; + afc_error_t ret = AFC_E_UNKNOWN_ERROR; + + if (!client || (handle == 0)) + return AFC_E_INVALID_ARGUMENT; afc_lock(client); @@ -1056,13 +1056,13 @@ iphone_error_t afc_seek_file(afc_client_t client, uint64_t handle, int64_t offse memcpy(buffer + 16, &offset, sizeof(uint64_t)); // offset client->afc_packet->operation = AFC_OP_FILE_SEEK; client->afc_packet->this_length = client->afc_packet->entire_length = 0; - bytes = dispatch_AFC_packet(client, buffer, 24); + bytes = afc_dispatch_packet(client, buffer, 24); free(buffer); buffer = NULL; if (bytes <= 0) { afc_unlock(client); - return IPHONE_E_NOT_ENOUGH_DATA; + return AFC_E_NOT_ENOUGH_DATA; } // Receive response bytes = receive_AFC_data(client, &buffer); @@ -1088,10 +1088,14 @@ iphone_error_t afc_seek_file(afc_client_t client, uint64_t handle, int64_t offse * @note This function is more akin to ftruncate than truncate, and truncate * calls would have to open the file before calling this, sadly. */ -iphone_error_t afc_truncate_file(afc_client_t client, uint64_t handle, uint64_t newsize) +afc_error_t afc_file_truncate(afc_client_t client, uint64_t handle, uint64_t newsize) { char *buffer = (char *) malloc(sizeof(char) * 16); int bytes = 0; + afc_error_t ret = AFC_E_UNKNOWN_ERROR; + + if (!client || (handle == 0)) + return AFC_E_INVALID_ARGUMENT; afc_lock(client); @@ -1100,13 +1104,13 @@ iphone_error_t afc_truncate_file(afc_client_t client, uint64_t handle, uint64_t memcpy(buffer + 8, &newsize, sizeof(uint64_t)); // newsize client->afc_packet->operation = AFC_OP_FILE_SET_SIZE; client->afc_packet->this_length = client->afc_packet->entire_length = 0; - bytes = dispatch_AFC_packet(client, buffer, 16); + bytes = afc_dispatch_packet(client, buffer, 16); free(buffer); buffer = NULL; if (bytes <= 0) { afc_unlock(client); - return IPHONE_E_NOT_ENOUGH_DATA; + return AFC_E_NOT_ENOUGH_DATA; } // Receive response bytes = receive_AFC_data(client, &buffer); @@ -1115,10 +1119,7 @@ iphone_error_t afc_truncate_file(afc_client_t client, uint64_t handle, uint64_t afc_unlock(client); - if (bytes < 0) { - return IPHONE_E_AFC_ERROR; - } - return IPHONE_E_SUCCESS; + return ret; } /** Sets the size of a file on the phone without prior opening it. @@ -1127,18 +1128,19 @@ iphone_error_t afc_truncate_file(afc_client_t client, uint64_t handle, uint64_t * @param path The path of the file to be truncated. * @param newsize The size to set the file to. * - * @return IPHONE_E_SUCCESS if everything went well, IPHONE_E_INVALID_ARG - * if arguments are NULL or invalid, IPHONE_E_NOT_ENOUGH_DATA otherwise. + * @return AFC_E_SUCCESS if everything went well, AFC_E_INVALID_ARGUMENT + * if arguments are NULL or invalid, AFC_E_NOT_ENOUGH_DATA otherwise. */ -iphone_error_t afc_truncate(afc_client_t client, const char *path, off_t newsize) +afc_error_t afc_truncate(afc_client_t client, const char *path, off_t newsize) { char *response = NULL; char *send = (char *) malloc(sizeof(char) * (strlen(path) + 1 + 8)); int bytes = 0; uint64_t size_requested = newsize; + afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || !path || !client->afc_packet || client->sfd < 0) - return IPHONE_E_INVALID_ARG; + return AFC_E_INVALID_ARGUMENT; afc_lock(client); @@ -1147,11 +1149,11 @@ iphone_error_t afc_truncate(afc_client_t client, const char *path, off_t newsize memcpy(send + 8, path, strlen(path) + 1); client->afc_packet->entire_length = client->afc_packet->this_length = 0; client->afc_packet->operation = AFC_OP_TRUNCATE; - bytes = dispatch_AFC_packet(client, send, 8 + strlen(path) + 1); + bytes = afc_dispatch_packet(client, send, 8 + strlen(path) + 1); free(send); if (bytes <= 0) { afc_unlock(client); - return IPHONE_E_NOT_ENOUGH_DATA; + return AFC_E_NOT_ENOUGH_DATA; } // Receive response bytes = receive_AFC_data(client, &response); @@ -1160,10 +1162,7 @@ iphone_error_t afc_truncate(afc_client_t client, const char *path, off_t newsize afc_unlock(client); - if (bytes < 0) { - return IPHONE_E_AFC_ERROR; - } - return IPHONE_E_SUCCESS; + return ret; } /** Creates a hard link or symbolic link on the device. @@ -1173,18 +1172,19 @@ iphone_error_t afc_truncate(afc_client_t client, const char *path, off_t newsize * @param target The file to be linked. * @param linkname The name of link. * - * @return IPHONE_E_SUCCESS if everything went well, IPHONE_E_INVALID_ARG - * if arguments are NULL or invalid, IPHONE_E_NOT_ENOUGH_DATA otherwise. + * @return AFC_E_SUCCESS if everything went well, AFC_E_INVALID_ARGUMENT + * if arguments are NULL or invalid, AFC_E_NOT_ENOUGH_DATA otherwise. */ -iphone_error_t afc_make_link(afc_client_t client, afc_link_type_t linktype, const char *target, const char *linkname) +afc_error_t afc_make_link(afc_client_t client, afc_link_type_t linktype, const char *target, const char *linkname) { char *response = NULL; char *send = (char *) malloc(sizeof(char) * (strlen(target)+1 + strlen(linkname)+1 + 8)); int bytes = 0; uint64_t type = linktype; + afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || !target || !linkname || !client->afc_packet || client->sfd < 0) - return IPHONE_E_INVALID_ARG; + return AFC_E_INVALID_ARGUMENT; afc_lock(client); @@ -1198,11 +1198,11 @@ iphone_error_t afc_make_link(afc_client_t client, afc_link_type_t linktype, cons memcpy(send + 8 + strlen(target) + 1, linkname, strlen(linkname) + 1); client->afc_packet->entire_length = client->afc_packet->this_length = 0; client->afc_packet->operation = AFC_OP_MAKE_LINK; - bytes = dispatch_AFC_packet(client, send, 8 + strlen(linkname) + 1 + strlen(target) + 1); + bytes = afc_dispatch_packet(client, send, 8 + strlen(linkname) + 1 + strlen(target) + 1); free(send); if (bytes <= 0) { afc_unlock(client); - return IPHONE_E_NOT_ENOUGH_DATA; + return AFC_E_NOT_ENOUGH_DATA; } // Receive response bytes = receive_AFC_data(client, &response); @@ -1211,9 +1211,5 @@ iphone_error_t afc_make_link(afc_client_t client, afc_link_type_t linktype, cons afc_unlock(client); - if (bytes < 0) { - return IPHONE_E_NOT_ENOUGH_DATA; - } else { - return IPHONE_E_SUCCESS; - } + return ret; } diff --git a/src/AFC.h b/src/AFC.h index a0ce0ef..909e8c3 100644 --- a/src/AFC.h +++ b/src/AFC.h @@ -26,6 +26,8 @@ #include #include +#include "libiphone/afc.h" + #define AFC_MAGIC "CFA6LPAA" #define AFC_MAGIC_LEN (8) -- 1.6.0.2