Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 17 | #include "android-base/file.h" |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 18 | |
| 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
Colin Cross | 58021d1 | 2017-02-23 21:23:05 -0800 | [diff] [blame] | 21 | #include <libgen.h> |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 22 | #include <sys/stat.h> |
| 23 | #include <sys/types.h> |
Elliott Hughes | d3ff6e5 | 2016-08-23 15:53:45 -0700 | [diff] [blame] | 24 | #include <unistd.h> |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 25 | |
Josh Gao | 7307f09 | 2016-09-01 12:31:42 -0700 | [diff] [blame] | 26 | #include <memory> |
Colin Cross | 58021d1 | 2017-02-23 21:23:05 -0800 | [diff] [blame] | 27 | #include <mutex> |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 28 | #include <string> |
Elliott Hughes | d3ff6e5 | 2016-08-23 15:53:45 -0700 | [diff] [blame] | 29 | #include <vector> |
Elliott Hughes | af4885a | 2015-02-03 13:02:57 -0800 | [diff] [blame] | 30 | |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 31 | #include "android-base/macros.h" // For TEMP_FAILURE_RETRY on Darwin. |
Elliott Hughes | e5dd71a | 2016-07-28 15:15:28 -0700 | [diff] [blame] | 32 | #include "android-base/logging.h" |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 33 | #include "android-base/utf8.h" |
Dan Albert | 94d1360 | 2015-03-26 23:33:28 -0700 | [diff] [blame] | 34 | #include "utils/Compat.h" |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 35 | |
Elliott Hughes | 82ff315 | 2016-08-31 15:07:18 -0700 | [diff] [blame] | 36 | #if defined(__APPLE__) |
Josh Gao | 7307f09 | 2016-09-01 12:31:42 -0700 | [diff] [blame] | 37 | #include <mach-o/dyld.h> |
Elliott Hughes | 82ff315 | 2016-08-31 15:07:18 -0700 | [diff] [blame] | 38 | #endif |
| 39 | #if defined(_WIN32) |
| 40 | #include <windows.h> |
| 41 | #endif |
| 42 | |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 43 | namespace android { |
| 44 | namespace base { |
| 45 | |
Elliott Hughes | c1fd492 | 2015-11-11 18:02:29 +0000 | [diff] [blame] | 46 | // Versions of standard library APIs that support UTF-8 strings. |
| 47 | using namespace android::base::utf8; |
| 48 | |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 49 | bool ReadFdToString(int fd, std::string* content) { |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 50 | content->clear(); |
| 51 | |
| 52 | char buf[BUFSIZ]; |
| 53 | ssize_t n; |
| 54 | while ((n = TEMP_FAILURE_RETRY(read(fd, &buf[0], sizeof(buf)))) > 0) { |
| 55 | content->append(buf, n); |
| 56 | } |
| 57 | return (n == 0) ? true : false; |
| 58 | } |
| 59 | |
Josh Gao | ffabc96 | 2016-09-14 16:11:45 -0700 | [diff] [blame] | 60 | bool ReadFileToString(const std::string& path, std::string* content, bool follow_symlinks) { |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 61 | content->clear(); |
| 62 | |
Josh Gao | ffabc96 | 2016-09-14 16:11:45 -0700 | [diff] [blame] | 63 | int flags = O_RDONLY | O_CLOEXEC | O_BINARY | (follow_symlinks ? 0 : O_NOFOLLOW); |
| 64 | int fd = TEMP_FAILURE_RETRY(open(path.c_str(), flags)); |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 65 | if (fd == -1) { |
| 66 | return false; |
| 67 | } |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 68 | bool result = ReadFdToString(fd, content); |
Nick Kralevich | 95db36e | 2015-05-20 08:59:21 -0700 | [diff] [blame] | 69 | close(fd); |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 70 | return result; |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 73 | bool WriteStringToFd(const std::string& content, int fd) { |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 74 | const char* p = content.data(); |
| 75 | size_t left = content.size(); |
| 76 | while (left > 0) { |
| 77 | ssize_t n = TEMP_FAILURE_RETRY(write(fd, p, left)); |
| 78 | if (n == -1) { |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 79 | return false; |
| 80 | } |
| 81 | p += n; |
| 82 | left -= n; |
| 83 | } |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 84 | return true; |
| 85 | } |
Elliott Hughes | 202f024 | 2015-02-04 13:19:13 -0800 | [diff] [blame] | 86 | |
| 87 | static bool CleanUpAfterFailedWrite(const std::string& path) { |
| 88 | // Something went wrong. Let's not leave a corrupt file lying around. |
| 89 | int saved_errno = errno; |
| 90 | unlink(path.c_str()); |
| 91 | errno = saved_errno; |
| 92 | return false; |
| 93 | } |
| 94 | |
Elliott Hughes | 31fa09c | 2015-02-04 19:38:28 -0800 | [diff] [blame] | 95 | #if !defined(_WIN32) |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 96 | bool WriteStringToFile(const std::string& content, const std::string& path, |
Josh Gao | ffabc96 | 2016-09-14 16:11:45 -0700 | [diff] [blame] | 97 | mode_t mode, uid_t owner, gid_t group, |
| 98 | bool follow_symlinks) { |
| 99 | int flags = O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY | |
| 100 | (follow_symlinks ? 0 : O_NOFOLLOW); |
Elliott Hughes | 470d79a | 2015-09-01 13:35:44 -0700 | [diff] [blame] | 101 | int fd = TEMP_FAILURE_RETRY(open(path.c_str(), flags, mode)); |
Elliott Hughes | 202f024 | 2015-02-04 13:19:13 -0800 | [diff] [blame] | 102 | if (fd == -1) { |
Elliott Hughes | e5dd71a | 2016-07-28 15:15:28 -0700 | [diff] [blame] | 103 | PLOG(ERROR) << "android::WriteStringToFile open failed"; |
Elliott Hughes | 202f024 | 2015-02-04 13:19:13 -0800 | [diff] [blame] | 104 | return false; |
| 105 | } |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 106 | |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 107 | // We do an explicit fchmod here because we assume that the caller really |
| 108 | // meant what they said and doesn't want the umask-influenced mode. |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 109 | if (fchmod(fd, mode) == -1) { |
Elliott Hughes | e5dd71a | 2016-07-28 15:15:28 -0700 | [diff] [blame] | 110 | PLOG(ERROR) << "android::WriteStringToFile fchmod failed"; |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 111 | return CleanUpAfterFailedWrite(path); |
| 112 | } |
| 113 | if (fchown(fd, owner, group) == -1) { |
Elliott Hughes | e5dd71a | 2016-07-28 15:15:28 -0700 | [diff] [blame] | 114 | PLOG(ERROR) << "android::WriteStringToFile fchown failed"; |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 115 | return CleanUpAfterFailedWrite(path); |
| 116 | } |
| 117 | if (!WriteStringToFd(content, fd)) { |
Elliott Hughes | e5dd71a | 2016-07-28 15:15:28 -0700 | [diff] [blame] | 118 | PLOG(ERROR) << "android::WriteStringToFile write failed"; |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 119 | return CleanUpAfterFailedWrite(path); |
| 120 | } |
Nick Kralevich | 95db36e | 2015-05-20 08:59:21 -0700 | [diff] [blame] | 121 | close(fd); |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 122 | return true; |
Elliott Hughes | 202f024 | 2015-02-04 13:19:13 -0800 | [diff] [blame] | 123 | } |
Elliott Hughes | 31fa09c | 2015-02-04 19:38:28 -0800 | [diff] [blame] | 124 | #endif |
Elliott Hughes | 202f024 | 2015-02-04 13:19:13 -0800 | [diff] [blame] | 125 | |
Josh Gao | ffabc96 | 2016-09-14 16:11:45 -0700 | [diff] [blame] | 126 | bool WriteStringToFile(const std::string& content, const std::string& path, |
| 127 | bool follow_symlinks) { |
| 128 | int flags = O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY | |
| 129 | (follow_symlinks ? 0 : O_NOFOLLOW); |
Elliott Hughes | 470d79a | 2015-09-01 13:35:44 -0700 | [diff] [blame] | 130 | int fd = TEMP_FAILURE_RETRY(open(path.c_str(), flags, DEFFILEMODE)); |
Elliott Hughes | 202f024 | 2015-02-04 13:19:13 -0800 | [diff] [blame] | 131 | if (fd == -1) { |
| 132 | return false; |
| 133 | } |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 134 | |
| 135 | bool result = WriteStringToFd(content, fd); |
Nick Kralevich | 95db36e | 2015-05-20 08:59:21 -0700 | [diff] [blame] | 136 | close(fd); |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 137 | return result || CleanUpAfterFailedWrite(path); |
Elliott Hughes | 202f024 | 2015-02-04 13:19:13 -0800 | [diff] [blame] | 138 | } |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 139 | |
Elliott Hughes | 56085ed | 2015-04-24 21:57:16 -0700 | [diff] [blame] | 140 | bool ReadFully(int fd, void* data, size_t byte_count) { |
| 141 | uint8_t* p = reinterpret_cast<uint8_t*>(data); |
| 142 | size_t remaining = byte_count; |
| 143 | while (remaining > 0) { |
| 144 | ssize_t n = TEMP_FAILURE_RETRY(read(fd, p, remaining)); |
| 145 | if (n <= 0) return false; |
| 146 | p += n; |
| 147 | remaining -= n; |
| 148 | } |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | bool WriteFully(int fd, const void* data, size_t byte_count) { |
| 153 | const uint8_t* p = reinterpret_cast<const uint8_t*>(data); |
| 154 | size_t remaining = byte_count; |
| 155 | while (remaining > 0) { |
| 156 | ssize_t n = TEMP_FAILURE_RETRY(write(fd, p, remaining)); |
| 157 | if (n == -1) return false; |
| 158 | p += n; |
| 159 | remaining -= n; |
| 160 | } |
| 161 | return true; |
| 162 | } |
| 163 | |
Yabin Cui | b6e314a | 2016-01-29 17:25:54 -0800 | [diff] [blame] | 164 | bool RemoveFileIfExists(const std::string& path, std::string* err) { |
| 165 | struct stat st; |
| 166 | #if defined(_WIN32) |
| 167 | //TODO: Windows version can't handle symbol link correctly. |
| 168 | int result = stat(path.c_str(), &st); |
| 169 | bool file_type_removable = (result == 0 && S_ISREG(st.st_mode)); |
| 170 | #else |
| 171 | int result = lstat(path.c_str(), &st); |
| 172 | bool file_type_removable = (result == 0 && (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))); |
| 173 | #endif |
| 174 | if (result == 0) { |
| 175 | if (!file_type_removable) { |
| 176 | if (err != nullptr) { |
| 177 | *err = "is not a regular or symbol link file"; |
| 178 | } |
| 179 | return false; |
| 180 | } |
| 181 | if (unlink(path.c_str()) == -1) { |
| 182 | if (err != nullptr) { |
| 183 | *err = strerror(errno); |
| 184 | } |
| 185 | return false; |
| 186 | } |
| 187 | } |
| 188 | return true; |
| 189 | } |
| 190 | |
Elliott Hughes | d3ff6e5 | 2016-08-23 15:53:45 -0700 | [diff] [blame] | 191 | #if !defined(_WIN32) |
| 192 | bool Readlink(const std::string& path, std::string* result) { |
| 193 | result->clear(); |
| 194 | |
| 195 | // Most Linux file systems (ext2 and ext4, say) limit symbolic links to |
| 196 | // 4095 bytes. Since we'll copy out into the string anyway, it doesn't |
| 197 | // waste memory to just start there. We add 1 so that we can recognize |
| 198 | // whether it actually fit (rather than being truncated to 4095). |
| 199 | std::vector<char> buf(4095 + 1); |
| 200 | while (true) { |
| 201 | ssize_t size = readlink(path.c_str(), &buf[0], buf.size()); |
| 202 | // Unrecoverable error? |
| 203 | if (size == -1) return false; |
| 204 | // It fit! (If size == buf.size(), it may have been truncated.) |
| 205 | if (static_cast<size_t>(size) < buf.size()) { |
| 206 | result->assign(&buf[0], size); |
| 207 | return true; |
| 208 | } |
| 209 | // Double our buffer and try again. |
| 210 | buf.resize(buf.size() * 2); |
| 211 | } |
| 212 | } |
| 213 | #endif |
| 214 | |
Dimitry Ivanov | 840b601 | 2016-09-09 10:49:21 -0700 | [diff] [blame] | 215 | #if !defined(_WIN32) |
| 216 | bool Realpath(const std::string& path, std::string* result) { |
| 217 | result->clear(); |
| 218 | |
| 219 | char* realpath_buf = realpath(path.c_str(), nullptr); |
| 220 | if (realpath_buf == nullptr) { |
| 221 | return false; |
| 222 | } |
| 223 | result->assign(realpath_buf); |
| 224 | free(realpath_buf); |
| 225 | return true; |
| 226 | } |
| 227 | #endif |
| 228 | |
Elliott Hughes | 82ff315 | 2016-08-31 15:07:18 -0700 | [diff] [blame] | 229 | std::string GetExecutablePath() { |
| 230 | #if defined(__linux__) |
| 231 | std::string path; |
| 232 | android::base::Readlink("/proc/self/exe", &path); |
| 233 | return path; |
| 234 | #elif defined(__APPLE__) |
Elliott Hughes | 82ff315 | 2016-08-31 15:07:18 -0700 | [diff] [blame] | 235 | char path[PATH_MAX + 1]; |
Josh Gao | 7307f09 | 2016-09-01 12:31:42 -0700 | [diff] [blame] | 236 | uint32_t path_len = sizeof(path); |
| 237 | int rc = _NSGetExecutablePath(path, &path_len); |
| 238 | if (rc < 0) { |
| 239 | std::unique_ptr<char> path_buf(new char[path_len]); |
| 240 | _NSGetExecutablePath(path_buf.get(), &path_len); |
| 241 | return path_buf.get(); |
| 242 | } |
Elliott Hughes | 82ff315 | 2016-08-31 15:07:18 -0700 | [diff] [blame] | 243 | return path; |
| 244 | #elif defined(_WIN32) |
| 245 | char path[PATH_MAX + 1]; |
| 246 | DWORD result = GetModuleFileName(NULL, path, sizeof(path) - 1); |
| 247 | if (result == 0 || result == sizeof(path) - 1) return ""; |
| 248 | path[PATH_MAX - 1] = 0; |
| 249 | return path; |
| 250 | #else |
| 251 | #error unknown OS |
| 252 | #endif |
| 253 | } |
| 254 | |
Colin Cross | bb3a515 | 2017-02-23 17:41:56 -0800 | [diff] [blame] | 255 | std::string GetExecutableDirectory() { |
| 256 | return Dirname(GetExecutablePath()); |
| 257 | } |
Colin Cross | 58021d1 | 2017-02-23 21:23:05 -0800 | [diff] [blame] | 258 | |
Colin Cross | bb3a515 | 2017-02-23 17:41:56 -0800 | [diff] [blame] | 259 | std::string Basename(const std::string& path) { |
Colin Cross | 58021d1 | 2017-02-23 21:23:05 -0800 | [diff] [blame] | 260 | // Copy path because basename may modify the string passed in. |
| 261 | std::string result(path); |
| 262 | |
| 263 | #if !defined(__BIONIC__) |
| 264 | // Use lock because basename() may write to a process global and return a |
| 265 | // pointer to that. Note that this locking strategy only works if all other |
| 266 | // callers to basename in the process also grab this same lock, but its |
| 267 | // better than nothing. Bionic's basename returns a thread-local buffer. |
| 268 | static std::mutex& basename_lock = *new std::mutex(); |
| 269 | std::lock_guard<std::mutex> lock(basename_lock); |
| 270 | #endif |
| 271 | |
| 272 | // Note that if std::string uses copy-on-write strings, &str[0] will cause |
| 273 | // the copy to be made, so there is no chance of us accidentally writing to |
| 274 | // the storage for 'path'. |
| 275 | char* name = basename(&result[0]); |
| 276 | |
| 277 | // In case basename returned a pointer to a process global, copy that string |
| 278 | // before leaving the lock. |
| 279 | result.assign(name); |
| 280 | |
| 281 | return result; |
| 282 | } |
| 283 | |
| 284 | std::string Dirname(const std::string& path) { |
| 285 | // Copy path because dirname may modify the string passed in. |
| 286 | std::string result(path); |
| 287 | |
| 288 | #if !defined(__BIONIC__) |
| 289 | // Use lock because dirname() may write to a process global and return a |
| 290 | // pointer to that. Note that this locking strategy only works if all other |
| 291 | // callers to dirname in the process also grab this same lock, but its |
| 292 | // better than nothing. Bionic's dirname returns a thread-local buffer. |
| 293 | static std::mutex& dirname_lock = *new std::mutex(); |
| 294 | std::lock_guard<std::mutex> lock(dirname_lock); |
| 295 | #endif |
| 296 | |
| 297 | // Note that if std::string uses copy-on-write strings, &str[0] will cause |
| 298 | // the copy to be made, so there is no chance of us accidentally writing to |
| 299 | // the storage for 'path'. |
| 300 | char* parent = dirname(&result[0]); |
| 301 | |
| 302 | // In case dirname returned a pointer to a process global, copy that string |
| 303 | // before leaving the lock. |
| 304 | result.assign(parent); |
| 305 | |
| 306 | return result; |
| 307 | } |
| 308 | |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 309 | } // namespace base |
| 310 | } // namespace android |