[lldb] Fix write only file action to truncate the file (#112657)

When `FileAction` opens file with write access, 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.

For example, by using the settings `target.output-path` and
`target.error-path`, lldb will redirect process stdout/stderr to files.
It then calls this function to write to the files which the above
symptoms appear.

## Test
- Added unit test checking the file flags
- Added 2 api tests checking
  - File content overwritten if the file path already exists
- Stdout and stderr redirection to the same file doesn't change its
behavior
This commit is contained in:
Wanyi
2024-10-29 14:22:51 -04:00
committed by GitHub
parent 8e14c6c172
commit efc6d33be9
5 changed files with 111 additions and 1 deletions

View File

@@ -95,6 +95,36 @@ class ProcessIOTestCase(TestBase):
error = self.read_error_file_and_delete()
self.check_process_output(output, error)
@skipIfWindows # stdio manipulation unsupported on Windows
@expectedFlakeyLinux(bugnumber="llvm.org/pr26437")
@skipIfDarwinEmbedded # debugserver can't create/write files on the device
def test_stdout_stderr_redirection_to_existing_files(self):
"""Exercise SBLaunchInfo::AddOpenFileAction() for STDOUT and STDERR without redirecting STDIN to output files already exist."""
self.setup_test()
self.build()
self.create_target()
self.write_file_with_placeholder(self.output_file)
self.write_file_with_placeholder(self.error_file)
self.redirect_stdout()
self.redirect_stderr()
self.run_process(True)
output = self.read_output_file_and_delete()
error = self.read_error_file_and_delete()
self.check_process_output(output, error)
def write_file_with_placeholder(self, target_file):
placeholder = "This content should be overwritten."
if lldb.remote_platform:
self.runCmd(
'platform file write "{target}" -d "{data}"'.format(
target=target_file, data=placeholder
)
)
else:
f = open(target_file, "w")
f.write(placeholder)
f.close()
# target_file - path on local file system or remote file system if running remote
# local_file - path on local system
def read_file_and_delete(self, target_file, local_file):