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