blob: 3468dcfbf789fbd751a7f63f05ecd987b8497e43 [file] [log] [blame]
Elliott Hughesdec12b22015-02-02 17:31:27 -08001/*
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 Albertc007bc32015-03-16 10:08:46 -070017#include "base/file.h"
Elliott Hughesdec12b22015-02-02 17:31:27 -080018
19#include <errno.h>
20#include <fcntl.h>
21#include <sys/stat.h>
22#include <sys/types.h>
23
Dan Albertc007bc32015-03-16 10:08:46 -070024#include <string>
Elliott Hughesaf4885a2015-02-03 13:02:57 -080025
Dan Albert76d9cad2015-03-16 10:09:07 -070026#include "base/macros.h" // For TEMP_FAILURE_RETRY on Darwin.
Dan Albertc007bc32015-03-16 10:08:46 -070027#define LOG_TAG "base.file"
28#include "cutils/log.h"
Dan Albert94d13602015-03-26 23:33:28 -070029#include "utils/Compat.h"
Dan Albertc007bc32015-03-16 10:08:46 -070030
Elliott Hughes470d79a2015-09-01 13:35:44 -070031#if !defined(_WIN32)
32#define O_BINARY 0
33#endif
34
Dan Albertc007bc32015-03-16 10:08:46 -070035namespace android {
36namespace base {
37
38bool ReadFdToString(int fd, std::string* content) {
Elliott Hughesf682b472015-02-06 12:19:48 -080039 content->clear();
40
41 char buf[BUFSIZ];
42 ssize_t n;
43 while ((n = TEMP_FAILURE_RETRY(read(fd, &buf[0], sizeof(buf)))) > 0) {
44 content->append(buf, n);
45 }
46 return (n == 0) ? true : false;
47}
48
Dan Albertc007bc32015-03-16 10:08:46 -070049bool ReadFileToString(const std::string& path, std::string* content) {
Elliott Hughesdec12b22015-02-02 17:31:27 -080050 content->clear();
51
Elliott Hughes470d79a2015-09-01 13:35:44 -070052 int fd = TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_BINARY));
Elliott Hughesdec12b22015-02-02 17:31:27 -080053 if (fd == -1) {
54 return false;
55 }
Elliott Hughesf682b472015-02-06 12:19:48 -080056 bool result = ReadFdToString(fd, content);
Nick Kralevich95db36e2015-05-20 08:59:21 -070057 close(fd);
Elliott Hughesf682b472015-02-06 12:19:48 -080058 return result;
Elliott Hughesdec12b22015-02-02 17:31:27 -080059}
60
Dan Albertc007bc32015-03-16 10:08:46 -070061bool WriteStringToFd(const std::string& content, int fd) {
Elliott Hughesdec12b22015-02-02 17:31:27 -080062 const char* p = content.data();
63 size_t left = content.size();
64 while (left > 0) {
65 ssize_t n = TEMP_FAILURE_RETRY(write(fd, p, left));
66 if (n == -1) {
Elliott Hughesdec12b22015-02-02 17:31:27 -080067 return false;
68 }
69 p += n;
70 left -= n;
71 }
Elliott Hughesdec12b22015-02-02 17:31:27 -080072 return true;
73}
Elliott Hughes202f0242015-02-04 13:19:13 -080074
75static bool CleanUpAfterFailedWrite(const std::string& path) {
76 // Something went wrong. Let's not leave a corrupt file lying around.
77 int saved_errno = errno;
78 unlink(path.c_str());
79 errno = saved_errno;
80 return false;
81}
82
Elliott Hughes31fa09c2015-02-04 19:38:28 -080083#if !defined(_WIN32)
Dan Albertc007bc32015-03-16 10:08:46 -070084bool WriteStringToFile(const std::string& content, const std::string& path,
85 mode_t mode, uid_t owner, gid_t group) {
Elliott Hughes470d79a2015-09-01 13:35:44 -070086 int flags = O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW | O_BINARY;
87 int fd = TEMP_FAILURE_RETRY(open(path.c_str(), flags, mode));
Elliott Hughes202f0242015-02-04 13:19:13 -080088 if (fd == -1) {
Elliott Hughes9d1f5152015-02-17 10:16:04 -080089 ALOGE("android::WriteStringToFile open failed: %s", strerror(errno));
Elliott Hughes202f0242015-02-04 13:19:13 -080090 return false;
91 }
Elliott Hughesf682b472015-02-06 12:19:48 -080092
Dan Albertc007bc32015-03-16 10:08:46 -070093 // We do an explicit fchmod here because we assume that the caller really
94 // meant what they said and doesn't want the umask-influenced mode.
Elliott Hughes9d1f5152015-02-17 10:16:04 -080095 if (fchmod(fd, mode) == -1) {
96 ALOGE("android::WriteStringToFile fchmod failed: %s", strerror(errno));
97 return CleanUpAfterFailedWrite(path);
98 }
99 if (fchown(fd, owner, group) == -1) {
100 ALOGE("android::WriteStringToFile fchown failed: %s", strerror(errno));
101 return CleanUpAfterFailedWrite(path);
102 }
103 if (!WriteStringToFd(content, fd)) {
104 ALOGE("android::WriteStringToFile write failed: %s", strerror(errno));
105 return CleanUpAfterFailedWrite(path);
106 }
Nick Kralevich95db36e2015-05-20 08:59:21 -0700107 close(fd);
Elliott Hughes9d1f5152015-02-17 10:16:04 -0800108 return true;
Elliott Hughes202f0242015-02-04 13:19:13 -0800109}
Elliott Hughes31fa09c2015-02-04 19:38:28 -0800110#endif
Elliott Hughes202f0242015-02-04 13:19:13 -0800111
Dan Albertc007bc32015-03-16 10:08:46 -0700112bool WriteStringToFile(const std::string& content, const std::string& path) {
Elliott Hughes470d79a2015-09-01 13:35:44 -0700113 int flags = O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW | O_BINARY;
114 int fd = TEMP_FAILURE_RETRY(open(path.c_str(), flags, DEFFILEMODE));
Elliott Hughes202f0242015-02-04 13:19:13 -0800115 if (fd == -1) {
116 return false;
117 }
Elliott Hughesf682b472015-02-06 12:19:48 -0800118
119 bool result = WriteStringToFd(content, fd);
Nick Kralevich95db36e2015-05-20 08:59:21 -0700120 close(fd);
Elliott Hughesf682b472015-02-06 12:19:48 -0800121 return result || CleanUpAfterFailedWrite(path);
Elliott Hughes202f0242015-02-04 13:19:13 -0800122}
Dan Albertc007bc32015-03-16 10:08:46 -0700123
Elliott Hughes56085ed2015-04-24 21:57:16 -0700124bool ReadFully(int fd, void* data, size_t byte_count) {
125 uint8_t* p = reinterpret_cast<uint8_t*>(data);
126 size_t remaining = byte_count;
127 while (remaining > 0) {
128 ssize_t n = TEMP_FAILURE_RETRY(read(fd, p, remaining));
129 if (n <= 0) return false;
130 p += n;
131 remaining -= n;
132 }
133 return true;
134}
135
136bool WriteFully(int fd, const void* data, size_t byte_count) {
137 const uint8_t* p = reinterpret_cast<const uint8_t*>(data);
138 size_t remaining = byte_count;
139 while (remaining > 0) {
140 ssize_t n = TEMP_FAILURE_RETRY(write(fd, p, remaining));
141 if (n == -1) return false;
142 p += n;
143 remaining -= n;
144 }
145 return true;
146}
147
Dan Albertc007bc32015-03-16 10:08:46 -0700148} // namespace base
149} // namespace android