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 | e5dd71a | 2016-07-28 15:15:28 -0700 | [diff] [blame] | 31 | #include "android-base/logging.h" |
Christopher Ferris | 48d08c2 | 2017-04-05 12:09:17 -0700 | [diff] [blame] | 32 | #include "android-base/macros.h" // For TEMP_FAILURE_RETRY on Darwin. |
| 33 | #include "android-base/unique_fd.h" |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 34 | #include "android-base/utf8.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> |
Elliott Hughes | 282ec45 | 2017-05-15 17:31:15 -0700 | [diff] [blame] | 41 | #define O_CLOEXEC O_NOINHERIT |
| 42 | #define O_NOFOLLOW 0 |
Elliott Hughes | 82ff315 | 2016-08-31 15:07:18 -0700 | [diff] [blame] | 43 | #endif |
| 44 | |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 45 | namespace android { |
| 46 | namespace base { |
| 47 | |
Elliott Hughes | c1fd492 | 2015-11-11 18:02:29 +0000 | [diff] [blame] | 48 | // Versions of standard library APIs that support UTF-8 strings. |
| 49 | using namespace android::base::utf8; |
| 50 | |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 51 | bool ReadFdToString(int fd, std::string* content) { |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 52 | content->clear(); |
| 53 | |
Elliott Hughes | 9bb7971 | 2017-03-20 19:16:18 -0700 | [diff] [blame] | 54 | // Although original we had small files in mind, this code gets used for |
| 55 | // very large files too, where the std::string growth heuristics might not |
| 56 | // be suitable. https://code.google.com/p/android/issues/detail?id=258500. |
| 57 | struct stat sb; |
| 58 | if (fstat(fd, &sb) != -1 && sb.st_size > 0) { |
| 59 | content->reserve(sb.st_size); |
| 60 | } |
| 61 | |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 62 | char buf[BUFSIZ]; |
| 63 | ssize_t n; |
| 64 | while ((n = TEMP_FAILURE_RETRY(read(fd, &buf[0], sizeof(buf)))) > 0) { |
| 65 | content->append(buf, n); |
| 66 | } |
| 67 | return (n == 0) ? true : false; |
| 68 | } |
| 69 | |
Josh Gao | ffabc96 | 2016-09-14 16:11:45 -0700 | [diff] [blame] | 70 | bool ReadFileToString(const std::string& path, std::string* content, bool follow_symlinks) { |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 71 | content->clear(); |
| 72 | |
Josh Gao | ffabc96 | 2016-09-14 16:11:45 -0700 | [diff] [blame] | 73 | int flags = O_RDONLY | O_CLOEXEC | O_BINARY | (follow_symlinks ? 0 : O_NOFOLLOW); |
Christopher Ferris | 48d08c2 | 2017-04-05 12:09:17 -0700 | [diff] [blame] | 74 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), flags))); |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 75 | if (fd == -1) { |
| 76 | return false; |
| 77 | } |
Christopher Ferris | 48d08c2 | 2017-04-05 12:09:17 -0700 | [diff] [blame] | 78 | return ReadFdToString(fd, content); |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 81 | bool WriteStringToFd(const std::string& content, int fd) { |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 82 | const char* p = content.data(); |
| 83 | size_t left = content.size(); |
| 84 | while (left > 0) { |
| 85 | ssize_t n = TEMP_FAILURE_RETRY(write(fd, p, left)); |
| 86 | if (n == -1) { |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 87 | return false; |
| 88 | } |
| 89 | p += n; |
| 90 | left -= n; |
| 91 | } |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 92 | return true; |
| 93 | } |
Elliott Hughes | 202f024 | 2015-02-04 13:19:13 -0800 | [diff] [blame] | 94 | |
| 95 | static bool CleanUpAfterFailedWrite(const std::string& path) { |
| 96 | // Something went wrong. Let's not leave a corrupt file lying around. |
| 97 | int saved_errno = errno; |
| 98 | unlink(path.c_str()); |
| 99 | errno = saved_errno; |
| 100 | return false; |
| 101 | } |
| 102 | |
Elliott Hughes | 31fa09c | 2015-02-04 19:38:28 -0800 | [diff] [blame] | 103 | #if !defined(_WIN32) |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 104 | bool WriteStringToFile(const std::string& content, const std::string& path, |
Josh Gao | ffabc96 | 2016-09-14 16:11:45 -0700 | [diff] [blame] | 105 | mode_t mode, uid_t owner, gid_t group, |
| 106 | bool follow_symlinks) { |
| 107 | int flags = O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY | |
| 108 | (follow_symlinks ? 0 : O_NOFOLLOW); |
Christopher Ferris | 48d08c2 | 2017-04-05 12:09:17 -0700 | [diff] [blame] | 109 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), flags, mode))); |
Elliott Hughes | 202f024 | 2015-02-04 13:19:13 -0800 | [diff] [blame] | 110 | if (fd == -1) { |
Elliott Hughes | e5dd71a | 2016-07-28 15:15:28 -0700 | [diff] [blame] | 111 | PLOG(ERROR) << "android::WriteStringToFile open failed"; |
Elliott Hughes | 202f024 | 2015-02-04 13:19:13 -0800 | [diff] [blame] | 112 | return false; |
| 113 | } |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 114 | |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 115 | // We do an explicit fchmod here because we assume that the caller really |
| 116 | // meant what they said and doesn't want the umask-influenced mode. |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 117 | if (fchmod(fd, mode) == -1) { |
Elliott Hughes | e5dd71a | 2016-07-28 15:15:28 -0700 | [diff] [blame] | 118 | PLOG(ERROR) << "android::WriteStringToFile fchmod failed"; |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 119 | return CleanUpAfterFailedWrite(path); |
| 120 | } |
| 121 | if (fchown(fd, owner, group) == -1) { |
Elliott Hughes | e5dd71a | 2016-07-28 15:15:28 -0700 | [diff] [blame] | 122 | PLOG(ERROR) << "android::WriteStringToFile fchown failed"; |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 123 | return CleanUpAfterFailedWrite(path); |
| 124 | } |
| 125 | if (!WriteStringToFd(content, fd)) { |
Elliott Hughes | e5dd71a | 2016-07-28 15:15:28 -0700 | [diff] [blame] | 126 | PLOG(ERROR) << "android::WriteStringToFile write failed"; |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 127 | return CleanUpAfterFailedWrite(path); |
| 128 | } |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 129 | return true; |
Elliott Hughes | 202f024 | 2015-02-04 13:19:13 -0800 | [diff] [blame] | 130 | } |
Elliott Hughes | 31fa09c | 2015-02-04 19:38:28 -0800 | [diff] [blame] | 131 | #endif |
Elliott Hughes | 202f024 | 2015-02-04 13:19:13 -0800 | [diff] [blame] | 132 | |
Josh Gao | ffabc96 | 2016-09-14 16:11:45 -0700 | [diff] [blame] | 133 | bool WriteStringToFile(const std::string& content, const std::string& path, |
| 134 | bool follow_symlinks) { |
| 135 | int flags = O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY | |
| 136 | (follow_symlinks ? 0 : O_NOFOLLOW); |
Elliott Hughes | 282ec45 | 2017-05-15 17:31:15 -0700 | [diff] [blame] | 137 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), flags, 0666))); |
Elliott Hughes | 202f024 | 2015-02-04 13:19:13 -0800 | [diff] [blame] | 138 | if (fd == -1) { |
| 139 | return false; |
| 140 | } |
Christopher Ferris | 48d08c2 | 2017-04-05 12:09:17 -0700 | [diff] [blame] | 141 | return WriteStringToFd(content, fd) || CleanUpAfterFailedWrite(path); |
Elliott Hughes | 202f024 | 2015-02-04 13:19:13 -0800 | [diff] [blame] | 142 | } |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 143 | |
Elliott Hughes | 56085ed | 2015-04-24 21:57:16 -0700 | [diff] [blame] | 144 | bool ReadFully(int fd, void* data, size_t byte_count) { |
| 145 | uint8_t* p = reinterpret_cast<uint8_t*>(data); |
| 146 | size_t remaining = byte_count; |
| 147 | while (remaining > 0) { |
| 148 | ssize_t n = TEMP_FAILURE_RETRY(read(fd, p, remaining)); |
| 149 | if (n <= 0) return false; |
| 150 | p += n; |
| 151 | remaining -= n; |
| 152 | } |
| 153 | return true; |
| 154 | } |
| 155 | |
Adam Lesinski | de117e4 | 2017-06-19 10:27:38 -0700 | [diff] [blame] | 156 | #if defined(_WIN32) |
| 157 | // Windows implementation of pread. Note that this DOES move the file descriptors read position, |
| 158 | // but it does so atomically. |
| 159 | static ssize_t pread(int fd, void* data, size_t byte_count, off64_t offset) { |
| 160 | DWORD bytes_read; |
| 161 | OVERLAPPED overlapped; |
| 162 | memset(&overlapped, 0, sizeof(OVERLAPPED)); |
| 163 | overlapped.Offset = static_cast<DWORD>(offset); |
| 164 | overlapped.OffsetHigh = static_cast<DWORD>(offset >> 32); |
| 165 | if (!ReadFile(reinterpret_cast<HANDLE>(_get_osfhandle(fd)), data, static_cast<DWORD>(byte_count), |
| 166 | &bytes_read, &overlapped)) { |
| 167 | // In case someone tries to read errno (since this is masquerading as a POSIX call) |
| 168 | errno = EIO; |
| 169 | return -1; |
| 170 | } |
| 171 | return static_cast<ssize_t>(bytes_read); |
| 172 | } |
| 173 | #endif |
| 174 | |
| 175 | bool ReadFullyAtOffset(int fd, void* data, size_t byte_count, off64_t offset) { |
| 176 | uint8_t* p = reinterpret_cast<uint8_t*>(data); |
| 177 | while (byte_count > 0) { |
| 178 | ssize_t n = TEMP_FAILURE_RETRY(pread(fd, p, byte_count, offset)); |
| 179 | if (n <= 0) return false; |
| 180 | p += n; |
| 181 | byte_count -= n; |
| 182 | offset += n; |
| 183 | } |
| 184 | return true; |
| 185 | } |
| 186 | |
Elliott Hughes | 56085ed | 2015-04-24 21:57:16 -0700 | [diff] [blame] | 187 | bool WriteFully(int fd, const void* data, size_t byte_count) { |
| 188 | const uint8_t* p = reinterpret_cast<const uint8_t*>(data); |
| 189 | size_t remaining = byte_count; |
| 190 | while (remaining > 0) { |
| 191 | ssize_t n = TEMP_FAILURE_RETRY(write(fd, p, remaining)); |
| 192 | if (n == -1) return false; |
| 193 | p += n; |
| 194 | remaining -= n; |
| 195 | } |
| 196 | return true; |
| 197 | } |
| 198 | |
Yabin Cui | b6e314a | 2016-01-29 17:25:54 -0800 | [diff] [blame] | 199 | bool RemoveFileIfExists(const std::string& path, std::string* err) { |
| 200 | struct stat st; |
| 201 | #if defined(_WIN32) |
| 202 | //TODO: Windows version can't handle symbol link correctly. |
| 203 | int result = stat(path.c_str(), &st); |
| 204 | bool file_type_removable = (result == 0 && S_ISREG(st.st_mode)); |
| 205 | #else |
| 206 | int result = lstat(path.c_str(), &st); |
| 207 | bool file_type_removable = (result == 0 && (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))); |
| 208 | #endif |
| 209 | if (result == 0) { |
| 210 | if (!file_type_removable) { |
| 211 | if (err != nullptr) { |
| 212 | *err = "is not a regular or symbol link file"; |
| 213 | } |
| 214 | return false; |
| 215 | } |
| 216 | if (unlink(path.c_str()) == -1) { |
| 217 | if (err != nullptr) { |
| 218 | *err = strerror(errno); |
| 219 | } |
| 220 | return false; |
| 221 | } |
| 222 | } |
| 223 | return true; |
| 224 | } |
| 225 | |
Elliott Hughes | d3ff6e5 | 2016-08-23 15:53:45 -0700 | [diff] [blame] | 226 | #if !defined(_WIN32) |
| 227 | bool Readlink(const std::string& path, std::string* result) { |
| 228 | result->clear(); |
| 229 | |
| 230 | // Most Linux file systems (ext2 and ext4, say) limit symbolic links to |
| 231 | // 4095 bytes. Since we'll copy out into the string anyway, it doesn't |
| 232 | // waste memory to just start there. We add 1 so that we can recognize |
| 233 | // whether it actually fit (rather than being truncated to 4095). |
| 234 | std::vector<char> buf(4095 + 1); |
| 235 | while (true) { |
| 236 | ssize_t size = readlink(path.c_str(), &buf[0], buf.size()); |
| 237 | // Unrecoverable error? |
| 238 | if (size == -1) return false; |
| 239 | // It fit! (If size == buf.size(), it may have been truncated.) |
| 240 | if (static_cast<size_t>(size) < buf.size()) { |
| 241 | result->assign(&buf[0], size); |
| 242 | return true; |
| 243 | } |
| 244 | // Double our buffer and try again. |
| 245 | buf.resize(buf.size() * 2); |
| 246 | } |
| 247 | } |
| 248 | #endif |
| 249 | |
Dimitry Ivanov | 840b601 | 2016-09-09 10:49:21 -0700 | [diff] [blame] | 250 | #if !defined(_WIN32) |
| 251 | bool Realpath(const std::string& path, std::string* result) { |
| 252 | result->clear(); |
| 253 | |
| 254 | char* realpath_buf = realpath(path.c_str(), nullptr); |
| 255 | if (realpath_buf == nullptr) { |
| 256 | return false; |
| 257 | } |
| 258 | result->assign(realpath_buf); |
| 259 | free(realpath_buf); |
| 260 | return true; |
| 261 | } |
| 262 | #endif |
| 263 | |
Elliott Hughes | 82ff315 | 2016-08-31 15:07:18 -0700 | [diff] [blame] | 264 | std::string GetExecutablePath() { |
| 265 | #if defined(__linux__) |
| 266 | std::string path; |
| 267 | android::base::Readlink("/proc/self/exe", &path); |
| 268 | return path; |
| 269 | #elif defined(__APPLE__) |
Elliott Hughes | 82ff315 | 2016-08-31 15:07:18 -0700 | [diff] [blame] | 270 | char path[PATH_MAX + 1]; |
Josh Gao | 7307f09 | 2016-09-01 12:31:42 -0700 | [diff] [blame] | 271 | uint32_t path_len = sizeof(path); |
| 272 | int rc = _NSGetExecutablePath(path, &path_len); |
| 273 | if (rc < 0) { |
| 274 | std::unique_ptr<char> path_buf(new char[path_len]); |
| 275 | _NSGetExecutablePath(path_buf.get(), &path_len); |
| 276 | return path_buf.get(); |
| 277 | } |
Elliott Hughes | 82ff315 | 2016-08-31 15:07:18 -0700 | [diff] [blame] | 278 | return path; |
| 279 | #elif defined(_WIN32) |
| 280 | char path[PATH_MAX + 1]; |
| 281 | DWORD result = GetModuleFileName(NULL, path, sizeof(path) - 1); |
| 282 | if (result == 0 || result == sizeof(path) - 1) return ""; |
| 283 | path[PATH_MAX - 1] = 0; |
| 284 | return path; |
| 285 | #else |
| 286 | #error unknown OS |
| 287 | #endif |
| 288 | } |
| 289 | |
Colin Cross | bb3a515 | 2017-02-23 17:41:56 -0800 | [diff] [blame] | 290 | std::string GetExecutableDirectory() { |
| 291 | return Dirname(GetExecutablePath()); |
| 292 | } |
Colin Cross | 58021d1 | 2017-02-23 21:23:05 -0800 | [diff] [blame] | 293 | |
Colin Cross | bb3a515 | 2017-02-23 17:41:56 -0800 | [diff] [blame] | 294 | std::string Basename(const std::string& path) { |
Colin Cross | 58021d1 | 2017-02-23 21:23:05 -0800 | [diff] [blame] | 295 | // Copy path because basename may modify the string passed in. |
| 296 | std::string result(path); |
| 297 | |
| 298 | #if !defined(__BIONIC__) |
| 299 | // Use lock because basename() may write to a process global and return a |
| 300 | // pointer to that. Note that this locking strategy only works if all other |
| 301 | // callers to basename in the process also grab this same lock, but its |
| 302 | // better than nothing. Bionic's basename returns a thread-local buffer. |
| 303 | static std::mutex& basename_lock = *new std::mutex(); |
| 304 | std::lock_guard<std::mutex> lock(basename_lock); |
| 305 | #endif |
| 306 | |
| 307 | // Note that if std::string uses copy-on-write strings, &str[0] will cause |
| 308 | // the copy to be made, so there is no chance of us accidentally writing to |
| 309 | // the storage for 'path'. |
| 310 | char* name = basename(&result[0]); |
| 311 | |
| 312 | // In case basename returned a pointer to a process global, copy that string |
| 313 | // before leaving the lock. |
| 314 | result.assign(name); |
| 315 | |
| 316 | return result; |
| 317 | } |
| 318 | |
| 319 | std::string Dirname(const std::string& path) { |
| 320 | // Copy path because dirname may modify the string passed in. |
| 321 | std::string result(path); |
| 322 | |
| 323 | #if !defined(__BIONIC__) |
| 324 | // Use lock because dirname() may write to a process global and return a |
| 325 | // pointer to that. Note that this locking strategy only works if all other |
| 326 | // callers to dirname in the process also grab this same lock, but its |
| 327 | // better than nothing. Bionic's dirname returns a thread-local buffer. |
| 328 | static std::mutex& dirname_lock = *new std::mutex(); |
| 329 | std::lock_guard<std::mutex> lock(dirname_lock); |
| 330 | #endif |
| 331 | |
| 332 | // Note that if std::string uses copy-on-write strings, &str[0] will cause |
| 333 | // the copy to be made, so there is no chance of us accidentally writing to |
| 334 | // the storage for 'path'. |
| 335 | char* parent = dirname(&result[0]); |
| 336 | |
| 337 | // In case dirname returned a pointer to a process global, copy that string |
| 338 | // before leaving the lock. |
| 339 | result.assign(parent); |
| 340 | |
| 341 | return result; |
| 342 | } |
| 343 | |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 344 | } // namespace base |
| 345 | } // namespace android |