blob: 0690bc24149aa3ea7b58dd18ca03c2a127fb49f2 [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
Elliott Hughes9d1f5152015-02-17 10:16:04 -080017#define LOG_TAG "utils.file"
18#include <cutils/log.h>
19
Elliott Hughesdec12b22015-02-02 17:31:27 -080020#include "utils/file.h"
21
22#include <errno.h>
23#include <fcntl.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
Elliott Hughesaf4885a2015-02-03 13:02:57 -080027#include <utils/Compat.h> // For TEMP_FAILURE_RETRY on Darwin.
28
Elliott Hughesf682b472015-02-06 12:19:48 -080029bool android::ReadFdToString(int fd, std::string* content) {
30 content->clear();
31
32 char buf[BUFSIZ];
33 ssize_t n;
34 while ((n = TEMP_FAILURE_RETRY(read(fd, &buf[0], sizeof(buf)))) > 0) {
35 content->append(buf, n);
36 }
37 return (n == 0) ? true : false;
38}
39
Elliott Hughesdec12b22015-02-02 17:31:27 -080040bool android::ReadFileToString(const std::string& path, std::string* content) {
41 content->clear();
42
43 int fd = TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_CLOEXEC | O_NOFOLLOW));
44 if (fd == -1) {
45 return false;
46 }
Elliott Hughesf682b472015-02-06 12:19:48 -080047 bool result = ReadFdToString(fd, content);
48 TEMP_FAILURE_RETRY(close(fd));
49 return result;
Elliott Hughesdec12b22015-02-02 17:31:27 -080050}
51
Elliott Hughesf682b472015-02-06 12:19:48 -080052bool android::WriteStringToFd(const std::string& content, int fd) {
Elliott Hughesdec12b22015-02-02 17:31:27 -080053 const char* p = content.data();
54 size_t left = content.size();
55 while (left > 0) {
56 ssize_t n = TEMP_FAILURE_RETRY(write(fd, p, left));
57 if (n == -1) {
Elliott Hughesdec12b22015-02-02 17:31:27 -080058 return false;
59 }
60 p += n;
61 left -= n;
62 }
Elliott Hughesdec12b22015-02-02 17:31:27 -080063 return true;
64}
Elliott Hughes202f0242015-02-04 13:19:13 -080065
66static bool CleanUpAfterFailedWrite(const std::string& path) {
67 // Something went wrong. Let's not leave a corrupt file lying around.
68 int saved_errno = errno;
69 unlink(path.c_str());
70 errno = saved_errno;
71 return false;
72}
73
Elliott Hughes31fa09c2015-02-04 19:38:28 -080074#if !defined(_WIN32)
Elliott Hughes202f0242015-02-04 13:19:13 -080075bool android::WriteStringToFile(const std::string& content, const std::string& path,
76 mode_t mode, uid_t owner, gid_t group) {
77 int fd = TEMP_FAILURE_RETRY(open(path.c_str(),
78 O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW,
79 mode));
80 if (fd == -1) {
Elliott Hughes9d1f5152015-02-17 10:16:04 -080081 ALOGE("android::WriteStringToFile open failed: %s", strerror(errno));
Elliott Hughes202f0242015-02-04 13:19:13 -080082 return false;
83 }
Elliott Hughesf682b472015-02-06 12:19:48 -080084
Elliott Hughes202f0242015-02-04 13:19:13 -080085 // We do an explicit fchmod here because we assume that the caller really meant what they
86 // said and doesn't want the umask-influenced mode.
Elliott Hughes9d1f5152015-02-17 10:16:04 -080087 if (fchmod(fd, mode) == -1) {
88 ALOGE("android::WriteStringToFile fchmod failed: %s", strerror(errno));
89 return CleanUpAfterFailedWrite(path);
90 }
91 if (fchown(fd, owner, group) == -1) {
92 ALOGE("android::WriteStringToFile fchown failed: %s", strerror(errno));
93 return CleanUpAfterFailedWrite(path);
94 }
95 if (!WriteStringToFd(content, fd)) {
96 ALOGE("android::WriteStringToFile write failed: %s", strerror(errno));
97 return CleanUpAfterFailedWrite(path);
98 }
Elliott Hughesf682b472015-02-06 12:19:48 -080099 TEMP_FAILURE_RETRY(close(fd));
Elliott Hughes9d1f5152015-02-17 10:16:04 -0800100 return true;
Elliott Hughes202f0242015-02-04 13:19:13 -0800101}
Elliott Hughes31fa09c2015-02-04 19:38:28 -0800102#endif
Elliott Hughes202f0242015-02-04 13:19:13 -0800103
104bool android::WriteStringToFile(const std::string& content, const std::string& path) {
105 int fd = TEMP_FAILURE_RETRY(open(path.c_str(),
106 O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW,
107 DEFFILEMODE));
108 if (fd == -1) {
109 return false;
110 }
Elliott Hughesf682b472015-02-06 12:19:48 -0800111
112 bool result = WriteStringToFd(content, fd);
113 TEMP_FAILURE_RETRY(close(fd));
114 return result || CleanUpAfterFailedWrite(path);
Elliott Hughes202f0242015-02-04 13:19:13 -0800115}