Skip to content

Commit

Permalink
Add & use dirutil_fsync()
Browse files Browse the repository at this point in the history
  • Loading branch information
gperciva authored and cperciva committed Jan 23, 2025
1 parent 445accd commit 60ecee0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
32 changes: 32 additions & 0 deletions lib/util/dirutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,38 @@ dirutil_fsyncdir(const char * path)
return (0);
}

/**
* dirutil_fsync(fp, name):
* Attempt to write the contents of ${fp} to disk. Do not close ${fp}.
*
* Caveat: "Disks lie" - Kirk McKusick.
*/
int
dirutil_fsync(FILE * fp, const char * name)
{
int fd;

if (fflush(fp)) {
warnp("fflush(%s)", name);
goto err0;
}
if ((fd = fileno(fp)) == -1) {
warnp("fileno(%s)", name);
goto err0;
}
if (fsync(fd)) {
warnp("fsync(%s)", name);
goto err0;
}

/* Success! */
return (0);

err0:
/* Failure! */
return (-1);
}

/**
* build_dir(dir, diropt):
* Make sure that ${dir} exists, creating it (and any parents) as necessary.
Expand Down
8 changes: 8 additions & 0 deletions lib/util/dirutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
*/
int dirutil_fsyncdir(const char *);

/**
* dirutil_fsync(fp, name):
* Attempt to write the contents of ${fp} to disk. Do not close ${fp}.
*
* Caveat: "Disks lie" - Kirk McKusick.
*/
int dirutil_fsync(FILE *, const char *);

/**
* build_dir(dir, diropt):
* Make sure that ${dir} exists, creating it (and any parents) as necessary.
Expand Down
5 changes: 5 additions & 0 deletions tar/ccache/ccache_write.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "asprintf.h"
#include "ccache_internal.h"
#include "dirutil.h"
#include "multitape_internal.h"
#include "patricia.h"
#include "sysendian.h"
Expand Down Expand Up @@ -266,6 +267,10 @@ ccache_write(CCACHE * cache, const char * path)
goto err2;
}

/* Finish writing the file. */
if (dirutil_fsync(W.f, W.s))
goto err2;

/* Close the file. */
if (fclose(W.f))
warnp("fclose");
Expand Down
13 changes: 1 addition & 12 deletions tar/chunks/chunks_directory.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,6 @@ chunks_directory_write(const char * cachepath, RWHASHTAB * HT,
struct chunkstats_external cse;
FILE * f;
char * s;
int fd;

/* The caller must pass the cachepath, and a suffix to use. */
assert(cachepath != NULL);
Expand Down Expand Up @@ -351,18 +350,8 @@ chunks_directory_write(const char * cachepath, RWHASHTAB * HT,
goto err2;

/* Call fsync on the new chunk directory and close it. */
if (fflush(f)) {
warnp("fflush(%s)", s);
if (dirutil_fsync(f, s))
goto err2;
}
if ((fd = fileno(f)) == -1) {
warnp("fileno(%s)", s);
goto err2;
}
if (fsync(fd)) {
warnp("fsync(%s)", s);
goto err2;
}
if (fclose(f)) {
warnp("fclose(%s)", s);
goto err1;
Expand Down

0 comments on commit 60ecee0

Please sign in to comment.