[clang] [llvm] [Support][vfs] Remove can_write check when replacing file (PR #219540)
Michael Kruse via cfe-commits
cfe-commits at lists.llvm.org
Fri Aug 28 15:52:15 PDT 2026
https://github.com/Meinersbur updated https://github.com/llvm/llvm-project/pull/219540
>From 7c97aa393792ead8e5a20c0fe31dd150d54f58b4 Mon Sep 17 00:00:00 2001
From: Michael Kruse <llvm-project at meinersbur.de>
Date: Fri, 28 Aug 2026 19:06:02 +0200
Subject: [PATCH 1/5] Remove can_write check
---
llvm/lib/Support/VirtualOutputBackends.cpp | 6 ------
1 file changed, 6 deletions(-)
diff --git a/llvm/lib/Support/VirtualOutputBackends.cpp b/llvm/lib/Support/VirtualOutputBackends.cpp
index 0c6ce825d02d0..56f6408e82f96 100644
--- a/llvm/lib/Support/VirtualOutputBackends.cpp
+++ b/llvm/lib/Support/VirtualOutputBackends.cpp
@@ -314,12 +314,6 @@ Error OnDiskOutputFile::initializeFile(std::optional<int> &FD) {
if (sys::fs::exists(Status)) {
if (!sys::fs::is_regular_file(Status))
Config.setNoAtomicWrite();
-
- // Fail now if we can't write to the final destination.
- if (!sys::fs::can_write(OutputPath))
- return make_error<OutputError>(
- OutputPath,
- std::make_error_code(std::errc::operation_not_permitted));
}
}
>From 7ba7fff8259d753f1562d2cefe04aedf1edd6612 Mon Sep 17 00:00:00 2001
From: Michael Kruse <llvm-project at meinersbur.de>
Date: Fri, 28 Aug 2026 19:26:14 +0200
Subject: [PATCH 2/5] Keep check in append mode
---
llvm/lib/Support/VirtualOutputBackends.cpp | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/llvm/lib/Support/VirtualOutputBackends.cpp b/llvm/lib/Support/VirtualOutputBackends.cpp
index 56f6408e82f96..a90d9c076fc63 100644
--- a/llvm/lib/Support/VirtualOutputBackends.cpp
+++ b/llvm/lib/Support/VirtualOutputBackends.cpp
@@ -314,6 +314,12 @@ Error OnDiskOutputFile::initializeFile(std::optional<int> &FD) {
if (sys::fs::exists(Status)) {
if (!sys::fs::is_regular_file(Status))
Config.setNoAtomicWrite();
+
+ // Fail now if we can't write to the final destination.
+ if (Config.getAppend() && !sys::fs::can_write(OutputPath))
+ return make_error<OutputError>(
+ OutputPath,
+ std::make_error_code(std::errc::operation_not_permitted));
}
}
>From 358765ae1b4e680e6f8a7291c3468153d39c2ff3 Mon Sep 17 00:00:00 2001
From: Michael Kruse <llvm-project at meinersbur.de>
Date: Fri, 28 Aug 2026 23:55:07 +0200
Subject: [PATCH 3/5] update comments
---
llvm/lib/Support/VirtualOutputBackends.cpp | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/llvm/lib/Support/VirtualOutputBackends.cpp b/llvm/lib/Support/VirtualOutputBackends.cpp
index a90d9c076fc63..91ed0113409af 100644
--- a/llvm/lib/Support/VirtualOutputBackends.cpp
+++ b/llvm/lib/Support/VirtualOutputBackends.cpp
@@ -306,8 +306,8 @@ Error OnDiskOutputFile::initializeFile(std::optional<int> &FD) {
assert(OutputPath != "-" && "Unexpected request for FD of stdout");
// Disable temporary file for other non-regular files, and if we get a status
- // object, also check if we can write and disable write-through buffers if
- // appropriate.
+ // object, also check if in append mode we can write and disable write-through
+ // buffers if appropriate.
if (Config.getAtomicWrite()) {
sys::fs::file_status Status;
sys::fs::status(OutputPath, Status);
@@ -315,7 +315,10 @@ Error OnDiskOutputFile::initializeFile(std::optional<int> &FD) {
if (!sys::fs::is_regular_file(Status))
Config.setNoAtomicWrite();
- // Fail now if we can't write to the final destination.
+ // In append mode, we will open the file for writing which will need write
+ // permission. Fail now if it is already clear that we can't write to the
+ // final destination. Otherwise, we will delete and replace the file.
+ // Permission bits of the file itself are irrelvant in this case.
if (Config.getAppend() && !sys::fs::can_write(OutputPath))
return make_error<OutputError>(
OutputPath,
>From 20852fe7a18d77b2764585e6a14e7e5394d0dec4 Mon Sep 17 00:00:00 2001
From: Michael Kruse <llvm-project at meinersbur.de>
Date: Sat, 29 Aug 2026 00:00:37 +0200
Subject: [PATCH 4/5] Fix typo
---
llvm/lib/Support/VirtualOutputBackends.cpp | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/Support/VirtualOutputBackends.cpp b/llvm/lib/Support/VirtualOutputBackends.cpp
index 91ed0113409af..82ce98355b1c3 100644
--- a/llvm/lib/Support/VirtualOutputBackends.cpp
+++ b/llvm/lib/Support/VirtualOutputBackends.cpp
@@ -317,8 +317,9 @@ Error OnDiskOutputFile::initializeFile(std::optional<int> &FD) {
// In append mode, we will open the file for writing which will need write
// permission. Fail now if it is already clear that we can't write to the
- // final destination. Otherwise, we will delete and replace the file.
- // Permission bits of the file itself are irrelvant in this case.
+ // final destination.
+ // In non-append more, we will delete and replace the file. Permission
+ // bits of the file itself are irrelevant in this case.
if (Config.getAppend() && !sys::fs::can_write(OutputPath))
return make_error<OutputError>(
OutputPath,
>From 376069e05d7b195152accbc7bf41f1c65600643d Mon Sep 17 00:00:00 2001
From: Michael Kruse <llvm-project at meinersbur.de>
Date: Sat, 29 Aug 2026 00:50:45 +0200
Subject: [PATCH 5/5] Add regression test
---
clang/test/VFS/output-file-permissions.c | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
create mode 100644 clang/test/VFS/output-file-permissions.c
diff --git a/clang/test/VFS/output-file-permissions.c b/clang/test/VFS/output-file-permissions.c
new file mode 100644
index 0000000000000..42d24b2e166c2
--- /dev/null
+++ b/clang/test/VFS/output-file-permissions.c
@@ -0,0 +1,16 @@
+// UNSUPPORTED: system-windows
+
+// Reset test
+// rm -f %t-ref.o %t-readonly.o
+
+// Create a reference file
+// RUN: %clang -c %s -o %t-ref.o
+
+// Compile something, mark the output as read-only and expect it to be replaced
+// (permission bits of the file itself are irrelevant)
+// RUN: touch %t-readonly.o
+// RUN: chmod 100 %t-readonly.o
+// RUN: %clang -c %s -o %t-readonly.o
+// RUN: cmp %t-ref.o %t-readonly.o
+
+void foo() {}
More information about the cfe-commits
mailing list