From fe790b1c8db993961f55578b17530af7749cd343 Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Mon, 18 Aug 2014 23:14:09 +0200 Subject: [PATCH] Improved handling of platform specific open flags There was support for platforms without O_DIRECTORY, but O_CLOEXEC is also Linux specific and was hardcoded, making the whole thing not portable. --- src/utilities.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/utilities.cpp b/src/utilities.cpp index b86151e..b8b8bd2 100644 --- a/src/utilities.cpp +++ b/src/utilities.cpp @@ -82,11 +82,17 @@ string readFileAsString(string const& filename) { } } +constexpr int writeOpenFlags = +#ifdef O_CLOEXEC + O_CLOEXEC | // Linux +#endif + O_CREAT | O_WRONLY | O_TRUNC; + // Use C functions since STL doesn't seem to have any way of applying fsync(). bool writeStringToFile(string const& filename, string const& data) { // Open temporary file. string tempname = filename + '~'; - int fd = open(tempname.c_str(), O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC); + int fd = open(tempname.c_str(), writeOpenFlags); if (fd < 0) { return false; } @@ -126,10 +132,12 @@ bool writeStringToFile(string const& filename, string const& data) { constexpr int dirOpenFlags = #ifdef O_DIRECTORY - O_DIRECTORY | O_RDONLY; // Linux specific -#else - O_RDONLY; + O_DIRECTORY | // Linux #endif +#ifdef O_CLOEXEC + O_CLOEXEC | // Linux +#endif + O_RDONLY; bool syncDir(string const& dirname) {