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