| Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [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 | b635162 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 17 | #include "android-base/file.h" |
| Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 18 | |
| 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
| Mark Salyzyn | f2e7615 | 2018-11-13 13:38:33 -0800 | [diff] [blame] | 21 | #include <ftw.h> |
| Colin Cross | 2e732e2 | 2017-02-23 21:23:05 -0800 | [diff] [blame] | 22 | #include <libgen.h> |
| Mark Salyzyn | 40a4cb4 | 2018-11-12 12:29:14 -0800 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| Florian Mayer | c08f3d4 | 2018-10-23 15:56:28 +0100 | [diff] [blame] | 25 | #include <string.h> |
| Elliott Hughes | 3e6e442 | 2022-05-26 16:37:25 -0700 | [diff] [blame] | 26 | #include <sys/param.h> |
| Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 27 | #include <sys/stat.h> |
| 28 | #include <sys/types.h> |
| Elliott Hughes | a634a9a | 2016-08-23 15:53:45 -0700 | [diff] [blame] | 29 | #include <unistd.h> |
| Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 30 | |
| Josh Gao | 58668ac | 2016-09-01 12:31:42 -0700 | [diff] [blame] | 31 | #include <memory> |
| Colin Cross | 2e732e2 | 2017-02-23 21:23:05 -0800 | [diff] [blame] | 32 | #include <mutex> |
| Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 33 | #include <string> |
| Elliott Hughes | a634a9a | 2016-08-23 15:53:45 -0700 | [diff] [blame] | 34 | #include <vector> |
| Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 35 | |
| Elliott Hughes | 48f0eb5 | 2016-08-31 15:07:18 -0700 | [diff] [blame] | 36 | #if defined(__APPLE__) |
| Josh Gao | 58668ac | 2016-09-01 12:31:42 -0700 | [diff] [blame] | 37 | #include <mach-o/dyld.h> |
| Elliott Hughes | 48f0eb5 | 2016-08-31 15:07:18 -0700 | [diff] [blame] | 38 | #endif |
| 39 | #if defined(_WIN32) |
| Mark Salyzyn | 40a4cb4 | 2018-11-12 12:29:14 -0800 | [diff] [blame] | 40 | #include <direct.h> |
| Elliott Hughes | 48f0eb5 | 2016-08-31 15:07:18 -0700 | [diff] [blame] | 41 | #include <windows.h> |
| Elliott Hughes | 62ecbaa | 2017-05-15 17:31:15 -0700 | [diff] [blame] | 42 | #define O_NOFOLLOW 0 |
| Mark Salyzyn | 40a4cb4 | 2018-11-12 12:29:14 -0800 | [diff] [blame] | 43 | #define OS_PATH_SEPARATOR '\\' |
| 44 | #else |
| 45 | #define OS_PATH_SEPARATOR '/' |
| Elliott Hughes | 48f0eb5 | 2016-08-31 15:07:18 -0700 | [diff] [blame] | 46 | #endif |
| 47 | |
| Mark Salyzyn | f2e7615 | 2018-11-13 13:38:33 -0800 | [diff] [blame] | 48 | #include "android-base/logging.h" // and must be after windows.h for ERROR |
| 49 | #include "android-base/macros.h" // For TEMP_FAILURE_RETRY on Darwin. |
| 50 | #include "android-base/unique_fd.h" |
| 51 | #include "android-base/utf8.h" |
| 52 | |
| Alex Buynytskyy | ab0407f | 2019-09-27 11:11:24 -0700 | [diff] [blame] | 53 | namespace { |
| 54 | |
| Mark Salyzyn | 40a4cb4 | 2018-11-12 12:29:14 -0800 | [diff] [blame] | 55 | #ifdef _WIN32 |
| Alex Buynytskyy | ab0407f | 2019-09-27 11:11:24 -0700 | [diff] [blame] | 56 | static int mkstemp(char* name_template, size_t size_in_chars) { |
| Alex Buynytskyy | 19ba088 | 2019-10-02 16:27:22 -0700 | [diff] [blame] | 57 | std::wstring path; |
| 58 | CHECK(android::base::UTF8ToWide(name_template, &path)) |
| 59 | << "path can't be converted to wchar: " << name_template; |
| 60 | if (_wmktemp_s(path.data(), path.size() + 1) != 0) { |
| Mark Salyzyn | 40a4cb4 | 2018-11-12 12:29:14 -0800 | [diff] [blame] | 61 | return -1; |
| 62 | } |
| Alex Buynytskyy | ab0407f | 2019-09-27 11:11:24 -0700 | [diff] [blame] | 63 | |
| Mark Salyzyn | 40a4cb4 | 2018-11-12 12:29:14 -0800 | [diff] [blame] | 64 | // Use open() to match the close() that TemporaryFile's destructor does. |
| 65 | // Use O_BINARY to match base file APIs. |
| Alex Buynytskyy | 19ba088 | 2019-10-02 16:27:22 -0700 | [diff] [blame] | 66 | int fd = _wopen(path.c_str(), O_CREAT | O_EXCL | O_RDWR | O_BINARY, S_IRUSR | S_IWUSR); |
| 67 | if (fd < 0) { |
| 68 | return -1; |
| 69 | } |
| 70 | |
| 71 | std::string path_utf8; |
| 72 | CHECK(android::base::WideToUTF8(path, &path_utf8)) << "path can't be converted to utf8"; |
| 73 | CHECK(strcpy_s(name_template, size_in_chars, path_utf8.c_str()) == 0) |
| 74 | << "utf8 path can't be assigned back to name_template"; |
| 75 | |
| 76 | return fd; |
| Mark Salyzyn | 40a4cb4 | 2018-11-12 12:29:14 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| Alex Buynytskyy | ab0407f | 2019-09-27 11:11:24 -0700 | [diff] [blame] | 79 | static char* mkdtemp(char* name_template, size_t size_in_chars) { |
| Alex Buynytskyy | 19ba088 | 2019-10-02 16:27:22 -0700 | [diff] [blame] | 80 | std::wstring path; |
| 81 | CHECK(android::base::UTF8ToWide(name_template, &path)) |
| 82 | << "path can't be converted to wchar: " << name_template; |
| 83 | |
| 84 | if (_wmktemp_s(path.data(), path.size() + 1) != 0) { |
| Mark Salyzyn | 40a4cb4 | 2018-11-12 12:29:14 -0800 | [diff] [blame] | 85 | return nullptr; |
| 86 | } |
| Alex Buynytskyy | ab0407f | 2019-09-27 11:11:24 -0700 | [diff] [blame] | 87 | |
| Alex Buynytskyy | 19ba088 | 2019-10-02 16:27:22 -0700 | [diff] [blame] | 88 | if (_wmkdir(path.c_str()) != 0) { |
| Mark Salyzyn | 40a4cb4 | 2018-11-12 12:29:14 -0800 | [diff] [blame] | 89 | return nullptr; |
| 90 | } |
| Alex Buynytskyy | 19ba088 | 2019-10-02 16:27:22 -0700 | [diff] [blame] | 91 | |
| 92 | std::string path_utf8; |
| 93 | CHECK(android::base::WideToUTF8(path, &path_utf8)) << "path can't be converted to utf8"; |
| 94 | CHECK(strcpy_s(name_template, size_in_chars, path_utf8.c_str()) == 0) |
| 95 | << "utf8 path can't be assigned back to name_template"; |
| 96 | |
| 97 | return name_template; |
| Mark Salyzyn | 40a4cb4 | 2018-11-12 12:29:14 -0800 | [diff] [blame] | 98 | } |
| 99 | #endif |
| 100 | |
| Mark Salyzyn | 40a4cb4 | 2018-11-12 12:29:14 -0800 | [diff] [blame] | 101 | std::string GetSystemTempDir() { |
| 102 | #if defined(__ANDROID__) |
| Mark Salyzyn | 00a6803 | 2018-11-13 15:34:38 -0800 | [diff] [blame] | 103 | const auto* tmpdir = getenv("TMPDIR"); |
| 104 | if (tmpdir == nullptr) tmpdir = "/data/local/tmp"; |
| Mark Salyzyn | 40a4cb4 | 2018-11-12 12:29:14 -0800 | [diff] [blame] | 105 | if (access(tmpdir, R_OK | W_OK | X_OK) == 0) { |
| 106 | return tmpdir; |
| 107 | } |
| 108 | // Tests running in app context can't access /data/local/tmp, |
| 109 | // so try current directory if /data/local/tmp is not accessible. |
| 110 | return "."; |
| 111 | #elif defined(_WIN32) |
| Alex Buynytskyy | ab0407f | 2019-09-27 11:11:24 -0700 | [diff] [blame] | 112 | wchar_t tmp_dir_w[MAX_PATH]; |
| 113 | DWORD result = GetTempPathW(std::size(tmp_dir_w), tmp_dir_w); // checks TMP env |
| 114 | CHECK_NE(result, 0ul) << "GetTempPathW failed, error: " << GetLastError(); |
| 115 | CHECK_LT(result, std::size(tmp_dir_w)) << "path truncated to: " << result; |
| Mark Salyzyn | 40a4cb4 | 2018-11-12 12:29:14 -0800 | [diff] [blame] | 116 | |
| 117 | // GetTempPath() returns a path with a trailing slash, but init() |
| 118 | // does not expect that, so remove it. |
| Alex Buynytskyy | ab0407f | 2019-09-27 11:11:24 -0700 | [diff] [blame] | 119 | if (tmp_dir_w[result - 1] == L'\\') { |
| 120 | tmp_dir_w[result - 1] = L'\0'; |
| 121 | } |
| 122 | |
| 123 | std::string tmp_dir; |
| 124 | CHECK(android::base::WideToUTF8(tmp_dir_w, &tmp_dir)) << "path can't be converted to utf8"; |
| 125 | |
| Mark Salyzyn | 40a4cb4 | 2018-11-12 12:29:14 -0800 | [diff] [blame] | 126 | return tmp_dir; |
| 127 | #else |
| Mark Salyzyn | 00a6803 | 2018-11-13 15:34:38 -0800 | [diff] [blame] | 128 | const auto* tmpdir = getenv("TMPDIR"); |
| 129 | if (tmpdir == nullptr) tmpdir = "/tmp"; |
| 130 | return tmpdir; |
| Mark Salyzyn | 40a4cb4 | 2018-11-12 12:29:14 -0800 | [diff] [blame] | 131 | #endif |
| 132 | } |
| 133 | |
| 134 | } // namespace |
| 135 | |
| 136 | TemporaryFile::TemporaryFile() { |
| 137 | init(GetSystemTempDir()); |
| 138 | } |
| 139 | |
| 140 | TemporaryFile::TemporaryFile(const std::string& tmp_dir) { |
| 141 | init(tmp_dir); |
| 142 | } |
| 143 | |
| 144 | TemporaryFile::~TemporaryFile() { |
| 145 | if (fd != -1) { |
| 146 | close(fd); |
| 147 | } |
| 148 | if (remove_file_) { |
| 149 | unlink(path); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | int TemporaryFile::release() { |
| 154 | int result = fd; |
| 155 | fd = -1; |
| 156 | return result; |
| 157 | } |
| 158 | |
| 159 | void TemporaryFile::init(const std::string& tmp_dir) { |
| 160 | snprintf(path, sizeof(path), "%s%cTemporaryFile-XXXXXX", tmp_dir.c_str(), OS_PATH_SEPARATOR); |
| Alex Buynytskyy | ab0407f | 2019-09-27 11:11:24 -0700 | [diff] [blame] | 161 | #if defined(_WIN32) |
| 162 | fd = mkstemp(path, sizeof(path)); |
| 163 | #else |
| Mark Salyzyn | 40a4cb4 | 2018-11-12 12:29:14 -0800 | [diff] [blame] | 164 | fd = mkstemp(path); |
| Alex Buynytskyy | ab0407f | 2019-09-27 11:11:24 -0700 | [diff] [blame] | 165 | #endif |
| Mark Salyzyn | 40a4cb4 | 2018-11-12 12:29:14 -0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | TemporaryDir::TemporaryDir() { |
| 169 | init(GetSystemTempDir()); |
| 170 | } |
| 171 | |
| 172 | TemporaryDir::~TemporaryDir() { |
| Mark Salyzyn | b6f6a20 | 2018-11-13 13:38:33 -0800 | [diff] [blame] | 173 | if (!remove_dir_and_contents_) return; |
| 174 | |
| Mark Salyzyn | f2e7615 | 2018-11-13 13:38:33 -0800 | [diff] [blame] | 175 | auto callback = [](const char* child, const struct stat*, int file_type, struct FTW*) -> int { |
| 176 | switch (file_type) { |
| 177 | case FTW_D: |
| 178 | case FTW_DP: |
| 179 | case FTW_DNR: |
| 180 | if (rmdir(child) == -1) { |
| 181 | PLOG(ERROR) << "rmdir " << child; |
| 182 | } |
| 183 | break; |
| 184 | case FTW_NS: |
| 185 | default: |
| 186 | if (rmdir(child) != -1) break; |
| 187 | // FALLTHRU (for gcc, lint, pcc, etc; and following for clang) |
| 188 | FALLTHROUGH_INTENDED; |
| 189 | case FTW_F: |
| 190 | case FTW_SL: |
| 191 | case FTW_SLN: |
| 192 | if (unlink(child) == -1) { |
| 193 | PLOG(ERROR) << "unlink " << child; |
| 194 | } |
| 195 | break; |
| 196 | } |
| 197 | return 0; |
| 198 | }; |
| 199 | |
| 200 | nftw(path, callback, 128, FTW_DEPTH | FTW_MOUNT | FTW_PHYS); |
| Mark Salyzyn | 40a4cb4 | 2018-11-12 12:29:14 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | bool TemporaryDir::init(const std::string& tmp_dir) { |
| 204 | snprintf(path, sizeof(path), "%s%cTemporaryDir-XXXXXX", tmp_dir.c_str(), OS_PATH_SEPARATOR); |
| Alex Buynytskyy | ab0407f | 2019-09-27 11:11:24 -0700 | [diff] [blame] | 205 | #if defined(_WIN32) |
| 206 | return (mkdtemp(path, sizeof(path)) != nullptr); |
| 207 | #else |
| Mark Salyzyn | 40a4cb4 | 2018-11-12 12:29:14 -0800 | [diff] [blame] | 208 | return (mkdtemp(path) != nullptr); |
| Alex Buynytskyy | ab0407f | 2019-09-27 11:11:24 -0700 | [diff] [blame] | 209 | #endif |
| Mark Salyzyn | 40a4cb4 | 2018-11-12 12:29:14 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 212 | namespace android { |
| 213 | namespace base { |
| 214 | |
| Elliott Hughes | 774d7f6 | 2015-11-11 18:02:29 +0000 | [diff] [blame] | 215 | // Versions of standard library APIs that support UTF-8 strings. |
| 216 | using namespace android::base::utf8; |
| 217 | |
| Josh Gao | e440199 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 218 | bool ReadFdToString(borrowed_fd fd, std::string* content) { |
| Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 219 | content->clear(); |
| 220 | |
| Elliott Hughes | e9ab7ad | 2017-03-20 19:16:18 -0700 | [diff] [blame] | 221 | // Although original we had small files in mind, this code gets used for |
| 222 | // very large files too, where the std::string growth heuristics might not |
| 223 | // be suitable. https://code.google.com/p/android/issues/detail?id=258500. |
| 224 | struct stat sb; |
| Josh Gao | e440199 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 225 | if (fstat(fd.get(), &sb) != -1 && sb.st_size > 0) { |
| Elliott Hughes | e9ab7ad | 2017-03-20 19:16:18 -0700 | [diff] [blame] | 226 | content->reserve(sb.st_size); |
| 227 | } |
| 228 | |
| Elliott Hughes | 1888033 | 2020-04-29 14:10:12 -0700 | [diff] [blame] | 229 | char buf[BUFSIZ] __attribute__((__uninitialized__)); |
| Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 230 | ssize_t n; |
| Josh Gao | e440199 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 231 | while ((n = TEMP_FAILURE_RETRY(read(fd.get(), &buf[0], sizeof(buf)))) > 0) { |
| Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 232 | content->append(buf, n); |
| 233 | } |
| 234 | return (n == 0) ? true : false; |
| 235 | } |
| 236 | |
| Josh Gao | 8e1f0d8 | 2016-09-14 16:11:45 -0700 | [diff] [blame] | 237 | bool ReadFileToString(const std::string& path, std::string* content, bool follow_symlinks) { |
| Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 238 | content->clear(); |
| 239 | |
| Josh Gao | 8e1f0d8 | 2016-09-14 16:11:45 -0700 | [diff] [blame] | 240 | int flags = O_RDONLY | O_CLOEXEC | O_BINARY | (follow_symlinks ? 0 : O_NOFOLLOW); |
| Christopher Ferris | bf0929f | 2017-04-05 12:09:17 -0700 | [diff] [blame] | 241 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), flags))); |
| Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 242 | if (fd == -1) { |
| 243 | return false; |
| 244 | } |
| Christopher Ferris | bf0929f | 2017-04-05 12:09:17 -0700 | [diff] [blame] | 245 | return ReadFdToString(fd, content); |
| Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| Chris Weir | fc25fae | 2022-11-15 16:15:14 -0800 | [diff] [blame] | 248 | bool WriteStringToFd(std::string_view content, borrowed_fd fd) { |
| Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 249 | const char* p = content.data(); |
| 250 | size_t left = content.size(); |
| 251 | while (left > 0) { |
| Josh Gao | e440199 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 252 | ssize_t n = TEMP_FAILURE_RETRY(write(fd.get(), p, left)); |
| Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 253 | if (n == -1) { |
| 254 | return false; |
| 255 | } |
| 256 | p += n; |
| 257 | left -= n; |
| 258 | } |
| 259 | return true; |
| 260 | } |
| 261 | |
| 262 | static bool CleanUpAfterFailedWrite(const std::string& path) { |
| 263 | // Something went wrong. Let's not leave a corrupt file lying around. |
| 264 | int saved_errno = errno; |
| 265 | unlink(path.c_str()); |
| 266 | errno = saved_errno; |
| 267 | return false; |
| 268 | } |
| 269 | |
| 270 | #if !defined(_WIN32) |
| 271 | bool WriteStringToFile(const std::string& content, const std::string& path, |
| Josh Gao | 8e1f0d8 | 2016-09-14 16:11:45 -0700 | [diff] [blame] | 272 | mode_t mode, uid_t owner, gid_t group, |
| 273 | bool follow_symlinks) { |
| 274 | int flags = O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY | |
| 275 | (follow_symlinks ? 0 : O_NOFOLLOW); |
| Christopher Ferris | bf0929f | 2017-04-05 12:09:17 -0700 | [diff] [blame] | 276 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), flags, mode))); |
| Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 277 | if (fd == -1) { |
| Elliott Hughes | 7d9a479 | 2016-07-28 15:15:28 -0700 | [diff] [blame] | 278 | PLOG(ERROR) << "android::WriteStringToFile open failed"; |
| Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 279 | return false; |
| 280 | } |
| 281 | |
| 282 | // We do an explicit fchmod here because we assume that the caller really |
| 283 | // meant what they said and doesn't want the umask-influenced mode. |
| 284 | if (fchmod(fd, mode) == -1) { |
| Elliott Hughes | 7d9a479 | 2016-07-28 15:15:28 -0700 | [diff] [blame] | 285 | PLOG(ERROR) << "android::WriteStringToFile fchmod failed"; |
| Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 286 | return CleanUpAfterFailedWrite(path); |
| 287 | } |
| 288 | if (fchown(fd, owner, group) == -1) { |
| Elliott Hughes | 7d9a479 | 2016-07-28 15:15:28 -0700 | [diff] [blame] | 289 | PLOG(ERROR) << "android::WriteStringToFile fchown failed"; |
| Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 290 | return CleanUpAfterFailedWrite(path); |
| 291 | } |
| 292 | if (!WriteStringToFd(content, fd)) { |
| Elliott Hughes | 7d9a479 | 2016-07-28 15:15:28 -0700 | [diff] [blame] | 293 | PLOG(ERROR) << "android::WriteStringToFile write failed"; |
| Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 294 | return CleanUpAfterFailedWrite(path); |
| 295 | } |
| Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 296 | return true; |
| 297 | } |
| 298 | #endif |
| 299 | |
| Josh Gao | 8e1f0d8 | 2016-09-14 16:11:45 -0700 | [diff] [blame] | 300 | bool WriteStringToFile(const std::string& content, const std::string& path, |
| 301 | bool follow_symlinks) { |
| 302 | int flags = O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY | |
| 303 | (follow_symlinks ? 0 : O_NOFOLLOW); |
| Elliott Hughes | 62ecbaa | 2017-05-15 17:31:15 -0700 | [diff] [blame] | 304 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), flags, 0666))); |
| Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 305 | if (fd == -1) { |
| 306 | return false; |
| 307 | } |
| Christopher Ferris | bf0929f | 2017-04-05 12:09:17 -0700 | [diff] [blame] | 308 | return WriteStringToFd(content, fd) || CleanUpAfterFailedWrite(path); |
| Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| Josh Gao | e440199 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 311 | bool ReadFully(borrowed_fd fd, void* data, size_t byte_count) { |
| Elliott Hughes | 20abc87 | 2015-04-24 21:57:16 -0700 | [diff] [blame] | 312 | uint8_t* p = reinterpret_cast<uint8_t*>(data); |
| 313 | size_t remaining = byte_count; |
| 314 | while (remaining > 0) { |
| Josh Gao | e440199 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 315 | ssize_t n = TEMP_FAILURE_RETRY(read(fd.get(), p, remaining)); |
| Elliott Hughes | 20abc87 | 2015-04-24 21:57:16 -0700 | [diff] [blame] | 316 | if (n <= 0) return false; |
| 317 | p += n; |
| 318 | remaining -= n; |
| 319 | } |
| 320 | return true; |
| 321 | } |
| 322 | |
| Adam Lesinski | 6d6f9b3 | 2017-06-19 10:27:38 -0700 | [diff] [blame] | 323 | #if defined(_WIN32) |
| 324 | // Windows implementation of pread. Note that this DOES move the file descriptors read position, |
| 325 | // but it does so atomically. |
| Josh Gao | e440199 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 326 | static ssize_t pread(borrowed_fd fd, void* data, size_t byte_count, off64_t offset) { |
| Adam Lesinski | 6d6f9b3 | 2017-06-19 10:27:38 -0700 | [diff] [blame] | 327 | DWORD bytes_read; |
| 328 | OVERLAPPED overlapped; |
| 329 | memset(&overlapped, 0, sizeof(OVERLAPPED)); |
| 330 | overlapped.Offset = static_cast<DWORD>(offset); |
| 331 | overlapped.OffsetHigh = static_cast<DWORD>(offset >> 32); |
| Josh Gao | e440199 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 332 | if (!ReadFile(reinterpret_cast<HANDLE>(_get_osfhandle(fd.get())), data, |
| 333 | static_cast<DWORD>(byte_count), &bytes_read, &overlapped)) { |
| Adam Lesinski | 6d6f9b3 | 2017-06-19 10:27:38 -0700 | [diff] [blame] | 334 | // In case someone tries to read errno (since this is masquerading as a POSIX call) |
| 335 | errno = EIO; |
| 336 | return -1; |
| 337 | } |
| 338 | return static_cast<ssize_t>(bytes_read); |
| 339 | } |
| Kelvin Zhang | a095db0 | 2022-11-02 09:51:21 -0700 | [diff] [blame] | 340 | |
| 341 | static ssize_t pwrite(borrowed_fd fd, const void* data, size_t byte_count, off64_t offset) { |
| 342 | DWORD bytes_written; |
| 343 | OVERLAPPED overlapped; |
| 344 | memset(&overlapped, 0, sizeof(OVERLAPPED)); |
| 345 | overlapped.Offset = static_cast<DWORD>(offset); |
| 346 | overlapped.OffsetHigh = static_cast<DWORD>(offset >> 32); |
| 347 | if (!WriteFile(reinterpret_cast<HANDLE>(_get_osfhandle(fd.get())), data, |
| 348 | static_cast<DWORD>(byte_count), &bytes_written, &overlapped)) { |
| 349 | // In case someone tries to read errno (since this is masquerading as a POSIX call) |
| 350 | errno = EIO; |
| 351 | return -1; |
| 352 | } |
| 353 | return static_cast<ssize_t>(bytes_written); |
| 354 | } |
| Adam Lesinski | 6d6f9b3 | 2017-06-19 10:27:38 -0700 | [diff] [blame] | 355 | #endif |
| 356 | |
| Josh Gao | e440199 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 357 | bool ReadFullyAtOffset(borrowed_fd fd, void* data, size_t byte_count, off64_t offset) { |
| Adam Lesinski | 6d6f9b3 | 2017-06-19 10:27:38 -0700 | [diff] [blame] | 358 | uint8_t* p = reinterpret_cast<uint8_t*>(data); |
| 359 | while (byte_count > 0) { |
| Josh Gao | e440199 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 360 | ssize_t n = TEMP_FAILURE_RETRY(pread(fd.get(), p, byte_count, offset)); |
| Adam Lesinski | 6d6f9b3 | 2017-06-19 10:27:38 -0700 | [diff] [blame] | 361 | if (n <= 0) return false; |
| 362 | p += n; |
| 363 | byte_count -= n; |
| 364 | offset += n; |
| 365 | } |
| 366 | return true; |
| 367 | } |
| 368 | |
| Kelvin Zhang | a095db0 | 2022-11-02 09:51:21 -0700 | [diff] [blame] | 369 | bool WriteFullyAtOffset(borrowed_fd fd, const void* data, size_t byte_count, off64_t offset) { |
| 370 | const uint8_t* p = reinterpret_cast<const uint8_t*>(data); |
| 371 | size_t remaining = byte_count; |
| 372 | while (remaining > 0) { |
| 373 | ssize_t n = TEMP_FAILURE_RETRY(pwrite(fd.get(), p, remaining, offset)); |
| 374 | if (n == -1) return false; |
| 375 | p += n; |
| 376 | remaining -= n; |
| 377 | offset += n; |
| 378 | } |
| 379 | return true; |
| 380 | } |
| 381 | |
| Josh Gao | e440199 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 382 | bool WriteFully(borrowed_fd fd, const void* data, size_t byte_count) { |
| Elliott Hughes | 20abc87 | 2015-04-24 21:57:16 -0700 | [diff] [blame] | 383 | const uint8_t* p = reinterpret_cast<const uint8_t*>(data); |
| 384 | size_t remaining = byte_count; |
| 385 | while (remaining > 0) { |
| Josh Gao | e440199 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 386 | ssize_t n = TEMP_FAILURE_RETRY(write(fd.get(), p, remaining)); |
| Elliott Hughes | 20abc87 | 2015-04-24 21:57:16 -0700 | [diff] [blame] | 387 | if (n == -1) return false; |
| 388 | p += n; |
| 389 | remaining -= n; |
| 390 | } |
| 391 | return true; |
| 392 | } |
| 393 | |
| Yabin Cui | 8f6a5a0 | 2016-01-29 17:25:54 -0800 | [diff] [blame] | 394 | bool RemoveFileIfExists(const std::string& path, std::string* err) { |
| 395 | struct stat st; |
| 396 | #if defined(_WIN32) |
| liwugang | 78cea68 | 2018-07-11 13:24:49 +0800 | [diff] [blame] | 397 | // TODO: Windows version can't handle symbolic links correctly. |
| Yabin Cui | 8f6a5a0 | 2016-01-29 17:25:54 -0800 | [diff] [blame] | 398 | int result = stat(path.c_str(), &st); |
| 399 | bool file_type_removable = (result == 0 && S_ISREG(st.st_mode)); |
| 400 | #else |
| 401 | int result = lstat(path.c_str(), &st); |
| 402 | bool file_type_removable = (result == 0 && (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))); |
| 403 | #endif |
| liwugang | 78cea68 | 2018-07-11 13:24:49 +0800 | [diff] [blame] | 404 | if (result == -1) { |
| 405 | if (errno == ENOENT || errno == ENOTDIR) return true; |
| 406 | if (err != nullptr) *err = strerror(errno); |
| 407 | return false; |
| 408 | } |
| 409 | |
| Yabin Cui | 8f6a5a0 | 2016-01-29 17:25:54 -0800 | [diff] [blame] | 410 | if (result == 0) { |
| 411 | if (!file_type_removable) { |
| 412 | if (err != nullptr) { |
| liwugang | 78cea68 | 2018-07-11 13:24:49 +0800 | [diff] [blame] | 413 | *err = "is not a regular file or symbolic link"; |
| Yabin Cui | 8f6a5a0 | 2016-01-29 17:25:54 -0800 | [diff] [blame] | 414 | } |
| 415 | return false; |
| 416 | } |
| 417 | if (unlink(path.c_str()) == -1) { |
| 418 | if (err != nullptr) { |
| 419 | *err = strerror(errno); |
| 420 | } |
| 421 | return false; |
| 422 | } |
| 423 | } |
| 424 | return true; |
| 425 | } |
| 426 | |
| Elliott Hughes | a634a9a | 2016-08-23 15:53:45 -0700 | [diff] [blame] | 427 | #if !defined(_WIN32) |
| 428 | bool Readlink(const std::string& path, std::string* result) { |
| 429 | result->clear(); |
| 430 | |
| 431 | // Most Linux file systems (ext2 and ext4, say) limit symbolic links to |
| 432 | // 4095 bytes. Since we'll copy out into the string anyway, it doesn't |
| 433 | // waste memory to just start there. We add 1 so that we can recognize |
| 434 | // whether it actually fit (rather than being truncated to 4095). |
| 435 | std::vector<char> buf(4095 + 1); |
| 436 | while (true) { |
| 437 | ssize_t size = readlink(path.c_str(), &buf[0], buf.size()); |
| 438 | // Unrecoverable error? |
| 439 | if (size == -1) return false; |
| 440 | // It fit! (If size == buf.size(), it may have been truncated.) |
| 441 | if (static_cast<size_t>(size) < buf.size()) { |
| 442 | result->assign(&buf[0], size); |
| 443 | return true; |
| 444 | } |
| 445 | // Double our buffer and try again. |
| 446 | buf.resize(buf.size() * 2); |
| 447 | } |
| 448 | } |
| 449 | #endif |
| 450 | |
| Dimitry Ivanov | 4edc04f | 2016-09-09 10:49:21 -0700 | [diff] [blame] | 451 | #if !defined(_WIN32) |
| 452 | bool Realpath(const std::string& path, std::string* result) { |
| 453 | result->clear(); |
| 454 | |
| Yifan Hong | 26fd234 | 2019-03-21 15:20:53 -0700 | [diff] [blame] | 455 | // realpath may exit with EINTR. Retry if so. |
| 456 | char* realpath_buf = nullptr; |
| 457 | do { |
| 458 | realpath_buf = realpath(path.c_str(), nullptr); |
| 459 | } while (realpath_buf == nullptr && errno == EINTR); |
| 460 | |
| Dimitry Ivanov | 4edc04f | 2016-09-09 10:49:21 -0700 | [diff] [blame] | 461 | if (realpath_buf == nullptr) { |
| 462 | return false; |
| 463 | } |
| 464 | result->assign(realpath_buf); |
| 465 | free(realpath_buf); |
| 466 | return true; |
| 467 | } |
| 468 | #endif |
| 469 | |
| Elliott Hughes | 48f0eb5 | 2016-08-31 15:07:18 -0700 | [diff] [blame] | 470 | std::string GetExecutablePath() { |
| 471 | #if defined(__linux__) |
| 472 | std::string path; |
| 473 | android::base::Readlink("/proc/self/exe", &path); |
| 474 | return path; |
| 475 | #elif defined(__APPLE__) |
| Elliott Hughes | 48f0eb5 | 2016-08-31 15:07:18 -0700 | [diff] [blame] | 476 | char path[PATH_MAX + 1]; |
| Josh Gao | 58668ac | 2016-09-01 12:31:42 -0700 | [diff] [blame] | 477 | uint32_t path_len = sizeof(path); |
| 478 | int rc = _NSGetExecutablePath(path, &path_len); |
| 479 | if (rc < 0) { |
| 480 | std::unique_ptr<char> path_buf(new char[path_len]); |
| 481 | _NSGetExecutablePath(path_buf.get(), &path_len); |
| 482 | return path_buf.get(); |
| 483 | } |
| Elliott Hughes | 48f0eb5 | 2016-08-31 15:07:18 -0700 | [diff] [blame] | 484 | return path; |
| 485 | #elif defined(_WIN32) |
| 486 | char path[PATH_MAX + 1]; |
| 487 | DWORD result = GetModuleFileName(NULL, path, sizeof(path) - 1); |
| 488 | if (result == 0 || result == sizeof(path) - 1) return ""; |
| 489 | path[PATH_MAX - 1] = 0; |
| 490 | return path; |
| 491 | #else |
| 492 | #error unknown OS |
| 493 | #endif |
| 494 | } |
| 495 | |
| Colin Cross | 2909be0 | 2017-02-23 17:41:56 -0800 | [diff] [blame] | 496 | std::string GetExecutableDirectory() { |
| 497 | return Dirname(GetExecutablePath()); |
| 498 | } |
| Colin Cross | 2e732e2 | 2017-02-23 21:23:05 -0800 | [diff] [blame] | 499 | |
| Elliott Hughes | 91a10d9 | 2022-05-24 17:44:36 -0700 | [diff] [blame] | 500 | #if defined(_WIN32) |
| Yurii Zubrytskyi | 2902e10 | 2022-11-08 23:32:50 -0800 | [diff] [blame^] | 501 | std::string Basename(std::string_view path) { |
| Elliott Hughes | 91a10d9 | 2022-05-24 17:44:36 -0700 | [diff] [blame] | 502 | // TODO: how much of this is actually necessary for mingw? |
| 503 | |
| Colin Cross | 2e732e2 | 2017-02-23 21:23:05 -0800 | [diff] [blame] | 504 | // Copy path because basename may modify the string passed in. |
| 505 | std::string result(path); |
| 506 | |
| Colin Cross | 2e732e2 | 2017-02-23 21:23:05 -0800 | [diff] [blame] | 507 | // Use lock because basename() may write to a process global and return a |
| 508 | // pointer to that. Note that this locking strategy only works if all other |
| 509 | // callers to basename in the process also grab this same lock, but its |
| 510 | // better than nothing. Bionic's basename returns a thread-local buffer. |
| 511 | static std::mutex& basename_lock = *new std::mutex(); |
| 512 | std::lock_guard<std::mutex> lock(basename_lock); |
| Colin Cross | 2e732e2 | 2017-02-23 21:23:05 -0800 | [diff] [blame] | 513 | |
| 514 | // Note that if std::string uses copy-on-write strings, &str[0] will cause |
| 515 | // the copy to be made, so there is no chance of us accidentally writing to |
| 516 | // the storage for 'path'. |
| 517 | char* name = basename(&result[0]); |
| 518 | |
| 519 | // In case basename returned a pointer to a process global, copy that string |
| 520 | // before leaving the lock. |
| 521 | result.assign(name); |
| 522 | |
| 523 | return result; |
| 524 | } |
| Elliott Hughes | 91a10d9 | 2022-05-24 17:44:36 -0700 | [diff] [blame] | 525 | #else |
| 526 | // Copied from bionic so that Basename() below can be portable and thread-safe. |
| Yurii Zubrytskyi | 2902e10 | 2022-11-08 23:32:50 -0800 | [diff] [blame^] | 527 | static int _basename_r(const char* path, size_t path_size, char* buffer, size_t buffer_size) { |
| Elliott Hughes | 91a10d9 | 2022-05-24 17:44:36 -0700 | [diff] [blame] | 528 | const char* startp = nullptr; |
| 529 | const char* endp = nullptr; |
| 530 | int len; |
| 531 | int result; |
| 532 | |
| 533 | // Empty or NULL string gets treated as ".". |
| Yurii Zubrytskyi | 2902e10 | 2022-11-08 23:32:50 -0800 | [diff] [blame^] | 534 | if (path == nullptr || path_size == 0) { |
| Elliott Hughes | 91a10d9 | 2022-05-24 17:44:36 -0700 | [diff] [blame] | 535 | startp = "."; |
| 536 | len = 1; |
| 537 | goto Exit; |
| 538 | } |
| 539 | |
| 540 | // Strip trailing slashes. |
| Yurii Zubrytskyi | 2902e10 | 2022-11-08 23:32:50 -0800 | [diff] [blame^] | 541 | endp = path + path_size - 1; |
| Elliott Hughes | 91a10d9 | 2022-05-24 17:44:36 -0700 | [diff] [blame] | 542 | while (endp > path && *endp == '/') { |
| 543 | endp--; |
| 544 | } |
| 545 | |
| 546 | // All slashes becomes "/". |
| 547 | if (endp == path && *endp == '/') { |
| 548 | startp = "/"; |
| 549 | len = 1; |
| 550 | goto Exit; |
| 551 | } |
| 552 | |
| 553 | // Find the start of the base. |
| 554 | startp = endp; |
| 555 | while (startp > path && *(startp - 1) != '/') { |
| 556 | startp--; |
| 557 | } |
| 558 | |
| 559 | len = endp - startp +1; |
| 560 | |
| 561 | Exit: |
| 562 | result = len; |
| 563 | if (buffer == nullptr) { |
| 564 | return result; |
| 565 | } |
| 566 | if (len > static_cast<int>(buffer_size) - 1) { |
| 567 | len = buffer_size - 1; |
| 568 | result = -1; |
| 569 | errno = ERANGE; |
| 570 | } |
| 571 | |
| 572 | if (len >= 0) { |
| 573 | memcpy(buffer, startp, len); |
| 574 | buffer[len] = 0; |
| 575 | } |
| 576 | return result; |
| 577 | } |
| Yurii Zubrytskyi | 2902e10 | 2022-11-08 23:32:50 -0800 | [diff] [blame^] | 578 | std::string Basename(std::string_view path) { |
| 579 | char buf[PATH_MAX] __attribute__((__uninitialized__)); |
| 580 | const auto size = _basename_r(path.data(), path.size(), buf, sizeof(buf)); |
| 581 | return size > 0 ? std::string(buf, size) : std::string(); |
| Elliott Hughes | 91a10d9 | 2022-05-24 17:44:36 -0700 | [diff] [blame] | 582 | } |
| 583 | #endif |
| Colin Cross | 2e732e2 | 2017-02-23 21:23:05 -0800 | [diff] [blame] | 584 | |
| Elliott Hughes | 3e6e442 | 2022-05-26 16:37:25 -0700 | [diff] [blame] | 585 | #if defined(_WIN32) |
| Yurii Zubrytskyi | 2902e10 | 2022-11-08 23:32:50 -0800 | [diff] [blame^] | 586 | std::string Dirname(std::string_view path) { |
| Elliott Hughes | 3e6e442 | 2022-05-26 16:37:25 -0700 | [diff] [blame] | 587 | // TODO: how much of this is actually necessary for mingw? |
| 588 | |
| Colin Cross | 2e732e2 | 2017-02-23 21:23:05 -0800 | [diff] [blame] | 589 | // Copy path because dirname may modify the string passed in. |
| 590 | std::string result(path); |
| 591 | |
| Colin Cross | 2e732e2 | 2017-02-23 21:23:05 -0800 | [diff] [blame] | 592 | // Use lock because dirname() may write to a process global and return a |
| 593 | // pointer to that. Note that this locking strategy only works if all other |
| 594 | // callers to dirname in the process also grab this same lock, but its |
| 595 | // better than nothing. Bionic's dirname returns a thread-local buffer. |
| 596 | static std::mutex& dirname_lock = *new std::mutex(); |
| 597 | std::lock_guard<std::mutex> lock(dirname_lock); |
| Colin Cross | 2e732e2 | 2017-02-23 21:23:05 -0800 | [diff] [blame] | 598 | |
| 599 | // Note that if std::string uses copy-on-write strings, &str[0] will cause |
| 600 | // the copy to be made, so there is no chance of us accidentally writing to |
| 601 | // the storage for 'path'. |
| 602 | char* parent = dirname(&result[0]); |
| 603 | |
| 604 | // In case dirname returned a pointer to a process global, copy that string |
| 605 | // before leaving the lock. |
| 606 | result.assign(parent); |
| 607 | |
| 608 | return result; |
| 609 | } |
| Elliott Hughes | 3e6e442 | 2022-05-26 16:37:25 -0700 | [diff] [blame] | 610 | #else |
| 611 | // Copied from bionic so that Dirname() below can be portable and thread-safe. |
| Yurii Zubrytskyi | 2902e10 | 2022-11-08 23:32:50 -0800 | [diff] [blame^] | 612 | static int _dirname_r(const char* path, size_t path_size, char* buffer, size_t buffer_size) { |
| Elliott Hughes | 3e6e442 | 2022-05-26 16:37:25 -0700 | [diff] [blame] | 613 | const char* endp = nullptr; |
| 614 | int len; |
| 615 | int result; |
| 616 | |
| 617 | // Empty or NULL string gets treated as ".". |
| Yurii Zubrytskyi | 2902e10 | 2022-11-08 23:32:50 -0800 | [diff] [blame^] | 618 | if (path == nullptr || path_size == 0) { |
| Elliott Hughes | 3e6e442 | 2022-05-26 16:37:25 -0700 | [diff] [blame] | 619 | path = "."; |
| 620 | len = 1; |
| 621 | goto Exit; |
| 622 | } |
| 623 | |
| 624 | // Strip trailing slashes. |
| Yurii Zubrytskyi | 2902e10 | 2022-11-08 23:32:50 -0800 | [diff] [blame^] | 625 | endp = path + path_size - 1; |
| Elliott Hughes | 3e6e442 | 2022-05-26 16:37:25 -0700 | [diff] [blame] | 626 | while (endp > path && *endp == '/') { |
| 627 | endp--; |
| 628 | } |
| 629 | |
| 630 | // Find the start of the dir. |
| 631 | while (endp > path && *endp != '/') { |
| 632 | endp--; |
| 633 | } |
| 634 | |
| 635 | // Either the dir is "/" or there are no slashes. |
| 636 | if (endp == path) { |
| 637 | path = (*endp == '/') ? "/" : "."; |
| 638 | len = 1; |
| 639 | goto Exit; |
| 640 | } |
| 641 | |
| 642 | do { |
| 643 | endp--; |
| 644 | } while (endp > path && *endp == '/'); |
| 645 | |
| 646 | len = endp - path + 1; |
| 647 | |
| 648 | Exit: |
| 649 | result = len; |
| 650 | if (len + 1 > MAXPATHLEN) { |
| 651 | errno = ENAMETOOLONG; |
| 652 | return -1; |
| 653 | } |
| 654 | if (buffer == nullptr) { |
| 655 | return result; |
| 656 | } |
| 657 | |
| 658 | if (len > static_cast<int>(buffer_size) - 1) { |
| 659 | len = buffer_size - 1; |
| 660 | result = -1; |
| 661 | errno = ERANGE; |
| 662 | } |
| 663 | |
| 664 | if (len >= 0) { |
| 665 | memcpy(buffer, path, len); |
| 666 | buffer[len] = 0; |
| 667 | } |
| 668 | return result; |
| 669 | } |
| Yurii Zubrytskyi | 2902e10 | 2022-11-08 23:32:50 -0800 | [diff] [blame^] | 670 | std::string Dirname(std::string_view path) { |
| 671 | char buf[PATH_MAX] __attribute__((__uninitialized__)); |
| 672 | const auto size = _dirname_r(path.data(), path.size(), buf, sizeof(buf)); |
| 673 | return size > 0 ? std::string(buf, size) : std::string(); |
| Elliott Hughes | 3e6e442 | 2022-05-26 16:37:25 -0700 | [diff] [blame] | 674 | } |
| 675 | #endif |
| Colin Cross | 2e732e2 | 2017-02-23 21:23:05 -0800 | [diff] [blame] | 676 | |
| Dan Albert | aac6b7c | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 677 | } // namespace base |
| 678 | } // namespace android |