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/posix/FileSystem.cpp | |
parent | 375432e4d8f50212bca3d9228e349b5a00f770d7 (diff) | |
download | llvm-d3173f34e8546a96b8d0df0d9de133f88f10c127.zip llvm-d3173f34e8546a96b8d0df0d9de133f88f10c127.tar.gz llvm-d3173f34e8546a96b8d0df0d9de133f88f10c127.tar.bz2 |
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/posix/FileSystem.cpp')
-rw-r--r-- | lldb/source/Host/posix/FileSystem.cpp | 85 |
1 files changed, 39 insertions, 46 deletions
diff --git a/lldb/source/Host/posix/FileSystem.cpp b/lldb/source/Host/posix/FileSystem.cpp index 15ded77..626c37d 100644 --- a/lldb/source/Host/posix/FileSystem.cpp +++ b/lldb/source/Host/posix/FileSystem.cpp @@ -35,70 +35,61 @@ FileSystem::GetNativePathSyntax() } Error -FileSystem::MakeDirectory(const char *path, uint32_t file_permissions) +FileSystem::MakeDirectory(const FileSpec &file_spec, uint32_t file_permissions) { - Error error; - if (path && path[0]) + if (file_spec) { - if (::mkdir(path, file_permissions) != 0) + Error error; + if (::mkdir(file_spec.GetCString(), file_permissions) == -1) { error.SetErrorToErrno(); + errno = 0; switch (error.GetError()) { case ENOENT: { // Parent directory doesn't exist, so lets make it if we can - FileSpec spec(path, false); - if (spec.GetDirectory() && spec.GetFilename()) + // Make the parent directory and try again + FileSpec parent_file_spec{file_spec.GetDirectory().GetCString(), false}; + error = MakeDirectory(parent_file_spec, file_permissions); + if (error.Fail()) + return error; + // Try and make the directory again now that the parent directory was made successfully + if (::mkdir(file_spec.GetCString(), file_permissions) == -1) { - // Make the parent directory and try again - Error error2 = MakeDirectory(spec.GetDirectory().GetCString(), file_permissions); - if (error2.Success()) - { - // Try and make the directory again now that the parent directory was made successfully - if (::mkdir(path, file_permissions) == 0) - error.Clear(); - else - error.SetErrorToErrno(); - } + error.SetErrorToErrno(); + return error; } } - break; - case EEXIST: { - FileSpec path_spec(path, false); - if (path_spec.IsDirectory()) - error.Clear(); // It is a directory and it already exists + if (file_spec.IsDirectory()) + return Error{}; // It is a directory and it already exists } - break; } } + return error; } - else - { - error.SetErrorString("empty path"); - } - return error; + return Error{"empty path"}; } Error -FileSystem::DeleteDirectory(const char *path, bool recurse) +FileSystem::DeleteDirectory(const FileSpec &file_spec, bool recurse) { Error error; - if (path && path[0]) + if (file_spec) { if (recurse) { StreamString command; - command.Printf("rm -rf \"%s\"", path); + command.Printf("rm -rf \"%s\"", file_spec.GetCString()); int status = ::system(command.GetString().c_str()); if (status != 0) error.SetError(status, eErrorTypeGeneric); } else { - if (::rmdir(path) != 0) + if (::rmdir(file_spec.GetCString()) != 0) error.SetErrorToErrno(); } } @@ -110,11 +101,11 @@ FileSystem::DeleteDirectory(const char *path, bool recurse) } Error -FileSystem::GetFilePermissions(const char *path, uint32_t &file_permissions) +FileSystem::GetFilePermissions(const FileSpec &file_spec, uint32_t &file_permissions) { Error error; struct stat file_stats; - if (::stat(path, &file_stats) == 0) + if (::stat(file_spec.GetCString(), &file_stats) == 0) { // The bits in "st_mode" currently match the definitions // for the file mode bits in unix. @@ -128,10 +119,10 @@ FileSystem::GetFilePermissions(const char *path, uint32_t &file_permissions) } Error -FileSystem::SetFilePermissions(const char *path, uint32_t file_permissions) +FileSystem::SetFilePermissions(const FileSpec &file_spec, uint32_t file_permissions) { Error error; - if (::chmod(path, file_permissions) != 0) + if (::chmod(file_spec.GetCString(), file_permissions) != 0) error.SetErrorToErrno(); return error; } @@ -149,43 +140,45 @@ FileSystem::GetFileExists(const FileSpec &file_spec) } Error -FileSystem::Hardlink(const char *src, const char *dst) +FileSystem::Hardlink(const FileSpec &src, const FileSpec &dst) { Error error; - if (::link(dst, src) == -1) + if (::link(dst.GetCString(), src.GetCString()) == -1) error.SetErrorToErrno(); return error; } Error -FileSystem::Symlink(const char *src, const char *dst) +FileSystem::Symlink(const FileSpec &src, const FileSpec &dst) { Error error; - if (::symlink(dst, src) == -1) + if (::symlink(dst.GetCString(), src.GetCString()) == -1) error.SetErrorToErrno(); return error; } Error -FileSystem::Unlink(const char *path) +FileSystem::Unlink(const FileSpec &file_spec) { Error error; - if (::unlink(path) == -1) + if (::unlink(file_spec.GetCString()) == -1) error.SetErrorToErrno(); return error; } Error -FileSystem::Readlink(const char *path, char *buf, size_t buf_len) +FileSystem::Readlink(const FileSpec &src, FileSpec &dst) { Error error; - ssize_t count = ::readlink(path, buf, buf_len); + char buf[PATH_MAX]; + ssize_t count = ::readlink(src.GetCString(), buf, sizeof(buf) - 1); if (count < 0) error.SetErrorToErrno(); - else if (static_cast<size_t>(count) < (buf_len - 1)) - buf[count] = '\0'; // Success else - error.SetErrorString("'buf' buffer is too small to contain link contents"); + { + buf[count] = '\0'; // Success + dst.SetFile(buf, false); + } return error; } |