1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-09-28 23:06:00 +03:00

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.
This commit is contained in:
Maarten ter Huurne 2014-08-18 23:14:09 +02:00
parent e12c896b99
commit fe790b1c8d

View File

@ -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(). // Use C functions since STL doesn't seem to have any way of applying fsync().
bool writeStringToFile(string const& filename, string const& data) { bool writeStringToFile(string const& filename, string const& data) {
// Open temporary file. // Open temporary file.
string tempname = filename + '~'; 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) { if (fd < 0) {
return false; return false;
} }
@ -126,10 +132,12 @@ bool writeStringToFile(string const& filename, string const& data) {
constexpr int dirOpenFlags = constexpr int dirOpenFlags =
#ifdef O_DIRECTORY #ifdef O_DIRECTORY
O_DIRECTORY | O_RDONLY; // Linux specific O_DIRECTORY | // Linux
#else
O_RDONLY;
#endif #endif
#ifdef O_CLOEXEC
O_CLOEXEC | // Linux
#endif
O_RDONLY;
bool syncDir(string const& dirname) bool syncDir(string const& dirname)
{ {