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