[Lldb-commits] [lldb] [lldb] Fix write only file action to truncate the file (PR #112657)
via lldb-commits
lldb-commits at lists.llvm.org
Wed Oct 16 20:36:12 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-lldb
Author: Wanyi (kusmour)
<details>
<summary>Changes</summary>
When FileAction opens file with write access for lldb stdout/stderr redirect, it doesn't clear the file nor append to the end of the file if it already exists. Instead, it writes from cursor index 0.
This function is only called when launch debugging with stdin/stdout/stderr redirection to files.
We can also implement open options like the `FileSystem::Open` method so that we can handle truncate vs. append
---
Full diff: https://github.com/llvm/llvm-project/pull/112657.diff
2 Files Affected:
- (modified) lldb/source/Host/common/FileAction.cpp (+1-1)
- (modified) lldb/unittests/Host/FileActionTest.cpp (+24)
``````````diff
diff --git a/lldb/source/Host/common/FileAction.cpp b/lldb/source/Host/common/FileAction.cpp
index f980d3224640e0..e1c3e14a165ea9 100644
--- a/lldb/source/Host/common/FileAction.cpp
+++ b/lldb/source/Host/common/FileAction.cpp
@@ -41,7 +41,7 @@ bool FileAction::Open(int fd, const FileSpec &file_spec, bool read,
else if (read)
m_arg = O_NOCTTY | O_RDONLY;
else
- m_arg = O_NOCTTY | O_CREAT | O_WRONLY;
+ m_arg = O_NOCTTY | O_CREAT | O_WRONLY | O_TRUNC;
m_file_spec = file_spec;
return true;
} else {
diff --git a/lldb/unittests/Host/FileActionTest.cpp b/lldb/unittests/Host/FileActionTest.cpp
index b208169aac20e6..8c8b81a9250310 100644
--- a/lldb/unittests/Host/FileActionTest.cpp
+++ b/lldb/unittests/Host/FileActionTest.cpp
@@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//
+#include <fcntl.h>
+
#include "lldb/Host/FileAction.h"
#include "gtest/gtest.h"
@@ -17,3 +19,25 @@ TEST(FileActionTest, Open) {
EXPECT_EQ(Action.GetAction(), FileAction::eFileActionOpen);
EXPECT_EQ(Action.GetFileSpec(), FileSpec("/tmp"));
}
+
+TEST(FileActionTest, OpenReadWrite) {
+ FileAction Action;
+ Action.Open(48, FileSpec("/tmp_0"), /*read*/ true, /*write*/ true);
+ EXPECT_TRUE(Action.GetActionArgument() & (O_NOCTTY | O_CREAT | O_RDWR));
+ EXPECT_FALSE(Action.GetActionArgument() & O_RDONLY);
+ EXPECT_FALSE(Action.GetActionArgument() & O_WRONLY);
+}
+
+TEST(FileActionTest, OpenReadOnly) {
+ FileAction Action;
+ Action.Open(49, FileSpec("/tmp_1"), /*read*/ true, /*write*/ false);
+ EXPECT_TRUE(Action.GetActionArgument() & (O_NOCTTY | O_RDONLY));
+ EXPECT_FALSE(Action.GetActionArgument() & O_WRONLY);
+}
+
+TEST(FileActionTest, OpenWriteOnly) {
+ FileAction Action;
+ Action.Open(50, FileSpec("/tmp_2"), /*read*/ false, /*write*/ true);
+ EXPECT_TRUE(Action.GetActionArgument() & (O_NOCTTY | O_CREAT | O_WRONLY | O_TRUNC));
+ EXPECT_FALSE(Action.GetActionArgument() & O_RDONLY);
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/112657
More information about the lldb-commits
mailing list