[PATCH] D47863: [Support] Add support for the OF_Delete open flag on posix platforms

Zachary Turner via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 6 19:57:47 PDT 2018


zturner created this revision.
zturner added reviewers: rnk, echristo.
Herald added a subscriber: hiraditya.



  Previously this was documented as only making a difference on
  Windows.  However, we can support this on all platform for
  symmetry.  Although the semantics differ slightly, the important
  part (i.e. the file should no longer exist after the descriptor
  is closed) is supportable everywhere.  On Windows we do this
  with FILE_FLAG_DELETE_ON_CLOSE, as we always have.  On Unixy
  platforms, we do this by simply unlinking the file after it
  has been opened.


https://reviews.llvm.org/D47863

Files:
  llvm/include/llvm/Support/FileSystem.h
  llvm/lib/Support/Unix/Path.inc
  llvm/unittests/Support/Path.cpp


Index: llvm/unittests/Support/Path.cpp
===================================================================
--- llvm/unittests/Support/Path.cpp
+++ llvm/unittests/Support/Path.cpp
@@ -1458,6 +1458,26 @@
   verifyWrite(FD, "Buzz", true);
 }
 
+TEST_F(FileSystemTest, DeleteOnClose) {
+  createFileWithData(NonExistantFile, false, fs::CD_CreateNew, "Fizz");
+
+  // Even though we expect the file to be deleted automatically on close, we put
+  // this here as a failsafe so it gets removed even if the test fails, so we
+  // don't clutter up a file system.
+  FileRemover Cleanup(NonExistantFile);
+
+  int FD;
+  Optional<FileDescriptorCloser> Closer;
+
+  ASSERT_NO_ERROR(fs::openFileForReadWrite(NonExistantFile, FD,
+                                           fs::CD_OpenExisting, fs::OF_Delete));
+  Closer.emplace(FD);
+  verifyRead(FD, "Fizz", true);
+  verifyWrite(FD, "Buzz", true);
+  Closer.reset();
+  ASSERT_FALSE(fs::exists(NonExistantFile));
+}
+
 TEST_F(FileSystemTest, set_current_path) {
   SmallString<128> path;
 
Index: llvm/lib/Support/Unix/Path.inc
===================================================================
--- llvm/lib/Support/Unix/Path.inc
+++ llvm/lib/Support/Unix/Path.inc
@@ -774,6 +774,9 @@
   (void)r;
   assert(r == 0 && "fcntl(F_SETFD, FD_CLOEXEC) failed");
 #endif
+  if (Flags & OF_Delete)
+    ::unlink(P.data());
+
   return std::error_code();
 }
 
Index: llvm/include/llvm/Support/FileSystem.h
===================================================================
--- llvm/include/llvm/Support/FileSystem.h
+++ llvm/include/llvm/Support/FileSystem.h
@@ -716,7 +716,11 @@
   OF_Append = 2,
   F_Append = 2, // For compatibility
 
-  /// Delete the file on close. Only makes a difference on windows.
+  /// Delete the file on close.  This is guaranteed to be supported on all
+  /// platforms, and it is guaranteed that the file descriptor / file object
+  /// that is returned from the open call is valid and usable in the way the
+  /// user has requested.  However, it is **not** guaranteed that
+  /// exists(Path) will return true after the call to open returns.
   OF_Delete = 4
 };
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D47863.150247.patch
Type: text/x-patch
Size: 2133 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180607/5bb0d9b7/attachment.bin>


More information about the llvm-commits mailing list