blob: da1adba1979922f6777c9581c5fc1bc53b99af49 [file] [log] [blame]
Dan Albertaac6b7c2015-03-16 10:08:46 -07001/*
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 Hughesb6351622015-12-04 22:00:26 -080017#include "android-base/file.h"
Dan Albertaac6b7c2015-03-16 10:08:46 -070018
19#include <errno.h>
20#include <fcntl.h>
21#include <sys/stat.h>
22#include <sys/types.h>
23
24#include <string>
25
Elliott Hughesb6351622015-12-04 22:00:26 -080026#include "android-base/macros.h" // For TEMP_FAILURE_RETRY on Darwin.
27#include "android-base/utf8.h"
Dan Albertaac6b7c2015-03-16 10:08:46 -070028#define LOG_TAG "base.file"
29#include "cutils/log.h"
Dan Albert5f770222015-03-26 23:33:28 -070030#include "utils/Compat.h"
Dan Albertaac6b7c2015-03-16 10:08:46 -070031
32namespace android {
33namespace base {
34
Elliott Hughes774d7f62015-11-11 18:02:29 +000035// Versions of standard library APIs that support UTF-8 strings.
36using namespace android::base::utf8;
37
Dan Albertaac6b7c2015-03-16 10:08:46 -070038bool ReadFdToString(int fd, std::string* content) {
39 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
49bool ReadFileToString(const std::string& path, std::string* content) {
50 content->clear();
51
Elliott Hughesd5f8ae62015-09-01 13:35:44 -070052 int fd = TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_BINARY));
Dan Albertaac6b7c2015-03-16 10:08:46 -070053 if (fd == -1) {
54 return false;
55 }
56 bool result = ReadFdToString(fd, content);
Nick Kralevich82921302015-05-20 08:59:21 -070057 close(fd);
Dan Albertaac6b7c2015-03-16 10:08:46 -070058 return result;
59}
60
61bool WriteStringToFd(const std::string& content, int fd) {
62 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) {
67 return false;
68 }
69 p += n;
70 left -= n;
71 }
72 return true;
73}
74
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
83#if !defined(_WIN32)
84bool WriteStringToFile(const std::string& content, const std::string& path,
85 mode_t mode, uid_t owner, gid_t group) {
Elliott Hughesd5f8ae62015-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));
Dan Albertaac6b7c2015-03-16 10:08:46 -070088 if (fd == -1) {
89 ALOGE("android::WriteStringToFile open failed: %s", strerror(errno));
90 return false;
91 }
92
93 // 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.
95 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 Kralevich82921302015-05-20 08:59:21 -0700107 close(fd);
Dan Albertaac6b7c2015-03-16 10:08:46 -0700108 return true;
109}
110#endif
111
112bool WriteStringToFile(const std::string& content, const std::string& path) {
Elliott Hughesd5f8ae62015-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));
Dan Albertaac6b7c2015-03-16 10:08:46 -0700115 if (fd == -1) {
116 return false;
117 }
118
119 bool result = WriteStringToFd(content, fd);
Nick Kralevich82921302015-05-20 08:59:21 -0700120 close(fd);
Dan Albertaac6b7c2015-03-16 10:08:46 -0700121 return result || CleanUpAfterFailedWrite(path);
122}
123
Elliott Hughes20abc872015-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
Yabin Cui8f6a5a02016-01-29 17:25:54 -0800148bool RemoveFileIfExists(const std::string& path, std::string* err) {
149 struct stat st;
150#if defined(_WIN32)
151 //TODO: Windows version can't handle symbol link correctly.
152 int result = stat(path.c_str(), &st);
153 bool file_type_removable = (result == 0 && S_ISREG(st.st_mode));
154#else
155 int result = lstat(path.c_str(), &st);
156 bool file_type_removable = (result == 0 && (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)));
157#endif
158 if (result == 0) {
159 if (!file_type_removable) {
160 if (err != nullptr) {
161 *err = "is not a regular or symbol link file";
162 }
163 return false;
164 }
165 if (unlink(path.c_str()) == -1) {
166 if (err != nullptr) {
167 *err = strerror(errno);
168 }
169 return false;
170 }
171 }
172 return true;
173}
174
Dan Albertaac6b7c2015-03-16 10:08:46 -0700175} // namespace base
176} // namespace android