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 | |
Dan Albert | 98ff772 | 2015-03-13 22:39:54 -0700 | [diff] [blame^] | 17 | #include "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 | 98ff772 | 2015-03-13 22:39:54 -0700 | [diff] [blame^] | 24 | #include <string> |
Elliott Hughes | af4885a | 2015-02-03 13:02:57 -0800 | [diff] [blame] | 25 | |
Dan Albert | 98ff772 | 2015-03-13 22:39:54 -0700 | [diff] [blame^] | 26 | #define LOG_TAG "base.file" |
| 27 | #include "cutils/log.h" |
| 28 | #include "utils/Compat.h" // For TEMP_FAILURE_RETRY on Darwin. |
| 29 | |
| 30 | namespace android { |
| 31 | namespace base { |
| 32 | |
| 33 | bool ReadFdToString(int fd, std::string* content) { |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 34 | content->clear(); |
| 35 | |
| 36 | char buf[BUFSIZ]; |
| 37 | ssize_t n; |
| 38 | while ((n = TEMP_FAILURE_RETRY(read(fd, &buf[0], sizeof(buf)))) > 0) { |
| 39 | content->append(buf, n); |
| 40 | } |
| 41 | return (n == 0) ? true : false; |
| 42 | } |
| 43 | |
Dan Albert | 98ff772 | 2015-03-13 22:39:54 -0700 | [diff] [blame^] | 44 | bool ReadFileToString(const std::string& path, std::string* content) { |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 45 | content->clear(); |
| 46 | |
Dan Albert | 98ff772 | 2015-03-13 22:39:54 -0700 | [diff] [blame^] | 47 | int fd = |
| 48 | TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_CLOEXEC | O_NOFOLLOW)); |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 49 | if (fd == -1) { |
| 50 | return false; |
| 51 | } |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 52 | bool result = ReadFdToString(fd, content); |
| 53 | TEMP_FAILURE_RETRY(close(fd)); |
| 54 | return result; |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Dan Albert | 98ff772 | 2015-03-13 22:39:54 -0700 | [diff] [blame^] | 57 | bool WriteStringToFd(const std::string& content, int fd) { |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 58 | const char* p = content.data(); |
| 59 | size_t left = content.size(); |
| 60 | while (left > 0) { |
| 61 | ssize_t n = TEMP_FAILURE_RETRY(write(fd, p, left)); |
| 62 | if (n == -1) { |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 63 | return false; |
| 64 | } |
| 65 | p += n; |
| 66 | left -= n; |
| 67 | } |
Elliott Hughes | dec12b2 | 2015-02-02 17:31:27 -0800 | [diff] [blame] | 68 | return true; |
| 69 | } |
Elliott Hughes | 202f024 | 2015-02-04 13:19:13 -0800 | [diff] [blame] | 70 | |
| 71 | static bool CleanUpAfterFailedWrite(const std::string& path) { |
| 72 | // Something went wrong. Let's not leave a corrupt file lying around. |
| 73 | int saved_errno = errno; |
| 74 | unlink(path.c_str()); |
| 75 | errno = saved_errno; |
| 76 | return false; |
| 77 | } |
| 78 | |
Elliott Hughes | 31fa09c | 2015-02-04 19:38:28 -0800 | [diff] [blame] | 79 | #if !defined(_WIN32) |
Dan Albert | 98ff772 | 2015-03-13 22:39:54 -0700 | [diff] [blame^] | 80 | bool WriteStringToFile(const std::string& content, const std::string& path, |
| 81 | mode_t mode, uid_t owner, gid_t group) { |
| 82 | int fd = TEMP_FAILURE_RETRY( |
| 83 | open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW, |
| 84 | mode)); |
Elliott Hughes | 202f024 | 2015-02-04 13:19:13 -0800 | [diff] [blame] | 85 | if (fd == -1) { |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 86 | ALOGE("android::WriteStringToFile open failed: %s", strerror(errno)); |
Elliott Hughes | 202f024 | 2015-02-04 13:19:13 -0800 | [diff] [blame] | 87 | return false; |
| 88 | } |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 89 | |
Dan Albert | 98ff772 | 2015-03-13 22:39:54 -0700 | [diff] [blame^] | 90 | // We do an explicit fchmod here because we assume that the caller really |
| 91 | // meant what they said and doesn't want the umask-influenced mode. |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 92 | if (fchmod(fd, mode) == -1) { |
| 93 | ALOGE("android::WriteStringToFile fchmod failed: %s", strerror(errno)); |
| 94 | return CleanUpAfterFailedWrite(path); |
| 95 | } |
| 96 | if (fchown(fd, owner, group) == -1) { |
| 97 | ALOGE("android::WriteStringToFile fchown failed: %s", strerror(errno)); |
| 98 | return CleanUpAfterFailedWrite(path); |
| 99 | } |
| 100 | if (!WriteStringToFd(content, fd)) { |
| 101 | ALOGE("android::WriteStringToFile write failed: %s", strerror(errno)); |
| 102 | return CleanUpAfterFailedWrite(path); |
| 103 | } |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 104 | TEMP_FAILURE_RETRY(close(fd)); |
Elliott Hughes | 9d1f515 | 2015-02-17 10:16:04 -0800 | [diff] [blame] | 105 | return true; |
Elliott Hughes | 202f024 | 2015-02-04 13:19:13 -0800 | [diff] [blame] | 106 | } |
Elliott Hughes | 31fa09c | 2015-02-04 19:38:28 -0800 | [diff] [blame] | 107 | #endif |
Elliott Hughes | 202f024 | 2015-02-04 13:19:13 -0800 | [diff] [blame] | 108 | |
Dan Albert | 98ff772 | 2015-03-13 22:39:54 -0700 | [diff] [blame^] | 109 | bool WriteStringToFile(const std::string& content, const std::string& path) { |
| 110 | int fd = TEMP_FAILURE_RETRY( |
| 111 | open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW, |
| 112 | DEFFILEMODE)); |
Elliott Hughes | 202f024 | 2015-02-04 13:19:13 -0800 | [diff] [blame] | 113 | if (fd == -1) { |
| 114 | return false; |
| 115 | } |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 116 | |
| 117 | bool result = WriteStringToFd(content, fd); |
| 118 | TEMP_FAILURE_RETRY(close(fd)); |
| 119 | return result || CleanUpAfterFailedWrite(path); |
Elliott Hughes | 202f024 | 2015-02-04 13:19:13 -0800 | [diff] [blame] | 120 | } |
Dan Albert | 98ff772 | 2015-03-13 22:39:54 -0700 | [diff] [blame^] | 121 | |
| 122 | } // namespace base |
| 123 | } // namespace android |