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