blob: 9a340b7f84f561a29e9c53026ae20fb616a14694 [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
31namespace android {
32namespace base {
33
34bool ReadFdToString(int fd, std::string* content) {
Elliott Hughesf682b472015-02-06 12:19:48 -080035 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 Albertc007bc32015-03-16 10:08:46 -070045bool ReadFileToString(const std::string& path, std::string* content) {
Elliott Hughesdec12b22015-02-02 17:31:27 -080046 content->clear();
47
Dan Albertc007bc32015-03-16 10:08:46 -070048 int fd =
49 TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_CLOEXEC | O_NOFOLLOW));
Elliott Hughesdec12b22015-02-02 17:31:27 -080050 if (fd == -1) {
51 return false;
52 }
Elliott Hughesf682b472015-02-06 12:19:48 -080053 bool result = ReadFdToString(fd, content);
Nick Kralevich95db36e2015-05-20 08:59:21 -070054 close(fd);
Elliott Hughesf682b472015-02-06 12:19:48 -080055 return result;
Elliott Hughesdec12b22015-02-02 17:31:27 -080056}
57
Dan Albertc007bc32015-03-16 10:08:46 -070058bool WriteStringToFd(const std::string& content, int fd) {
Elliott Hughesdec12b22015-02-02 17:31:27 -080059 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 Hughesdec12b22015-02-02 17:31:27 -080064 return false;
65 }
66 p += n;
67 left -= n;
68 }
Elliott Hughesdec12b22015-02-02 17:31:27 -080069 return true;
70}
Elliott Hughes202f0242015-02-04 13:19:13 -080071
72static 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 Hughes31fa09c2015-02-04 19:38:28 -080080#if !defined(_WIN32)
Dan Albertc007bc32015-03-16 10:08:46 -070081bool 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 Hughes202f0242015-02-04 13:19:13 -080086 if (fd == -1) {
Elliott Hughes9d1f5152015-02-17 10:16:04 -080087 ALOGE("android::WriteStringToFile open failed: %s", strerror(errno));
Elliott Hughes202f0242015-02-04 13:19:13 -080088 return false;
89 }
Elliott Hughesf682b472015-02-06 12:19:48 -080090
Dan Albertc007bc32015-03-16 10:08:46 -070091 // 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 Hughes9d1f5152015-02-17 10:16:04 -080093 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 Kralevich95db36e2015-05-20 08:59:21 -0700105 close(fd);
Elliott Hughes9d1f5152015-02-17 10:16:04 -0800106 return true;
Elliott Hughes202f0242015-02-04 13:19:13 -0800107}
Elliott Hughes31fa09c2015-02-04 19:38:28 -0800108#endif
Elliott Hughes202f0242015-02-04 13:19:13 -0800109
Dan Albertc007bc32015-03-16 10:08:46 -0700110bool 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 Hughes202f0242015-02-04 13:19:13 -0800114 if (fd == -1) {
115 return false;
116 }
Elliott Hughesf682b472015-02-06 12:19:48 -0800117
118 bool result = WriteStringToFd(content, fd);
Nick Kralevich95db36e2015-05-20 08:59:21 -0700119 close(fd);
Elliott Hughesf682b472015-02-06 12:19:48 -0800120 return result || CleanUpAfterFailedWrite(path);
Elliott Hughes202f0242015-02-04 13:19:13 -0800121}
Dan Albertc007bc32015-03-16 10:08:46 -0700122
Elliott Hughes56085ed2015-04-24 21:57:16 -0700123bool 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
135bool 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 Albertc007bc32015-03-16 10:08:46 -0700147} // namespace base
148} // namespace android