diff options
Diffstat (limited to 'src/AFC.c')
-rw-r--r-- | src/AFC.c | 266 |
1 files changed, 142 insertions, 124 deletions
@@ -119,21 +119,24 @@ afc_error_t afc_client_free(afc_client_t client) * @param client The client to send data through. * @param data The data to send. * @param length The length to send. - * - * @return The number of bytes actually sent, or -1 on error. + * @param bytes_sent The number of bytes actually sent. + * + * @return AFC_E_SUCCESS on success, or an AFC_E_* error value on error. * * @warning set client->afc_packet->this_length and * client->afc_packet->entire_length to 0 before calling this. The * reason is that if you set them to different values, it indicates * you want to send the data as two packets. */ -static int afc_dispatch_packet(afc_client_t client, const char *data, uint64_t length) +static afc_error_t afc_dispatch_packet(afc_client_t client, const char *data, uint32_t length, uint32_t *bytes_sent) { - int bytes = 0, offset = 0; + uint32_t offset = 0; uint32_t sent = 0; if (!client || !client->connection || !client->afc_packet) - return 0; + return AFC_E_INVALID_ARGUMENT; + + *bytes_sent = 0; if (!data || !length) length = 0; @@ -158,20 +161,26 @@ static int afc_dispatch_packet(afc_client_t client, const char *data, uint64_t l log_debug_msg("to based on the packet.\n"); log_debug_msg("%s: length minus offset: %i\n", __func__, length - offset); log_debug_msg("%s: rest of packet: %i\n", __func__, client->afc_packet->entire_length - client->afc_packet->this_length); - return -1; + return AFC_E_INTERNAL_ERROR; } + /* send AFC packet header */ + AFCPacket_to_LE(client->afc_packet); + sent = 0; iphone_device_send(client->connection, (void*)client->afc_packet, sizeof(AFCPacket), &sent); if (sent == 0) { - return bytes; + /* FIXME: should this be handled as success?! */ + return AFC_E_SUCCESS; } - bytes += sent; + *bytes_sent += sent; + /* send AFC packet data */ + sent = 0; iphone_device_send(client->connection, data, offset, &sent); if (sent == 0) { - return bytes; + return AFC_E_SUCCESS; } - bytes += sent; + *bytes_sent += sent; log_debug_msg("%s: sent the first now go with the second\n", __func__); log_debug_msg("%s: Length: %i\n", __func__, length - offset); @@ -181,8 +190,8 @@ static int afc_dispatch_packet(afc_client_t client, const char *data, uint64_t l sent = 0; iphone_device_send(client->connection, data + offset, length - offset, &sent); - bytes = sent; - return bytes; + *bytes_sent = sent; + return AFC_E_SUCCESS; } else { log_debug_msg("%s: doin things the old way\n", __func__); log_debug_msg("%s: packet length = %i\n", __func__, client->afc_packet->this_length); @@ -190,35 +199,38 @@ static int afc_dispatch_packet(afc_client_t client, const char *data, uint64_t l log_debug_buffer((char*)client->afc_packet, sizeof(AFCPacket)); log_debug_msg("\n"); + /* send AFC packet header */ + AFCPacket_to_LE(client->afc_packet); + sent = 0; iphone_device_send(client->connection, (void*)client->afc_packet, sizeof(AFCPacket), &sent); if (sent == 0) { - return bytes; + return AFC_E_SUCCESS; } - bytes += sent; + *bytes_sent += sent; + /* send AFC packet data (if there's data to send) */ if (length > 0) { log_debug_msg("%s: packet data follows\n", __func__); log_debug_buffer(data, length); log_debug_msg("\n"); iphone_device_send(client->connection, data, length, &sent); - bytes += sent; + *bytes_sent += sent; } - return bytes; + return AFC_E_SUCCESS; } - return -1; + return AFC_E_INTERNAL_ERROR; } /** Receives data through an AFC client and sets a variable to the received data. * * @param client The client to receive data on. * @param dump_here The char* to point to the newly-received data. + * @param bytes_recv How much data was received. * - * @return How much data was received, 0 on successful receive with no errors, - * -1 if there was an error involved with receiving or if the packet - * received raised a non-trivial error condition (i.e. non-zero with - * AFC_ERROR operation) + * @return AFC_E_SUCCESS when data has been received, or an AFC_E_* error value + * when an error occured. */ -static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, int *bytes) +static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, uint32_t *bytes_recv) { AFCPacket header; uint32_t entire_len = 0; @@ -226,15 +238,16 @@ static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, int * uint32_t current_count = 0; uint64_t param1 = -1; - *bytes = 0; + *bytes_recv = 0; /* first, read the AFC header */ - iphone_device_recv(client->connection, (char*)&header, sizeof(AFCPacket), (uint32_t*)bytes); - if (*bytes <= 0) { + iphone_device_recv(client->connection, (char*)&header, sizeof(AFCPacket), bytes_recv); + AFCPacket_from_LE(&header); + if (*bytes_recv == 0) { log_debug_msg("%s: Just didn't get enough.\n", __func__); *dump_here = NULL; return AFC_E_MUX_ERROR; - } else if ((uint32_t)*bytes < sizeof(AFCPacket)) { + } else if (*bytes_recv < sizeof(AFCPacket)) { log_debug_msg("%s: Did not even get the AFCPacket header\n", __func__); *dump_here = NULL; return AFC_E_MUX_ERROR; @@ -262,7 +275,7 @@ static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, int * && header.entire_length == sizeof(AFCPacket)) { log_debug_msg("%s: Empty AFCPacket received!\n", __func__); *dump_here = NULL; - *bytes = 0; + *bytes_recv = 0; if (header.operation == AFC_OP_DATA) { return AFC_E_SUCCESS; } else { @@ -282,13 +295,13 @@ static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, int * *dump_here = (char*)malloc(entire_len); if (this_len > 0) { - iphone_device_recv(client->connection, *dump_here, this_len, (uint32_t*)bytes); - if (*bytes <= 0) { + iphone_device_recv(client->connection, *dump_here, this_len, bytes_recv); + if (*bytes_recv <= 0) { free(*dump_here); *dump_here = NULL; log_debug_msg("%s: Did not get packet contents!\n", __func__); return AFC_E_NOT_ENOUGH_DATA; - } else if ((uint32_t)*bytes < this_len) { + } else if (*bytes_recv < this_len) { free(*dump_here); *dump_here = NULL; log_debug_msg("%s: Could not receive this_len=%d bytes\n", __func__, this_len); @@ -300,12 +313,12 @@ static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, int * if (entire_len > this_len) { while (current_count < entire_len) { - iphone_device_recv(client->connection, (*dump_here)+current_count, entire_len - current_count, (uint32_t*)bytes); - if (*bytes <= 0) { - log_debug_msg("%s: Error receiving data (recv returned %d)\n", __func__, *bytes); + iphone_device_recv(client->connection, (*dump_here)+current_count, entire_len - current_count, bytes_recv); + if (*bytes_recv <= 0) { + log_debug_msg("%s: Error receiving data (recv returned %d)\n", __func__, *bytes_recv); break; } - current_count += *bytes; + current_count += *bytes_recv; } if (current_count < entire_len) { log_debug_msg("%s: WARNING: could not receive full packet (read %s, size %d)\n", __func__, current_count, entire_len); @@ -345,7 +358,7 @@ static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, int * /* unknown operation code received */ free(*dump_here); *dump_here = NULL; - *bytes = 0; + *bytes_recv = 0; log_debug_msg("%s: WARNING: Unknown operation code received 0x%llx param1=%lld\n", __func__, header.operation, param1); fprintf(stderr, "%s: WARNING: Unknown operation code received 0x%llx param1=%lld\n", __func__, (long long)header.operation, (long long)param1); @@ -353,13 +366,13 @@ static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, int * return AFC_E_OP_NOT_SUPPORTED; } - *bytes = current_count; + *bytes_recv = current_count; return AFC_E_SUCCESS; } -static int count_nullspaces(char *string, int number) +static uint32_t count_nullspaces(char *string, uint32_t number) { - int i = 0, nulls = 0; + uint32_t i = 0, nulls = 0; for (i = 0; i < number; i++) { if (string[i] == '\0') @@ -369,9 +382,9 @@ static int count_nullspaces(char *string, int number) return nulls; } -static char **make_strings_list(char *tokens, int true_length) +static char **make_strings_list(char *tokens, uint32_t true_length) { - int nulls = 0, i = 0, j = 0; + uint32_t nulls = 0, i = 0, j = 0; char **list = NULL; if (!tokens || !true_length) @@ -398,7 +411,7 @@ static char **make_strings_list(char *tokens, int true_length) */ afc_error_t afc_read_directory(afc_client_t client, const char *dir, char ***list) { - int bytes = 0; + uint32_t bytes = 0; char *data = NULL, **list_loc = NULL; afc_error_t ret = AFC_E_UNKNOWN_ERROR; @@ -411,8 +424,8 @@ afc_error_t afc_read_directory(afc_client_t client, const char *dir, char ***lis client->afc_packet->operation = AFC_OP_READ_DIR; client->afc_packet->entire_length = 0; client->afc_packet->this_length = 0; - bytes = afc_dispatch_packet(client, dir, strlen(dir)+1); - if (bytes <= 0) { + ret = afc_dispatch_packet(client, dir, strlen(dir)+1, &bytes); + if (ret != AFC_E_SUCCESS) { afc_unlock(client); return AFC_E_NOT_ENOUGH_DATA; } @@ -442,7 +455,7 @@ afc_error_t afc_read_directory(afc_client_t client, const char *dir, char ***lis */ afc_error_t afc_get_device_info(afc_client_t client, char ***infos) { - int bytes = 0; + uint32_t bytes = 0; char *data = NULL, **list = NULL; afc_error_t ret = AFC_E_UNKNOWN_ERROR; @@ -454,8 +467,8 @@ afc_error_t afc_get_device_info(afc_client_t client, char ***infos) // Send the command client->afc_packet->operation = AFC_OP_GET_DEVINFO; client->afc_packet->entire_length = client->afc_packet->this_length = 0; - bytes = afc_dispatch_packet(client, NULL, 0); - if (bytes < 0) { + ret = afc_dispatch_packet(client, NULL, 0, &bytes); + if (ret != AFC_E_SUCCESS) { afc_unlock(client); return AFC_E_NOT_ENOUGH_DATA; } @@ -523,7 +536,7 @@ afc_error_t afc_get_device_info_key(afc_client_t client, const char *key, char * afc_error_t afc_remove_path(afc_client_t client, const char *path) { char *response = NULL; - int bytes; + uint32_t bytes = 0; afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || !path || !client->afc_packet || !client->connection) @@ -534,8 +547,8 @@ afc_error_t afc_remove_path(afc_client_t client, const char *path) // Send command client->afc_packet->this_length = client->afc_packet->entire_length = 0; client->afc_packet->operation = AFC_OP_REMOVE_PATH; - bytes = afc_dispatch_packet(client, path, strlen(path)+1); - if (bytes <= 0) { + ret = afc_dispatch_packet(client, path, strlen(path)+1, &bytes); + if (ret != AFC_E_SUCCESS) { afc_unlock(client); return AFC_E_NOT_ENOUGH_DATA; } @@ -566,7 +579,7 @@ afc_error_t afc_rename_path(afc_client_t client, const char *from, const char *t { char *response = NULL; char *send = (char *) malloc(sizeof(char) * (strlen(from) + strlen(to) + 1 + sizeof(uint32_t))); - int bytes = 0; + uint32_t bytes = 0; afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || !from || !to || !client->afc_packet || !client->connection) @@ -579,9 +592,9 @@ afc_error_t afc_rename_path(afc_client_t client, const char *from, const char *t 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 = afc_dispatch_packet(client, send, strlen(to)+1 + strlen(from)+1); + ret = afc_dispatch_packet(client, send, strlen(to)+1 + strlen(from)+1, &bytes); free(send); - if (bytes <= 0) { + if (ret != AFC_E_SUCCESS) { afc_unlock(client); return AFC_E_NOT_ENOUGH_DATA; } @@ -606,7 +619,7 @@ afc_error_t afc_rename_path(afc_client_t client, const char *from, const char *t */ afc_error_t afc_make_directory(afc_client_t client, const char *dir) { - int bytes = 0; + uint32_t bytes = 0; char *response = NULL; afc_error_t ret = AFC_E_UNKNOWN_ERROR; @@ -618,8 +631,8 @@ afc_error_t afc_make_directory(afc_client_t client, const char *dir) // Send command client->afc_packet->operation = AFC_OP_MAKE_DIR; client->afc_packet->this_length = client->afc_packet->entire_length = 0; - bytes = afc_dispatch_packet(client, dir, strlen(dir)+1); - if (bytes <= 0) { + ret = afc_dispatch_packet(client, dir, strlen(dir)+1, &bytes); + if (ret != AFC_E_SUCCESS) { afc_unlock(client); return AFC_E_NOT_ENOUGH_DATA; } @@ -647,7 +660,7 @@ afc_error_t afc_make_directory(afc_client_t client, const char *dir) afc_error_t afc_get_file_info(afc_client_t client, const char *path, char ***infolist) { char *received = NULL; - int bytes; + uint32_t bytes = 0; afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || !path || !infolist) @@ -658,7 +671,11 @@ afc_error_t afc_get_file_info(afc_client_t client, const char *path, char ***inf // Send command client->afc_packet->operation = AFC_OP_GET_FILE_INFO; client->afc_packet->entire_length = client->afc_packet->this_length = 0; - afc_dispatch_packet(client, path, strlen(path)+1); + ret = afc_dispatch_packet(client, path, strlen(path)+1, &bytes); + if (ret != AFC_E_SUCCESS) { + afc_unlock(client); + return AFC_E_NOT_ENOUGH_DATA; + } // Receive data ret = afc_receive_data(client, &received, &bytes); @@ -688,8 +705,8 @@ iphone_error_t 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; + uint64_t file_mode_loc = GUINT64_TO_LE(file_mode); + uint32_t bytes = 0; char *data = (char *) malloc(sizeof(char) * (8 + strlen(filename) + 1)); afc_error_t ret = AFC_E_UNKNOWN_ERROR; @@ -702,16 +719,15 @@ afc_file_open(afc_client_t client, const char *filename, afc_lock(client); // Send command - memcpy(data, &file_mode, 4); - memcpy(data + 4, &ag, 4); + memcpy(data, &file_mode_loc, 8); memcpy(data + 8, filename, strlen(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 = afc_dispatch_packet(client, data, 8 + strlen(filename) + 1); + ret = afc_dispatch_packet(client, data, 8 + strlen(filename) + 1, &bytes); free(data); - if (bytes <= 0) { + if (ret != AFC_E_SUCCESS) { log_debug_msg("%s: Didn't receive a response to the command\n", __func__); afc_unlock(client); return AFC_E_NOT_ENOUGH_DATA; @@ -740,18 +756,19 @@ afc_file_open(afc_client_t client, const char *filename, * @param handle File handle of a previously opened file * @param data The pointer to the memory region to store the read data * @param length The number of bytes to read + * @param bytes_read The number of bytes actually read. * - * @return The number of bytes read if successful. If there was an error -1. + * @return AFC_E_SUCCESS on success or an AFC_E_* error value on error. */ iphone_error_t -afc_file_read(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, uint32_t length, uint32_t *bytes_read) { char *input = NULL; - int current_count = 0, bytes_loc = 0; - const int MAXIMUM_READ_SIZE = 1 << 16; + uint32_t current_count = 0, bytes_loc = 0; + const uint32_t MAXIMUM_READ_SIZE = 1 << 16; afc_error_t ret = AFC_E_SUCCESS; - if (!client || !client->afc_packet || !client->connection || handle == 0 || (length < 0)) + if (!client || !client->afc_packet || !client->connection || handle == 0) return AFC_E_INVALID_ARGUMENT; log_debug_msg("%s: called for length %i\n", __func__, length); @@ -765,13 +782,13 @@ afc_file_read(afc_client_t client, uint64_t handle, char *data, int length, uint // Send the read command AFCFilePacket *packet = (AFCFilePacket *) malloc(sizeof(AFCFilePacket)); packet->filehandle = handle; - packet->size = ((length - current_count) < MAXIMUM_READ_SIZE) ? (length - current_count) : MAXIMUM_READ_SIZE; + packet->size = GUINT64_TO_LE(((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 = afc_dispatch_packet(client, (char *) packet, sizeof(AFCFilePacket)); + ret = afc_dispatch_packet(client, (char *) packet, sizeof(AFCFilePacket), &bytes_loc); free(packet); - if (bytes_loc <= 0) { + if (ret != AFC_E_SUCCESS) { afc_unlock(client); return AFC_E_NOT_ENOUGH_DATA; } @@ -786,7 +803,7 @@ afc_file_read(afc_client_t client, uint64_t handle, char *data, int length, uint if (input) free(input); afc_unlock(client); - *bytes = current_count; + *bytes_read = current_count; /* FIXME: check that's actually a success */ return ret; } else { @@ -802,7 +819,7 @@ afc_file_read(afc_client_t client, uint64_t handle, char *data, int length, uint log_debug_msg("%s: returning current_count as %i\n", __func__, current_count); afc_unlock(client); - *bytes = current_count; + *bytes_read = current_count; return ret; } @@ -812,23 +829,22 @@ afc_file_read(afc_client_t client, uint64_t handle, char *data, int length, uint * @param handle File handle of previously opened file. * @param data The data to write to the file. * @param length How much data to write. + * @param bytes_written The number of bytes actually written to the file. * - * @return The number of bytes written to the file, or a value less than 0 if - * none were written... + * @return AFC_E_SUCCESS on success, or an AFC_E_* error value on error. */ iphone_error_t -afc_file_write(afc_client_t client, uint64_t handle, - const char *data, int length, uint32_t * bytes) +afc_file_write(afc_client_t client, uint64_t handle, const char *data, uint32_t length, uint32_t *bytes_written) { char *acknowledgement = NULL; - const int MAXIMUM_WRITE_SIZE = 1 << 15; - uint32_t zero = 0, current_count = 0, i = 0; + const uint32_t MAXIMUM_WRITE_SIZE = 1 << 15; + uint32_t current_count = 0, i = 0; uint32_t segments = (length / MAXIMUM_WRITE_SIZE); - int bytes_loc = 0; + uint32_t bytes_loc = 0; char *out_buffer = NULL; afc_error_t ret = AFC_E_SUCCESS; - if (!client || !client->afc_packet || !client->connection || !bytes || (handle == 0) || (length < 0)) + if (!client || !client->afc_packet || !client->connection || !bytes_written || (handle == 0)) return AFC_E_INVALID_ARGUMENT; afc_lock(client); @@ -844,8 +860,8 @@ afc_file_write(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 = afc_dispatch_packet(client, out_buffer, MAXIMUM_WRITE_SIZE + 8); - if (bytes_loc < 0) { + ret = afc_dispatch_packet(client, out_buffer, MAXIMUM_WRITE_SIZE + 8, &bytes_loc); + if (ret != AFC_E_SUCCESS) { afc_unlock(client); return AFC_E_NOT_ENOUGH_DATA; } @@ -866,9 +882,9 @@ afc_file_write(afc_client_t client, uint64_t handle, // didn't get sent in the for loop // this length is fine because it's always sizeof(AFCPacket) + 8, but // to be sure we do it again - if (current_count == (uint32_t)length) { + if (current_count == length) { afc_unlock(client); - *bytes = current_count; + *bytes_written = current_count; return ret; } @@ -878,19 +894,18 @@ afc_file_write(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 = afc_dispatch_packet(client, out_buffer, (length - current_count) + 8); + ret = afc_dispatch_packet(client, out_buffer, (length - current_count) + 8, &bytes_loc); free(out_buffer); out_buffer = NULL; current_count += bytes_loc; - if (bytes_loc <= 0) { + if (ret != AFC_E_SUCCESS) { afc_unlock(client); - *bytes = current_count; + *bytes_written = current_count; return AFC_E_SUCCESS; } - zero = bytes_loc; ret = afc_receive_data(client, &acknowledgement, &bytes_loc); afc_unlock(client); if (ret != AFC_E_SUCCESS) { @@ -898,7 +913,7 @@ afc_file_write(afc_client_t client, uint64_t handle, } else { free(acknowledgement); } - *bytes = current_count; + *bytes_written = current_count; return ret; } @@ -910,7 +925,7 @@ afc_file_write(afc_client_t client, uint64_t handle, afc_error_t afc_file_close(afc_client_t client, uint64_t handle) { char *buffer = malloc(sizeof(char) * 8); - int bytes = 0; + uint32_t bytes = 0; afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || (handle == 0)) @@ -924,11 +939,11 @@ afc_error_t afc_file_close(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 = afc_dispatch_packet(client, buffer, 8); + ret = afc_dispatch_packet(client, buffer, 8, &bytes); free(buffer); buffer = NULL; - if (bytes <= 0) { + if (ret != AFC_E_SUCCESS) { afc_unlock(client); return AFC_E_UNKNOWN_ERROR; } @@ -957,8 +972,8 @@ 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) { char *buffer = malloc(16); - int bytes = 0; - uint64_t op = operation; + uint32_t bytes = 0; + uint64_t op = GUINT64_TO_LE(operation); afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || (handle == 0)) @@ -974,11 +989,11 @@ afc_error_t afc_file_lock(afc_client_t client, uint64_t handle, afc_lock_op_t op client->afc_packet->operation = AFC_OP_FILE_LOCK; client->afc_packet->entire_length = client->afc_packet->this_length = 0; - bytes = afc_dispatch_packet(client, buffer, 16); + ret = afc_dispatch_packet(client, buffer, 16, &bytes); free(buffer); buffer = NULL; - if (bytes <= 0) { + if (ret != AFC_E_SUCCESS) { afc_unlock(client); log_debug_msg("%s: could not send lock command\n", __func__); return AFC_E_UNKNOWN_ERROR; @@ -1006,8 +1021,9 @@ afc_error_t afc_file_lock(afc_client_t client, uint64_t handle, afc_lock_op_t op 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; + int64_t offset_loc = (int64_t)GUINT64_TO_LE(offset); + uint64_t whence_loc = GUINT64_TO_LE(whence); + uint32_t bytes = 0; afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || (handle == 0)) @@ -1017,16 +1033,15 @@ afc_error_t afc_file_seek(afc_client_t client, uint64_t handle, int64_t offset, // Send the command memcpy(buffer, &handle, sizeof(uint64_t)); // handle - memcpy(buffer + 8, &whence, sizeof(int32_t)); // fromwhere - memcpy(buffer + 12, &zero, sizeof(uint32_t)); // pad - memcpy(buffer + 16, &offset, sizeof(uint64_t)); // offset + memcpy(buffer + 8, &whence_loc, sizeof(uint64_t)); // fromwhere + memcpy(buffer + 16, &offset_loc, sizeof(uint64_t)); // offset client->afc_packet->operation = AFC_OP_FILE_SEEK; client->afc_packet->this_length = client->afc_packet->entire_length = 0; - bytes = afc_dispatch_packet(client, buffer, 24); + ret = afc_dispatch_packet(client, buffer, 24, &bytes); free(buffer); buffer = NULL; - if (bytes <= 0) { + if (ret != AFC_E_SUCCESS) { afc_unlock(client); return AFC_E_NOT_ENOUGH_DATA; } @@ -1051,7 +1066,7 @@ afc_error_t afc_file_seek(afc_client_t client, uint64_t handle, int64_t offset, afc_error_t afc_file_tell(afc_client_t client, uint64_t handle, uint64_t *position) { char *buffer = (char *) malloc(sizeof(char) * 8); - int bytes = 0; + uint32_t bytes = 0; afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || (handle == 0)) @@ -1063,11 +1078,11 @@ afc_error_t afc_file_tell(afc_client_t client, uint64_t handle, uint64_t *positi memcpy(buffer, &handle, sizeof(uint64_t)); // handle client->afc_packet->operation = AFC_OP_FILE_TELL; client->afc_packet->this_length = client->afc_packet->entire_length = 0; - bytes = afc_dispatch_packet(client, buffer, 8); + ret = afc_dispatch_packet(client, buffer, 8, &bytes); free(buffer); buffer = NULL; - if (bytes <= 0) { + if (ret != AFC_E_SUCCESS) { afc_unlock(client); return AFC_E_NOT_ENOUGH_DATA; } @@ -1077,6 +1092,7 @@ afc_error_t afc_file_tell(afc_client_t client, uint64_t handle, uint64_t *positi if (bytes > 0 && buffer) { /* Get the position */ memcpy(position, buffer, sizeof(uint64_t)); + *position = GUINT64_FROM_LE(*position); } if (buffer) free(buffer); @@ -1100,7 +1116,8 @@ afc_error_t afc_file_tell(afc_client_t client, uint64_t handle, uint64_t *positi 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; + uint32_t bytes = 0; + uint64_t newsize_loc = GUINT64_TO_LE(newsize); afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || (handle == 0)) @@ -1110,14 +1127,14 @@ afc_error_t afc_file_truncate(afc_client_t client, uint64_t handle, uint64_t new // Send command memcpy(buffer, &handle, sizeof(uint64_t)); // handle - memcpy(buffer + 8, &newsize, sizeof(uint64_t)); // newsize + memcpy(buffer + 8, &newsize_loc, 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 = afc_dispatch_packet(client, buffer, 16); + ret = afc_dispatch_packet(client, buffer, 16, &bytes); free(buffer); buffer = NULL; - if (bytes <= 0) { + if (ret != AFC_E_SUCCESS) { afc_unlock(client); return AFC_E_NOT_ENOUGH_DATA; } @@ -1140,12 +1157,12 @@ afc_error_t afc_file_truncate(afc_client_t client, uint64_t handle, uint64_t new * @return AFC_E_SUCCESS if everything went well, AFC_E_INVALID_ARGUMENT * if arguments are NULL or invalid, AFC_E_NOT_ENOUGH_DATA otherwise. */ -afc_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, uint64_t newsize) { char *response = NULL; char *send = (char *) malloc(sizeof(char) * (strlen(path) + 1 + 8)); - int bytes = 0; - uint64_t size_requested = newsize; + uint32_t bytes = 0; + uint64_t size_requested = GUINT64_TO_LE(newsize); afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || !path || !client->afc_packet || !client->connection) @@ -1158,9 +1175,9 @@ afc_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 = afc_dispatch_packet(client, send, 8 + strlen(path) + 1); + ret = afc_dispatch_packet(client, send, 8 + strlen(path) + 1, &bytes); free(send); - if (bytes <= 0) { + if (ret != AFC_E_SUCCESS) { afc_unlock(client); return AFC_E_NOT_ENOUGH_DATA; } @@ -1188,8 +1205,8 @@ afc_error_t afc_make_link(afc_client_t client, afc_link_type_t linktype, const c { char *response = NULL; char *send = (char *) malloc(sizeof(char) * (strlen(target)+1 + strlen(linkname)+1 + 8)); - int bytes = 0; - uint64_t type = linktype; + uint32_t bytes = 0; + uint64_t type = GUINT64_TO_LE(linktype); afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || !target || !linkname || !client->afc_packet || !client->connection) @@ -1207,9 +1224,9 @@ afc_error_t afc_make_link(afc_client_t client, afc_link_type_t linktype, const c 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 = afc_dispatch_packet(client, send, 8 + strlen(linkname) + 1 + strlen(target) + 1); + ret = afc_dispatch_packet(client, send, 8 + strlen(linkname) + 1 + strlen(target) + 1, &bytes); free(send); - if (bytes <= 0) { + if (ret != AFC_E_SUCCESS) { afc_unlock(client); return AFC_E_NOT_ENOUGH_DATA; } @@ -1236,7 +1253,8 @@ afc_error_t afc_set_file_time(afc_client_t client, const char *path, uint64_t mt { char *response = NULL; char *send = (char *) malloc(sizeof(char) * (strlen(path) + 1 + 8)); - int bytes = 0; + uint32_t bytes = 0; + uint64_t mtime_loc = GUINT64_TO_LE(mtime); afc_error_t ret = AFC_E_UNKNOWN_ERROR; if (!client || !path || !client->afc_packet || !client->connection) @@ -1245,13 +1263,13 @@ afc_error_t afc_set_file_time(afc_client_t client, const char *path, uint64_t mt afc_lock(client); // Send command - memcpy(send, &mtime, 8); + memcpy(send, &mtime_loc, 8); memcpy(send + 8, path, strlen(path) + 1); client->afc_packet->entire_length = client->afc_packet->this_length = 0; client->afc_packet->operation = AFC_OP_SET_FILE_TIME; - bytes = afc_dispatch_packet(client, send, 8 + strlen(path) + 1); + ret = afc_dispatch_packet(client, send, 8 + strlen(path) + 1, &bytes); free(send); - if (bytes <= 0) { + if (ret != AFC_E_SUCCESS) { afc_unlock(client); return AFC_E_NOT_ENOUGH_DATA; } |