diff options
author | Chaoren Lin <chaorenl@google.com> | 2015-05-29 19:52:29 +0000 |
---|---|---|
committer | Chaoren Lin <chaorenl@google.com> | 2015-05-29 19:52:29 +0000 |
commit | d3173f34e8546a96b8d0df0d9de133f88f10c127 (patch) | |
tree | 5e778446085cbd4a4d43fc3b488b3ac6ff17f2b5 /lldb/source/Host/common | |
parent | 375432e4d8f50212bca3d9228e349b5a00f770d7 (diff) | |
download | bcm5719-llvm-d3173f34e8546a96b8d0df0d9de133f88f10c127.tar.gz bcm5719-llvm-d3173f34e8546a96b8d0df0d9de133f88f10c127.zip |
Refactor many file functions to use FileSpec over strings.
Summary:
This should solve the issue of sending denormalized paths over gdb-remote
if we stick to GetPath(false) in GDBRemoteCommunicationClient, and let the
server handle any denormalization.
Reviewers: ovyalov, zturner, vharron, clayborg
Reviewed By: clayborg
Subscribers: tberghammer, emaste, lldb-commits
Differential Revision: http://reviews.llvm.org/D9728
llvm-svn: 238604
Diffstat (limited to 'lldb/source/Host/common')
-rw-r--r-- | lldb/source/Host/common/Editline.cpp | 4 | ||||
-rw-r--r-- | lldb/source/Host/common/File.cpp | 13 | ||||
-rw-r--r-- | lldb/source/Host/common/FileSpec.cpp | 32 | ||||
-rw-r--r-- | lldb/source/Host/common/Host.cpp | 65 | ||||
-rw-r--r-- | lldb/source/Host/common/HostInfoBase.cpp | 15 | ||||
-rw-r--r-- | lldb/source/Host/common/Socket.cpp | 2 |
6 files changed, 78 insertions, 53 deletions
diff --git a/lldb/source/Host/common/Editline.cpp b/lldb/source/Host/common/Editline.cpp index 0264c4b3309..ed67d0c2c2b 100644 --- a/lldb/source/Host/common/Editline.cpp +++ b/lldb/source/Host/common/Editline.cpp @@ -193,9 +193,9 @@ namespace lldb_private { if (m_path.empty() && m_history && !m_prefix.empty()) { - std::string parent_path = FileSpec ("~/.lldb", true).GetPath(); + FileSpec parent_path{"~/.lldb", true}; char history_path[PATH_MAX]; - if (FileSystem::MakeDirectory(parent_path.c_str(), lldb::eFilePermissionsDirectoryDefault).Success()) + if (FileSystem::MakeDirectory(parent_path, lldb::eFilePermissionsDirectoryDefault).Success()) { snprintf (history_path, sizeof (history_path), "~/.lldb/%s-history", m_prefix.c_str()); } diff --git a/lldb/source/Host/common/File.cpp b/lldb/source/Host/common/File.cpp index 4361eae78c8..a3420bff65f 100644 --- a/lldb/source/Host/common/File.cpp +++ b/lldb/source/Host/common/File.cpp @@ -325,12 +325,12 @@ File::Open (const char *path, uint32_t options, uint32_t permissions) } uint32_t -File::GetPermissions (const char *path, Error &error) +File::GetPermissions(const FileSpec &file_spec, Error &error) { - if (path && path[0]) + if (file_spec) { struct stat file_stats; - if (::stat (path, &file_stats) == -1) + if (::stat(file_spec.GetCString(), &file_stats) == -1) error.SetErrorToErrno(); else { @@ -339,12 +339,7 @@ File::GetPermissions (const char *path, Error &error) } } else - { - if (path) - error.SetErrorString ("invalid path"); - else - error.SetErrorString ("empty path"); - } + error.SetErrorString ("empty file spec"); return 0; } diff --git a/lldb/source/Host/common/FileSpec.cpp b/lldb/source/Host/common/FileSpec.cpp index c0efa71aad1..65d6543c86e 100644 --- a/lldb/source/Host/common/FileSpec.cpp +++ b/lldb/source/Host/common/FileSpec.cpp @@ -243,7 +243,17 @@ FileSpec::FileSpec(const char *pathname, bool resolve_path, PathSyntax syntax) : } FileSpec::FileSpec(const char *pathname, bool resolve_path, ArchSpec arch) : - FileSpec(pathname, resolve_path, arch.GetTriple().isOSWindows() ? ePathSyntaxWindows : ePathSyntaxPosix) + FileSpec{pathname, resolve_path, arch.GetTriple().isOSWindows() ? ePathSyntaxWindows : ePathSyntaxPosix} +{ +} + +FileSpec::FileSpec(const std::string &path, bool resolve_path, PathSyntax syntax) : + FileSpec{path.c_str(), resolve_path, syntax} +{ +} + +FileSpec::FileSpec(const std::string &path, bool resolve_path, ArchSpec arch) : + FileSpec{path.c_str(), resolve_path, arch} { } @@ -334,6 +344,12 @@ FileSpec::SetFile (const char *pathname, bool resolve, PathSyntax syntax) m_directory.SetCString(normalized.c_str()); } +void +FileSpec::SetFile(const std::string &pathname, bool resolve, PathSyntax syntax) +{ + return SetFile(pathname.c_str(), resolve, syntax); +} + //---------------------------------------------------------------------- // Convert to pointer operator. This allows code to check any FileSpec // objects to see if they contain anything valid using code such as: @@ -755,7 +771,7 @@ FileSpec::GetPermissions () const { uint32_t file_permissions = 0; if (*this) - FileSystem::GetFilePermissions(GetPath().c_str(), file_permissions); + FileSystem::GetFilePermissions(*this, file_permissions); return file_permissions; } @@ -829,6 +845,12 @@ FileSpec::GetPath(bool denormalize) const return std::string(result.begin(), result.end()); } +const char * +FileSpec::GetCString(bool denormalize) const +{ + return ConstString{GetPath(denormalize)}.AsCString(NULL); +} + void FileSpec::GetPath(llvm::SmallVectorImpl<char> &path, bool denormalize) const { @@ -1336,6 +1358,12 @@ FileSpec::AppendPathComponent (const char *new_path) } void +FileSpec::AppendPathComponent(const std::string &new_path) +{ + return AppendPathComponent(new_path.c_str()); +} + +void FileSpec::RemoveLastPathComponent () { const bool resolve = false; diff --git a/lldb/source/Host/common/Host.cpp b/lldb/source/Host/common/Host.cpp index e2471d21078..20d6355e619 100644 --- a/lldb/source/Host/common/Host.cpp +++ b/lldb/source/Host/common/Host.cpp @@ -533,25 +533,25 @@ MonitorShellCommand (void *callback_baton, } Error -Host::RunShellCommand (const char *command, - const char *working_dir, - int *status_ptr, - int *signo_ptr, - std::string *command_output_ptr, - uint32_t timeout_sec, - bool run_in_default_shell) +Host::RunShellCommand(const char *command, + const FileSpec &working_dir, + int *status_ptr, + int *signo_ptr, + std::string *command_output_ptr, + uint32_t timeout_sec, + bool run_in_default_shell) { return RunShellCommand(Args(command), working_dir, status_ptr, signo_ptr, command_output_ptr, timeout_sec, run_in_default_shell); } Error -Host::RunShellCommand (const Args &args, - const char *working_dir, - int *status_ptr, - int *signo_ptr, - std::string *command_output_ptr, - uint32_t timeout_sec, - bool run_in_default_shell) +Host::RunShellCommand(const Args &args, + const FileSpec &working_dir, + int *status_ptr, + int *signo_ptr, + std::string *command_output_ptr, + uint32_t timeout_sec, + bool run_in_default_shell) { Error error; ProcessLaunchInfo launch_info; @@ -597,11 +597,13 @@ Host::RunShellCommand (const Args &args, llvm::sys::fs::createTemporaryFile("lldb-shell-output.%%%%%%", "", output_file_path); } } - + + FileSpec output_file_spec{output_file_path.c_str(), false}; + launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false); - if (!output_file_path.empty()) + if (output_file_spec) { - launch_info.AppendOpenFileAction(STDOUT_FILENO, output_file_path.c_str(), false, true); + launch_info.AppendOpenFileAction(STDOUT_FILENO, output_file_spec, false, true); launch_info.AppendDuplicateFileAction(STDOUT_FILENO, STDERR_FILENO); } else @@ -660,8 +662,7 @@ Host::RunShellCommand (const Args &args, if (command_output_ptr) { command_output_ptr->clear(); - FileSpec file_spec(output_file_path.c_str(), File::eOpenOptionRead); - uint64_t file_size = file_spec.GetByteSize(); + uint64_t file_size = output_file_spec.GetByteSize(); if (file_size > 0) { if (file_size > command_output_ptr->max_size()) @@ -670,18 +671,19 @@ Host::RunShellCommand (const Args &args, } else { - command_output_ptr->resize(file_size); - file_spec.ReadFileContents(0, &((*command_output_ptr)[0]), command_output_ptr->size(), &error); + std::vector<char> command_output(file_size); + output_file_spec.ReadFileContents(0, command_output.data(), file_size, &error); + if (error.Success()) + command_output_ptr->assign(command_output.data(), file_size); } } } } shell_info->can_delete.SetValue(true, eBroadcastAlways); } - - FileSpec output_file_spec(output_file_path.c_str(), false); + if (FileSystem::GetFileExists(output_file_spec)) - FileSystem::Unlink(output_file_path.c_str()); + FileSystem::Unlink(output_file_spec); // Handshake with the monitor thread, or just let it know in advance that // it can delete "shell_info" in case we timed out and were not able to kill // the process... @@ -832,16 +834,18 @@ Host::LaunchProcessPosixSpawn(const char *exe_path, const ProcessLaunchInfo &lau current_dir[0] = '\0'; #endif - const char *working_dir = launch_info.GetWorkingDirectory(); + FileSpec working_dir{launch_info.GetWorkingDirectory()}; if (working_dir) { #if defined (__APPLE__) // Set the working directory on this thread only - if (__pthread_chdir (working_dir) < 0) { + if (__pthread_chdir(working_dir.GetCString()) < 0) { if (errno == ENOENT) { - error.SetErrorStringWithFormat("No such file or directory: %s", working_dir); + error.SetErrorStringWithFormat("No such file or directory: %s", + working_dir.GetCString()); } else if (errno == ENOTDIR) { - error.SetErrorStringWithFormat("Path doesn't name a directory: %s", working_dir); + error.SetErrorStringWithFormat("Path doesn't name a directory: %s", + working_dir.GetCString()); } else { error.SetErrorStringWithFormat("An unknown error occurred when changing directory for process execution."); } @@ -855,10 +859,11 @@ Host::LaunchProcessPosixSpawn(const char *exe_path, const ProcessLaunchInfo &lau return error; } - if (::chdir(working_dir) == -1) + if (::chdir(working_dir.GetCString()) == -1) { error.SetError(errno, eErrorTypePOSIX); - error.LogIfError(log, "unable to change working directory to %s", working_dir); + error.LogIfError(log, "unable to change working directory to %s", + working_dir.GetCString()); return error; } #endif diff --git a/lldb/source/Host/common/HostInfoBase.cpp b/lldb/source/Host/common/HostInfoBase.cpp index f44ad961653..cd4815a767e 100644 --- a/lldb/source/Host/common/HostInfoBase.cpp +++ b/lldb/source/Host/common/HostInfoBase.cpp @@ -40,7 +40,7 @@ namespace // Remove the LLDB temporary directory if we have one. Set "recurse" to // true to all files that were created for the LLDB process can be cleaned up. - FileSystem::DeleteDirectory(tmpdir_file_spec.GetDirectory().GetCString(), true); + FileSystem::DeleteDirectory(tmpdir_file_spec, true); } //---------------------------------------------------------------------- @@ -326,18 +326,15 @@ HostInfoBase::ComputeProcessTempFileDirectory(FileSpec &file_spec) if (!HostInfo::ComputeGlobalTempFileDirectory(temp_file_spec)) return false; - std::string pid_str; - llvm::raw_string_ostream pid_stream(pid_str); - pid_stream << Host::GetCurrentProcessID(); - temp_file_spec.AppendPathComponent(pid_stream.str().c_str()); - std::string final_path = temp_file_spec.GetPath(); - if (!FileSystem::MakeDirectory(final_path.c_str(), eFilePermissionsDirectoryDefault).Success()) + std::string pid_str{std::to_string(Host::GetCurrentProcessID())}; + temp_file_spec.AppendPathComponent(pid_str); + if (!FileSystem::MakeDirectory(temp_file_spec, eFilePermissionsDirectoryDefault).Success()) return false; // Make an atexit handler to clean up the process specify LLDB temp dir // and all of its contents. ::atexit(CleanupProcessSpecificLLDBTempDir); - file_spec.GetDirectory().SetCStringWithLength(final_path.c_str(), final_path.size()); + file_spec = temp_file_spec; return true; } @@ -370,7 +367,7 @@ HostInfoBase::ComputeGlobalTempFileDirectory(FileSpec &file_spec) return false; temp_file_spec.AppendPathComponent("lldb"); - if (!FileSystem::MakeDirectory(temp_file_spec.GetPath().c_str(), eFilePermissionsDirectoryDefault).Success()) + if (!FileSystem::MakeDirectory(temp_file_spec, eFilePermissionsDirectoryDefault).Success()) return false; file_spec = temp_file_spec; diff --git a/lldb/source/Host/common/Socket.cpp b/lldb/source/Host/common/Socket.cpp index 2059fec5f51..f7e93c634a1 100644 --- a/lldb/source/Host/common/Socket.cpp +++ b/lldb/source/Host/common/Socket.cpp @@ -504,7 +504,7 @@ Error Socket::UnixDomainAccept(llvm::StringRef name, bool child_processes_inheri saddr_un.sun_len = SUN_LEN (&saddr_un); #endif - FileSystem::Unlink(name.data()); + FileSystem::Unlink(FileSpec{name, true}); bool success = false; if (::bind (listen_fd, (struct sockaddr *)&saddr_un, SUN_LEN (&saddr_un)) == 0) { |