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