diff options
| author | 2008-08-10 17:24:32 -0700 | |
|---|---|---|
| committer | 2008-08-10 17:24:32 -0700 | |
| commit | 020d7c23b17956098379140f1f0047ae8e78df1b (patch) | |
| tree | 310d14b340309ba79ed5b8c15fe0e39d99c2f207 /src/AFC.c | |
| parent | 4c3f86499f8dfe344fb5f92b805e8c090290a79e (diff) | |
| download | libimobiledevice-020d7c23b17956098379140f1f0047ae8e78df1b.tar.gz libimobiledevice-020d7c23b17956098379140f1f0047ae8e78df1b.tar.bz2 | |
Adds seeking, directory creation and improves file writing. Also various other cleanups.
Implements creating directories as well as writing and deleting files in iFuse.
Signed-off-by: Matthew Colyer <mcolyer@mcolyer-laptop.(none)>
Diffstat (limited to 'src/AFC.c')
| -rw-r--r-- | src/AFC.c | 180 |
1 files changed, 148 insertions, 32 deletions
| @@ -29,13 +29,15 @@ extern int debug; | |||
| 29 | 29 | ||
| 30 | /* Locking, for thread-safety (well... kind of, hehe) */ | 30 | /* Locking, for thread-safety (well... kind of, hehe) */ |
| 31 | void afc_lock(AFClient *client) { | 31 | void afc_lock(AFClient *client) { |
| 32 | if (debug) printf("In the midst of a lock...\n"); | ||
| 32 | while (client->lock) { | 33 | while (client->lock) { |
| 33 | sleep(200); | 34 | usleep(500); // they say it's obsolete, but whatever |
| 34 | } | 35 | } |
| 35 | client->lock = 1; | 36 | client->lock = 1; |
| 36 | } | 37 | } |
| 37 | 38 | ||
| 38 | void afc_unlock(AFClient *client) { // just to be pretty | 39 | void afc_unlock(AFClient *client) { // just to be pretty |
| 40 | if (debug) printf("Unlock!\n"); | ||
| 39 | client->lock = 0; | 41 | client->lock = 0; |
| 40 | } | 42 | } |
| 41 | 43 | ||
| @@ -102,13 +104,13 @@ int dispatch_AFC_packet(AFClient *client, const char *data, int length) { | |||
| 102 | if ((length) < (client->afc_packet->entire_length - client->afc_packet->this_length)) { | 104 | if ((length) < (client->afc_packet->entire_length - client->afc_packet->this_length)) { |
| 103 | if (debug) printf("dispatch_AFC_packet: Length did not resemble what it was supposed to based on the packet.\nlength minus offset: %i\nrest of packet: %i\n", length-offset, client->afc_packet->entire_length - client->afc_packet->this_length); | 105 | if (debug) printf("dispatch_AFC_packet: Length did not resemble what it was supposed to based on the packet.\nlength minus offset: %i\nrest of packet: %i\n", length-offset, client->afc_packet->entire_length - client->afc_packet->this_length); |
| 104 | free(buffer); | 106 | free(buffer); |
| 105 | return 0; | 107 | return -1; |
| 106 | } | 108 | } |
| 107 | if (debug) printf("dispatch_AFC_packet: fucked-up packet method (probably a write)\n"); | 109 | if (debug) printf("dispatch_AFC_packet: fucked-up packet method (probably a write)\n"); |
| 108 | memcpy(buffer+sizeof(AFCPacket), data, offset); | 110 | memcpy(buffer+sizeof(AFCPacket), data, offset); |
| 109 | bytes = mux_send(client->connection, buffer, client->afc_packet->this_length); | 111 | bytes = mux_send(client->connection, buffer, client->afc_packet->this_length); |
| 110 | free(buffer); | 112 | free(buffer); |
| 111 | if (bytes <= 0) { return 0; } | 113 | if (bytes <= 0) { return bytes; } |
| 112 | if (debug) { | 114 | if (debug) { |
| 113 | printf("dispatch_AFC_packet: sent the first now go with the second\n"); | 115 | printf("dispatch_AFC_packet: sent the first now go with the second\n"); |
| 114 | printf("Length: %i\n", length-offset); | 116 | printf("Length: %i\n", length-offset); |
| @@ -118,8 +120,7 @@ int dispatch_AFC_packet(AFClient *client, const char *data, int length) { | |||
| 118 | 120 | ||
| 119 | 121 | ||
| 120 | bytes = mux_send(client->connection, data+offset, length-offset); | 122 | bytes = mux_send(client->connection, data+offset, length-offset); |
| 121 | if (bytes <= 0) { return 0; } | 123 | return bytes; |
| 122 | else { return bytes; } | ||
| 123 | } else { | 124 | } else { |
| 124 | if (debug) printf("dispatch_AFC_packet doin things the old way\n"); | 125 | if (debug) printf("dispatch_AFC_packet doin things the old way\n"); |
| 125 | char *buffer = (char*)malloc(sizeof(char) * client->afc_packet->this_length); | 126 | char *buffer = (char*)malloc(sizeof(char) * client->afc_packet->this_length); |
| @@ -130,10 +131,9 @@ int dispatch_AFC_packet(AFClient *client, const char *data, int length) { | |||
| 130 | if (debug) fwrite(buffer, 1, client->afc_packet->this_length, stdout); | 131 | if (debug) fwrite(buffer, 1, client->afc_packet->this_length, stdout); |
| 131 | if (debug) printf("\n"); | 132 | if (debug) printf("\n"); |
| 132 | bytes = mux_send(client->connection, buffer, client->afc_packet->this_length); | 133 | bytes = mux_send(client->connection, buffer, client->afc_packet->this_length); |
| 133 | if (bytes <= 0) return 0; | 134 | return bytes; |
| 134 | else return bytes; | ||
| 135 | } | 135 | } |
| 136 | return 0; | 136 | return -1; |
| 137 | } | 137 | } |
| 138 | 138 | ||
| 139 | int receive_AFC_data(AFClient *client, char **dump_here) { | 139 | int receive_AFC_data(AFClient *client, char **dump_here) { |
| @@ -148,7 +148,7 @@ int receive_AFC_data(AFClient *client, char **dump_here) { | |||
| 148 | free(buffer); | 148 | free(buffer); |
| 149 | printf("Just didn't get enough.\n"); | 149 | printf("Just didn't get enough.\n"); |
| 150 | *dump_here = NULL; | 150 | *dump_here = NULL; |
| 151 | return 0; | 151 | return -1; |
| 152 | } | 152 | } |
| 153 | 153 | ||
| 154 | r_packet = (AFCPacket*)malloc(sizeof(AFCPacket)); | 154 | r_packet = (AFCPacket*)malloc(sizeof(AFCPacket)); |
| @@ -177,19 +177,19 @@ int receive_AFC_data(AFClient *client, char **dump_here) { | |||
| 177 | if(param1 == 0) { | 177 | if(param1 == 0) { |
| 178 | if (debug) printf("... false alarm, but still\n"); | 178 | if (debug) printf("... false alarm, but still\n"); |
| 179 | *dump_here = NULL; | 179 | *dump_here = NULL; |
| 180 | return 1; | 180 | return 0; |
| 181 | } | 181 | } |
| 182 | else { if (debug) printf("Errno %i\n", param1); } | 182 | else { if (debug) printf("Errno %i\n", param1); } |
| 183 | free(r_packet); | 183 | free(r_packet); |
| 184 | *dump_here = NULL; | 184 | *dump_here = NULL; |
| 185 | return 0; | 185 | return -1; |
| 186 | } else { | 186 | } else { |
| 187 | if (debug) printf("Operation code %x\nFull length %i and this length %i\n", r_packet->operation, r_packet->entire_length, r_packet->this_length); | 187 | if (debug) printf("Operation code %x\nFull length %i and this length %i\n", r_packet->operation, r_packet->entire_length, r_packet->this_length); |
| 188 | } | 188 | } |
| 189 | 189 | ||
| 190 | recv_len = r_packet->entire_length - r_packet->this_length; | 190 | recv_len = r_packet->entire_length - r_packet->this_length; |
| 191 | free(r_packet); | 191 | free(r_packet); |
| 192 | if (!recv_len) | 192 | if (!recv_len && r_packet->operation == AFC_SUCCESS_RESPONSE) |
| 193 | { | 193 | { |
| 194 | *dump_here = NULL; | 194 | *dump_here = NULL; |
| 195 | return 0; | 195 | return 0; |
| @@ -240,10 +240,28 @@ char **afc_get_dir_list(AFClient *client, const char *dir) { | |||
| 240 | char *blah = NULL, **list = NULL; | 240 | char *blah = NULL, **list = NULL; |
| 241 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; | 241 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; |
| 242 | bytes = dispatch_AFC_packet(client, dir, strlen(dir)); | 242 | bytes = dispatch_AFC_packet(client, dir, strlen(dir)); |
| 243 | if (!bytes) { afc_unlock(client); return NULL; } | 243 | if (bytes <= 0) { afc_unlock(client); return NULL; } |
| 244 | |||
| 245 | bytes = receive_AFC_data(client, &blah); | ||
| 246 | if (bytes < 0 && !blah) { afc_unlock(client); return NULL; } | ||
| 247 | |||
| 248 | list = make_strings_list(blah, bytes); | ||
| 249 | free(blah); | ||
| 250 | afc_unlock(client); | ||
| 251 | return list; | ||
| 252 | } | ||
| 253 | |||
| 254 | char **afc_get_devinfo(AFClient *client) { | ||
| 255 | afc_lock(client); | ||
| 256 | client->afc_packet->operation = AFC_GET_DEVINFO; | ||
| 257 | int bytes = 0; | ||
| 258 | char *blah = NULL, **list = NULL; | ||
| 259 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; | ||
| 260 | bytes = dispatch_AFC_packet(client, NULL, 0); | ||
| 261 | if (bytes < 0) { afc_unlock(client); return NULL; } | ||
| 244 | 262 | ||
| 245 | bytes = receive_AFC_data(client, &blah); | 263 | bytes = receive_AFC_data(client, &blah); |
| 246 | if (!bytes && !blah) { afc_unlock(client); return NULL; } | 264 | if (bytes < 0 && !blah) { afc_unlock(client); return NULL; } |
| 247 | 265 | ||
| 248 | list = make_strings_list(blah, bytes); | 266 | list = make_strings_list(blah, bytes); |
| 249 | free(blah); | 267 | free(blah); |
| @@ -251,6 +269,7 @@ char **afc_get_dir_list(AFClient *client, const char *dir) { | |||
| 251 | return list; | 269 | return list; |
| 252 | } | 270 | } |
| 253 | 271 | ||
| 272 | |||
| 254 | char **make_strings_list(char *tokens, int true_length) { | 273 | char **make_strings_list(char *tokens, int true_length) { |
| 255 | if (!tokens || !true_length) return NULL; | 274 | if (!tokens || !true_length) return NULL; |
| 256 | int nulls = 0, i = 0, j = 0; | 275 | int nulls = 0, i = 0, j = 0; |
| @@ -279,7 +298,7 @@ int afc_delete_file(AFClient *client, const char *path) { | |||
| 279 | bytes = receive_AFC_data(client, &receive); | 298 | bytes = receive_AFC_data(client, &receive); |
| 280 | free(receive); | 299 | free(receive); |
| 281 | afc_unlock(client); | 300 | afc_unlock(client); |
| 282 | if (bytes <= 0) { return 0; } | 301 | if (bytes < 0) { return 0; } |
| 283 | else return 1; | 302 | else return 1; |
| 284 | } | 303 | } |
| 285 | 304 | ||
| @@ -302,12 +321,28 @@ int afc_rename_file(AFClient *client, const char *from, const char *to) { | |||
| 302 | bytes = receive_AFC_data(client, &receive); | 321 | bytes = receive_AFC_data(client, &receive); |
| 303 | free(receive); | 322 | free(receive); |
| 304 | afc_unlock(client); | 323 | afc_unlock(client); |
| 305 | if (bytes <= 0) return 0; | 324 | if (bytes < 0) return 0; |
| 306 | else return 1; | 325 | else return 1; |
| 307 | } | 326 | } |
| 308 | 327 | ||
| 328 | int afc_mkdir(AFClient *client, const char *dir) { | ||
| 329 | if (!client) return 0; | ||
| 330 | afc_lock(client); | ||
| 331 | int bytes = 0; | ||
| 332 | char *recvd = NULL; | ||
| 309 | 333 | ||
| 334 | client->afc_packet->operation = AFC_MAKE_DIR; | ||
| 335 | client->afc_packet->this_length = client->afc_packet->entire_length = 0; | ||
| 336 | bytes = dispatch_AFC_packet(client, dir, strlen(dir)); | ||
| 337 | if (bytes <= 0) { afc_unlock(client); return 0; } | ||
| 310 | 338 | ||
| 339 | bytes = receive_AFC_data(client, &recvd); | ||
| 340 | afc_unlock(client); | ||
| 341 | if (recvd) { free(recvd); recvd = NULL; } | ||
| 342 | if (bytes == 0) return 1; | ||
| 343 | else return 0; | ||
| 344 | } | ||
| 345 | |||
| 311 | AFCFile *afc_get_file_info(AFClient *client, const char *path) { | 346 | AFCFile *afc_get_file_info(AFClient *client, const char *path) { |
| 312 | client->afc_packet->operation = AFC_GET_INFO; | 347 | client->afc_packet->operation = AFC_GET_INFO; |
| 313 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; | 348 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; |
| @@ -350,7 +385,7 @@ AFCFile *afc_get_file_info(AFClient *client, const char *path) { | |||
| 350 | } | 385 | } |
| 351 | 386 | ||
| 352 | AFCFile *afc_open_file(AFClient *client, const char *filename, uint32 file_mode) { | 387 | AFCFile *afc_open_file(AFClient *client, const char *filename, uint32 file_mode) { |
| 353 | if (file_mode != AFC_FILE_READ && file_mode != AFC_FILE_WRITE) return NULL; | 388 | //if (file_mode != AFC_FILE_READ && file_mode != AFC_FILE_WRITE) return NULL; |
| 354 | if (!client ||!client->connection || !client->afc_packet) return NULL; | 389 | if (!client ||!client->connection || !client->afc_packet) return NULL; |
| 355 | afc_lock(client); | 390 | afc_lock(client); |
| 356 | char *further_data = (char*)malloc(sizeof(char) * (8 + strlen(filename) + 1)); | 391 | char *further_data = (char*)malloc(sizeof(char) * (8 + strlen(filename) + 1)); |
| @@ -372,17 +407,19 @@ AFCFile *afc_open_file(AFClient *client, const char *filename, uint32 file_mode) | |||
| 372 | return NULL; | 407 | return NULL; |
| 373 | } else { | 408 | } else { |
| 374 | length_thing = receive_AFC_data(client, &further_data); | 409 | length_thing = receive_AFC_data(client, &further_data); |
| 375 | if (length_thing && further_data) { | 410 | if (length_thing > 0 && further_data) { |
| 376 | afc_unlock(client); // don't want to hang on the next call... and besides, it'll re-lock, do its thing, and unlock again anyway. | 411 | afc_unlock(client); // don't want to hang on the next call... and besides, it'll re-lock, do its thing, and unlock again anyway. |
| 377 | file_infos = afc_get_file_info(client, filename); | 412 | file_infos = afc_get_file_info(client, filename); |
| 378 | memcpy(&file_infos->filehandle, further_data, 4); | 413 | memcpy(&file_infos->filehandle, further_data, 4); |
| 379 | return file_infos; | 414 | return file_infos; |
| 380 | } else { | 415 | } else { |
| 381 | if (debug) printf("didn't get further data or something\n"); | 416 | if (debug) printf("didn't get further data or something\n"); |
| 417 | afc_unlock(client); | ||
| 382 | return NULL; | 418 | return NULL; |
| 383 | } | 419 | } |
| 384 | } | 420 | } |
| 385 | if (debug) printf("what the fuck\n"); | 421 | if (debug) printf("what the fuck\n"); |
| 422 | afc_unlock(client); | ||
| 386 | return NULL; | 423 | return NULL; |
| 387 | } | 424 | } |
| 388 | 425 | ||
| @@ -408,7 +445,8 @@ int afc_read_file(AFClient *client, AFCFile *file, char *data, int length) { | |||
| 408 | 445 | ||
| 409 | // Looping here to get around the maximum amount of data that recieve_AFC_data can handle | 446 | // Looping here to get around the maximum amount of data that recieve_AFC_data can handle |
| 410 | while (current_count < length){ | 447 | while (current_count < length){ |
| 411 | 448 | if (debug) printf("afc_read_file: current count is %i but length is %i\n", current_count, length); | |
| 449 | |||
| 412 | // Send the read command | 450 | // Send the read command |
| 413 | AFCFilePacket *packet = (AFCFilePacket*)malloc(sizeof(AFCFilePacket)); | 451 | AFCFilePacket *packet = (AFCFilePacket*)malloc(sizeof(AFCFilePacket)); |
| 414 | packet->unknown1 = packet->unknown2 = 0; | 452 | packet->unknown1 = packet->unknown2 = 0; |
| @@ -421,6 +459,7 @@ int afc_read_file(AFClient *client, AFCFile *file, char *data, int length) { | |||
| 421 | // If we get a positive reponse to the command gather the data | 459 | // If we get a positive reponse to the command gather the data |
| 422 | if (bytes > 0) { | 460 | if (bytes > 0) { |
| 423 | bytes = receive_AFC_data(client, &input); | 461 | bytes = receive_AFC_data(client, &input); |
| 462 | if (debug) printf("afc_read_file: bytes returned: %i\n", bytes); | ||
| 424 | if (bytes < 0) { | 463 | if (bytes < 0) { |
| 425 | if (input) free(input); | 464 | if (input) free(input); |
| 426 | afc_unlock(client); | 465 | afc_unlock(client); |
| @@ -431,7 +470,7 @@ int afc_read_file(AFClient *client, AFCFile *file, char *data, int length) { | |||
| 431 | return current_count; | 470 | return current_count; |
| 432 | } else { | 471 | } else { |
| 433 | if (input) { | 472 | if (input) { |
| 434 | printf("afc_read_file: %d\n", bytes); | 473 | if (debug) printf("afc_read_file: %d\n", bytes); |
| 435 | memcpy(data+current_count, input, (bytes > length) ? length : bytes); | 474 | memcpy(data+current_count, input, (bytes > length) ? length : bytes); |
| 436 | free(input); | 475 | free(input); |
| 437 | input = NULL; | 476 | input = NULL; |
| @@ -444,6 +483,7 @@ int afc_read_file(AFClient *client, AFCFile *file, char *data, int length) { | |||
| 444 | } | 483 | } |
| 445 | } | 484 | } |
| 446 | afc_unlock(client); | 485 | afc_unlock(client); |
| 486 | if (debug) printf("afc_read_file: returning current_count as %i\n", current_count); | ||
| 447 | return current_count; | 487 | return current_count; |
| 448 | } | 488 | } |
| 449 | 489 | ||
| @@ -451,29 +491,53 @@ int afc_write_file(AFClient *client, AFCFile *file, const char *data, int length | |||
| 451 | char *acknowledgement = NULL; | 491 | char *acknowledgement = NULL; |
| 452 | if (!client ||!client->afc_packet || !client->connection || !file) return -1; | 492 | if (!client ||!client->afc_packet || !client->connection || !file) return -1; |
| 453 | afc_lock(client); | 493 | afc_lock(client); |
| 454 | client->afc_packet->this_length = sizeof(AFCPacket) + 8; | ||
| 455 | client->afc_packet->entire_length = client->afc_packet->this_length + length; | ||
| 456 | client->afc_packet->operation = AFC_WRITE; | ||
| 457 | if (debug) printf("afc_write_file: Write length: %i\n", length); | 494 | if (debug) printf("afc_write_file: Write length: %i\n", length); |
| 458 | uint32 zero = 0, bytes = 0; | 495 | const int MAXIMUM_WRITE_SIZE = 1 << 16; |
| 459 | 496 | uint32 zero = 0, bytes = 0, segments = (length / MAXIMUM_WRITE_SIZE), current_count = 0, i = 0; | |
| 460 | char *out_buffer = NULL; | 497 | char *out_buffer = NULL; |
| 498 | |||
| 499 | for (i = 0; i < segments; i++) { // Essentially, yeah, divide it into segments. | ||
| 500 | client->afc_packet->this_length = sizeof(AFCPacket) + 8; | ||
| 501 | //client->afc_packet->entire_length = client->afc_packet->this_length + length; | ||
| 502 | client->afc_packet->entire_length = client->afc_packet->this_length + MAXIMUM_WRITE_SIZE; | ||
| 503 | client->afc_packet->operation = AFC_WRITE; | ||
| 504 | out_buffer = (char*)malloc(sizeof(char) * client->afc_packet->entire_length - sizeof(AFCPacket)); | ||
| 505 | memcpy(out_buffer, (char*)&file->filehandle, sizeof(uint32)); | ||
| 506 | memcpy(out_buffer+4, (char*)&zero, sizeof(uint32)); | ||
| 507 | memcpy(out_buffer+8, data+current_count, MAXIMUM_WRITE_SIZE); | ||
| 508 | |||
| 509 | bytes = dispatch_AFC_packet(client, out_buffer, MAXIMUM_WRITE_SIZE + 8); | ||
| 510 | if (bytes < 0) { afc_unlock(client); return bytes; } | ||
| 511 | free(out_buffer); out_buffer = NULL; // cleanup and hope it works | ||
| 512 | current_count += bytes; | ||
| 513 | bytes = receive_AFC_data(client, &acknowledgement); | ||
| 514 | if (bytes < 0) { afc_unlock(client); return current_count; } | ||
| 515 | } | ||
| 516 | |||
| 517 | // By this point, we should be at the end. i.e. the last segment that didn't get sent in the for loop | ||
| 518 | // this length is fine because it's always sizeof(AFCPacket) + 8, but to be sure we do it again | ||
| 519 | if (current_count == length) { afc_unlock(client); return current_count; } | ||
| 520 | client->afc_packet->this_length = sizeof(AFCPacket) + 8; | ||
| 521 | client->afc_packet->entire_length = client->afc_packet->this_length + (length - current_count); | ||
| 522 | // operation is already AFC_WRITE, but set again to be sure | ||
| 523 | client->afc_packet->operation = AFC_WRITE; | ||
| 461 | out_buffer = (char*)malloc(sizeof(char) * client->afc_packet->entire_length - sizeof(AFCPacket)); | 524 | out_buffer = (char*)malloc(sizeof(char) * client->afc_packet->entire_length - sizeof(AFCPacket)); |
| 462 | memcpy(out_buffer, (char*)&file->filehandle, sizeof(uint32)); | 525 | memcpy(out_buffer, (char*)&file->filehandle, sizeof(uint32)); |
| 463 | memcpy(out_buffer+4, (char*)&zero, sizeof(uint32)); | 526 | memcpy(out_buffer+4, (char*)&zero, sizeof(uint32)); |
| 464 | memcpy(out_buffer+8, data, length); | 527 | memcpy(out_buffer+8, data+current_count, (length - current_count)); |
| 465 | 528 | bytes = dispatch_AFC_packet(client, out_buffer, (length - current_count) + 8); | |
| 466 | bytes = dispatch_AFC_packet(client, out_buffer, length + 8); | 529 | free(out_buffer); out_buffer = NULL; |
| 467 | if (!bytes) return -1; | 530 | current_count += bytes; |
| 531 | if (bytes <= 0) { afc_unlock(client); return current_count; } | ||
| 468 | 532 | ||
| 469 | zero = bytes; | 533 | zero = bytes; |
| 470 | bytes = receive_AFC_data(client, &acknowledgement); | 534 | bytes = receive_AFC_data(client, &acknowledgement); |
| 471 | afc_unlock(client); | 535 | afc_unlock(client); |
| 472 | if (bytes <= 0) { | 536 | if (bytes < 0) { |
| 473 | if (debug) printf("afc_write_file: uh oh?\n"); | 537 | if (debug) printf("afc_write_file: uh oh?\n"); |
| 474 | } | 538 | } |
| 475 | 539 | ||
| 476 | return zero; | 540 | return current_count; |
| 477 | } | 541 | } |
| 478 | 542 | ||
| 479 | void afc_close_file(AFClient *client, AFCFile *file) { | 543 | void afc_close_file(AFClient *client, AFCFile *file) { |
| @@ -490,9 +554,61 @@ void afc_close_file(AFClient *client, AFCFile *file) { | |||
| 490 | 554 | ||
| 491 | free(buffer); | 555 | free(buffer); |
| 492 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; | 556 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; |
| 493 | if (!bytes) return; | 557 | if (bytes <= 0) { afc_unlock(client); return; } |
| 494 | 558 | ||
| 495 | bytes = receive_AFC_data(client, &buffer); | 559 | bytes = receive_AFC_data(client, &buffer); |
| 496 | afc_unlock(client); | 560 | afc_unlock(client); |
| 561 | if (buffer) { free(buffer); } | ||
| 497 | return; | 562 | return; |
| 498 | } | 563 | } |
| 564 | |||
| 565 | int afc_seek_file(AFClient *client, AFCFile *file, int seekpos) { | ||
| 566 | afc_lock(client); | ||
| 567 | |||
| 568 | char *buffer = (char*)malloc(sizeof(char) * 24); | ||
| 569 | uint32 seekto = 0, bytes = 0, zero = 0; | ||
| 570 | if (seekpos < 0) seekpos = file->size - abs(seekpos); | ||
| 571 | seekto = seekpos; | ||
| 572 | |||
| 573 | memcpy(buffer, &file->filehandle, sizeof(uint32)); // handle | ||
| 574 | memcpy(buffer+4, &zero, sizeof(uint32)); // pad | ||
| 575 | memcpy(buffer+8, &zero, sizeof(uint32)); // fromwhere | ||
| 576 | memcpy(buffer+12, &zero, sizeof(uint32)); // pad | ||
| 577 | memcpy(buffer+16, &seekto, sizeof(uint32)); // offset | ||
| 578 | memcpy(buffer+20, &zero, sizeof(uint32)); // pad | ||
| 579 | client->afc_packet->operation = AFC_FILE_SEEK; | ||
| 580 | client->afc_packet->this_length = client->afc_packet->entire_length = 0; | ||
| 581 | bytes = dispatch_AFC_packet(client, buffer, 23); | ||
| 582 | free(buffer); buffer = NULL; | ||
| 583 | if (bytes <= 0) { afc_unlock(client); return -1; } | ||
| 584 | |||
| 585 | bytes = receive_AFC_data(client, &buffer); | ||
| 586 | if (buffer) free(buffer); | ||
| 587 | afc_unlock(client); | ||
| 588 | if (bytes >= 0) return 0; | ||
| 589 | else return -1; | ||
| 590 | } | ||
| 591 | |||
| 592 | int afc_truncate_file(AFClient *client, AFCFile *file, uint32 newsize) { | ||
| 593 | afc_lock(client); | ||
| 594 | |||
| 595 | char *buffer = (char*)malloc(sizeof(char) * 16); | ||
| 596 | uint32 bytes = 0, zero = 0; | ||
| 597 | |||
| 598 | memcpy(buffer, &file->filehandle, sizeof(uint32)); // handle | ||
| 599 | memcpy(buffer+4, &zero, sizeof(uint32)); // pad | ||
| 600 | memcpy(buffer+8, &newsize, sizeof(uint32)); // newsize | ||
| 601 | memcpy(buffer+12, &zero, 3); // pad | ||
| 602 | client->afc_packet->operation = AFC_FILE_TRUNCATE; | ||
| 603 | client->afc_packet->this_length = client->afc_packet->entire_length = 0; | ||
| 604 | bytes = dispatch_AFC_packet(client, buffer, 15); | ||
| 605 | free(buffer); buffer = NULL; | ||
| 606 | if (bytes <= 0) { afc_unlock(client); return -1; } | ||
| 607 | |||
| 608 | bytes = receive_AFC_data(client, &buffer); | ||
| 609 | if (buffer) free(buffer); | ||
| 610 | afc_unlock(client); | ||
| 611 | if (bytes >= 0) return 0; | ||
| 612 | else return -1; | ||
| 613 | } | ||
| 614 | |||
