Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util.c: add fsync_close() helper, use where appropriate #268

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading