diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/AFC.c | 306 | ||||
| -rw-r--r-- | src/AFC.h | 2 | 
2 files changed, 153 insertions, 155 deletions
| @@ -23,13 +23,11 @@  #include <stdlib.h>  #include <errno.h>  #include <unistd.h> +  #include "AFC.h"  #include "iphone.h"  #include "utils.h" -#include <libiphone/afc.h> - -  // 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;  } @@ -26,6 +26,8 @@  #include <glib.h>  #include <stdint.h> +#include "libiphone/afc.h" +  #define AFC_MAGIC "CFA6LPAA"  #define AFC_MAGIC_LEN (8) | 
