fs/io/FileOutputStream: add macro HAVE_O_TMPFILE

This commit is contained in:
Max Kellermann 2018-10-31 17:10:52 +01:00
parent 4b8ee58638
commit c60cf944f5
2 changed files with 11 additions and 7 deletions

View File

@ -138,7 +138,7 @@ FileOutputStream::Cancel() noexcept
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#ifdef __linux__ #ifdef HAVE_O_TMPFILE
#ifndef O_TMPFILE #ifndef O_TMPFILE
/* supported since Linux 3.11 */ /* supported since Linux 3.11 */
#define __O_TMPFILE 020000000 #define __O_TMPFILE 020000000
@ -159,12 +159,12 @@ OpenTempFile(FileDescriptor &fd, Path path)
return fd.Open(directory.c_str(), O_TMPFILE|O_WRONLY, 0666); return fd.Open(directory.c_str(), O_TMPFILE|O_WRONLY, 0666);
} }
#endif /* __linux__ */ #endif /* HAVE_O_TMPFILE */
inline void inline void
FileOutputStream::OpenCreate(bool visible) FileOutputStream::OpenCreate(bool visible)
{ {
#ifdef __linux__ #ifdef HAVE_O_TMPFILE
/* try Linux's O_TMPFILE first */ /* try Linux's O_TMPFILE first */
is_tmpfile = !visible && OpenTempFile(fd, GetPath()); is_tmpfile = !visible && OpenTempFile(fd, GetPath());
if (!is_tmpfile) { if (!is_tmpfile) {
@ -175,7 +175,7 @@ FileOutputStream::OpenCreate(bool visible)
0666)) 0666))
throw FormatErrno("Failed to create %s", throw FormatErrno("Failed to create %s",
GetPath().c_str()); GetPath().c_str());
#ifdef __linux__ #ifdef HAVE_O_TMPFILE
} }
#else #else
(void)visible; (void)visible;
@ -218,7 +218,7 @@ FileOutputStream::Commit()
{ {
assert(IsDefined()); assert(IsDefined());
#ifdef __linux__ #ifdef HAVE_O_TMPFILE
if (is_tmpfile) { if (is_tmpfile) {
unlink(GetPath().c_str()); unlink(GetPath().c_str());
@ -251,7 +251,7 @@ FileOutputStream::Cancel() noexcept
switch (mode) { switch (mode) {
case Mode::CREATE: case Mode::CREATE:
#ifdef __linux__ #ifdef HAVE_O_TMPFILE
if (!is_tmpfile) if (!is_tmpfile)
#endif #endif
unlink(GetPath().c_str()); unlink(GetPath().c_str());

View File

@ -46,6 +46,10 @@
#include <windows.h> #include <windows.h>
#endif #endif
#ifdef __linux__
#define HAVE_O_TMPFILE
#endif
class Path; class Path;
class FileOutputStream final : public OutputStream { class FileOutputStream final : public OutputStream {
@ -57,7 +61,7 @@ class FileOutputStream final : public OutputStream {
FileDescriptor fd = FileDescriptor::Undefined(); FileDescriptor fd = FileDescriptor::Undefined();
#endif #endif
#ifdef __linux__ #ifdef HAVE_O_TMPFILE
/** /**
* Was O_TMPFILE used? If yes, then linkat() must be used to * Was O_TMPFILE used? If yes, then linkat() must be used to
* create a link to this file. * create a link to this file.