Commit 1c6aa3dd authored by Matt Colyer's avatar Matt Colyer

Fixed the data corruption, it turns out that there is a maximum length

that you can request in an AFC packet 2^16.
parent bef65596
......@@ -386,41 +386,63 @@ AFCFile *afc_open_file(AFClient *client, const char *filename, uint32 file_mode)
return NULL;
}
/** Attempts to the read the given number of bytes from the given file.
*
* @param client The relevant AFC client
* @param file The AFCFile to read from
* @param data The pointer to the memory region to store the read data
* @param length The number of bytes to read
*
* @return The number of bytes read if successful. If there was an error
*/
int afc_read_file(AFClient *client, AFCFile *file, char *data, int length) {
if (!client || !client->afc_packet || !client->connection || !file) return -1;
AFCFilePacket *packet = (AFCFilePacket*)malloc(sizeof(AFCFilePacket));
if (debug) printf("afc_read_file called for length %i\n");
afc_lock(client);
if (debug) printf("afc_read_file called for length %i\n", length);
char *input = NULL;
int current_count = 0, bytes = 0;
const int MAXIMUM_READ_SIZE = 1 << 16;
afc_lock(client);
// Looping here to get around the maximum amount of data that recieve_AFC_data can handle
while (current_count < length){
// Send the read command
AFCFilePacket *packet = (AFCFilePacket*)malloc(sizeof(AFCFilePacket));
packet->unknown1 = packet->unknown2 = 0;
packet->filehandle = file->filehandle;
packet->size = length;
int bytes = 0;
packet->size = ((length - current_count) < MAXIMUM_READ_SIZE) ? (length - current_count) : MAXIMUM_READ_SIZE;
client->afc_packet->operation = AFC_READ;
client->afc_packet->entire_length = client->afc_packet->this_length = 0;
bytes = dispatch_AFC_packet(client, (char*)packet, sizeof(AFCFilePacket));
// If we get a positive reponse to the command gather the data
if (bytes > 0) {
bytes = receive_AFC_data(client, &input);
afc_unlock(client);
if (bytes < 0) {
if (input) free(input);
return -1;
} else if (bytes == 0) {
if (input) free(input);
return 0;
return current_count;
} else {
if (input)
memcpy(data, input, (bytes > length) ? length : bytes);
if (input) {
printf("afc_read_file: %d\n", bytes);
memcpy(data+current_count, input, (bytes > length) ? length : bytes);
free(input);
return (bytes > length) ? length : bytes;
input = NULL;
current_count += (bytes > length) ? length : bytes;
}
}
} else {
afc_unlock(client);
return -1;
}
return 0;
}
afc_unlock(client);
return current_count;
}
int afc_write_file(AFClient *client, AFCFile *file, const char *data, int length) {
......
......@@ -103,6 +103,9 @@ static int ifuse_read(const char *path, char *buf, size_t size, off_t offset,
AFCFile *file;
AFClient *afc = fuse_get_context()->private_data;
if (size == 0)
return 0;
file = g_hash_table_lookup(file_handles, &(fi->fh));
if (!file){
return -ENOENT;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment