summaryrefslogtreecommitdiffstats
path: root/src/AFC.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/AFC.c')
-rw-r--r--src/AFC.c266
1 files changed, 142 insertions, 124 deletions
diff --git a/src/AFC.c b/src/AFC.c
index fe6fc00..956c8fc 100644
--- a/src/AFC.c
+++ b/src/AFC.c
@@ -119,21 +119,24 @@ afc_error_t afc_client_free(afc_client_t client)
119 * @param client The client to send data through. 119 * @param client The client to send data through.
120 * @param data The data to send. 120 * @param data The data to send.
121 * @param length The length to send. 121 * @param length The length to send.
122 * 122 * @param bytes_sent The number of bytes actually sent.
123 * @return The number of bytes actually sent, or -1 on error. 123 *
124 * @return AFC_E_SUCCESS on success, or an AFC_E_* error value on error.
124 * 125 *
125 * @warning set client->afc_packet->this_length and 126 * @warning set client->afc_packet->this_length and
126 * client->afc_packet->entire_length to 0 before calling this. The 127 * client->afc_packet->entire_length to 0 before calling this. The
127 * reason is that if you set them to different values, it indicates 128 * reason is that if you set them to different values, it indicates
128 * you want to send the data as two packets. 129 * you want to send the data as two packets.
129 */ 130 */
130static int afc_dispatch_packet(afc_client_t client, const char *data, uint64_t length) 131static afc_error_t afc_dispatch_packet(afc_client_t client, const char *data, uint32_t length, uint32_t *bytes_sent)
131{ 132{
132 int bytes = 0, offset = 0; 133 uint32_t offset = 0;
133 uint32_t sent = 0; 134 uint32_t sent = 0;
134 135
135 if (!client || !client->connection || !client->afc_packet) 136 if (!client || !client->connection || !client->afc_packet)
136 return 0; 137 return AFC_E_INVALID_ARGUMENT;
138
139 *bytes_sent = 0;
137 140
138 if (!data || !length) 141 if (!data || !length)
139 length = 0; 142 length = 0;
@@ -158,20 +161,26 @@ static int afc_dispatch_packet(afc_client_t client, const char *data, uint64_t l
158 log_debug_msg("to based on the packet.\n"); 161 log_debug_msg("to based on the packet.\n");
159 log_debug_msg("%s: length minus offset: %i\n", __func__, length - offset); 162 log_debug_msg("%s: length minus offset: %i\n", __func__, length - offset);
160 log_debug_msg("%s: rest of packet: %i\n", __func__, client->afc_packet->entire_length - client->afc_packet->this_length); 163 log_debug_msg("%s: rest of packet: %i\n", __func__, client->afc_packet->entire_length - client->afc_packet->this_length);
161 return -1; 164 return AFC_E_INTERNAL_ERROR;
162 } 165 }
163 166
167 /* send AFC packet header */
168 AFCPacket_to_LE(client->afc_packet);
169 sent = 0;
164 iphone_device_send(client->connection, (void*)client->afc_packet, sizeof(AFCPacket), &sent); 170 iphone_device_send(client->connection, (void*)client->afc_packet, sizeof(AFCPacket), &sent);
165 if (sent == 0) { 171 if (sent == 0) {
166 return bytes; 172 /* FIXME: should this be handled as success?! */
173 return AFC_E_SUCCESS;
167 } 174 }
168 bytes += sent; 175 *bytes_sent += sent;
169 176
177 /* send AFC packet data */
178 sent = 0;
170 iphone_device_send(client->connection, data, offset, &sent); 179 iphone_device_send(client->connection, data, offset, &sent);
171 if (sent == 0) { 180 if (sent == 0) {
172 return bytes; 181 return AFC_E_SUCCESS;
173 } 182 }
174 bytes += sent; 183 *bytes_sent += sent;
175 184
176 log_debug_msg("%s: sent the first now go with the second\n", __func__); 185 log_debug_msg("%s: sent the first now go with the second\n", __func__);
177 log_debug_msg("%s: Length: %i\n", __func__, length - offset); 186 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
181 sent = 0; 190 sent = 0;
182 iphone_device_send(client->connection, data + offset, length - offset, &sent); 191 iphone_device_send(client->connection, data + offset, length - offset, &sent);
183 192
184 bytes = sent; 193 *bytes_sent = sent;
185 return bytes; 194 return AFC_E_SUCCESS;
186 } else { 195 } else {
187 log_debug_msg("%s: doin things the old way\n", __func__); 196 log_debug_msg("%s: doin things the old way\n", __func__);
188 log_debug_msg("%s: packet length = %i\n", __func__, client->afc_packet->this_length); 197 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
190 log_debug_buffer((char*)client->afc_packet, sizeof(AFCPacket)); 199 log_debug_buffer((char*)client->afc_packet, sizeof(AFCPacket));
191 log_debug_msg("\n"); 200 log_debug_msg("\n");
192 201
202 /* send AFC packet header */
203 AFCPacket_to_LE(client->afc_packet);
204 sent = 0;
193 iphone_device_send(client->connection, (void*)client->afc_packet, sizeof(AFCPacket), &sent); 205 iphone_device_send(client->connection, (void*)client->afc_packet, sizeof(AFCPacket), &sent);
194 if (sent == 0) { 206 if (sent == 0) {
195 return bytes; 207 return AFC_E_SUCCESS;
196 } 208 }
197 bytes += sent; 209 *bytes_sent += sent;
210 /* send AFC packet data (if there's data to send) */
198 if (length > 0) { 211 if (length > 0) {
199 log_debug_msg("%s: packet data follows\n", __func__); 212 log_debug_msg("%s: packet data follows\n", __func__);
200 213
201 log_debug_buffer(data, length); 214 log_debug_buffer(data, length);
202 log_debug_msg("\n"); 215 log_debug_msg("\n");
203 iphone_device_send(client->connection, data, length, &sent); 216 iphone_device_send(client->connection, data, length, &sent);
204 bytes += sent; 217 *bytes_sent += sent;
205 } 218 }
206 return bytes; 219 return AFC_E_SUCCESS;
207 } 220 }
208 return -1; 221 return AFC_E_INTERNAL_ERROR;
209} 222}
210 223
211/** Receives data through an AFC client and sets a variable to the received data. 224/** Receives data through an AFC client and sets a variable to the received data.
212 * 225 *
213 * @param client The client to receive data on. 226 * @param client The client to receive data on.
214 * @param dump_here The char* to point to the newly-received data. 227 * @param dump_here The char* to point to the newly-received data.
228 * @param bytes_recv How much data was received.
215 * 229 *
216 * @return How much data was received, 0 on successful receive with no errors, 230 * @return AFC_E_SUCCESS when data has been received, or an AFC_E_* error value
217 * -1 if there was an error involved with receiving or if the packet 231 * when an error occured.
218 * received raised a non-trivial error condition (i.e. non-zero with
219 * AFC_ERROR operation)
220 */ 232 */
221static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, int *bytes) 233static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, uint32_t *bytes_recv)
222{ 234{
223 AFCPacket header; 235 AFCPacket header;
224 uint32_t entire_len = 0; 236 uint32_t entire_len = 0;
@@ -226,15 +238,16 @@ static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, int *
226 uint32_t current_count = 0; 238 uint32_t current_count = 0;
227 uint64_t param1 = -1; 239 uint64_t param1 = -1;
228 240
229 *bytes = 0; 241 *bytes_recv = 0;
230 242
231 /* first, read the AFC header */ 243 /* first, read the AFC header */
232 iphone_device_recv(client->connection, (char*)&header, sizeof(AFCPacket), (uint32_t*)bytes); 244 iphone_device_recv(client->connection, (char*)&header, sizeof(AFCPacket), bytes_recv);
233 if (*bytes <= 0) { 245 AFCPacket_from_LE(&header);
246 if (*bytes_recv == 0) {
234 log_debug_msg("%s: Just didn't get enough.\n", __func__); 247 log_debug_msg("%s: Just didn't get enough.\n", __func__);
235 *dump_here = NULL; 248 *dump_here = NULL;
236 return AFC_E_MUX_ERROR; 249 return AFC_E_MUX_ERROR;
237 } else if ((uint32_t)*bytes < sizeof(AFCPacket)) { 250 } else if (*bytes_recv < sizeof(AFCPacket)) {
238 log_debug_msg("%s: Did not even get the AFCPacket header\n", __func__); 251 log_debug_msg("%s: Did not even get the AFCPacket header\n", __func__);
239 *dump_here = NULL; 252 *dump_here = NULL;
240 return AFC_E_MUX_ERROR; 253 return AFC_E_MUX_ERROR;
@@ -262,7 +275,7 @@ static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, int *
262 && header.entire_length == sizeof(AFCPacket)) { 275 && header.entire_length == sizeof(AFCPacket)) {
263 log_debug_msg("%s: Empty AFCPacket received!\n", __func__); 276 log_debug_msg("%s: Empty AFCPacket received!\n", __func__);
264 *dump_here = NULL; 277 *dump_here = NULL;
265 *bytes = 0; 278 *bytes_recv = 0;
266 if (header.operation == AFC_OP_DATA) { 279 if (header.operation == AFC_OP_DATA) {
267 return AFC_E_SUCCESS; 280 return AFC_E_SUCCESS;
268 } else { 281 } else {
@@ -282,13 +295,13 @@ static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, int *
282 295
283 *dump_here = (char*)malloc(entire_len); 296 *dump_here = (char*)malloc(entire_len);
284 if (this_len > 0) { 297 if (this_len > 0) {
285 iphone_device_recv(client->connection, *dump_here, this_len, (uint32_t*)bytes); 298 iphone_device_recv(client->connection, *dump_here, this_len, bytes_recv);
286 if (*bytes <= 0) { 299 if (*bytes_recv <= 0) {
287 free(*dump_here); 300 free(*dump_here);
288 *dump_here = NULL; 301 *dump_here = NULL;
289 log_debug_msg("%s: Did not get packet contents!\n", __func__); 302 log_debug_msg("%s: Did not get packet contents!\n", __func__);
290 return AFC_E_NOT_ENOUGH_DATA; 303 return AFC_E_NOT_ENOUGH_DATA;
291 } else if ((uint32_t)*bytes < this_len) { 304 } else if (*bytes_recv < this_len) {
292 free(*dump_here); 305 free(*dump_here);
293 *dump_here = NULL; 306 *dump_here = NULL;
294 log_debug_msg("%s: Could not receive this_len=%d bytes\n", __func__, this_len); 307 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 *
300 313
301 if (entire_len > this_len) { 314 if (entire_len > this_len) {
302 while (current_count < entire_len) { 315 while (current_count < entire_len) {
303 iphone_device_recv(client->connection, (*dump_here)+current_count, entire_len - current_count, (uint32_t*)bytes); 316 iphone_device_recv(client->connection, (*dump_here)+current_count, entire_len - current_count, bytes_recv);
304 if (*bytes <= 0) { 317 if (*bytes_recv <= 0) {
305 log_debug_msg("%s: Error receiving data (recv returned %d)\n", __func__, *bytes); 318 log_debug_msg("%s: Error receiving data (recv returned %d)\n", __func__, *bytes_recv);
306 break; 319 break;
307 } 320 }
308 current_count += *bytes; 321 current_count += *bytes_recv;
309 } 322 }
310 if (current_count < entire_len) { 323 if (current_count < entire_len) {
311 log_debug_msg("%s: WARNING: could not receive full packet (read %s, size %d)\n", __func__, current_count, entire_len); 324 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 *
345 /* unknown operation code received */ 358 /* unknown operation code received */
346 free(*dump_here); 359 free(*dump_here);
347 *dump_here = NULL; 360 *dump_here = NULL;
348 *bytes = 0; 361 *bytes_recv = 0;
349 362
350 log_debug_msg("%s: WARNING: Unknown operation code received 0x%llx param1=%lld\n", __func__, header.operation, param1); 363 log_debug_msg("%s: WARNING: Unknown operation code received 0x%llx param1=%lld\n", __func__, header.operation, param1);
351 fprintf(stderr, "%s: WARNING: Unknown operation code received 0x%llx param1=%lld\n", __func__, (long long)header.operation, (long long)param1); 364 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 *
353 return AFC_E_OP_NOT_SUPPORTED; 366 return AFC_E_OP_NOT_SUPPORTED;
354 } 367 }
355 368
356 *bytes = current_count; 369 *bytes_recv = current_count;
357 return AFC_E_SUCCESS; 370 return AFC_E_SUCCESS;
358} 371}
359 372
360static int count_nullspaces(char *string, int number) 373static uint32_t count_nullspaces(char *string, uint32_t number)
361{ 374{
362 int i = 0, nulls = 0; 375 uint32_t i = 0, nulls = 0;
363 376
364 for (i = 0; i < number; i++) { 377 for (i = 0; i < number; i++) {
365 if (string[i] == '\0') 378 if (string[i] == '\0')
@@ -369,9 +382,9 @@ static int count_nullspaces(char *string, int number)
369 return nulls; 382 return nulls;
370} 383}
371 384
372static char **make_strings_list(char *tokens, int true_length) 385static char **make_strings_list(char *tokens, uint32_t true_length)
373{ 386{
374 int nulls = 0, i = 0, j = 0; 387 uint32_t nulls = 0, i = 0, j = 0;
375 char **list = NULL; 388 char **list = NULL;
376 389
377 if (!tokens || !true_length) 390 if (!tokens || !true_length)
@@ -398,7 +411,7 @@ static char **make_strings_list(char *tokens, int true_length)
398 */ 411 */
399afc_error_t afc_read_directory(afc_client_t client, const char *dir, char ***list) 412afc_error_t afc_read_directory(afc_client_t client, const char *dir, char ***list)
400{ 413{
401 int bytes = 0; 414 uint32_t bytes = 0;
402 char *data = NULL, **list_loc = NULL; 415 char *data = NULL, **list_loc = NULL;
403 afc_error_t ret = AFC_E_UNKNOWN_ERROR; 416 afc_error_t ret = AFC_E_UNKNOWN_ERROR;
404 417
@@ -411,8 +424,8 @@ afc_error_t afc_read_directory(afc_client_t client, const char *dir, char ***lis
411 client->afc_packet->operation = AFC_OP_READ_DIR; 424 client->afc_packet->operation = AFC_OP_READ_DIR;
412 client->afc_packet->entire_length = 0; 425 client->afc_packet->entire_length = 0;
413 client->afc_packet->this_length = 0; 426 client->afc_packet->this_length = 0;
414 bytes = afc_dispatch_packet(client, dir, strlen(dir)+1); 427 ret = afc_dispatch_packet(client, dir, strlen(dir)+1, &bytes);
415 if (bytes <= 0) { 428 if (ret != AFC_E_SUCCESS) {
416 afc_unlock(client); 429 afc_unlock(client);
417 return AFC_E_NOT_ENOUGH_DATA; 430 return AFC_E_NOT_ENOUGH_DATA;
418 } 431 }
@@ -442,7 +455,7 @@ afc_error_t afc_read_directory(afc_client_t client, const char *dir, char ***lis
442 */ 455 */
443afc_error_t afc_get_device_info(afc_client_t client, char ***infos) 456afc_error_t afc_get_device_info(afc_client_t client, char ***infos)
444{ 457{
445 int bytes = 0; 458 uint32_t bytes = 0;
446 char *data = NULL, **list = NULL; 459 char *data = NULL, **list = NULL;
447 afc_error_t ret = AFC_E_UNKNOWN_ERROR; 460 afc_error_t ret = AFC_E_UNKNOWN_ERROR;
448 461
@@ -454,8 +467,8 @@ afc_error_t afc_get_device_info(afc_client_t client, char ***infos)
454 // Send the command 467 // Send the command
455 client->afc_packet->operation = AFC_OP_GET_DEVINFO; 468 client->afc_packet->operation = AFC_OP_GET_DEVINFO;
456 client->afc_packet->entire_length = client->afc_packet->this_length = 0; 469 client->afc_packet->entire_length = client->afc_packet->this_length = 0;
457 bytes = afc_dispatch_packet(client, NULL, 0); 470 ret = afc_dispatch_packet(client, NULL, 0, &bytes);
458 if (bytes < 0) { 471 if (ret != AFC_E_SUCCESS) {
459 afc_unlock(client); 472 afc_unlock(client);
460 return AFC_E_NOT_ENOUGH_DATA; 473 return AFC_E_NOT_ENOUGH_DATA;
461 } 474 }
@@ -523,7 +536,7 @@ afc_error_t afc_get_device_info_key(afc_client_t client, const char *key, char *
523afc_error_t afc_remove_path(afc_client_t client, const char *path) 536afc_error_t afc_remove_path(afc_client_t client, const char *path)
524{ 537{
525 char *response = NULL; 538 char *response = NULL;
526 int bytes; 539 uint32_t bytes = 0;
527 afc_error_t ret = AFC_E_UNKNOWN_ERROR; 540 afc_error_t ret = AFC_E_UNKNOWN_ERROR;
528 541
529 if (!client || !path || !client->afc_packet || !client->connection) 542 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)
534 // Send command 547 // Send command
535 client->afc_packet->this_length = client->afc_packet->entire_length = 0; 548 client->afc_packet->this_length = client->afc_packet->entire_length = 0;
536 client->afc_packet->operation = AFC_OP_REMOVE_PATH; 549 client->afc_packet->operation = AFC_OP_REMOVE_PATH;
537 bytes = afc_dispatch_packet(client, path, strlen(path)+1); 550 ret = afc_dispatch_packet(client, path, strlen(path)+1, &bytes);
538 if (bytes <= 0) { 551 if (ret != AFC_E_SUCCESS) {
539 afc_unlock(client); 552 afc_unlock(client);
540 return AFC_E_NOT_ENOUGH_DATA; 553 return AFC_E_NOT_ENOUGH_DATA;
541 } 554 }
@@ -566,7 +579,7 @@ afc_error_t afc_rename_path(afc_client_t client, const char *from, const char *t
566{ 579{
567 char *response = NULL; 580 char *response = NULL;
568 char *send = (char *) malloc(sizeof(char) * (strlen(from) + strlen(to) + 1 + sizeof(uint32_t))); 581 char *send = (char *) malloc(sizeof(char) * (strlen(from) + strlen(to) + 1 + sizeof(uint32_t)));
569 int bytes = 0; 582 uint32_t bytes = 0;
570 afc_error_t ret = AFC_E_UNKNOWN_ERROR; 583 afc_error_t ret = AFC_E_UNKNOWN_ERROR;
571 584
572 if (!client || !from || !to || !client->afc_packet || !client->connection) 585 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
579 memcpy(send + strlen(from) + 1, to, strlen(to) + 1); 592 memcpy(send + strlen(from) + 1, to, strlen(to) + 1);
580 client->afc_packet->entire_length = client->afc_packet->this_length = 0; 593 client->afc_packet->entire_length = client->afc_packet->this_length = 0;
581 client->afc_packet->operation = AFC_OP_RENAME_PATH; 594 client->afc_packet->operation = AFC_OP_RENAME_PATH;
582 bytes = afc_dispatch_packet(client, send, strlen(to)+1 + strlen(from)+1); 595 ret = afc_dispatch_packet(client, send, strlen(to)+1 + strlen(from)+1, &bytes);
583 free(send); 596 free(send);
584 if (bytes <= 0) { 597 if (ret != AFC_E_SUCCESS) {
585 afc_unlock(client); 598 afc_unlock(client);
586 return AFC_E_NOT_ENOUGH_DATA; 599 return AFC_E_NOT_ENOUGH_DATA;
587 } 600 }
@@ -606,7 +619,7 @@ afc_error_t afc_rename_path(afc_client_t client, const char *from, const char *t
606 */ 619 */
607afc_error_t afc_make_directory(afc_client_t client, const char *dir) 620afc_error_t afc_make_directory(afc_client_t client, const char *dir)
608{ 621{
609 int bytes = 0; 622 uint32_t bytes = 0;
610 char *response = NULL; 623 char *response = NULL;
611 afc_error_t ret = AFC_E_UNKNOWN_ERROR; 624 afc_error_t ret = AFC_E_UNKNOWN_ERROR;
612 625
@@ -618,8 +631,8 @@ afc_error_t afc_make_directory(afc_client_t client, const char *dir)
618 // Send command 631 // Send command
619 client->afc_packet->operation = AFC_OP_MAKE_DIR; 632 client->afc_packet->operation = AFC_OP_MAKE_DIR;
620 client->afc_packet->this_length = client->afc_packet->entire_length = 0; 633 client->afc_packet->this_length = client->afc_packet->entire_length = 0;
621 bytes = afc_dispatch_packet(client, dir, strlen(dir)+1); 634 ret = afc_dispatch_packet(client, dir, strlen(dir)+1, &bytes);
622 if (bytes <= 0) { 635 if (ret != AFC_E_SUCCESS) {
623 afc_unlock(client); 636 afc_unlock(client);
624 return AFC_E_NOT_ENOUGH_DATA; 637 return AFC_E_NOT_ENOUGH_DATA;
625 } 638 }
@@ -647,7 +660,7 @@ afc_error_t afc_make_directory(afc_client_t client, const char *dir)
647afc_error_t afc_get_file_info(afc_client_t client, const char *path, char ***infolist) 660afc_error_t afc_get_file_info(afc_client_t client, const char *path, char ***infolist)
648{ 661{
649 char *received = NULL; 662 char *received = NULL;
650 int bytes; 663 uint32_t bytes = 0;
651 afc_error_t ret = AFC_E_UNKNOWN_ERROR; 664 afc_error_t ret = AFC_E_UNKNOWN_ERROR;
652 665
653 if (!client || !path || !infolist) 666 if (!client || !path || !infolist)
@@ -658,7 +671,11 @@ afc_error_t afc_get_file_info(afc_client_t client, const char *path, char ***inf
658 // Send command 671 // Send command
659 client->afc_packet->operation = AFC_OP_GET_FILE_INFO; 672 client->afc_packet->operation = AFC_OP_GET_FILE_INFO;
660 client->afc_packet->entire_length = client->afc_packet->this_length = 0; 673 client->afc_packet->entire_length = client->afc_packet->this_length = 0;
661 afc_dispatch_packet(client, path, strlen(path)+1); 674 ret = afc_dispatch_packet(client, path, strlen(path)+1, &bytes);
675 if (ret != AFC_E_SUCCESS) {
676 afc_unlock(client);
677 return AFC_E_NOT_ENOUGH_DATA;
678 }
662 679
663 // Receive data 680 // Receive data
664 ret = afc_receive_data(client, &received, &bytes); 681 ret = afc_receive_data(client, &received, &bytes);
@@ -688,8 +705,8 @@ iphone_error_t
688afc_file_open(afc_client_t client, const char *filename, 705afc_file_open(afc_client_t client, const char *filename,
689 afc_file_mode_t file_mode, uint64_t *handle) 706 afc_file_mode_t file_mode, uint64_t *handle)
690{ 707{
691 uint32_t ag = 0; 708 uint64_t file_mode_loc = GUINT64_TO_LE(file_mode);
692 int bytes = 0; 709 uint32_t bytes = 0;
693 char *data = (char *) malloc(sizeof(char) * (8 + strlen(filename) + 1)); 710 char *data = (char *) malloc(sizeof(char) * (8 + strlen(filename) + 1));
694 afc_error_t ret = AFC_E_UNKNOWN_ERROR; 711 afc_error_t ret = AFC_E_UNKNOWN_ERROR;
695 712
@@ -702,16 +719,15 @@ afc_file_open(afc_client_t client, const char *filename,
702 afc_lock(client); 719 afc_lock(client);
703 720
704 // Send command 721 // Send command
705 memcpy(data, &file_mode, 4); 722 memcpy(data, &file_mode_loc, 8);
706 memcpy(data + 4, &ag, 4);
707 memcpy(data + 8, filename, strlen(filename)); 723 memcpy(data + 8, filename, strlen(filename));
708 data[8 + strlen(filename)] = '\0'; 724 data[8 + strlen(filename)] = '\0';
709 client->afc_packet->operation = AFC_OP_FILE_OPEN; 725 client->afc_packet->operation = AFC_OP_FILE_OPEN;
710 client->afc_packet->entire_length = client->afc_packet->this_length = 0; 726 client->afc_packet->entire_length = client->afc_packet->this_length = 0;
711 bytes = afc_dispatch_packet(client, data, 8 + strlen(filename) + 1); 727 ret = afc_dispatch_packet(client, data, 8 + strlen(filename) + 1, &bytes);
712 free(data); 728 free(data);
713 729
714 if (bytes <= 0) { 730 if (ret != AFC_E_SUCCESS) {
715 log_debug_msg("%s: Didn't receive a response to the command\n", __func__); 731 log_debug_msg("%s: Didn't receive a response to the command\n", __func__);
716 afc_unlock(client); 732 afc_unlock(client);
717 return AFC_E_NOT_ENOUGH_DATA; 733 return AFC_E_NOT_ENOUGH_DATA;
@@ -740,18 +756,19 @@ afc_file_open(afc_client_t client, const char *filename,
740 * @param handle File handle of a previously opened file 756 * @param handle File handle of a previously opened file
741 * @param data The pointer to the memory region to store the read data 757 * @param data The pointer to the memory region to store the read data
742 * @param length The number of bytes to read 758 * @param length The number of bytes to read
759 * @param bytes_read The number of bytes actually read.
743 * 760 *
744 * @return The number of bytes read if successful. If there was an error -1. 761 * @return AFC_E_SUCCESS on success or an AFC_E_* error value on error.
745 */ 762 */
746iphone_error_t 763iphone_error_t
747afc_file_read(afc_client_t client, uint64_t handle, char *data, int length, uint32_t * bytes) 764afc_file_read(afc_client_t client, uint64_t handle, char *data, uint32_t length, uint32_t *bytes_read)
748{ 765{
749 char *input = NULL; 766 char *input = NULL;
750 int current_count = 0, bytes_loc = 0; 767 uint32_t current_count = 0, bytes_loc = 0;
751 const int MAXIMUM_READ_SIZE = 1 << 16; 768 const uint32_t MAXIMUM_READ_SIZE = 1 << 16;
752 afc_error_t ret = AFC_E_SUCCESS; 769 afc_error_t ret = AFC_E_SUCCESS;
753 770
754 if (!client || !client->afc_packet || !client->connection || handle == 0 || (length < 0)) 771 if (!client || !client->afc_packet || !client->connection || handle == 0)
755 return AFC_E_INVALID_ARGUMENT; 772 return AFC_E_INVALID_ARGUMENT;
756 log_debug_msg("%s: called for length %i\n", __func__, length); 773 log_debug_msg("%s: called for length %i\n", __func__, length);
757 774
@@ -765,13 +782,13 @@ afc_file_read(afc_client_t client, uint64_t handle, char *data, int length, uint
765 // Send the read command 782 // Send the read command
766 AFCFilePacket *packet = (AFCFilePacket *) malloc(sizeof(AFCFilePacket)); 783 AFCFilePacket *packet = (AFCFilePacket *) malloc(sizeof(AFCFilePacket));
767 packet->filehandle = handle; 784 packet->filehandle = handle;
768 packet->size = ((length - current_count) < MAXIMUM_READ_SIZE) ? (length - current_count) : MAXIMUM_READ_SIZE; 785 packet->size = GUINT64_TO_LE(((length - current_count) < MAXIMUM_READ_SIZE) ? (length - current_count) : MAXIMUM_READ_SIZE);
769 client->afc_packet->operation = AFC_OP_READ; 786 client->afc_packet->operation = AFC_OP_READ;
770 client->afc_packet->entire_length = client->afc_packet->this_length = 0; 787 client->afc_packet->entire_length = client->afc_packet->this_length = 0;
771 bytes_loc = afc_dispatch_packet(client, (char *) packet, sizeof(AFCFilePacket)); 788 ret = afc_dispatch_packet(client, (char *) packet, sizeof(AFCFilePacket), &bytes_loc);
772 free(packet); 789 free(packet);
773 790
774 if (bytes_loc <= 0) { 791 if (ret != AFC_E_SUCCESS) {
775 afc_unlock(client); 792 afc_unlock(client);
776 return AFC_E_NOT_ENOUGH_DATA; 793 return AFC_E_NOT_ENOUGH_DATA;
777 } 794 }
@@ -786,7 +803,7 @@ afc_file_read(afc_client_t client, uint64_t handle, char *data, int length, uint
786 if (input) 803 if (input)
787 free(input); 804 free(input);
788 afc_unlock(client); 805 afc_unlock(client);
789 *bytes = current_count; 806 *bytes_read = current_count;
790 /* FIXME: check that's actually a success */ 807 /* FIXME: check that's actually a success */
791 return ret; 808 return ret;
792 } else { 809 } else {
@@ -802,7 +819,7 @@ afc_file_read(afc_client_t client, uint64_t handle, char *data, int length, uint
802 log_debug_msg("%s: returning current_count as %i\n", __func__, current_count); 819 log_debug_msg("%s: returning current_count as %i\n", __func__, current_count);
803 820
804 afc_unlock(client); 821 afc_unlock(client);
805 *bytes = current_count; 822 *bytes_read = current_count;
806 return ret; 823 return ret;
807} 824}
808 825
@@ -812,23 +829,22 @@ afc_file_read(afc_client_t client, uint64_t handle, char *data, int length, uint
812 * @param handle File handle of previously opened file. 829 * @param handle File handle of previously opened file.
813 * @param data The data to write to the file. 830 * @param data The data to write to the file.
814 * @param length How much data to write. 831 * @param length How much data to write.
832 * @param bytes_written The number of bytes actually written to the file.
815 * 833 *
816 * @return The number of bytes written to the file, or a value less than 0 if 834 * @return AFC_E_SUCCESS on success, or an AFC_E_* error value on error.
817 * none were written...
818 */ 835 */
819iphone_error_t 836iphone_error_t
820afc_file_write(afc_client_t client, uint64_t handle, 837afc_file_write(afc_client_t client, uint64_t handle, const char *data, uint32_t length, uint32_t *bytes_written)
821 const char *data, int length, uint32_t * bytes)
822{ 838{
823 char *acknowledgement = NULL; 839 char *acknowledgement = NULL;
824 const int MAXIMUM_WRITE_SIZE = 1 << 15; 840 const uint32_t MAXIMUM_WRITE_SIZE = 1 << 15;
825 uint32_t zero = 0, current_count = 0, i = 0; 841 uint32_t current_count = 0, i = 0;
826 uint32_t segments = (length / MAXIMUM_WRITE_SIZE); 842 uint32_t segments = (length / MAXIMUM_WRITE_SIZE);
827 int bytes_loc = 0; 843 uint32_t bytes_loc = 0;
828 char *out_buffer = NULL; 844 char *out_buffer = NULL;
829 afc_error_t ret = AFC_E_SUCCESS; 845 afc_error_t ret = AFC_E_SUCCESS;
830 846
831 if (!client || !client->afc_packet || !client->connection || !bytes || (handle == 0) || (length < 0)) 847 if (!client || !client->afc_packet || !client->connection || !bytes_written || (handle == 0))
832 return AFC_E_INVALID_ARGUMENT; 848 return AFC_E_INVALID_ARGUMENT;
833 849
834 afc_lock(client); 850 afc_lock(client);
@@ -844,8 +860,8 @@ afc_file_write(afc_client_t client, uint64_t handle,
844 out_buffer = (char *) malloc(sizeof(char) * client->afc_packet->entire_length - sizeof(AFCPacket)); 860 out_buffer = (char *) malloc(sizeof(char) * client->afc_packet->entire_length - sizeof(AFCPacket));
845 memcpy(out_buffer, (char *)&handle, sizeof(uint64_t)); 861 memcpy(out_buffer, (char *)&handle, sizeof(uint64_t));
846 memcpy(out_buffer + 8, data + current_count, MAXIMUM_WRITE_SIZE); 862 memcpy(out_buffer + 8, data + current_count, MAXIMUM_WRITE_SIZE);
847 bytes_loc = afc_dispatch_packet(client, out_buffer, MAXIMUM_WRITE_SIZE + 8); 863 ret = afc_dispatch_packet(client, out_buffer, MAXIMUM_WRITE_SIZE + 8, &bytes_loc);
848 if (bytes_loc < 0) { 864 if (ret != AFC_E_SUCCESS) {
849 afc_unlock(client); 865 afc_unlock(client);
850 return AFC_E_NOT_ENOUGH_DATA; 866 return AFC_E_NOT_ENOUGH_DATA;
851 } 867 }
@@ -866,9 +882,9 @@ afc_file_write(afc_client_t client, uint64_t handle,
866 // didn't get sent in the for loop 882 // didn't get sent in the for loop
867 // this length is fine because it's always sizeof(AFCPacket) + 8, but 883 // this length is fine because it's always sizeof(AFCPacket) + 8, but
868 // to be sure we do it again 884 // to be sure we do it again
869 if (current_count == (uint32_t)length) { 885 if (current_count == length) {
870 afc_unlock(client); 886 afc_unlock(client);
871 *bytes = current_count; 887 *bytes_written = current_count;
872 return ret; 888 return ret;
873 } 889 }
874 890
@@ -878,19 +894,18 @@ afc_file_write(afc_client_t client, uint64_t handle,
878 out_buffer = (char *) malloc(sizeof(char) * client->afc_packet->entire_length - sizeof(AFCPacket)); 894 out_buffer = (char *) malloc(sizeof(char) * client->afc_packet->entire_length - sizeof(AFCPacket));
879 memcpy(out_buffer, (char *) &handle, sizeof(uint64_t)); 895 memcpy(out_buffer, (char *) &handle, sizeof(uint64_t));
880 memcpy(out_buffer + 8, data + current_count, (length - current_count)); 896 memcpy(out_buffer + 8, data + current_count, (length - current_count));
881 bytes_loc = afc_dispatch_packet(client, out_buffer, (length - current_count) + 8); 897 ret = afc_dispatch_packet(client, out_buffer, (length - current_count) + 8, &bytes_loc);
882 free(out_buffer); 898 free(out_buffer);
883 out_buffer = NULL; 899 out_buffer = NULL;
884 900
885 current_count += bytes_loc; 901 current_count += bytes_loc;
886 902
887 if (bytes_loc <= 0) { 903 if (ret != AFC_E_SUCCESS) {
888 afc_unlock(client); 904 afc_unlock(client);
889 *bytes = current_count; 905 *bytes_written = current_count;
890 return AFC_E_SUCCESS; 906 return AFC_E_SUCCESS;
891 } 907 }
892 908
893 zero = bytes_loc;
894 ret = afc_receive_data(client, &acknowledgement, &bytes_loc); 909 ret = afc_receive_data(client, &acknowledgement, &bytes_loc);
895 afc_unlock(client); 910 afc_unlock(client);
896 if (ret != AFC_E_SUCCESS) { 911 if (ret != AFC_E_SUCCESS) {
@@ -898,7 +913,7 @@ afc_file_write(afc_client_t client, uint64_t handle,
898 } else { 913 } else {
899 free(acknowledgement); 914 free(acknowledgement);
900 } 915 }
901 *bytes = current_count; 916 *bytes_written = current_count;
902 return ret; 917 return ret;
903} 918}
904 919
@@ -910,7 +925,7 @@ afc_file_write(afc_client_t client, uint64_t handle,
910afc_error_t afc_file_close(afc_client_t client, uint64_t handle) 925afc_error_t afc_file_close(afc_client_t client, uint64_t handle)
911{ 926{
912 char *buffer = malloc(sizeof(char) * 8); 927 char *buffer = malloc(sizeof(char) * 8);
913 int bytes = 0; 928 uint32_t bytes = 0;
914 afc_error_t ret = AFC_E_UNKNOWN_ERROR; 929 afc_error_t ret = AFC_E_UNKNOWN_ERROR;
915 930
916 if (!client || (handle == 0)) 931 if (!client || (handle == 0))
@@ -924,11 +939,11 @@ afc_error_t afc_file_close(afc_client_t client, uint64_t handle)
924 memcpy(buffer, &handle, sizeof(uint64_t)); 939 memcpy(buffer, &handle, sizeof(uint64_t));
925 client->afc_packet->operation = AFC_OP_FILE_CLOSE; 940 client->afc_packet->operation = AFC_OP_FILE_CLOSE;
926 client->afc_packet->entire_length = client->afc_packet->this_length = 0; 941 client->afc_packet->entire_length = client->afc_packet->this_length = 0;
927 bytes = afc_dispatch_packet(client, buffer, 8); 942 ret = afc_dispatch_packet(client, buffer, 8, &bytes);
928 free(buffer); 943 free(buffer);
929 buffer = NULL; 944 buffer = NULL;
930 945
931 if (bytes <= 0) { 946 if (ret != AFC_E_SUCCESS) {
932 afc_unlock(client); 947 afc_unlock(client);
933 return AFC_E_UNKNOWN_ERROR; 948 return AFC_E_UNKNOWN_ERROR;
934 } 949 }
@@ -957,8 +972,8 @@ afc_error_t afc_file_close(afc_client_t client, uint64_t handle)
957afc_error_t afc_file_lock(afc_client_t client, uint64_t handle, afc_lock_op_t operation) 972afc_error_t afc_file_lock(afc_client_t client, uint64_t handle, afc_lock_op_t operation)
958{ 973{
959 char *buffer = malloc(16); 974 char *buffer = malloc(16);
960 int bytes = 0; 975 uint32_t bytes = 0;
961 uint64_t op = operation; 976 uint64_t op = GUINT64_TO_LE(operation);
962 afc_error_t ret = AFC_E_UNKNOWN_ERROR; 977 afc_error_t ret = AFC_E_UNKNOWN_ERROR;
963 978
964 if (!client || (handle == 0)) 979 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
974 989
975 client->afc_packet->operation = AFC_OP_FILE_LOCK; 990 client->afc_packet->operation = AFC_OP_FILE_LOCK;
976 client->afc_packet->entire_length = client->afc_packet->this_length = 0; 991 client->afc_packet->entire_length = client->afc_packet->this_length = 0;
977 bytes = afc_dispatch_packet(client, buffer, 16); 992 ret = afc_dispatch_packet(client, buffer, 16, &bytes);
978 free(buffer); 993 free(buffer);
979 buffer = NULL; 994 buffer = NULL;
980 995
981 if (bytes <= 0) { 996 if (ret != AFC_E_SUCCESS) {
982 afc_unlock(client); 997 afc_unlock(client);
983 log_debug_msg("%s: could not send lock command\n", __func__); 998 log_debug_msg("%s: could not send lock command\n", __func__);
984 return AFC_E_UNKNOWN_ERROR; 999 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
1006afc_error_t afc_file_seek(afc_client_t client, uint64_t handle, int64_t offset, int whence) 1021afc_error_t afc_file_seek(afc_client_t client, uint64_t handle, int64_t offset, int whence)
1007{ 1022{
1008 char *buffer = (char *) malloc(sizeof(char) * 24); 1023 char *buffer = (char *) malloc(sizeof(char) * 24);
1009 uint32_t zero = 0; 1024 int64_t offset_loc = (int64_t)GUINT64_TO_LE(offset);
1010 int bytes = 0; 1025 uint64_t whence_loc = GUINT64_TO_LE(whence);
1026 uint32_t bytes = 0;
1011 afc_error_t ret = AFC_E_UNKNOWN_ERROR; 1027 afc_error_t ret = AFC_E_UNKNOWN_ERROR;
1012 1028
1013 if (!client || (handle == 0)) 1029 if (!client || (handle == 0))
@@ -1017,16 +1033,15 @@ afc_error_t afc_file_seek(afc_client_t client, uint64_t handle, int64_t offset,
1017 1033
1018 // Send the command 1034 // Send the command
1019 memcpy(buffer, &handle, sizeof(uint64_t)); // handle 1035 memcpy(buffer, &handle, sizeof(uint64_t)); // handle
1020 memcpy(buffer + 8, &whence, sizeof(int32_t)); // fromwhere 1036 memcpy(buffer + 8, &whence_loc, sizeof(uint64_t)); // fromwhere
1021 memcpy(buffer + 12, &zero, sizeof(uint32_t)); // pad 1037 memcpy(buffer + 16, &offset_loc, sizeof(uint64_t)); // offset
1022 memcpy(buffer + 16, &offset, sizeof(uint64_t)); // offset
1023 client->afc_packet->operation = AFC_OP_FILE_SEEK; 1038 client->afc_packet->operation = AFC_OP_FILE_SEEK;
1024 client->afc_packet->this_length = client->afc_packet->entire_length = 0; 1039 client->afc_packet->this_length = client->afc_packet->entire_length = 0;
1025 bytes = afc_dispatch_packet(client, buffer, 24); 1040 ret = afc_dispatch_packet(client, buffer, 24, &bytes);
1026 free(buffer); 1041 free(buffer);
1027 buffer = NULL; 1042 buffer = NULL;
1028 1043
1029 if (bytes <= 0) { 1044 if (ret != AFC_E_SUCCESS) {
1030 afc_unlock(client); 1045 afc_unlock(client);
1031 return AFC_E_NOT_ENOUGH_DATA; 1046 return AFC_E_NOT_ENOUGH_DATA;
1032 } 1047 }
@@ -1051,7 +1066,7 @@ afc_error_t afc_file_seek(afc_client_t client, uint64_t handle, int64_t offset,
1051afc_error_t afc_file_tell(afc_client_t client, uint64_t handle, uint64_t *position) 1066afc_error_t afc_file_tell(afc_client_t client, uint64_t handle, uint64_t *position)
1052{ 1067{
1053 char *buffer = (char *) malloc(sizeof(char) * 8); 1068 char *buffer = (char *) malloc(sizeof(char) * 8);
1054 int bytes = 0; 1069 uint32_t bytes = 0;
1055 afc_error_t ret = AFC_E_UNKNOWN_ERROR; 1070 afc_error_t ret = AFC_E_UNKNOWN_ERROR;
1056 1071
1057 if (!client || (handle == 0)) 1072 if (!client || (handle == 0))
@@ -1063,11 +1078,11 @@ afc_error_t afc_file_tell(afc_client_t client, uint64_t handle, uint64_t *positi
1063 memcpy(buffer, &handle, sizeof(uint64_t)); // handle 1078 memcpy(buffer, &handle, sizeof(uint64_t)); // handle
1064 client->afc_packet->operation = AFC_OP_FILE_TELL; 1079 client->afc_packet->operation = AFC_OP_FILE_TELL;
1065 client->afc_packet->this_length = client->afc_packet->entire_length = 0; 1080 client->afc_packet->this_length = client->afc_packet->entire_length = 0;
1066 bytes = afc_dispatch_packet(client, buffer, 8); 1081 ret = afc_dispatch_packet(client, buffer, 8, &bytes);
1067 free(buffer); 1082 free(buffer);
1068 buffer = NULL; 1083 buffer = NULL;
1069 1084
1070 if (bytes <= 0) { 1085 if (ret != AFC_E_SUCCESS) {
1071 afc_unlock(client); 1086 afc_unlock(client);
1072 return AFC_E_NOT_ENOUGH_DATA; 1087 return AFC_E_NOT_ENOUGH_DATA;
1073 } 1088 }
@@ -1077,6 +1092,7 @@ afc_error_t afc_file_tell(afc_client_t client, uint64_t handle, uint64_t *positi
1077 if (bytes > 0 && buffer) { 1092 if (bytes > 0 && buffer) {
1078 /* Get the position */ 1093 /* Get the position */
1079 memcpy(position, buffer, sizeof(uint64_t)); 1094 memcpy(position, buffer, sizeof(uint64_t));
1095 *position = GUINT64_FROM_LE(*position);
1080 } 1096 }
1081 if (buffer) 1097 if (buffer)
1082 free(buffer); 1098 free(buffer);
@@ -1100,7 +1116,8 @@ afc_error_t afc_file_tell(afc_client_t client, uint64_t handle, uint64_t *positi
1100afc_error_t afc_file_truncate(afc_client_t client, uint64_t handle, uint64_t newsize) 1116afc_error_t afc_file_truncate(afc_client_t client, uint64_t handle, uint64_t newsize)
1101{ 1117{
1102 char *buffer = (char *) malloc(sizeof(char) * 16); 1118 char *buffer = (char *) malloc(sizeof(char) * 16);
1103 int bytes = 0; 1119 uint32_t bytes = 0;
1120 uint64_t newsize_loc = GUINT64_TO_LE(newsize);
1104 afc_error_t ret = AFC_E_UNKNOWN_ERROR; 1121 afc_error_t ret = AFC_E_UNKNOWN_ERROR;
1105 1122
1106 if (!client || (handle == 0)) 1123 if (!client || (handle == 0))
@@ -1110,14 +1127,14 @@ afc_error_t afc_file_truncate(afc_client_t client, uint64_t handle, uint64_t new
1110 1127
1111 // Send command 1128 // Send command
1112 memcpy(buffer, &handle, sizeof(uint64_t)); // handle 1129 memcpy(buffer, &handle, sizeof(uint64_t)); // handle
1113 memcpy(buffer + 8, &newsize, sizeof(uint64_t)); // newsize 1130 memcpy(buffer + 8, &newsize_loc, sizeof(uint64_t)); // newsize
1114 client->afc_packet->operation = AFC_OP_FILE_SET_SIZE; 1131 client->afc_packet->operation = AFC_OP_FILE_SET_SIZE;
1115 client->afc_packet->this_length = client->afc_packet->entire_length = 0; 1132 client->afc_packet->this_length = client->afc_packet->entire_length = 0;
1116 bytes = afc_dispatch_packet(client, buffer, 16); 1133 ret = afc_dispatch_packet(client, buffer, 16, &bytes);
1117 free(buffer); 1134 free(buffer);
1118 buffer = NULL; 1135 buffer = NULL;
1119 1136
1120 if (bytes <= 0) { 1137 if (ret != AFC_E_SUCCESS) {
1121 afc_unlock(client); 1138 afc_unlock(client);
1122 return AFC_E_NOT_ENOUGH_DATA; 1139 return AFC_E_NOT_ENOUGH_DATA;
1123 } 1140 }
@@ -1140,12 +1157,12 @@ afc_error_t afc_file_truncate(afc_client_t client, uint64_t handle, uint64_t new
1140 * @return AFC_E_SUCCESS if everything went well, AFC_E_INVALID_ARGUMENT 1157 * @return AFC_E_SUCCESS if everything went well, AFC_E_INVALID_ARGUMENT
1141 * if arguments are NULL or invalid, AFC_E_NOT_ENOUGH_DATA otherwise. 1158 * if arguments are NULL or invalid, AFC_E_NOT_ENOUGH_DATA otherwise.
1142 */ 1159 */
1143afc_error_t afc_truncate(afc_client_t client, const char *path, off_t newsize) 1160afc_error_t afc_truncate(afc_client_t client, const char *path, uint64_t newsize)
1144{ 1161{
1145 char *response = NULL; 1162 char *response = NULL;
1146 char *send = (char *) malloc(sizeof(char) * (strlen(path) + 1 + 8)); 1163 char *send = (char *) malloc(sizeof(char) * (strlen(path) + 1 + 8));
1147 int bytes = 0; 1164 uint32_t bytes = 0;
1148 uint64_t size_requested = newsize; 1165 uint64_t size_requested = GUINT64_TO_LE(newsize);
1149 afc_error_t ret = AFC_E_UNKNOWN_ERROR; 1166 afc_error_t ret = AFC_E_UNKNOWN_ERROR;
1150 1167
1151 if (!client || !path || !client->afc_packet || !client->connection) 1168 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)
1158 memcpy(send + 8, path, strlen(path) + 1); 1175 memcpy(send + 8, path, strlen(path) + 1);
1159 client->afc_packet->entire_length = client->afc_packet->this_length = 0; 1176 client->afc_packet->entire_length = client->afc_packet->this_length = 0;
1160 client->afc_packet->operation = AFC_OP_TRUNCATE; 1177 client->afc_packet->operation = AFC_OP_TRUNCATE;
1161 bytes = afc_dispatch_packet(client, send, 8 + strlen(path) + 1); 1178 ret = afc_dispatch_packet(client, send, 8 + strlen(path) + 1, &bytes);
1162 free(send); 1179 free(send);
1163 if (bytes <= 0) { 1180 if (ret != AFC_E_SUCCESS) {
1164 afc_unlock(client); 1181 afc_unlock(client);
1165 return AFC_E_NOT_ENOUGH_DATA; 1182 return AFC_E_NOT_ENOUGH_DATA;
1166 } 1183 }
@@ -1188,8 +1205,8 @@ afc_error_t afc_make_link(afc_client_t client, afc_link_type_t linktype, const c
1188{ 1205{
1189 char *response = NULL; 1206 char *response = NULL;
1190 char *send = (char *) malloc(sizeof(char) * (strlen(target)+1 + strlen(linkname)+1 + 8)); 1207 char *send = (char *) malloc(sizeof(char) * (strlen(target)+1 + strlen(linkname)+1 + 8));
1191 int bytes = 0; 1208 uint32_t bytes = 0;
1192 uint64_t type = linktype; 1209 uint64_t type = GUINT64_TO_LE(linktype);
1193 afc_error_t ret = AFC_E_UNKNOWN_ERROR; 1210 afc_error_t ret = AFC_E_UNKNOWN_ERROR;
1194 1211
1195 if (!client || !target || !linkname || !client->afc_packet || !client->connection) 1212 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
1207 memcpy(send + 8 + strlen(target) + 1, linkname, strlen(linkname) + 1); 1224 memcpy(send + 8 + strlen(target) + 1, linkname, strlen(linkname) + 1);
1208 client->afc_packet->entire_length = client->afc_packet->this_length = 0; 1225 client->afc_packet->entire_length = client->afc_packet->this_length = 0;
1209 client->afc_packet->operation = AFC_OP_MAKE_LINK; 1226 client->afc_packet->operation = AFC_OP_MAKE_LINK;
1210 bytes = afc_dispatch_packet(client, send, 8 + strlen(linkname) + 1 + strlen(target) + 1); 1227 ret = afc_dispatch_packet(client, send, 8 + strlen(linkname) + 1 + strlen(target) + 1, &bytes);
1211 free(send); 1228 free(send);
1212 if (bytes <= 0) { 1229 if (ret != AFC_E_SUCCESS) {
1213 afc_unlock(client); 1230 afc_unlock(client);
1214 return AFC_E_NOT_ENOUGH_DATA; 1231 return AFC_E_NOT_ENOUGH_DATA;
1215 } 1232 }
@@ -1236,7 +1253,8 @@ afc_error_t afc_set_file_time(afc_client_t client, const char *path, uint64_t mt
1236{ 1253{
1237 char *response = NULL; 1254 char *response = NULL;
1238 char *send = (char *) malloc(sizeof(char) * (strlen(path) + 1 + 8)); 1255 char *send = (char *) malloc(sizeof(char) * (strlen(path) + 1 + 8));
1239 int bytes = 0; 1256 uint32_t bytes = 0;
1257 uint64_t mtime_loc = GUINT64_TO_LE(mtime);
1240 afc_error_t ret = AFC_E_UNKNOWN_ERROR; 1258 afc_error_t ret = AFC_E_UNKNOWN_ERROR;
1241 1259
1242 if (!client || !path || !client->afc_packet || !client->connection) 1260 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
1245 afc_lock(client); 1263 afc_lock(client);
1246 1264
1247 // Send command 1265 // Send command
1248 memcpy(send, &mtime, 8); 1266 memcpy(send, &mtime_loc, 8);
1249 memcpy(send + 8, path, strlen(path) + 1); 1267 memcpy(send + 8, path, strlen(path) + 1);
1250 client->afc_packet->entire_length = client->afc_packet->this_length = 0; 1268 client->afc_packet->entire_length = client->afc_packet->this_length = 0;
1251 client->afc_packet->operation = AFC_OP_SET_FILE_TIME; 1269 client->afc_packet->operation = AFC_OP_SET_FILE_TIME;
1252 bytes = afc_dispatch_packet(client, send, 8 + strlen(path) + 1); 1270 ret = afc_dispatch_packet(client, send, 8 + strlen(path) + 1, &bytes);
1253 free(send); 1271 free(send);
1254 if (bytes <= 0) { 1272 if (ret != AFC_E_SUCCESS) {
1255 afc_unlock(client); 1273 afc_unlock(client);
1256 return AFC_E_NOT_ENOUGH_DATA; 1274 return AFC_E_NOT_ENOUGH_DATA;
1257 } 1275 }