blob: 773f33bd635afb4c63cddb2d1773d5446469031a [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 Albertc007bc32015-03-16 10:08:46 -070026#define LOG_TAG "base.file"
27#include "cutils/log.h"
28#include "utils/Compat.h" // For TEMP_FAILURE_RETRY on Darwin.
29
30namespace android {
31namespace base {
32
33bool ReadFdToString(int fd, std::string* content) {
Elliott Hughesf682b472015-02-06 12:19:48 -080034 content->clear();
35
36 char buf[BUFSIZ];
37 ssize_t n;
38 while ((n = TEMP_FAILURE_RETRY(read(fd, &buf[0], sizeof(buf)))) > 0) {
39 content->append(buf, n);
40 }
41 return (n == 0) ? true : false;
42}
43
Dan Albertc007bc32015-03-16 10:08:46 -070044bool ReadFileToString(const std::string& path, std::string* content) {
Elliott Hughesdec12b22015-02-02 17:31:27 -080045 content->clear();
46
Dan Albertc007bc32015-03-16 10:08:46 -070047 int fd =
48 TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_CLOEXEC | O_NOFOLLOW));
Elliott Hughesdec12b22015-02-02 17:31:27 -080049 if (fd == -1) {
50 return false;
51 }
Elliott Hughesf682b472015-02-06 12:19:48 -080052 bool result = ReadFdToString(fd, content);
53 TEMP_FAILURE_RETRY(close(fd));
54 return result;
Elliott Hughesdec12b22015-02-02 17:31:27 -080055}
56
Dan Albertc007bc32015-03-16 10:08:46 -070057bool WriteStringToFd(const std::string& content, int fd) {
Elliott Hughesdec12b22015-02-02 17:31:27 -080058 const char* p = content.data();
59 size_t left = content.size();
60 while (left > 0) {
61 ssize_t n = TEMP_FAILURE_RETRY(write(fd, p, left));
62 if (n == -1) {
Elliott Hughesdec12b22015-02-02 17:31:27 -080063 return false;
64 }
65 p += n;
66 left -= n;
67 }
Elliott Hughesdec12b22015-02-02 17:31:27 -080068 return true;
69}
Elliott Hughes202f0242015-02-04 13:19:13 -080070
71static bool CleanUpAfterFailedWrite(const std::string& path) {
72 // Something went wrong. Let's not leave a corrupt file lying around.
73 int saved_errno = errno;
74 unlink(path.c_str());
75 errno = saved_errno;
76 return false;
77}
78
Elliott Hughes31fa09c2015-02-04 19:38:28 -080079#if !defined(_WIN32)
Dan Albertc007bc32015-03-16 10:08:46 -070080bool WriteStringToFile(const std::string& content, const std::string& path,
81 mode_t mode, uid_t owner, gid_t group) {
82 int fd = TEMP_FAILURE_RETRY(
83 open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW,
84 mode));
Elliott Hughes202f0242015-02-04 13:19:13 -080085 if (fd == -1) {
Elliott Hughes9d1f5152015-02-17 10:16:04 -080086 ALOGE("android::WriteStringToFile open failed: %s", strerror(errno));
Elliott Hughes202f0242015-02-04 13:19:13 -080087 return false;
88 }
Elliott Hughesf682b472015-02-06 12:19:48 -080089
Dan Albertc007bc32015-03-16 10:08:46 -070090 // We do an explicit fchmod here because we assume that the caller really
91 // meant what they said and doesn't want the umask-influenced mode.
Elliott Hughes9d1f5152015-02-17 10:16:04 -080092 if (fchmod(fd, mode) == -1) {
93 ALOGE("android::WriteStringToFile fchmod failed: %s", strerror(errno));
94 return CleanUpAfterFailedWrite(path);
95 }
96 if (fchown(fd, owner, group) == -1) {
97 ALOGE("android::WriteStringToFile fchown failed: %s", strerror(errno));
98 return CleanUpAfterFailedWrite(path);
99 }
100 if (!WriteStringToFd(content, fd)) {
101 ALOGE("android::WriteStringToFile write failed: %s", strerror(errno));
102 return CleanUpAfterFailedWrite(path);
103 }
Elliott Hughesf682b472015-02-06 12:19:48 -0800104 TEMP_FAILURE_RETRY(close(fd));
Elliott Hughes9d1f5152015-02-17 10:16:04 -0800105 return true;
Elliott Hughes202f0242015-02-04 13:19:13 -0800106}
Elliott Hughes31fa09c2015-02-04 19:38:28 -0800107#endif
Elliott Hughes202f0242015-02-04 13:19:13 -0800108
Dan Albertc007bc32015-03-16 10:08:46 -0700109bool WriteStringToFile(const std::string& content, const std::string& path) {
110 int fd = TEMP_FAILURE_RETRY(
111 open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW,
112 DEFFILEMODE));
Elliott Hughes202f0242015-02-04 13:19:13 -0800113 if (fd == -1) {
114 return false;
115 }
Elliott Hughesf682b472015-02-06 12:19:48 -0800116
117 bool result = WriteStringToFd(content, fd);
118 TEMP_FAILURE_RETRY(close(fd));
119 return result || CleanUpAfterFailedWrite(path);
Elliott Hughes202f0242015-02-04 13:19:13 -0800120}
Dan Albertc007bc32015-03-16 10:08:46 -0700121
122} // namespace base
123} // namespace android