Skip to content

Commit

Permalink
fileutil_fsync(): rename from dirutil_fsync()
Browse files Browse the repository at this point in the history
  • Loading branch information
gperciva committed Feb 23, 2025
1 parent d51ca6d commit 54b41e7
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 43 deletions.
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,7 @@ tarsnap_recrypt_LDADD= $(LIBTARSNAP_A)
tarsnap_recrypt_CPPFLAGS= \
-I$(top_srcdir)/lib \
-I$(top_srcdir)/lib-platform \
-I$(top_srcdir)/lib-platform/util \
-I$(top_srcdir)/lib/crypto \
-I$(top_srcdir)/lib/datastruct \
-I$(top_srcdir)/lib/keyfile \
Expand Down
36 changes: 36 additions & 0 deletions lib-platform/util/fileutil.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#include "platform.h"

#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

#include "warnp.h"

#include "fileutil.h"

Expand Down Expand Up @@ -35,3 +39,35 @@ fileutil_open_noatime(const char * path, int flags, int noatime)
/* Failure! */
return (fd);
}

/**
* fileutil_fsync(fp, name):
* Attempt to write the contents of ${fp} to disk. Do not close ${fp}.
*
* Caveat: "Disks lie" - Kirk McKusick.
*/
int
fileutil_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);
}
10 changes: 10 additions & 0 deletions lib-platform/util/fileutil.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef FILEUTIL_H_
#define FILEUTIL_H_

#include <stdio.h>

/**
* fileutil_open_noatime(path, flags, noatime):
* Act the same as open(2), except that if the OS supports O_NOATIME and
Expand All @@ -10,4 +12,12 @@
*/
int fileutil_open_noatime(const char *, int, int);

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

#endif /* !FILEUTIL_H_ */
32 changes: 0 additions & 32 deletions lib/util/dirutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,38 +60,6 @@ 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: 0 additions & 8 deletions lib/util/dirutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@
*/
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
4 changes: 2 additions & 2 deletions tar/ccache/ccache_write.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#include "asprintf.h"
#include "ccache_internal.h"
#include "dirutil.h"
#include "fileutil.h"
#include "multitape_internal.h"
#include "patricia.h"
#include "sysendian.h"
Expand Down Expand Up @@ -268,7 +268,7 @@ ccache_write(CCACHE * cache, const char * path)
}

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

/* Close the file. */
Expand Down
3 changes: 2 additions & 1 deletion tar/chunks/chunks_directory.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "asprintf.h"
#include "ctassert.h"
#include "dirutil.h"
#include "fileutil.h"
#include "rwhashtab.h"
#include "sysendian.h"
#include "warnp.h"
Expand Down Expand Up @@ -350,7 +351,7 @@ chunks_directory_write(const char * cachepath, RWHASHTAB * HT,
goto err2;

/* Call fsync on the new chunk directory and close it. */
if (dirutil_fsync(f, s))
if (fileutil_fsync(f, s))
goto err2;
if (fclose(f)) {
warnp("fclose(%s)", s);
Expand Down

0 comments on commit 54b41e7

Please sign in to comment.