[llvm-branch-commits] [llvm] [Support][Path] `TempFile::keep` only falls back to copy on `EXDEV`. (PR #240318)

Juan Manuel Martinez CaamaƱo via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Nov 9 02:18:43 PST 2026


https://github.com/jmmartinez created https://github.com/llvm/llvm-project/pull/240318

If `rename` fails, `TempFile::keep` fell back to `copy_file` regardless of the error on Linux, but only on a cross-device error on Windows.

This patch makes the Linux behavior match the Windows one.

As a result, if `rename` fails with any other error (e.g. `EACCES` or `EBUSY`), `keep` now returns that error instead of attempting a copy.

There are other places in LLVM where the same thing is done (which may indicate that we need a higher-level helper).

* [LLVM's `Caching.cpp`](https://github.com/llvm/llvm-project/blob/f5744e07d2dba7367ec68426fd6b6b07e2916b39/llvm/lib/Support/Caching.cpp#L179);
* [LLDB's `SymbolLocatorSymStore.cpp`](https://github.com/llvm/llvm-project/blob/f5744e07d2dba7367ec68426fd6b6b07e2916b39/lldb/source/Plugins/SymbolLocator/SymStore/SymbolLocatorSymStore.cpp#L422).

This patch contains no tests since triggering the
`std::errc::cross_device_link` and other error kinds depends on the test environment.

>From f3e71901d55691c3da966fdb6a956311185d89e0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juan=20Manuel=20Martinez=20Caama=C3=B1o?=
 <jmartinezcaamao at gmail.com>
Date: Mon, 9 Nov 2026 10:56:49 +0100
Subject: [PATCH] [Support][Path] `TempFile::keep` only falls back to copy on
 `EXDEV`.

If `rename` fails, `TempFile::keep` fell back to `copy_file` regardless
of the error on Linux, but only on a cross-device error on Windows.

This patch makes the Linux behavior match the Windows one.

As a result, if `rename` fails with any other error (e.g. `EACCES` or `EBUSY`),
`keep` now returns that error instead of attempting a copy.

There are other places in LLVM where the same thing is done (which may
indicate that we need a higher-level helper).

* [LLVM's
  `Caching.cpp`](https://github.com/llvm/llvm-project/blob/f5744e07d2dba7367ec68426fd6b6b07e2916b39/llvm/lib/Support/Caching.cpp#L179);
* [LLDB's
  `SymbolLocatorSymStore.cpp`](https://github.com/llvm/llvm-project/blob/f5744e07d2dba7367ec68426fd6b6b07e2916b39/lldb/source/Plugins/SymbolLocator/SymStore/SymbolLocatorSymStore.cpp#L422).

This patch contains no tests since triggering the
`std::errc::cross_device_link` and other error kinds depends on the
test environment.
---
 llvm/lib/Support/Path.cpp | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Support/Path.cpp b/llvm/lib/Support/Path.cpp
index 44011e2858ac2f..c2bca3b19f60a8 100644
--- a/llvm/lib/Support/Path.cpp
+++ b/llvm/lib/Support/Path.cpp
@@ -1338,10 +1338,11 @@ Error TempFile::keep(const Twine &Name) {
 #else
   std::error_code RenameEC = fs::rename(TmpName, Name);
   if (RenameEC) {
-    // If we can't rename, try to copy to work around cross-device link issues.
-    RenameEC = sys::fs::copy_file(TmpName, Name);
-    // Whether the copy succeeded or not, the temporary file is no longer
-    // needed.
+    // If rename failed because it's cross-device, copy instead.
+    if (RenameEC == std::errc::cross_device_link)
+      RenameEC = copy_file(TmpName, Name);
+    // Whether the rename or the copy failed or not, the temporary file is no
+    // longer needed.
     remove(TmpName);
   }
 #endif



More information about the llvm-branch-commits mailing list