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> |
| 21 | #include <sys/stat.h> |
| 22 | #include <sys/types.h> |
Elliott Hughes | d3ff6e5 | 2016-08-23 15:53:45 -0700 | [diff] [blame] | 23 | #include <unistd.h> |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 24 | |
Josh Gao | 7307f09 | 2016-09-01 12:31:42 -0700 | [diff] [blame] | 25 | #include <memory> |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 26 | #include <string> |
Elliott Hughes | d3ff6e5 | 2016-08-23 15:53:45 -0700 | [diff] [blame] | 27 | #include <vector> |
Elliott Hughes | af4885a | 2015-02-03 13:02:57 -0800 | [diff] [blame] | 28 | |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 29 | #include "android-base/macros.h" // For TEMP_FAILURE_RETRY on Darwin. |
Elliott Hughes | e5dd71a | 2016-07-28 15:15:28 -0700 | [diff] [blame] | 30 | #include "android-base/logging.h" |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 31 | #include "android-base/utf8.h" |
Dan Albert | 94d1360 | 2015-03-26 23:33:28 -0700 | [diff] [blame] | 32 | #include "utils/Compat.h" |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 33 | |
Elliott Hughes | 82ff315 | 2016-08-31 15:07:18 -0700 | [diff] [blame] | 34 | #if defined(__APPLE__) |
Josh Gao | 7307f09 | 2016-09-01 12:31:42 -0700 | [diff] [blame] | 35 | #include <mach-o/dyld.h> |
Elliott Hughes | 82ff315 | 2016-08-31 15:07:18 -0700 | [diff] [blame] | 36 | #endif |
| 37 | #if defined(_WIN32) |
| 38 | #include <windows.h> |
| 39 | #endif |
| 40 | |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 41 | namespace android { |
| 42 | namespace base { |
| 43 | |
Elliott Hughes | c1fd492 | 2015-11-11 18:02:29 +0000 | [diff] [blame] | 44 | // Versions of standard library APIs that support UTF-8 strings. |
| 45 | using namespace android::base::utf8; |
| 46 | |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 47 | bool ReadFdToString(int fd, std::string* content) { |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 48 | content->clear(); |
| 49 | |
| 50 | char buf[BUFSIZ]; |
| 51 | ssize_t n; |
| 52 | while ((n = TEMP_FAILURE_RETRY(read(fd, &buf[0], sizeof(buf)))) > 0) { |
| 53 | content->append(buf, n); |
| 54 | } |
| 55 | return (n == 0) ? true : false; |
| 56 | } |
| 57 | |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 58 | bool ReadFileToString(const std::string& path, std::string* content) { |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 59 | content->clear(); |
| 60 | |
Elliott Hughes | 470d79a | 2015-09-01 13:35:44 -0700 | [diff] [blame] | 61 | int fd = TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_BINARY)); |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 62 | if (fd == -1) { |
| 63 | return false; |
| 64 | } |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 65 | bool result = ReadFdToString(fd, content); |
Nick Kralevich | 95db36e | 2015-05-20 08:59:21 -0700 | [diff] [blame] | 66 | close(fd); |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 67 | return result; |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 70 | bool WriteStringToFd(const std::string& content, int fd) { |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 71 | const char* p = content.data(); |
| 72 | size_t left = content.size(); |
| 73 | while (left > 0) { |
| 74 | ssize_t n = TEMP_FAILURE_RETRY(write(fd, p, left)); |
| 75 | if (n == -1) { |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 76 | return false; |
| 77 | } |
| 78 | p += n; |
| 79 | left -= n; |
| 80 | } |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 81 | return true; |
| 82 | } |
Elliott Hughes | 202f024 | 2015-02-04 13:19:13 -0800 | [diff] [blame] | 83 | |
| 84 | static bool CleanUpAfterFailedWrite(const std::string& path) { |
| 85 | // Something went wrong. Let's not leave a corrupt file lying around. |
| 86 | int saved_errno = errno; |
| 87 | unlink(path.c_str()); |
| 88 | errno = saved_errno; |
| 89 | return false; |
| 90 | } |
| 91 | |
Elliott Hughes | 31fa09c | 2015-02-04 19:38:28 -0800 | [diff] [blame] | 92 | #if !defined(_WIN32) |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 93 | bool WriteStringToFile(const std::string& content, const std::string& path, |
| 94 | mode_t mode, uid_t owner, gid_t group) { |
Elliott Hughes | 470d79a | 2015-09-01 13:35:44 -0700 | [diff] [blame] | 95 | int flags = O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW | O_BINARY; |
| 96 | int fd = TEMP_FAILURE_RETRY(open(path.c_str(), flags, mode)); |
Elliott Hughes | 202f024 | 2015-02-04 13:19:13 -0800 | [diff] [blame] | 97 | if (fd == -1) { |
Elliott Hughes | e5dd71a | 2016-07-28 15:15:28 -0700 | [diff] [blame] | 98 | PLOG(ERROR) << "android::WriteStringToFile open failed"; |
Elliott Hughes | 202f024 | 2015-02-04 13:19:13 -0800 | [diff] [blame] | 99 | return false; |
| 100 | } |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 101 | |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 102 | // We do an explicit fchmod here because we assume that the caller really |
| 103 | // meant what they said and doesn't want the umask-influenced mode. |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 104 | if (fchmod(fd, mode) == -1) { |
Elliott Hughes | e5dd71a | 2016-07-28 15:15:28 -0700 | [diff] [blame] | 105 | PLOG(ERROR) << "android::WriteStringToFile fchmod failed"; |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 106 | return CleanUpAfterFailedWrite(path); |
| 107 | } |
| 108 | if (fchown(fd, owner, group) == -1) { |
Elliott Hughes | e5dd71a | 2016-07-28 15:15:28 -0700 | [diff] [blame] | 109 | PLOG(ERROR) << "android::WriteStringToFile fchown failed"; |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 110 | return CleanUpAfterFailedWrite(path); |
| 111 | } |
| 112 | if (!WriteStringToFd(content, fd)) { |
Elliott Hughes | e5dd71a | 2016-07-28 15:15:28 -0700 | [diff] [blame] | 113 | PLOG(ERROR) << "android::WriteStringToFile write failed"; |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 114 | return CleanUpAfterFailedWrite(path); |
| 115 | } |
Nick Kralevich | 95db36e | 2015-05-20 08:59:21 -0700 | [diff] [blame] | 116 | close(fd); |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 117 | return true; |
Elliott Hughes | 202f024 | 2015-02-04 13:19:13 -0800 | [diff] [blame] | 118 | } |
Elliott Hughes | 31fa09c | 2015-02-04 19:38:28 -0800 | [diff] [blame] | 119 | #endif |
Elliott Hughes | 202f024 | 2015-02-04 13:19:13 -0800 | [diff] [blame] | 120 | |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 121 | bool WriteStringToFile(const std::string& content, const std::string& path) { |
Elliott Hughes | 470d79a | 2015-09-01 13:35:44 -0700 | [diff] [blame] | 122 | int flags = O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW | O_BINARY; |
| 123 | int fd = TEMP_FAILURE_RETRY(open(path.c_str(), flags, DEFFILEMODE)); |
Elliott Hughes | 202f024 | 2015-02-04 13:19:13 -0800 | [diff] [blame] | 124 | if (fd == -1) { |
| 125 | return false; |
| 126 | } |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 127 | |
| 128 | bool result = WriteStringToFd(content, fd); |
Nick Kralevich | 95db36e | 2015-05-20 08:59:21 -0700 | [diff] [blame] | 129 | close(fd); |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 130 | return result || CleanUpAfterFailedWrite(path); |
Elliott Hughes | 202f024 | 2015-02-04 13:19:13 -0800 | [diff] [blame] | 131 | } |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 132 | |
Elliott Hughes | 56085ed | 2015-04-24 21:57:16 -0700 | [diff] [blame] | 133 | bool ReadFully(int fd, void* data, size_t byte_count) { |
| 134 | uint8_t* p = reinterpret_cast<uint8_t*>(data); |
| 135 | size_t remaining = byte_count; |
| 136 | while (remaining > 0) { |
| 137 | ssize_t n = TEMP_FAILURE_RETRY(read(fd, p, remaining)); |
| 138 | if (n <= 0) return false; |
| 139 | p += n; |
| 140 | remaining -= n; |
| 141 | } |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | bool WriteFully(int fd, const void* data, size_t byte_count) { |
| 146 | const uint8_t* p = reinterpret_cast<const uint8_t*>(data); |
| 147 | size_t remaining = byte_count; |
| 148 | while (remaining > 0) { |
| 149 | ssize_t n = TEMP_FAILURE_RETRY(write(fd, p, remaining)); |
| 150 | if (n == -1) return false; |
| 151 | p += n; |
| 152 | remaining -= n; |
| 153 | } |
| 154 | return true; |
| 155 | } |
| 156 | |
Yabin Cui | b6e314a | 2016-01-29 17:25:54 -0800 | [diff] [blame] | 157 | bool RemoveFileIfExists(const std::string& path, std::string* err) { |
| 158 | struct stat st; |
| 159 | #if defined(_WIN32) |
| 160 | //TODO: Windows version can't handle symbol link correctly. |
| 161 | int result = stat(path.c_str(), &st); |
| 162 | bool file_type_removable = (result == 0 && S_ISREG(st.st_mode)); |
| 163 | #else |
| 164 | int result = lstat(path.c_str(), &st); |
| 165 | bool file_type_removable = (result == 0 && (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))); |
| 166 | #endif |
| 167 | if (result == 0) { |
| 168 | if (!file_type_removable) { |
| 169 | if (err != nullptr) { |
| 170 | *err = "is not a regular or symbol link file"; |
| 171 | } |
| 172 | return false; |
| 173 | } |
| 174 | if (unlink(path.c_str()) == -1) { |
| 175 | if (err != nullptr) { |
| 176 | *err = strerror(errno); |
| 177 | } |
| 178 | return false; |
| 179 | } |
| 180 | } |
| 181 | return true; |
| 182 | } |
| 183 | |
Elliott Hughes | d3ff6e5 | 2016-08-23 15:53:45 -0700 | [diff] [blame] | 184 | #if !defined(_WIN32) |
| 185 | bool Readlink(const std::string& path, std::string* result) { |
| 186 | result->clear(); |
| 187 | |
| 188 | // Most Linux file systems (ext2 and ext4, say) limit symbolic links to |
| 189 | // 4095 bytes. Since we'll copy out into the string anyway, it doesn't |
| 190 | // waste memory to just start there. We add 1 so that we can recognize |
| 191 | // whether it actually fit (rather than being truncated to 4095). |
| 192 | std::vector<char> buf(4095 + 1); |
| 193 | while (true) { |
| 194 | ssize_t size = readlink(path.c_str(), &buf[0], buf.size()); |
| 195 | // Unrecoverable error? |
| 196 | if (size == -1) return false; |
| 197 | // It fit! (If size == buf.size(), it may have been truncated.) |
| 198 | if (static_cast<size_t>(size) < buf.size()) { |
| 199 | result->assign(&buf[0], size); |
| 200 | return true; |
| 201 | } |
| 202 | // Double our buffer and try again. |
| 203 | buf.resize(buf.size() * 2); |
| 204 | } |
| 205 | } |
| 206 | #endif |
| 207 | |
Elliott Hughes | 82ff315 | 2016-08-31 15:07:18 -0700 | [diff] [blame] | 208 | std::string GetExecutablePath() { |
| 209 | #if defined(__linux__) |
| 210 | std::string path; |
| 211 | android::base::Readlink("/proc/self/exe", &path); |
| 212 | return path; |
| 213 | #elif defined(__APPLE__) |
Elliott Hughes | 82ff315 | 2016-08-31 15:07:18 -0700 | [diff] [blame] | 214 | char path[PATH_MAX + 1]; |
Josh Gao | 7307f09 | 2016-09-01 12:31:42 -0700 | [diff] [blame] | 215 | uint32_t path_len = sizeof(path); |
| 216 | int rc = _NSGetExecutablePath(path, &path_len); |
| 217 | if (rc < 0) { |
| 218 | std::unique_ptr<char> path_buf(new char[path_len]); |
| 219 | _NSGetExecutablePath(path_buf.get(), &path_len); |
| 220 | return path_buf.get(); |
| 221 | } |
Elliott Hughes | 82ff315 | 2016-08-31 15:07:18 -0700 | [diff] [blame] | 222 | return path; |
| 223 | #elif defined(_WIN32) |
| 224 | char path[PATH_MAX + 1]; |
| 225 | DWORD result = GetModuleFileName(NULL, path, sizeof(path) - 1); |
| 226 | if (result == 0 || result == sizeof(path) - 1) return ""; |
| 227 | path[PATH_MAX - 1] = 0; |
| 228 | return path; |
| 229 | #else |
| 230 | #error unknown OS |
| 231 | #endif |
| 232 | } |
| 233 | |
Dan Albert | c007bc3 | 2015-03-16 10:08:46 -0700 | [diff] [blame] | 234 | } // namespace base |
| 235 | } // namespace android |