static int commit_one_file(BDRVVVFATState* s,

	int dir_index, uint32_t offset)

{

    direntry_t* direntry = array_get(&(s->directory), dir_index);

    uint32_t c = begin_of_direntry(direntry);

    uint32_t first_cluster = c;

    mapping_t* mapping = find_mapping_for_cluster(s, c);

    uint32_t size = filesize_of_direntry(direntry);

    char* cluster = g_malloc(s->cluster_size);

    uint32_t i;

    int fd = 0;



    assert(offset < size);

    assert((offset % s->cluster_size) == 0);



    for (i = s->cluster_size; i < offset; i += s->cluster_size)

	c = modified_fat_get(s, c);



    fd = open(mapping->path, O_RDWR | O_CREAT | O_BINARY, 0666);

    if (fd < 0) {

	fprintf(stderr, "Could not open %s... (%s, %d)\n", mapping->path,

		strerror(errno), errno);

	return fd;

    }

    if (offset > 0)

	if (lseek(fd, offset, SEEK_SET) != offset)

	    return -3;



    while (offset < size) {

	uint32_t c1;

	int rest_size = (size - offset > s->cluster_size ?

		s->cluster_size : size - offset);

	int ret;



	c1 = modified_fat_get(s, c);



	assert((size - offset == 0 && fat_eof(s, c)) ||

		(size > offset && c >=2 && !fat_eof(s, c)));



	ret = vvfat_read(s->bs, cluster2sector(s, c),

	    (uint8_t*)cluster, (rest_size + 0x1ff) / 0x200);



	if (ret < 0)

	    return ret;



	if (write(fd, cluster, rest_size) < 0)

	    return -2;



	offset += rest_size;

	c = c1;

    }



    if (ftruncate(fd, size)) {

        perror("ftruncate()");

        close(fd);

        return -4;

    }

    close(fd);



    return commit_mappings(s, first_cluster, dir_index);

}

static int commit_one_file(BDRVVVFATState* s,

	int dir_index, uint32_t offset)

{

    direntry_t* direntry = array_get(&(s->directory), dir_index);

    uint32_t c = begin_of_direntry(direntry);

    uint32_t first_cluster = c;

    mapping_t* mapping = find_mapping_for_cluster(s, c);

    uint32_t size = filesize_of_direntry(direntry);

    char* cluster = g_malloc(s->cluster_size);

    uint32_t i;

    int fd = 0;



    assert(offset < size);

    assert((offset % s->cluster_size) == 0);



    for (i = s->cluster_size; i < offset; i += s->cluster_size)

	c = modified_fat_get(s, c);



    fd = open(mapping->path, O_RDWR | O_CREAT | O_BINARY, 0666);

    if (fd < 0) {

	fprintf(stderr, "Could not open %s... (%s, %d)\n", mapping->path,

		strerror(errno), errno);

	return fd;

    }

    if (offset > 0)

	if (lseek(fd, offset, SEEK_SET) != offset)

	    return -3;



    while (offset < size) {

	uint32_t c1;

	int rest_size = (size - offset > s->cluster_size ?

		s->cluster_size : size - offset);

	int ret;



	c1 = modified_fat_get(s, c);



	assert((size - offset == 0 && fat_eof(s, c)) ||

		(size > offset && c >=2 && !fat_eof(s, c)));



	ret = vvfat_read(s->bs, cluster2sector(s, c),

	    (uint8_t*)cluster, (rest_size + 0x1ff) / 0x200);



	if (ret < 0)

	    return ret;



	if (write(fd, cluster, rest_size) < 0)

	    return -2;



	offset += rest_size;

	c = c1;

    }



    if (ftruncate(fd, size)) {

        perror("ftruncate()");

        close(fd);

        return -4;

    }

    close(fd);



    return commit_mappings(s, first_cluster, dir_index);

}

    if (offset > 0) 	if (lseek(fd, offset, SEEK_SET) != offset) 	    return -3; 	if (ret < 0) 	    return ret; 	if (write(fd, cluster, rest_size) < 0) 	    return -2;
static int FUNC_0(BDRVVVFATState* VAR_0,

	int VAR_1, uint32_t VAR_2)

