Skip to content

Commit eac4e0b

Browse files
committed
[lldb] Fix write only file action to truncate the file
1 parent 1b4a173 commit eac4e0b

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-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/unittests/Host/FileActionTest.cpp

Lines changed: 24 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,25 @@ 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() & (O_NOCTTY | O_CREAT | O_WRONLY | O_TRUNC));
42+
EXPECT_FALSE(Action.GetActionArgument() & O_RDONLY);
43+
}

0 commit comments

Comments
 (0)