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