Commit 06302af7 authored by Adam Simpkins's avatar Adam Simpkins Committed by Facebook GitHub Bot

disable the writeFileAtomic() function on Windows

Summary:
This puts the `writeFileAtomic()` function behind and `#ifndef _WIN32` check,
to disable it on Windows.

While this function currently compiles on Windows, it does not behave
correctly: it often fails with "Permission denied" errors when trying to use
folly's emulated `fchmod()` function.  Additionally, even the `fchmod()`
succeeds (or is skipped) it fails if the destination path already exists, as
`rename()` on Windows does not allow replacing an existing file.  While we
could attempt to replace the existing file, doing so would not be atomic.  We
might be able to achieve this with transactional NTFS features if the
underlying file system is NTFS, but according to Microsoft these APIs are
discouraged and may be removed in future versions of Windows.

Therefore for now simply disable `writeFileAtomic()` on Windows.

Differential Revision: D21367511

fbshipit-source-id: 50a5a1004feacc8bdb0a24421bea339962e1d546
parent 6af9edb3
......@@ -230,6 +230,20 @@ enum class SyncType {
WITHOUT_SYNC,
};
/*
* writeFileAtomic() does not currently work on Windows.
* Windows does not provide atomic file renames, which makes implementing this
* tricky. Windows does have a MoveFileTransactedA() API which could
* potentially be used, but according to the Microsoft documentation this API is
* discouraged and may be removed in a future version.
*
* In order to implement this properly on Windows we would probably need a pair
* of functions: one for writing the file, and one for reading the contents,
* where the two functions synchronize with each other. We can probably only
* provide atomic update behavior with cooperation from the reader.
*/
#ifndef _WIN32
/**
* Write file contents "atomically".
*
......@@ -281,4 +295,6 @@ int writeFileAtomicNoThrow(
mode_t permissions = 0644,
SyncType syncType = SyncType::WITHOUT_SYNC);
#endif // !_WIN32
} // namespace folly
......@@ -385,6 +385,7 @@ TEST_F(ReadFileFd, InvalidFd) {
PLOG(INFO);
}
#ifndef _WIN32
class WriteFileAtomic : public ::testing::Test {
protected:
WriteFileAtomic() {}
......@@ -547,6 +548,8 @@ TEST_F(WriteFileAtomic, multipleFiles) {
EXPECT_EQ(0440, getPerms(tmpPath("foo_txt")));
EXPECT_EQ(0444, getPerms(tmpPath("foo.txt2")));
}
#endif // !_WIN32
} // namespace test
} // namespace folly
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment