1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-12-26 05:26:28 +02: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().
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)
{