blob: e1ed8747fbf4cfde2a6fd9f9cf755a82134220e3 [file] [log] [blame]
adlr@google.com3defe6a2009-12-04 20:57:17 +00001// Copyright (c) 2009 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "update_engine/utils.h"
6#include <sys/mount.h>
7#include <sys/stat.h>
8#include <sys/types.h>
9#include <dirent.h>
10#include <errno.h>
Andrew de los Reyes970bb282009-12-09 16:34:04 -080011#include <fcntl.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000012#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15#include <unistd.h>
16#include <algorithm>
17#include "chromeos/obsolete_logging.h"
Andrew de los Reyes970bb282009-12-09 16:34:04 -080018#include "update_engine/file_writer.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000019
20using std::min;
21using std::string;
22using std::vector;
23
24namespace chromeos_update_engine {
25
26namespace utils {
27
Andrew de los Reyes970bb282009-12-09 16:34:04 -080028bool WriteFile(const char* path, const char* data, int data_len) {
29 DirectFileWriter writer;
30 TEST_AND_RETURN_FALSE_ERRNO(0 == writer.Open(path,
31 O_WRONLY | O_CREAT | O_TRUNC,
32 0666));
33 ScopedFileWriterCloser closer(&writer);
34 TEST_AND_RETURN_FALSE_ERRNO(data_len == writer.Write(data, data_len));
35 return true;
36}
37
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070038bool WriteAll(int fd, const void *buf, size_t count) {
39 const char* c_buf = static_cast<const char*>(buf);
40 ssize_t bytes_written = 0;
41 while (bytes_written < static_cast<ssize_t>(count)) {
42 ssize_t rc = write(fd, c_buf + bytes_written, count - bytes_written);
43 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0);
44 bytes_written += rc;
45 }
46 return true;
47}
48
adlr@google.com3defe6a2009-12-04 20:57:17 +000049bool ReadFile(const std::string& path, std::vector<char>* out) {
50 CHECK(out);
51 FILE* fp = fopen(path.c_str(), "r");
52 if (!fp)
53 return false;
54 const size_t kChunkSize = 1024;
55 size_t read_size;
56 do {
57 char buf[kChunkSize];
58 read_size = fread(buf, 1, kChunkSize, fp);
59 if (read_size == 0)
60 break;
61 out->insert(out->end(), buf, buf + read_size);
62 } while (read_size == kChunkSize);
63 bool success = !ferror(fp);
64 TEST_AND_RETURN_FALSE_ERRNO(fclose(fp) == 0);
65 return success;
66}
67
68bool ReadFileToString(const std::string& path, std::string* out) {
69 vector<char> data;
70 bool success = ReadFile(path, &data);
71 if (!success) {
72 return false;
73 }
74 (*out) = string(&data[0], data.size());
75 return true;
76}
77
78void HexDumpArray(const unsigned char* const arr, const size_t length) {
79 const unsigned char* const char_arr =
80 reinterpret_cast<const unsigned char* const>(arr);
81 LOG(INFO) << "Logging array of length: " << length;
82 const unsigned int bytes_per_line = 16;
83 for (size_t i = 0; i < length; i += bytes_per_line) {
84 const unsigned int bytes_remaining = length - i;
85 const unsigned int bytes_per_this_line = min(bytes_per_line,
86 bytes_remaining);
87 char header[100];
88 int r = snprintf(header, sizeof(header), "0x%08x : ", i);
89 TEST_AND_RETURN(r == 13);
90 string line = header;
91 for (unsigned int j = 0; j < bytes_per_this_line; j++) {
92 char buf[20];
93 unsigned char c = char_arr[i + j];
94 r = snprintf(buf, sizeof(buf), "%02x ", static_cast<unsigned int>(c));
95 TEST_AND_RETURN(r == 3);
96 line += buf;
97 }
98 LOG(INFO) << line;
99 }
100}
101
102namespace {
103class ScopedDirCloser {
104 public:
105 explicit ScopedDirCloser(DIR** dir) : dir_(dir) {}
106 ~ScopedDirCloser() {
107 if (dir_ && *dir_) {
108 int r = closedir(*dir_);
109 TEST_AND_RETURN_ERRNO(r == 0);
110 *dir_ = NULL;
111 dir_ = NULL;
112 }
113 }
114 private:
115 DIR** dir_;
116};
117} // namespace {}
118
119bool RecursiveUnlinkDir(const std::string& path) {
120 struct stat stbuf;
121 int r = lstat(path.c_str(), &stbuf);
122 TEST_AND_RETURN_FALSE_ERRNO((r == 0) || (errno == ENOENT));
123 if ((r < 0) && (errno == ENOENT))
124 // path request is missing. that's fine.
125 return true;
126 if (!S_ISDIR(stbuf.st_mode)) {
127 TEST_AND_RETURN_FALSE_ERRNO((unlink(path.c_str()) == 0) ||
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700128 (errno == ENOENT));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000129 // success or path disappeared before we could unlink.
130 return true;
131 }
132 {
133 // We have a dir, unlink all children, then delete dir
134 DIR *dir = opendir(path.c_str());
135 TEST_AND_RETURN_FALSE_ERRNO(dir);
136 ScopedDirCloser dir_closer(&dir);
137 struct dirent dir_entry;
138 struct dirent *dir_entry_p;
139 int err = 0;
140 while ((err = readdir_r(dir, &dir_entry, &dir_entry_p)) == 0) {
141 if (dir_entry_p == NULL) {
142 // end of stream reached
143 break;
144 }
145 // Skip . and ..
146 if (!strcmp(dir_entry_p->d_name, ".") ||
147 !strcmp(dir_entry_p->d_name, ".."))
148 continue;
149 TEST_AND_RETURN_FALSE(RecursiveUnlinkDir(path + "/" +
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700150 dir_entry_p->d_name));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000151 }
152 TEST_AND_RETURN_FALSE(err == 0);
153 }
154 // unlink dir
155 TEST_AND_RETURN_FALSE_ERRNO((rmdir(path.c_str()) == 0) || (errno == ENOENT));
156 return true;
157}
158
159std::string ErrnoNumberAsString(int err) {
160 char buf[100];
161 buf[0] = '\0';
162 return strerror_r(err, buf, sizeof(buf));
163}
164
165std::string NormalizePath(const std::string& path, bool strip_trailing_slash) {
166 string ret;
167 bool last_insert_was_slash = false;
168 for (string::const_iterator it = path.begin(); it != path.end(); ++it) {
169 if (*it == '/') {
170 if (last_insert_was_slash)
171 continue;
172 last_insert_was_slash = true;
173 } else {
174 last_insert_was_slash = false;
175 }
176 ret.push_back(*it);
177 }
178 if (strip_trailing_slash && last_insert_was_slash) {
179 string::size_type last_non_slash = ret.find_last_not_of('/');
180 if (last_non_slash != string::npos) {
181 ret.resize(last_non_slash + 1);
182 } else {
183 ret = "";
184 }
185 }
186 return ret;
187}
188
189bool FileExists(const char* path) {
190 struct stat stbuf;
191 return 0 == lstat(path, &stbuf);
192}
193
194std::string TempFilename(string path) {
195 static const string suffix("XXXXXX");
196 CHECK(StringHasSuffix(path, suffix));
197 do {
198 string new_suffix;
199 for (unsigned int i = 0; i < suffix.size(); i++) {
200 int r = rand() % (26 * 2 + 10); // [a-zA-Z0-9]
201 if (r < 26)
202 new_suffix.append(1, 'a' + r);
203 else if (r < (26 * 2))
204 new_suffix.append(1, 'A' + r - 26);
205 else
206 new_suffix.append(1, '0' + r - (26 * 2));
207 }
208 CHECK_EQ(new_suffix.size(), suffix.size());
209 path.resize(path.size() - new_suffix.size());
210 path.append(new_suffix);
211 } while (FileExists(path.c_str()));
212 return path;
213}
214
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700215bool MakeTempFile(const std::string& filename_template,
216 std::string* filename,
217 int* fd) {
218 DCHECK(filename || fd);
219 vector<char> buf(filename_template.size() + 1);
220 memcpy(&buf[0], filename_template.data(), filename_template.size());
221 buf[filename_template.size()] = '\0';
222
223 int mkstemp_fd = mkstemp(&buf[0]);
224 TEST_AND_RETURN_FALSE_ERRNO(mkstemp_fd >= 0);
225 if (filename) {
226 *filename = &buf[0];
227 }
228 if (fd) {
229 *fd = mkstemp_fd;
230 } else {
231 close(mkstemp_fd);
232 }
233 return true;
234}
235
adlr@google.com3defe6a2009-12-04 20:57:17 +0000236bool StringHasSuffix(const std::string& str, const std::string& suffix) {
237 if (suffix.size() > str.size())
238 return false;
239 return 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix);
240}
241
242bool StringHasPrefix(const std::string& str, const std::string& prefix) {
243 if (prefix.size() > str.size())
244 return false;
245 return 0 == str.compare(0, prefix.size(), prefix);
246}
247
248const std::string BootDevice() {
249 string proc_cmdline;
250 if (!ReadFileToString("/proc/cmdline", &proc_cmdline))
251 return "";
252 // look for "root=" in the command line
253 string::size_type pos = 0;
254 if (!StringHasPrefix(proc_cmdline, "root=")) {
255 pos = proc_cmdline.find(" root=") + 1;
256 }
257 if (pos == string::npos) {
258 // can't find root=
259 return "";
260 }
261 // at this point, pos is the point in the string where "root=" starts
262 string ret;
263 pos += strlen("root="); // advance to the device name itself
264 while (pos < proc_cmdline.size()) {
265 char c = proc_cmdline[pos];
266 if (c == ' ')
267 break;
268 ret += c;
269 pos++;
270 }
271 return ret;
272 // TODO(adlr): use findfs to figure out UUID= or LABEL= filesystems
273}
274
275bool MountFilesystem(const string& device,
276 const string& mountpoint) {
277 int rc = mount(device.c_str(), mountpoint.c_str(), "ext3", 0, NULL);
278 if (rc < 0) {
279 string msg = ErrnoNumberAsString(errno);
280 LOG(ERROR) << "Unable to mount destination device: " << msg << ". "
281 << device << " on " << mountpoint;
282 return false;
283 }
284 return true;
285}
286
287bool UnmountFilesystem(const string& mountpoint) {
288 TEST_AND_RETURN_FALSE_ERRNO(umount(mountpoint.c_str()) == 0);
289 return true;
290}
291
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800292const char* const kStatefulPartition = "/mnt/stateful_partition";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000293
294} // namespace utils
295
296} // namespace chromeos_update_engine
297