blob: bfd1f55215d832683b97d5f89a053c6c8796efbc [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
17#include "utils/file.h"
18
19#include <errno.h>
20#include <fcntl.h>
21#include <sys/stat.h>
22#include <sys/types.h>
23
Elliott Hughesaf4885a2015-02-03 13:02:57 -080024#include <utils/Compat.h> // For TEMP_FAILURE_RETRY on Darwin.
25
Elliott Hughesdec12b22015-02-02 17:31:27 -080026bool android::ReadFileToString(const std::string& path, std::string* content) {
27 content->clear();
28
29 int fd = TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_CLOEXEC | O_NOFOLLOW));
30 if (fd == -1) {
31 return false;
32 }
33
34 while (true) {
35 char buf[BUFSIZ];
36 ssize_t n = TEMP_FAILURE_RETRY(read(fd, &buf[0], sizeof(buf)));
37 if (n == -1) {
38 TEMP_FAILURE_RETRY(close(fd));
39 return false;
40 }
41 if (n == 0) {
42 TEMP_FAILURE_RETRY(close(fd));
43 return true;
44 }
45 content->append(buf, n);
46 }
47}
48
Elliott Hughes202f0242015-02-04 13:19:13 -080049static bool WriteStringToFd(const std::string& content, int fd) {
Elliott Hughesdec12b22015-02-02 17:31:27 -080050 const char* p = content.data();
51 size_t left = content.size();
52 while (left > 0) {
53 ssize_t n = TEMP_FAILURE_RETRY(write(fd, p, left));
54 if (n == -1) {
55 TEMP_FAILURE_RETRY(close(fd));
56 return false;
57 }
58 p += n;
59 left -= n;
60 }
61 TEMP_FAILURE_RETRY(close(fd));
62 return true;
63}
Elliott Hughes202f0242015-02-04 13:19:13 -080064
65static bool CleanUpAfterFailedWrite(const std::string& path) {
66 // Something went wrong. Let's not leave a corrupt file lying around.
67 int saved_errno = errno;
68 unlink(path.c_str());
69 errno = saved_errno;
70 return false;
71}
72
73bool android::WriteStringToFile(const std::string& content, const std::string& path,
74 mode_t mode, uid_t owner, gid_t group) {
75 int fd = TEMP_FAILURE_RETRY(open(path.c_str(),
76 O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW,
77 mode));
78 if (fd == -1) {
79 return false;
80 }
81 // We do an explicit fchmod here because we assume that the caller really meant what they
82 // said and doesn't want the umask-influenced mode.
83 if (fchmod(fd, mode) != -1 && fchown(fd, owner, group) == -1 && WriteStringToFd(content, fd)) {
84 return true;
85 }
86 return CleanUpAfterFailedWrite(path);
87}
88
89bool android::WriteStringToFile(const std::string& content, const std::string& path) {
90 int fd = TEMP_FAILURE_RETRY(open(path.c_str(),
91 O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW,
92 DEFFILEMODE));
93 if (fd == -1) {
94 return false;
95 }
96 return WriteStringToFd(content, fd) || CleanUpAfterFailedWrite(path);
97}