{

    direntry_t* direntry = array_get(&(VAR_0->directory), VAR_1);

    uint32_t c = begin_of_direntry(direntry);

    uint32_t first_cluster = c;

    mapping_t* mapping = find_mapping_for_cluster(VAR_0, c);

    uint32_t size = filesize_of_direntry(direntry);

    char* VAR_3 = g_malloc(VAR_0->cluster_size);

    uint32_t i;

    int VAR_4 = 0;



    assert(VAR_2 < size);

    assert((VAR_2 % VAR_0->cluster_size) == 0);



    for (i = VAR_0->cluster_size; i < VAR_2; i += VAR_0->cluster_size)

	c = modified_fat_get(VAR_0, c);



    VAR_4 = open(mapping->path, O_RDWR | O_CREAT | O_BINARY, 0666);

    if (VAR_4 < 0) {

	fprintf(stderr, "Could not open %VAR_0... (%VAR_0, %d)\n", mapping->path,

		strerror(errno), errno);

	return VAR_4;

    }

    if (VAR_2 > 0)

	if (lseek(VAR_4, VAR_2, SEEK_SET) != VAR_2)

	    return -3;



    while (VAR_2 < size) {

	uint32_t c1;

	int VAR_5 = (size - VAR_2 > VAR_0->cluster_size ?

		VAR_0->cluster_size : size - VAR_2);

	int VAR_6;



	c1 = modified_fat_get(VAR_0, c);



	assert((size - VAR_2 == 0 && fat_eof(VAR_0, c)) ||

		(size > VAR_2 && c >=2 && !fat_eof(VAR_0, c)));



	VAR_6 = vvfat_read(VAR_0->bs, cluster2sector(VAR_0, c),

	    (uint8_t*)VAR_3, (VAR_5 + 0x1ff) / 0x200);



	if (VAR_6 < 0)

	    return VAR_6;



	if (write(VAR_4, VAR_3, VAR_5) < 0)

	    return -2;



	VAR_2 += VAR_5;

	c = c1;

    }



    if (ftruncate(VAR_4, size)) {

        perror("ftruncate()");

        close(VAR_4);

        return -4;

    }

    close(VAR_4);



    return commit_mappings(VAR_0, first_cluster, VAR_1);

}

static int FUNC_0(BDRVVVFATState* VAR_0,
int VAR_1, uint32_t VAR_2)
{ direntry_t* direntry = array_get(&(VAR_0->directory), VAR_1); uint32_t c = begin_of_direntry(direntry); uint32_t first_cluster = c; mapping_t* mapping = find_mapping_for_cluster(VAR_0, c); uint32_t size = filesize_of_direntry(direntry); char* VAR_3 = g_malloc(VAR_0->cluster_size); uint32_t i; int VAR_4 = 0; assert(VAR_2 < size); assert((VAR_2 % VAR_0->cluster_size) == 0); for (i = VAR_0->cluster_size; i < VAR_2; i += VAR_0->cluster_size) c = modified_fat_get(VAR_0, c); VAR_4 = open(mapping->path, O_RDWR | O_CREAT | O_BINARY, 0666); if (VAR_4 < 0) { fprintf(stderr, "Could not open %VAR_0... (%VAR_0, %d)\n", mapping->path,
strerror(errno), errno); return VAR_4; } if (VAR_2 > 0)
if (lseek(VAR_4, VAR_2, SEEK_SET) != VAR_2)
return -3; while (VAR_2 < size) { uint32_t c1; int VAR_5 = (size - VAR_2 > VAR_0->cluster_size ?
VAR_0->cluster_size : size - VAR_2); int VAR_6; c1 = modified_fat_get(VAR_0, c); assert((size - VAR_2 == 0 && fat_eof(VAR_0, c)) ||
(size > VAR_2 && c >=2 && !fat_eof(VAR_0, c))); VAR_6 = vvfat_read(VAR_0->bs, cluster2sector(VAR_0, c),
(uint8_t*)VAR_3, (VAR_5 + 0x1ff) / 0x200); if (VAR_6 < 0)
return VAR_6; if (write(VAR_4, VAR_3, VAR_5) < 0)
return -2; VAR_2 += VAR_5; c = c1; } if (ftruncate(VAR_4, size)) { perror("ftruncate()"); close(VAR_4); return -4; } close(VAR_4); return commit_mappings(VAR_0, first_cluster, VAR_1); }