Skip to content

Commit 48831c6

Browse files
committed
[lldb] Fix write only file action to truncate the file
1 parent 5033ea7 commit 48831c6

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

lldb/source/Host/common/FileAction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ bool FileAction::Open(int fd, const FileSpec &file_spec, bool read,
4141
else if (read)
4242
m_arg = O_NOCTTY | O_RDONLY;
4343
else
44-
m_arg = O_NOCTTY | O_CREAT | O_WRONLY;
44+
m_arg = O_NOCTTY | O_CREAT | O_WRONLY | O_TRUNC;
4545
m_file_spec = file_spec;
4646
return true;
4747
} else {

lldb/test/API/python_api/process/io/TestProcessIO.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,36 @@ def test_stdout_stderr_redirection(self):
9595
error = self.read_error_file_and_delete()
9696
self.check_process_output(output, error)
9797

98+
@skipIfWindows # stdio manipulation unsupported on Windows
99+
@expectedFlakeyLinux(bugnumber="llvm.org/pr26437")
100+
@skipIfDarwinEmbedded # debugserver can't create/write files on the device
101+
def test_stdout_stderr_redirection_to_existing_files(self):
102+
"""Exercise SBLaunchInfo::AddOpenFileAction() for STDOUT and STDERR without redirecting STDIN to output files already exist."""
103+
self.setup_test()
104+
self.build()
105+
self.create_target()
106+
self.write_file_with_placeholder(self.output_file)
107+
self.write_file_with_placeholder(self.error_file)
108+
self.redirect_stdout()
109+
self.redirect_stderr()
110+
self.run_process(True)
111+
output = self.read_output_file_and_delete()
112+
error = self.read_error_file_and_delete()
113+
self.check_process_output(output, error)
114+
115+
def write_file_with_placeholder(self, target_file):
116+
placeholder = "This content should be overwritten."
117+
if lldb.remote_platform:
118+
self.runCmd(
119+
'platform file write "{target}" -d "{data}"'.format(
120+
target=target_file, data=placeholder
121+
)
122+
)
123+
else:
124+
f = open(target_file, "w")
125+
f.write(placeholder)
126+
f.close()
127+
98128
# target_file - path on local file system or remote file system if running remote
99129
# local_file - path on local system
100130
def read_file_and_delete(self, target_file, local_file):

lldb/unittests/Host/FileActionTest.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include <fcntl.h>
10+
911
#include "lldb/Host/FileAction.h"
1012
#include "gtest/gtest.h"
1113

@@ -17,3 +19,26 @@ TEST(FileActionTest, Open) {
1719
EXPECT_EQ(Action.GetAction(), FileAction::eFileActionOpen);
1820
EXPECT_EQ(Action.GetFileSpec(), FileSpec("/tmp"));
1921
}
22+
23+
TEST(FileActionTest, OpenReadWrite) {
24+
FileAction Action;
25+
Action.Open(48, FileSpec("/tmp_0"), /*read*/ true, /*write*/ true);
26+
EXPECT_TRUE(Action.GetActionArgument() & (O_NOCTTY | O_CREAT | O_RDWR));
27+
EXPECT_FALSE(Action.GetActionArgument() & O_RDONLY);
28+
EXPECT_FALSE(Action.GetActionArgument() & O_WRONLY);
29+
}
30+
31+
TEST(FileActionTest, OpenReadOnly) {
32+
FileAction Action;
33+
Action.Open(49, FileSpec("/tmp_1"), /*read*/ true, /*write*/ false);
34+
EXPECT_TRUE(Action.GetActionArgument() & (O_NOCTTY | O_RDONLY));
35+
EXPECT_FALSE(Action.GetActionArgument() & O_WRONLY);
36+
}
37+
38+
TEST(FileActionTest, OpenWriteOnly) {
39+
FileAction Action;
40+
Action.Open(50, FileSpec("/tmp_2"), /*read*/ false, /*write*/ true);
41+
EXPECT_TRUE(Action.GetActionArgument() &
42+
(O_NOCTTY | O_CREAT | O_WRONLY | O_TRUNC));
43+
EXPECT_FALSE(Action.GetActionArgument() & O_RDONLY);
44+
}

0 commit comments

Comments
 (0)