Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 1 | //===-- FileSystem.cpp ------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "lldb/Host/FileSystem.h" |
| 11 | |
| 12 | // C includes |
Chaoren Lin | 226937e | 2015-06-27 23:11:34 +0000 | [diff] [blame^] | 13 | #include <dirent.h> |
Greg Clayton | 736888c | 2015-02-23 23:47:09 +0000 | [diff] [blame] | 14 | #include <sys/mount.h> |
| 15 | #include <sys/param.h> |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 16 | #include <sys/stat.h> |
| 17 | #include <sys/types.h> |
Vince Harron | 294aeb9 | 2015-02-24 05:14:49 +0000 | [diff] [blame] | 18 | #ifdef __linux__ |
| 19 | #include <sys/statfs.h> |
| 20 | #include <sys/mount.h> |
| 21 | #include <linux/magic.h> |
| 22 | #endif |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 23 | |
| 24 | // lldb Includes |
| 25 | #include "lldb/Core/Error.h" |
| 26 | #include "lldb/Core/StreamString.h" |
| 27 | #include "lldb/Host/Host.h" |
| 28 | |
| 29 | using namespace lldb; |
| 30 | using namespace lldb_private; |
| 31 | |
| 32 | FileSpec::PathSyntax |
| 33 | FileSystem::GetNativePathSyntax() |
| 34 | { |
| 35 | return FileSpec::ePathSyntaxPosix; |
| 36 | } |
| 37 | |
| 38 | Error |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 39 | FileSystem::MakeDirectory(const FileSpec &file_spec, uint32_t file_permissions) |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 40 | { |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 41 | if (file_spec) |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 42 | { |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 43 | Error error; |
| 44 | if (::mkdir(file_spec.GetCString(), file_permissions) == -1) |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 45 | { |
| 46 | error.SetErrorToErrno(); |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 47 | errno = 0; |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 48 | switch (error.GetError()) |
| 49 | { |
| 50 | case ENOENT: |
| 51 | { |
| 52 | // Parent directory doesn't exist, so lets make it if we can |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 53 | // Make the parent directory and try again |
| 54 | FileSpec parent_file_spec{file_spec.GetDirectory().GetCString(), false}; |
| 55 | error = MakeDirectory(parent_file_spec, file_permissions); |
| 56 | if (error.Fail()) |
| 57 | return error; |
| 58 | // Try and make the directory again now that the parent directory was made successfully |
| 59 | if (::mkdir(file_spec.GetCString(), file_permissions) == -1) |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 60 | { |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 61 | error.SetErrorToErrno(); |
| 62 | return error; |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 63 | } |
| 64 | } |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 65 | case EEXIST: |
| 66 | { |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 67 | if (file_spec.IsDirectory()) |
| 68 | return Error{}; // It is a directory and it already exists |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 69 | } |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 70 | } |
| 71 | } |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 72 | return error; |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 73 | } |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 74 | return Error{"empty path"}; |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | Error |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 78 | FileSystem::DeleteDirectory(const FileSpec &file_spec, bool recurse) |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 79 | { |
| 80 | Error error; |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 81 | if (file_spec) |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 82 | { |
| 83 | if (recurse) |
| 84 | { |
Chaoren Lin | 226937e | 2015-06-27 23:11:34 +0000 | [diff] [blame^] | 85 | DIR *dirp = opendir(file_spec.GetCString()); |
| 86 | if (!dirp) |
| 87 | { |
| 88 | error.SetErrorToErrno(); |
| 89 | return error; |
| 90 | } |
| 91 | struct dirent *direntp; |
| 92 | while (error.Success() && (direntp = readdir(dirp))) |
| 93 | { |
| 94 | if (direntp->d_type == DT_DIR) |
| 95 | error = DeleteDirectory(FileSpec{direntp->d_name, false}, true); |
| 96 | else if (::unlink(direntp->d_name) != 0) |
| 97 | error.SetErrorToErrno(); |
| 98 | } |
| 99 | if (closedir(dirp) != 0) |
| 100 | error.SetErrorToErrno(); |
| 101 | if (error.Fail()) |
| 102 | return error; |
| 103 | return DeleteDirectory(file_spec, false); |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 104 | } |
| 105 | else |
| 106 | { |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 107 | if (::rmdir(file_spec.GetCString()) != 0) |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 108 | error.SetErrorToErrno(); |
| 109 | } |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | error.SetErrorString("empty path"); |
| 114 | } |
| 115 | return error; |
| 116 | } |
| 117 | |
| 118 | Error |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 119 | FileSystem::GetFilePermissions(const FileSpec &file_spec, uint32_t &file_permissions) |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 120 | { |
| 121 | Error error; |
| 122 | struct stat file_stats; |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 123 | if (::stat(file_spec.GetCString(), &file_stats) == 0) |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 124 | { |
| 125 | // The bits in "st_mode" currently match the definitions |
| 126 | // for the file mode bits in unix. |
| 127 | file_permissions = file_stats.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); |
| 128 | } |
| 129 | else |
| 130 | { |
| 131 | error.SetErrorToErrno(); |
| 132 | } |
| 133 | return error; |
| 134 | } |
| 135 | |
| 136 | Error |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 137 | FileSystem::SetFilePermissions(const FileSpec &file_spec, uint32_t file_permissions) |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 138 | { |
| 139 | Error error; |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 140 | if (::chmod(file_spec.GetCString(), file_permissions) != 0) |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 141 | error.SetErrorToErrno(); |
| 142 | return error; |
| 143 | } |
| 144 | |
| 145 | lldb::user_id_t |
| 146 | FileSystem::GetFileSize(const FileSpec &file_spec) |
| 147 | { |
| 148 | return file_spec.GetByteSize(); |
| 149 | } |
| 150 | |
| 151 | bool |
| 152 | FileSystem::GetFileExists(const FileSpec &file_spec) |
| 153 | { |
| 154 | return file_spec.Exists(); |
| 155 | } |
| 156 | |
| 157 | Error |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 158 | FileSystem::Hardlink(const FileSpec &src, const FileSpec &dst) |
Oleksiy Vyalov | a9ea071 | 2015-05-08 23:54:34 +0000 | [diff] [blame] | 159 | { |
| 160 | Error error; |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 161 | if (::link(dst.GetCString(), src.GetCString()) == -1) |
Oleksiy Vyalov | a9ea071 | 2015-05-08 23:54:34 +0000 | [diff] [blame] | 162 | error.SetErrorToErrno(); |
| 163 | return error; |
| 164 | } |
| 165 | |
| 166 | Error |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 167 | FileSystem::Symlink(const FileSpec &src, const FileSpec &dst) |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 168 | { |
| 169 | Error error; |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 170 | if (::symlink(dst.GetCString(), src.GetCString()) == -1) |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 171 | error.SetErrorToErrno(); |
| 172 | return error; |
| 173 | } |
| 174 | |
| 175 | Error |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 176 | FileSystem::Unlink(const FileSpec &file_spec) |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 177 | { |
| 178 | Error error; |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 179 | if (::unlink(file_spec.GetCString()) == -1) |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 180 | error.SetErrorToErrno(); |
| 181 | return error; |
| 182 | } |
| 183 | |
| 184 | Error |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 185 | FileSystem::Readlink(const FileSpec &src, FileSpec &dst) |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 186 | { |
| 187 | Error error; |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 188 | char buf[PATH_MAX]; |
| 189 | ssize_t count = ::readlink(src.GetCString(), buf, sizeof(buf) - 1); |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 190 | if (count < 0) |
| 191 | error.SetErrorToErrno(); |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 192 | else |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 193 | { |
| 194 | buf[count] = '\0'; // Success |
| 195 | dst.SetFile(buf, false); |
| 196 | } |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 197 | return error; |
| 198 | } |
Greg Clayton | 736888c | 2015-02-23 23:47:09 +0000 | [diff] [blame] | 199 | |
Vince Harron | 294aeb9 | 2015-02-24 05:14:49 +0000 | [diff] [blame] | 200 | static bool IsLocal(const struct statfs& info) |
| 201 | { |
| 202 | #ifdef __linux__ |
| 203 | #define CIFS_MAGIC_NUMBER 0xFF534D42 |
Omair Javaid | a77ca51 | 2015-04-27 12:01:59 +0000 | [diff] [blame] | 204 | switch ((uint32_t)info.f_type) |
Vince Harron | 294aeb9 | 2015-02-24 05:14:49 +0000 | [diff] [blame] | 205 | { |
| 206 | case NFS_SUPER_MAGIC: |
| 207 | case SMB_SUPER_MAGIC: |
| 208 | case CIFS_MAGIC_NUMBER: |
| 209 | return false; |
| 210 | default: |
| 211 | return true; |
| 212 | } |
| 213 | #else |
| 214 | return (info.f_flags & MNT_LOCAL) != 0; |
| 215 | #endif |
| 216 | } |
| 217 | |
Greg Clayton | 736888c | 2015-02-23 23:47:09 +0000 | [diff] [blame] | 218 | bool |
| 219 | FileSystem::IsLocal(const FileSpec &spec) |
| 220 | { |
| 221 | struct statfs statfs_info; |
| 222 | std::string path (spec.GetPath()); |
Vince Harron | f783922 | 2015-02-24 05:24:12 +0000 | [diff] [blame] | 223 | if (statfs(path.c_str(), &statfs_info) == 0) |
Vince Harron | 294aeb9 | 2015-02-24 05:14:49 +0000 | [diff] [blame] | 224 | return ::IsLocal(statfs_info); |
Greg Clayton | 736888c | 2015-02-23 23:47:09 +0000 | [diff] [blame] | 225 | return false; |
| 226 | } |