diff --git a/Makefile.am b/Makefile.am index 631652bc..fea66673 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 \ diff --git a/lib-platform/util/fileutil.c b/lib-platform/util/fileutil.c index a864233c..4d3f7f64 100644 --- a/lib-platform/util/fileutil.c +++ b/lib-platform/util/fileutil.c @@ -1,6 +1,10 @@ #include "platform.h" #include +#include +#include + +#include "warnp.h" #include "fileutil.h" @@ -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); +} diff --git a/lib-platform/util/fileutil.h b/lib-platform/util/fileutil.h index 6cef9860..1c25d215 100644 --- a/lib-platform/util/fileutil.h +++ b/lib-platform/util/fileutil.h @@ -1,6 +1,8 @@ #ifndef FILEUTIL_H_ #define FILEUTIL_H_ +#include + /** * fileutil_open_noatime(path, flags, noatime): * Act the same as open(2), except that if the OS supports O_NOATIME and @@ -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_ */ diff --git a/lib/util/dirutil.c b/lib/util/dirutil.c index 56a8a3dc..fcccc4cc 100644 --- a/lib/util/dirutil.c +++ b/lib/util/dirutil.c @@ -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. diff --git a/lib/util/dirutil.h b/lib/util/dirutil.h index 98cf43f0..b602ceb7 100644 --- a/lib/util/dirutil.h +++ b/lib/util/dirutil.h @@ -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. diff --git a/tar/ccache/ccache_write.c b/tar/ccache/ccache_write.c index cfb9dd01..25560d64 100644 --- a/tar/ccache/ccache_write.c +++ b/tar/ccache/ccache_write.c @@ -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" @@ -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. */ diff --git a/tar/chunks/chunks_directory.c b/tar/chunks/chunks_directory.c index b20b8038..85d9ed06 100644 --- a/tar/chunks/chunks_directory.c +++ b/tar/chunks/chunks_directory.c @@ -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" @@ -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);