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