Skip to content

Commit

Permalink
Merge pull request #268 from Villemoes/fsync
Browse files Browse the repository at this point in the history
util.c: add fsync_close() helper, use where appropriate
  • Loading branch information
michaelolbrich authored Nov 8, 2024
2 parents 92cc7c5 + ec5fb97 commit d785239
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion test/hdimage.test
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ test_expect_success fdisk,sfdisk "hdimage" "
test_cmp '${testdir}/hdimage.fdisk' hdimage.fdisk &&
check_size images/test.hdimage-2 11539968 &&
sfdisk_validate images/test.hdimage-2 &&
check_disk_usage_range images/test.hdimage-2 61290 65376 &&
check_disk_usage_range images/test.hdimage-2 61290 65536 &&
sanitized_fdisk_sfdisk images/test.hdimage-2 > hdimage.fdisk-2 &&
test_cmp '${testdir}/hdimage.fdisk-2' hdimage.fdisk-2
"
Expand Down
21 changes: 17 additions & 4 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,19 @@ void debug(const char *fmt, ...)
va_end(args);
}

static int fsync_close(struct image *image, int fd)
{
int a, b;

a = fsync(fd);
if (a)
image_error(image, "fsync() failed: %s\n", strerror(errno));
b = close(fd);
if (b)
image_error(image, "close() failed: %s\n", strerror(errno));
return (a || b) ? -1 : 0;
}

/*
* printf wrapper around 'system'
*/
Expand Down Expand Up @@ -519,7 +532,7 @@ int prepare_image(struct image *image, unsigned long long size)
* than necessary.
*/
ret = ftruncate(fd, size);
close(fd);
fsync_close(image, fd);
if (ret < 0) {
ret = -errno;
image_error(image, "failed to truncate %s to %lld: %s\n",
Expand Down Expand Up @@ -634,7 +647,7 @@ int insert_image(struct image *image, struct image *sub,

out:
if (fd >= 0)
close(fd);
fsync_close(image, fd);
if (in_fd >= 0)
close(in_fd);
free(extents);
Expand Down Expand Up @@ -671,7 +684,7 @@ int insert_data(struct image *image, const void *_data, const char *outfile,
data += now;
}
err_out:
close(outf);
fsync_close(image, outf);

return ret;
}
Expand Down Expand Up @@ -709,7 +722,7 @@ int extend_file(struct image *image, size_t size)
}
ret = 0;
out:
close(f);
fsync_close(image, f);
return ret;
}

Expand Down

0 comments on commit d785239

Please sign in to comment.