Summary:
The file itself will still get removed when `TemporaryFile` is destroyed, but closing it before then allows it to be read when on Windows, because `TemporaryFile` opens the temporary file with `O_EXCL`, so Windows denies read access of the file to everything else.
This is needed to fix one of the AsyncFileWriter tests on Windows.
Reviewed By: simpkins
Differential Revision:
D5307719
fbshipit-source-id:
bd65962cc3d34f382cc7b0b55dbf2659ed5ebfce
}
}
+void TemporaryFile::close() {
+ if (::close(fd_) == -1) {
+ PLOG(ERROR) << "close failed";
+ }
+ fd_ = -1;
+}
+
const fs::path& TemporaryFile::path() const {
CHECK(scope_ != Scope::UNLINK_IMMEDIATELY);
DCHECK(!path_.empty());
TemporaryFile::~TemporaryFile() {
if (fd_ != -1 && closeOnDestruction_) {
- if (close(fd_) == -1) {
+ if (::close(fd_) == -1) {
PLOG(ERROR) << "close failed";
}
}
TemporaryFile(TemporaryFile&&) = default;
TemporaryFile& operator=(TemporaryFile&&) = default;
+ void close();
int fd() const { return fd_; }
const fs::path& path() const;
});
}
+TEST(TemporaryFile, EarlyClose) {
+ fs::path p;
+ {
+ TemporaryFile f;
+ p = f.path();
+ EXPECT_TRUE(fs::exists(p));
+ f.close();
+ EXPECT_EQ(-1, f.fd());
+ EXPECT_TRUE(fs::exists(p));
+ }
+ EXPECT_FALSE(fs::exists(p));
+}
+
TEST(TemporaryFile, Prefix) {
TemporaryFile f("Foo");
EXPECT_TRUE(f.path().is_absolute());