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> |
Mark Salyzyn | 0790b24 | 2018-11-12 12:29:14 -0800 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 24 | #include <sys/stat.h> |
| 25 | #include <sys/types.h> |
Elliott Hughes | d3ff6e5 | 2016-08-23 15:53:45 -0700 | [diff] [blame] | 26 | #include <unistd.h> |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 27 | |
Josh Gao | 7307f09 | 2016-09-01 12:31:42 -0700 | [diff] [blame] | 28 | #include <memory> |
Colin Cross | 58021d1 | 2017-02-23 21:23:05 -0800 | [diff] [blame] | 29 | #include <mutex> |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 30 | #include <string> |
Elliott Hughes | d3ff6e5 | 2016-08-23 15:53:45 -0700 | [diff] [blame] | 31 | #include <vector> |
Elliott Hughes | af4885a | 2015-02-03 13:02:57 -0800 | [diff] [blame] | 32 | |
Elliott Hughes | e5dd71a | 2016-07-28 15:15:28 -0700 | [diff] [blame] | 33 | #include "android-base/logging.h" |
Christopher Ferris | 48d08c2 | 2017-04-05 12:09:17 -0700 | [diff] [blame] | 34 | #include "android-base/macros.h" // For TEMP_FAILURE_RETRY on Darwin. |
| 35 | #include "android-base/unique_fd.h" |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 36 | #include "android-base/utf8.h" |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 37 | |
Elliott Hughes | 82ff315 | 2016-08-31 15:07:18 -0700 | [diff] [blame] | 38 | #if defined(__APPLE__) |
Josh Gao | 7307f09 | 2016-09-01 12:31:42 -0700 | [diff] [blame] | 39 | #include <mach-o/dyld.h> |
Elliott Hughes | 82ff315 | 2016-08-31 15:07:18 -0700 | [diff] [blame] | 40 | #endif |
| 41 | #if defined(_WIN32) |
Mark Salyzyn | 0790b24 | 2018-11-12 12:29:14 -0800 | [diff] [blame] | 42 | #include <direct.h> |
Elliott Hughes | 82ff315 | 2016-08-31 15:07:18 -0700 | [diff] [blame] | 43 | #include <windows.h> |
Elliott Hughes | 282ec45 | 2017-05-15 17:31:15 -0700 | [diff] [blame] | 44 | #define O_NOFOLLOW 0 |
Mark Salyzyn | 0790b24 | 2018-11-12 12:29:14 -0800 | [diff] [blame] | 45 | #define OS_PATH_SEPARATOR '\\' |
| 46 | #else |
| 47 | #define OS_PATH_SEPARATOR '/' |
Elliott Hughes | 82ff315 | 2016-08-31 15:07:18 -0700 | [diff] [blame] | 48 | #endif |
| 49 | |
Mark Salyzyn | 0790b24 | 2018-11-12 12:29:14 -0800 | [diff] [blame] | 50 | #ifdef _WIN32 |
| 51 | int mkstemp(char* template_name) { |
| 52 | if (_mktemp(template_name) == nullptr) { |
| 53 | return -1; |
| 54 | } |
| 55 | // Use open() to match the close() that TemporaryFile's destructor does. |
| 56 | // Use O_BINARY to match base file APIs. |
| 57 | return open(template_name, O_CREAT | O_EXCL | O_RDWR | O_BINARY, S_IRUSR | S_IWUSR); |
| 58 | } |
| 59 | |
| 60 | char* mkdtemp(char* template_name) { |
| 61 | if (_mktemp(template_name) == nullptr) { |
| 62 | return nullptr; |
| 63 | } |
| 64 | if (_mkdir(template_name) == -1) { |
| 65 | return nullptr; |
| 66 | } |
| 67 | return template_name; |
| 68 | } |
| 69 | #endif |
| 70 | |
| 71 | namespace { |
| 72 | |
| 73 | std::string GetSystemTempDir() { |
| 74 | #if defined(__ANDROID__) |
Mark Salyzyn | 6009a2d | 2018-11-13 15:34:38 -0800 | [diff] [blame^] | 75 | const auto* tmpdir = getenv("TMPDIR"); |
| 76 | if (tmpdir == nullptr) tmpdir = "/data/local/tmp"; |
Mark Salyzyn | 0790b24 | 2018-11-12 12:29:14 -0800 | [diff] [blame] | 77 | if (access(tmpdir, R_OK | W_OK | X_OK) == 0) { |
| 78 | return tmpdir; |
| 79 | } |
| 80 | // Tests running in app context can't access /data/local/tmp, |
| 81 | // so try current directory if /data/local/tmp is not accessible. |
| 82 | return "."; |
| 83 | #elif defined(_WIN32) |
| 84 | char tmp_dir[MAX_PATH]; |
Mark Salyzyn | 6009a2d | 2018-11-13 15:34:38 -0800 | [diff] [blame^] | 85 | DWORD result = GetTempPathA(sizeof(tmp_dir), tmp_dir); // checks TMP env |
Mark Salyzyn | 0790b24 | 2018-11-12 12:29:14 -0800 | [diff] [blame] | 86 | CHECK_NE(result, 0ul) << "GetTempPathA failed, error: " << GetLastError(); |
| 87 | CHECK_LT(result, sizeof(tmp_dir)) << "path truncated to: " << result; |
| 88 | |
| 89 | // GetTempPath() returns a path with a trailing slash, but init() |
| 90 | // does not expect that, so remove it. |
| 91 | CHECK_EQ(tmp_dir[result - 1], '\\'); |
| 92 | tmp_dir[result - 1] = '\0'; |
| 93 | return tmp_dir; |
| 94 | #else |
Mark Salyzyn | 6009a2d | 2018-11-13 15:34:38 -0800 | [diff] [blame^] | 95 | const auto* tmpdir = getenv("TMPDIR"); |
| 96 | if (tmpdir == nullptr) tmpdir = "/tmp"; |
| 97 | return tmpdir; |
Mark Salyzyn | 0790b24 | 2018-11-12 12:29:14 -0800 | [diff] [blame] | 98 | #endif |
| 99 | } |
| 100 | |
| 101 | } // namespace |
| 102 | |
| 103 | TemporaryFile::TemporaryFile() { |
| 104 | init(GetSystemTempDir()); |
| 105 | } |
| 106 | |
| 107 | TemporaryFile::TemporaryFile(const std::string& tmp_dir) { |
| 108 | init(tmp_dir); |
| 109 | } |
| 110 | |
| 111 | TemporaryFile::~TemporaryFile() { |
| 112 | if (fd != -1) { |
| 113 | close(fd); |
| 114 | } |
| 115 | if (remove_file_) { |
| 116 | unlink(path); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | int TemporaryFile::release() { |
| 121 | int result = fd; |
| 122 | fd = -1; |
| 123 | return result; |
| 124 | } |
| 125 | |
| 126 | void TemporaryFile::init(const std::string& tmp_dir) { |
| 127 | snprintf(path, sizeof(path), "%s%cTemporaryFile-XXXXXX", tmp_dir.c_str(), OS_PATH_SEPARATOR); |
| 128 | fd = mkstemp(path); |
| 129 | } |
| 130 | |
| 131 | TemporaryDir::TemporaryDir() { |
| 132 | init(GetSystemTempDir()); |
| 133 | } |
| 134 | |
| 135 | TemporaryDir::~TemporaryDir() { |
| 136 | rmdir(path); |
| 137 | } |
| 138 | |
| 139 | bool TemporaryDir::init(const std::string& tmp_dir) { |
| 140 | snprintf(path, sizeof(path), "%s%cTemporaryDir-XXXXXX", tmp_dir.c_str(), OS_PATH_SEPARATOR); |
| 141 | return (mkdtemp(path) != nullptr); |
| 142 | } |
| 143 | |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 144 | namespace android { |
| 145 | namespace base { |
| 146 | |
Elliott Hughes | c1fd492 | 2015-11-11 18:02:29 +0000 | [diff] [blame] | 147 | // Versions of standard library APIs that support UTF-8 strings. |
| 148 | using namespace android::base::utf8; |
| 149 | |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 150 | bool ReadFdToString(int fd, std::string* content) { |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 151 | content->clear(); |
| 152 | |
Elliott Hughes | 9bb7971 | 2017-03-20 19:16:18 -0700 | [diff] [blame] | 153 | // Although original we had small files in mind, this code gets used for |
| 154 | // very large files too, where the std::string growth heuristics might not |
| 155 | // be suitable. https://code.google.com/p/android/issues/detail?id=258500. |
| 156 | struct stat sb; |
| 157 | if (fstat(fd, &sb) != -1 && sb.st_size > 0) { |
| 158 | content->reserve(sb.st_size); |
| 159 | } |
| 160 | |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 161 | char buf[BUFSIZ]; |
| 162 | ssize_t n; |
| 163 | while ((n = TEMP_FAILURE_RETRY(read(fd, &buf[0], sizeof(buf)))) > 0) { |
| 164 | content->append(buf, n); |
| 165 | } |
| 166 | return (n == 0) ? true : false; |
| 167 | } |
| 168 | |
Josh Gao | ffabc96 | 2016-09-14 16:11:45 -0700 | [diff] [blame] | 169 | bool ReadFileToString(const std::string& path, std::string* content, bool follow_symlinks) { |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 170 | content->clear(); |
| 171 | |
Josh Gao | ffabc96 | 2016-09-14 16:11:45 -0700 | [diff] [blame] | 172 | 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] | 173 | 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] | 174 | if (fd == -1) { |
| 175 | return false; |
| 176 | } |
Christopher Ferris | 48d08c2 | 2017-04-05 12:09:17 -0700 | [diff] [blame] | 177 | return ReadFdToString(fd, content); |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 180 | bool WriteStringToFd(const std::string& content, int fd) { |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 181 | const char* p = content.data(); |
| 182 | size_t left = content.size(); |
| 183 | while (left > 0) { |
| 184 | ssize_t n = TEMP_FAILURE_RETRY(write(fd, p, left)); |
| 185 | if (n == -1) { |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 186 | return false; |
| 187 | } |
| 188 | p += n; |
| 189 | left -= n; |
| 190 | } |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 191 | return true; |
| 192 | } |
Elliott Hughes | 202f024 | 2015-02-04 13:19:13 -0800 | [diff] [blame] | 193 | |
| 194 | static bool CleanUpAfterFailedWrite(const std::string& path) { |
| 195 | // Something went wrong. Let's not leave a corrupt file lying around. |
| 196 | int saved_errno = errno; |
| 197 | unlink(path.c_str()); |
| 198 | errno = saved_errno; |
| 199 | return false; |
| 200 | } |
| 201 | |
Elliott Hughes | 31fa09c | 2015-02-04 19:38:28 -0800 | [diff] [blame] | 202 | #if !defined(_WIN32) |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 203 | bool WriteStringToFile(const std::string& content, const std::string& path, |
Josh Gao | ffabc96 | 2016-09-14 16:11:45 -0700 | [diff] [blame] | 204 | mode_t mode, uid_t owner, gid_t group, |
| 205 | bool follow_symlinks) { |
| 206 | int flags = O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY | |
| 207 | (follow_symlinks ? 0 : O_NOFOLLOW); |
Christopher Ferris | 48d08c2 | 2017-04-05 12:09:17 -0700 | [diff] [blame] | 208 | 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] | 209 | if (fd == -1) { |
Elliott Hughes | e5dd71a | 2016-07-28 15:15:28 -0700 | [diff] [blame] | 210 | PLOG(ERROR) << "android::WriteStringToFile open failed"; |
Elliott Hughes | 202f024 | 2015-02-04 13:19:13 -0800 | [diff] [blame] | 211 | return false; |
| 212 | } |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 213 | |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 214 | // We do an explicit fchmod here because we assume that the caller really |
| 215 | // meant what they said and doesn't want the umask-influenced mode. |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 216 | if (fchmod(fd, mode) == -1) { |
Elliott Hughes | e5dd71a | 2016-07-28 15:15:28 -0700 | [diff] [blame] | 217 | PLOG(ERROR) << "android::WriteStringToFile fchmod failed"; |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 218 | return CleanUpAfterFailedWrite(path); |
| 219 | } |
| 220 | if (fchown(fd, owner, group) == -1) { |
Elliott Hughes | e5dd71a | 2016-07-28 15:15:28 -0700 | [diff] [blame] | 221 | PLOG(ERROR) << "android::WriteStringToFile fchown failed"; |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 222 | return CleanUpAfterFailedWrite(path); |
| 223 | } |
| 224 | if (!WriteStringToFd(content, fd)) { |
Elliott Hughes | e5dd71a | 2016-07-28 15:15:28 -0700 | [diff] [blame] | 225 | PLOG(ERROR) << "android::WriteStringToFile write failed"; |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 226 | return CleanUpAfterFailedWrite(path); |
| 227 | } |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 228 | return true; |
Elliott Hughes | 202f024 | 2015-02-04 13:19:13 -0800 | [diff] [blame] | 229 | } |
Elliott Hughes | 31fa09c | 2015-02-04 19:38:28 -0800 | [diff] [blame] | 230 | #endif |
Elliott Hughes | 202f024 | 2015-02-04 13:19:13 -0800 | [diff] [blame] | 231 | |
Josh Gao | ffabc96 | 2016-09-14 16:11:45 -0700 | [diff] [blame] | 232 | bool WriteStringToFile(const std::string& content, const std::string& path, |
| 233 | bool follow_symlinks) { |
| 234 | int flags = O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY | |
| 235 | (follow_symlinks ? 0 : O_NOFOLLOW); |
Elliott Hughes | 282ec45 | 2017-05-15 17:31:15 -0700 | [diff] [blame] | 236 | 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] | 237 | if (fd == -1) { |
| 238 | return false; |
| 239 | } |
Christopher Ferris | 48d08c2 | 2017-04-05 12:09:17 -0700 | [diff] [blame] | 240 | return WriteStringToFd(content, fd) || CleanUpAfterFailedWrite(path); |
Elliott Hughes | 202f024 | 2015-02-04 13:19:13 -0800 | [diff] [blame] | 241 | } |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 242 | |
Elliott Hughes | 56085ed | 2015-04-24 21:57:16 -0700 | [diff] [blame] | 243 | bool ReadFully(int fd, void* data, size_t byte_count) { |
| 244 | uint8_t* p = reinterpret_cast<uint8_t*>(data); |
| 245 | size_t remaining = byte_count; |
| 246 | while (remaining > 0) { |
| 247 | ssize_t n = TEMP_FAILURE_RETRY(read(fd, p, remaining)); |
| 248 | if (n <= 0) return false; |
| 249 | p += n; |
| 250 | remaining -= n; |
| 251 | } |
| 252 | return true; |
| 253 | } |
| 254 | |
Adam Lesinski | de117e4 | 2017-06-19 10:27:38 -0700 | [diff] [blame] | 255 | #if defined(_WIN32) |
| 256 | // Windows implementation of pread. Note that this DOES move the file descriptors read position, |
| 257 | // but it does so atomically. |
| 258 | static ssize_t pread(int fd, void* data, size_t byte_count, off64_t offset) { |
| 259 | DWORD bytes_read; |
| 260 | OVERLAPPED overlapped; |
| 261 | memset(&overlapped, 0, sizeof(OVERLAPPED)); |
| 262 | overlapped.Offset = static_cast<DWORD>(offset); |
| 263 | overlapped.OffsetHigh = static_cast<DWORD>(offset >> 32); |
| 264 | if (!ReadFile(reinterpret_cast<HANDLE>(_get_osfhandle(fd)), data, static_cast<DWORD>(byte_count), |
| 265 | &bytes_read, &overlapped)) { |
| 266 | // In case someone tries to read errno (since this is masquerading as a POSIX call) |
| 267 | errno = EIO; |
| 268 | return -1; |
| 269 | } |
| 270 | return static_cast<ssize_t>(bytes_read); |
| 271 | } |
| 272 | #endif |
| 273 | |
| 274 | bool ReadFullyAtOffset(int fd, void* data, size_t byte_count, off64_t offset) { |
| 275 | uint8_t* p = reinterpret_cast<uint8_t*>(data); |
| 276 | while (byte_count > 0) { |
| 277 | ssize_t n = TEMP_FAILURE_RETRY(pread(fd, p, byte_count, offset)); |
| 278 | if (n <= 0) return false; |
| 279 | p += n; |
| 280 | byte_count -= n; |
| 281 | offset += n; |
| 282 | } |
| 283 | return true; |
| 284 | } |
| 285 | |
Elliott Hughes | 56085ed | 2015-04-24 21:57:16 -0700 | [diff] [blame] | 286 | bool WriteFully(int fd, const void* data, size_t byte_count) { |
| 287 | const uint8_t* p = reinterpret_cast<const uint8_t*>(data); |
| 288 | size_t remaining = byte_count; |
| 289 | while (remaining > 0) { |
| 290 | ssize_t n = TEMP_FAILURE_RETRY(write(fd, p, remaining)); |
| 291 | if (n == -1) return false; |
| 292 | p += n; |
| 293 | remaining -= n; |
| 294 | } |
| 295 | return true; |
| 296 | } |
| 297 | |
Yabin Cui | b6e314a | 2016-01-29 17:25:54 -0800 | [diff] [blame] | 298 | bool RemoveFileIfExists(const std::string& path, std::string* err) { |
| 299 | struct stat st; |
| 300 | #if defined(_WIN32) |
liwugang | c63cb07 | 2018-07-11 13:24:49 +0800 | [diff] [blame] | 301 | // TODO: Windows version can't handle symbolic links correctly. |
Yabin Cui | b6e314a | 2016-01-29 17:25:54 -0800 | [diff] [blame] | 302 | int result = stat(path.c_str(), &st); |
| 303 | bool file_type_removable = (result == 0 && S_ISREG(st.st_mode)); |
| 304 | #else |
| 305 | int result = lstat(path.c_str(), &st); |
| 306 | bool file_type_removable = (result == 0 && (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))); |
| 307 | #endif |
liwugang | c63cb07 | 2018-07-11 13:24:49 +0800 | [diff] [blame] | 308 | if (result == -1) { |
| 309 | if (errno == ENOENT || errno == ENOTDIR) return true; |
| 310 | if (err != nullptr) *err = strerror(errno); |
| 311 | return false; |
| 312 | } |
| 313 | |
Yabin Cui | b6e314a | 2016-01-29 17:25:54 -0800 | [diff] [blame] | 314 | if (result == 0) { |
| 315 | if (!file_type_removable) { |
| 316 | if (err != nullptr) { |
liwugang | c63cb07 | 2018-07-11 13:24:49 +0800 | [diff] [blame] | 317 | *err = "is not a regular file or symbolic link"; |
Yabin Cui | b6e314a | 2016-01-29 17:25:54 -0800 | [diff] [blame] | 318 | } |
| 319 | return false; |
| 320 | } |
| 321 | if (unlink(path.c_str()) == -1) { |
| 322 | if (err != nullptr) { |
| 323 | *err = strerror(errno); |
| 324 | } |
| 325 | return false; |
| 326 | } |
| 327 | } |
| 328 | return true; |
| 329 | } |
| 330 | |
Elliott Hughes | d3ff6e5 | 2016-08-23 15:53:45 -0700 | [diff] [blame] | 331 | #if !defined(_WIN32) |
| 332 | bool Readlink(const std::string& path, std::string* result) { |
| 333 | result->clear(); |
| 334 | |
| 335 | // Most Linux file systems (ext2 and ext4, say) limit symbolic links to |
| 336 | // 4095 bytes. Since we'll copy out into the string anyway, it doesn't |
| 337 | // waste memory to just start there. We add 1 so that we can recognize |
| 338 | // whether it actually fit (rather than being truncated to 4095). |
| 339 | std::vector<char> buf(4095 + 1); |
| 340 | while (true) { |
| 341 | ssize_t size = readlink(path.c_str(), &buf[0], buf.size()); |
| 342 | // Unrecoverable error? |
| 343 | if (size == -1) return false; |
| 344 | // It fit! (If size == buf.size(), it may have been truncated.) |
| 345 | if (static_cast<size_t>(size) < buf.size()) { |
| 346 | result->assign(&buf[0], size); |
| 347 | return true; |
| 348 | } |
| 349 | // Double our buffer and try again. |
| 350 | buf.resize(buf.size() * 2); |
| 351 | } |
| 352 | } |
| 353 | #endif |
| 354 | |
Dimitry Ivanov | 840b601 | 2016-09-09 10:49:21 -0700 | [diff] [blame] | 355 | #if !defined(_WIN32) |
| 356 | bool Realpath(const std::string& path, std::string* result) { |
| 357 | result->clear(); |
| 358 | |
| 359 | char* realpath_buf = realpath(path.c_str(), nullptr); |
| 360 | if (realpath_buf == nullptr) { |
| 361 | return false; |
| 362 | } |
| 363 | result->assign(realpath_buf); |
| 364 | free(realpath_buf); |
| 365 | return true; |
| 366 | } |
| 367 | #endif |
| 368 | |
Elliott Hughes | 82ff315 | 2016-08-31 15:07:18 -0700 | [diff] [blame] | 369 | std::string GetExecutablePath() { |
| 370 | #if defined(__linux__) |
| 371 | std::string path; |
| 372 | android::base::Readlink("/proc/self/exe", &path); |
| 373 | return path; |
| 374 | #elif defined(__APPLE__) |
Elliott Hughes | 82ff315 | 2016-08-31 15:07:18 -0700 | [diff] [blame] | 375 | char path[PATH_MAX + 1]; |
Josh Gao | 7307f09 | 2016-09-01 12:31:42 -0700 | [diff] [blame] | 376 | uint32_t path_len = sizeof(path); |
| 377 | int rc = _NSGetExecutablePath(path, &path_len); |
| 378 | if (rc < 0) { |
| 379 | std::unique_ptr<char> path_buf(new char[path_len]); |
| 380 | _NSGetExecutablePath(path_buf.get(), &path_len); |
| 381 | return path_buf.get(); |
| 382 | } |
Elliott Hughes | 82ff315 | 2016-08-31 15:07:18 -0700 | [diff] [blame] | 383 | return path; |
| 384 | #elif defined(_WIN32) |
| 385 | char path[PATH_MAX + 1]; |
| 386 | DWORD result = GetModuleFileName(NULL, path, sizeof(path) - 1); |
| 387 | if (result == 0 || result == sizeof(path) - 1) return ""; |
| 388 | path[PATH_MAX - 1] = 0; |
| 389 | return path; |
| 390 | #else |
| 391 | #error unknown OS |
| 392 | #endif |
| 393 | } |
| 394 | |
Colin Cross | bb3a515 | 2017-02-23 17:41:56 -0800 | [diff] [blame] | 395 | std::string GetExecutableDirectory() { |
| 396 | return Dirname(GetExecutablePath()); |
| 397 | } |
Colin Cross | 58021d1 | 2017-02-23 21:23:05 -0800 | [diff] [blame] | 398 | |
Colin Cross | bb3a515 | 2017-02-23 17:41:56 -0800 | [diff] [blame] | 399 | std::string Basename(const std::string& path) { |
Colin Cross | 58021d1 | 2017-02-23 21:23:05 -0800 | [diff] [blame] | 400 | // Copy path because basename may modify the string passed in. |
| 401 | std::string result(path); |
| 402 | |
| 403 | #if !defined(__BIONIC__) |
| 404 | // Use lock because basename() may write to a process global and return a |
| 405 | // pointer to that. Note that this locking strategy only works if all other |
| 406 | // callers to basename in the process also grab this same lock, but its |
| 407 | // better than nothing. Bionic's basename returns a thread-local buffer. |
| 408 | static std::mutex& basename_lock = *new std::mutex(); |
| 409 | std::lock_guard<std::mutex> lock(basename_lock); |
| 410 | #endif |
| 411 | |
| 412 | // Note that if std::string uses copy-on-write strings, &str[0] will cause |
| 413 | // the copy to be made, so there is no chance of us accidentally writing to |
| 414 | // the storage for 'path'. |
| 415 | char* name = basename(&result[0]); |
| 416 | |
| 417 | // In case basename returned a pointer to a process global, copy that string |
| 418 | // before leaving the lock. |
| 419 | result.assign(name); |
| 420 | |
| 421 | return result; |
| 422 | } |
| 423 | |
| 424 | std::string Dirname(const std::string& path) { |
| 425 | // Copy path because dirname may modify the string passed in. |
| 426 | std::string result(path); |
| 427 | |
| 428 | #if !defined(__BIONIC__) |
| 429 | // Use lock because dirname() may write to a process global and return a |
| 430 | // pointer to that. Note that this locking strategy only works if all other |
| 431 | // callers to dirname in the process also grab this same lock, but its |
| 432 | // better than nothing. Bionic's dirname returns a thread-local buffer. |
| 433 | static std::mutex& dirname_lock = *new std::mutex(); |
| 434 | std::lock_guard<std::mutex> lock(dirname_lock); |
| 435 | #endif |
| 436 | |
| 437 | // Note that if std::string uses copy-on-write strings, &str[0] will cause |
| 438 | // the copy to be made, so there is no chance of us accidentally writing to |
| 439 | // the storage for 'path'. |
| 440 | char* parent = dirname(&result[0]); |
| 441 | |
| 442 | // In case dirname returned a pointer to a process global, copy that string |
| 443 | // before leaving the lock. |
| 444 | result.assign(parent); |
| 445 | |
| 446 | return result; |
| 447 | } |
| 448 | |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 449 | } // namespace base |
| 450 | } // namespace android |