1 /*
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 "base/file.h"
18 
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 
24 #include <string>
25 
26 #include "base/macros.h"  // For TEMP_FAILURE_RETRY on Darwin.
27 #define LOG_TAG "base.file"
28 #include "cutils/log.h"
29 #include "utils/Compat.h"
30 
31 namespace android {
32 namespace base {
33 
ReadFdToString(int fd,std::string * content)34 bool ReadFdToString(int fd, std::string* content) {
35   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 
ReadFileToString(const std::string & path,std::string * content)45 bool ReadFileToString(const std::string& path, std::string* content) {
46   content->clear();
47 
48   int fd =
49       TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_CLOEXEC | O_NOFOLLOW));
50   if (fd == -1) {
51     return false;
52   }
53   bool result = ReadFdToString(fd, content);
54   close(fd);
55   return result;
56 }
57 
WriteStringToFd(const std::string & content,int fd)58 bool WriteStringToFd(const std::string& content, int fd) {
59   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) {
64       return false;
65     }
66     p += n;
67     left -= n;
68   }
69   return true;
70 }
71 
CleanUpAfterFailedWrite(const std::string & path)72 static 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 
80 #if !defined(_WIN32)
WriteStringToFile(const std::string & content,const std::string & path,mode_t mode,uid_t owner,gid_t group)81 bool 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));
86   if (fd == -1) {
87     ALOGE("android::WriteStringToFile open failed: %s", strerror(errno));
88     return false;
89   }
90 
91   // 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.
93   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   }
105   close(fd);
106   return true;
107 }
108 #endif
109 
WriteStringToFile(const std::string & content,const std::string & path)110 bool 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));
114   if (fd == -1) {
115     return false;
116   }
117 
118   bool result = WriteStringToFd(content, fd);
119   close(fd);
120   return result || CleanUpAfterFailedWrite(path);
121 }
122 
ReadFully(int fd,void * data,size_t byte_count)123 bool 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 
WriteFully(int fd,const void * data,size_t byte_count)135 bool 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 
147 }  // namespace base
148 }  // namespace android
149