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 |
Stephane Sezer | 851f23d | 2015-09-09 01:19:05 +0000 | [diff] [blame] | 23 | #if defined(__NetBSD__) |
| 24 | #include <sys/statvfs.h> |
| 25 | #endif |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 26 | |
| 27 | // lldb Includes |
| 28 | #include "lldb/Core/Error.h" |
| 29 | #include "lldb/Core/StreamString.h" |
| 30 | #include "lldb/Host/Host.h" |
| 31 | |
| 32 | using namespace lldb; |
| 33 | using namespace lldb_private; |
| 34 | |
Zachary Turner | 4eff2d3 | 2015-10-14 21:37:36 +0000 | [diff] [blame^] | 35 | const char * |
| 36 | FileSystem::DEV_NULL = "/dev/null"; |
| 37 | |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 38 | FileSpec::PathSyntax |
| 39 | FileSystem::GetNativePathSyntax() |
| 40 | { |
| 41 | return FileSpec::ePathSyntaxPosix; |
| 42 | } |
| 43 | |
| 44 | Error |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 45 | FileSystem::MakeDirectory(const FileSpec &file_spec, uint32_t file_permissions) |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 46 | { |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 47 | if (file_spec) |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 48 | { |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 49 | Error error; |
| 50 | if (::mkdir(file_spec.GetCString(), file_permissions) == -1) |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 51 | { |
| 52 | error.SetErrorToErrno(); |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 53 | errno = 0; |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 54 | switch (error.GetError()) |
| 55 | { |
| 56 | case ENOENT: |
| 57 | { |
| 58 | // Parent directory doesn't exist, so lets make it if we can |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 59 | // Make the parent directory and try again |
| 60 | FileSpec parent_file_spec{file_spec.GetDirectory().GetCString(), false}; |
| 61 | error = MakeDirectory(parent_file_spec, file_permissions); |
| 62 | if (error.Fail()) |
| 63 | return error; |
| 64 | // Try and make the directory again now that the parent directory was made successfully |
| 65 | if (::mkdir(file_spec.GetCString(), file_permissions) == -1) |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 66 | { |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 67 | error.SetErrorToErrno(); |
| 68 | return error; |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 71 | case EEXIST: |
| 72 | { |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 73 | if (file_spec.IsDirectory()) |
| 74 | return Error{}; // It is a directory and it already exists |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 75 | } |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 78 | return error; |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 79 | } |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 80 | return Error{"empty path"}; |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | Error |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 84 | FileSystem::DeleteDirectory(const FileSpec &file_spec, bool recurse) |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 85 | { |
| 86 | Error error; |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 87 | if (file_spec) |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 88 | { |
| 89 | if (recurse) |
| 90 | { |
Greg Clayton | 58c65f0 | 2015-06-29 18:29:00 +0000 | [diff] [blame] | 91 | // Save all sub directories in a list so we don't recursively call this function |
| 92 | // and possibly run out of file descriptors if the directory is too deep. |
| 93 | std::vector<FileSpec> sub_directories; |
| 94 | |
| 95 | FileSpec::ForEachItemInDirectory (file_spec.GetCString(), [&error, &sub_directories](FileSpec::FileType file_type, const FileSpec &spec) -> FileSpec::EnumerateDirectoryResult { |
| 96 | if (file_type == FileSpec::eFileTypeDirectory) |
| 97 | { |
| 98 | // Save all directorires and process them after iterating through this directory |
| 99 | sub_directories.push_back(spec); |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | // Update sub_spec to point to the current file and delete it |
| 104 | error = FileSystem::Unlink(spec); |
| 105 | } |
| 106 | // If anything went wrong, stop iterating, else process the next file |
| 107 | if (error.Fail()) |
| 108 | return FileSpec::eEnumerateDirectoryResultQuit; |
| 109 | else |
| 110 | return FileSpec::eEnumerateDirectoryResultNext; |
| 111 | }); |
| 112 | |
| 113 | if (error.Success()) |
Chaoren Lin | 226937e | 2015-06-27 23:11:34 +0000 | [diff] [blame] | 114 | { |
Greg Clayton | 58c65f0 | 2015-06-29 18:29:00 +0000 | [diff] [blame] | 115 | // Now delete all sub directories with separate calls that aren't |
| 116 | // recursively calling into this function _while_ this function is |
| 117 | // iterating through the current directory. |
| 118 | for (const auto &sub_directory : sub_directories) |
| 119 | { |
| 120 | error = DeleteDirectory(sub_directory, recurse); |
| 121 | if (error.Fail()) |
| 122 | break; |
| 123 | } |
Chaoren Lin | 226937e | 2015-06-27 23:11:34 +0000 | [diff] [blame] | 124 | } |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 125 | } |
Greg Clayton | 58c65f0 | 2015-06-29 18:29:00 +0000 | [diff] [blame] | 126 | |
| 127 | if (error.Success()) |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 128 | { |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 129 | if (::rmdir(file_spec.GetCString()) != 0) |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 130 | error.SetErrorToErrno(); |
| 131 | } |
| 132 | } |
| 133 | else |
| 134 | { |
| 135 | error.SetErrorString("empty path"); |
| 136 | } |
| 137 | return error; |
| 138 | } |
| 139 | |
| 140 | Error |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 141 | FileSystem::GetFilePermissions(const FileSpec &file_spec, uint32_t &file_permissions) |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 142 | { |
| 143 | Error error; |
| 144 | struct stat file_stats; |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 145 | if (::stat(file_spec.GetCString(), &file_stats) == 0) |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 146 | { |
| 147 | // The bits in "st_mode" currently match the definitions |
| 148 | // for the file mode bits in unix. |
| 149 | file_permissions = file_stats.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); |
| 150 | } |
| 151 | else |
| 152 | { |
| 153 | error.SetErrorToErrno(); |
| 154 | } |
| 155 | return error; |
| 156 | } |
| 157 | |
| 158 | Error |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 159 | FileSystem::SetFilePermissions(const FileSpec &file_spec, uint32_t file_permissions) |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 160 | { |
| 161 | Error error; |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 162 | if (::chmod(file_spec.GetCString(), file_permissions) != 0) |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 163 | error.SetErrorToErrno(); |
| 164 | return error; |
| 165 | } |
| 166 | |
| 167 | lldb::user_id_t |
| 168 | FileSystem::GetFileSize(const FileSpec &file_spec) |
| 169 | { |
| 170 | return file_spec.GetByteSize(); |
| 171 | } |
| 172 | |
| 173 | bool |
| 174 | FileSystem::GetFileExists(const FileSpec &file_spec) |
| 175 | { |
| 176 | return file_spec.Exists(); |
| 177 | } |
| 178 | |
| 179 | Error |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 180 | FileSystem::Hardlink(const FileSpec &src, const FileSpec &dst) |
Oleksiy Vyalov | a9ea071 | 2015-05-08 23:54:34 +0000 | [diff] [blame] | 181 | { |
| 182 | Error error; |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 183 | if (::link(dst.GetCString(), src.GetCString()) == -1) |
Oleksiy Vyalov | a9ea071 | 2015-05-08 23:54:34 +0000 | [diff] [blame] | 184 | error.SetErrorToErrno(); |
| 185 | return error; |
| 186 | } |
| 187 | |
Oleksiy Vyalov | 2b38f33 | 2015-09-18 18:12:39 +0000 | [diff] [blame] | 188 | int |
| 189 | FileSystem::GetHardlinkCount(const FileSpec &file_spec) |
| 190 | { |
| 191 | struct stat file_stat; |
| 192 | if (::stat(file_spec.GetCString(), &file_stat) == 0) |
| 193 | return file_stat.st_nlink; |
| 194 | |
| 195 | return -1; |
| 196 | } |
| 197 | |
Oleksiy Vyalov | a9ea071 | 2015-05-08 23:54:34 +0000 | [diff] [blame] | 198 | Error |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 199 | FileSystem::Symlink(const FileSpec &src, const FileSpec &dst) |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 200 | { |
| 201 | Error error; |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 202 | if (::symlink(dst.GetCString(), src.GetCString()) == -1) |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 203 | error.SetErrorToErrno(); |
| 204 | return error; |
| 205 | } |
| 206 | |
| 207 | Error |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 208 | FileSystem::Unlink(const FileSpec &file_spec) |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 209 | { |
| 210 | Error error; |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 211 | if (::unlink(file_spec.GetCString()) == -1) |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 212 | error.SetErrorToErrno(); |
| 213 | return error; |
| 214 | } |
| 215 | |
| 216 | Error |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 217 | FileSystem::Readlink(const FileSpec &src, FileSpec &dst) |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 218 | { |
| 219 | Error error; |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 220 | char buf[PATH_MAX]; |
| 221 | ssize_t count = ::readlink(src.GetCString(), buf, sizeof(buf) - 1); |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 222 | if (count < 0) |
| 223 | error.SetErrorToErrno(); |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 224 | else |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 225 | { |
| 226 | buf[count] = '\0'; // Success |
| 227 | dst.SetFile(buf, false); |
| 228 | } |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 229 | return error; |
| 230 | } |
Greg Clayton | 736888c | 2015-02-23 23:47:09 +0000 | [diff] [blame] | 231 | |
Sean Callanan | 9077e9f | 2015-09-18 22:24:57 +0000 | [diff] [blame] | 232 | Error |
| 233 | FileSystem::ResolveSymbolicLink(const FileSpec &src, FileSpec &dst) |
| 234 | { |
| 235 | char resolved_path[PATH_MAX]; |
| 236 | if (!src.GetPath (resolved_path, sizeof (resolved_path))) |
| 237 | { |
| 238 | return Error("Couldn't get the canonical path for %s", src.GetCString()); |
| 239 | } |
| 240 | |
| 241 | char real_path[PATH_MAX + 1]; |
| 242 | if (realpath(resolved_path, real_path) == nullptr) |
| 243 | { |
| 244 | Error err; |
| 245 | err.SetErrorToErrno(); |
| 246 | return err; |
| 247 | } |
| 248 | |
| 249 | dst = FileSpec(real_path, false); |
| 250 | |
| 251 | return Error(); |
| 252 | } |
| 253 | |
Stephane Sezer | 851f23d | 2015-09-09 01:19:05 +0000 | [diff] [blame] | 254 | #if defined(__NetBSD__) |
| 255 | static bool IsLocal(const struct statvfs& info) |
| 256 | { |
| 257 | return (info.f_flag & MNT_LOCAL) != 0; |
| 258 | } |
| 259 | #else |
Vince Harron | 294aeb9 | 2015-02-24 05:14:49 +0000 | [diff] [blame] | 260 | static bool IsLocal(const struct statfs& info) |
| 261 | { |
| 262 | #ifdef __linux__ |
| 263 | #define CIFS_MAGIC_NUMBER 0xFF534D42 |
Omair Javaid | a77ca51 | 2015-04-27 12:01:59 +0000 | [diff] [blame] | 264 | switch ((uint32_t)info.f_type) |
Vince Harron | 294aeb9 | 2015-02-24 05:14:49 +0000 | [diff] [blame] | 265 | { |
| 266 | case NFS_SUPER_MAGIC: |
| 267 | case SMB_SUPER_MAGIC: |
| 268 | case CIFS_MAGIC_NUMBER: |
| 269 | return false; |
| 270 | default: |
| 271 | return true; |
| 272 | } |
| 273 | #else |
| 274 | return (info.f_flags & MNT_LOCAL) != 0; |
| 275 | #endif |
| 276 | } |
Stephane Sezer | 851f23d | 2015-09-09 01:19:05 +0000 | [diff] [blame] | 277 | #endif |
Vince Harron | 294aeb9 | 2015-02-24 05:14:49 +0000 | [diff] [blame] | 278 | |
Stephane Sezer | 851f23d | 2015-09-09 01:19:05 +0000 | [diff] [blame] | 279 | #if defined(__NetBSD__) |
| 280 | bool |
| 281 | FileSystem::IsLocal(const FileSpec &spec) |
| 282 | { |
| 283 | struct statvfs statfs_info; |
| 284 | std::string path (spec.GetPath()); |
| 285 | if (statvfs(path.c_str(), &statfs_info) == 0) |
| 286 | return ::IsLocal(statfs_info); |
| 287 | return false; |
| 288 | } |
| 289 | #else |
Greg Clayton | 736888c | 2015-02-23 23:47:09 +0000 | [diff] [blame] | 290 | bool |
| 291 | FileSystem::IsLocal(const FileSpec &spec) |
| 292 | { |
| 293 | struct statfs statfs_info; |
| 294 | std::string path (spec.GetPath()); |
Vince Harron | f783922 | 2015-02-24 05:24:12 +0000 | [diff] [blame] | 295 | if (statfs(path.c_str(), &statfs_info) == 0) |
Vince Harron | 294aeb9 | 2015-02-24 05:14:49 +0000 | [diff] [blame] | 296 | return ::IsLocal(statfs_info); |
Greg Clayton | 736888c | 2015-02-23 23:47:09 +0000 | [diff] [blame] | 297 | return false; |
| 298 | } |
Stephane Sezer | 851f23d | 2015-09-09 01:19:05 +0000 | [diff] [blame] | 299 | #endif |