blob: 85d9e7db559d884414c7016d8fb2f763b08feffb [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 Reyes09e56d62010-04-23 13:45:53 -070038bool WriteAll(int fd, const void* buf, size_t count) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070039 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
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070049bool PWriteAll(int fd, const void* buf, size_t count, off_t offset) {
50 const char* c_buf = static_cast<const char*>(buf);
51 ssize_t bytes_written = 0;
52 while (bytes_written < static_cast<ssize_t>(count)) {
53 ssize_t rc = pwrite(fd, c_buf + bytes_written, count - bytes_written,
54 offset + bytes_written);
55 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0);
56 bytes_written += rc;
57 }
58 return true;
59}
60
61bool PReadAll(int fd, void* buf, size_t count, off_t offset,
62 ssize_t* out_bytes_read) {
63 char* c_buf = static_cast<char*>(buf);
64 ssize_t bytes_read = 0;
65 while (bytes_read < static_cast<ssize_t>(count)) {
66 ssize_t rc = pread(fd, c_buf + bytes_read, count - bytes_read,
67 offset + bytes_read);
68 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0);
69 if (rc == 0) {
70 break;
71 }
72 bytes_read += rc;
73 }
74 *out_bytes_read = bytes_read;
75 return true;
76
77}
78
adlr@google.com3defe6a2009-12-04 20:57:17 +000079bool ReadFile(const std::string& path, std::vector<char>* out) {
80 CHECK(out);
81 FILE* fp = fopen(path.c_str(), "r");
82 if (!fp)
83 return false;
84 const size_t kChunkSize = 1024;
85 size_t read_size;
86 do {
87 char buf[kChunkSize];
88 read_size = fread(buf, 1, kChunkSize, fp);
89 if (read_size == 0)
90 break;
91 out->insert(out->end(), buf, buf + read_size);
92 } while (read_size == kChunkSize);
93 bool success = !ferror(fp);
94 TEST_AND_RETURN_FALSE_ERRNO(fclose(fp) == 0);
95 return success;
96}
97
98bool ReadFileToString(const std::string& path, std::string* out) {
99 vector<char> data;
100 bool success = ReadFile(path, &data);
101 if (!success) {
102 return false;
103 }
104 (*out) = string(&data[0], data.size());
105 return true;
106}
107
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700108off_t FileSize(const string& path) {
109 struct stat stbuf;
110 int rc = stat(path.c_str(), &stbuf);
111 CHECK_EQ(rc, 0);
112 if (rc < 0)
113 return rc;
114 return stbuf.st_size;
115}
116
adlr@google.com3defe6a2009-12-04 20:57:17 +0000117void HexDumpArray(const unsigned char* const arr, const size_t length) {
118 const unsigned char* const char_arr =
119 reinterpret_cast<const unsigned char* const>(arr);
120 LOG(INFO) << "Logging array of length: " << length;
121 const unsigned int bytes_per_line = 16;
Andrew de los Reyes08c4e272010-04-15 14:02:17 -0700122 for (uint32_t i = 0; i < length; i += bytes_per_line) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000123 const unsigned int bytes_remaining = length - i;
124 const unsigned int bytes_per_this_line = min(bytes_per_line,
125 bytes_remaining);
126 char header[100];
127 int r = snprintf(header, sizeof(header), "0x%08x : ", i);
128 TEST_AND_RETURN(r == 13);
129 string line = header;
130 for (unsigned int j = 0; j < bytes_per_this_line; j++) {
131 char buf[20];
132 unsigned char c = char_arr[i + j];
133 r = snprintf(buf, sizeof(buf), "%02x ", static_cast<unsigned int>(c));
134 TEST_AND_RETURN(r == 3);
135 line += buf;
136 }
137 LOG(INFO) << line;
138 }
139}
140
141namespace {
142class ScopedDirCloser {
143 public:
144 explicit ScopedDirCloser(DIR** dir) : dir_(dir) {}
145 ~ScopedDirCloser() {
146 if (dir_ && *dir_) {
147 int r = closedir(*dir_);
148 TEST_AND_RETURN_ERRNO(r == 0);
149 *dir_ = NULL;
150 dir_ = NULL;
151 }
152 }
153 private:
154 DIR** dir_;
155};
156} // namespace {}
157
158bool RecursiveUnlinkDir(const std::string& path) {
159 struct stat stbuf;
160 int r = lstat(path.c_str(), &stbuf);
161 TEST_AND_RETURN_FALSE_ERRNO((r == 0) || (errno == ENOENT));
162 if ((r < 0) && (errno == ENOENT))
163 // path request is missing. that's fine.
164 return true;
165 if (!S_ISDIR(stbuf.st_mode)) {
166 TEST_AND_RETURN_FALSE_ERRNO((unlink(path.c_str()) == 0) ||
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700167 (errno == ENOENT));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000168 // success or path disappeared before we could unlink.
169 return true;
170 }
171 {
172 // We have a dir, unlink all children, then delete dir
173 DIR *dir = opendir(path.c_str());
174 TEST_AND_RETURN_FALSE_ERRNO(dir);
175 ScopedDirCloser dir_closer(&dir);
176 struct dirent dir_entry;
177 struct dirent *dir_entry_p;
178 int err = 0;
179 while ((err = readdir_r(dir, &dir_entry, &dir_entry_p)) == 0) {
180 if (dir_entry_p == NULL) {
181 // end of stream reached
182 break;
183 }
184 // Skip . and ..
185 if (!strcmp(dir_entry_p->d_name, ".") ||
186 !strcmp(dir_entry_p->d_name, ".."))
187 continue;
188 TEST_AND_RETURN_FALSE(RecursiveUnlinkDir(path + "/" +
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700189 dir_entry_p->d_name));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000190 }
191 TEST_AND_RETURN_FALSE(err == 0);
192 }
193 // unlink dir
194 TEST_AND_RETURN_FALSE_ERRNO((rmdir(path.c_str()) == 0) || (errno == ENOENT));
195 return true;
196}
197
198std::string ErrnoNumberAsString(int err) {
199 char buf[100];
200 buf[0] = '\0';
201 return strerror_r(err, buf, sizeof(buf));
202}
203
204std::string NormalizePath(const std::string& path, bool strip_trailing_slash) {
205 string ret;
206 bool last_insert_was_slash = false;
207 for (string::const_iterator it = path.begin(); it != path.end(); ++it) {
208 if (*it == '/') {
209 if (last_insert_was_slash)
210 continue;
211 last_insert_was_slash = true;
212 } else {
213 last_insert_was_slash = false;
214 }
215 ret.push_back(*it);
216 }
217 if (strip_trailing_slash && last_insert_was_slash) {
218 string::size_type last_non_slash = ret.find_last_not_of('/');
219 if (last_non_slash != string::npos) {
220 ret.resize(last_non_slash + 1);
221 } else {
222 ret = "";
223 }
224 }
225 return ret;
226}
227
228bool FileExists(const char* path) {
229 struct stat stbuf;
230 return 0 == lstat(path, &stbuf);
231}
232
233std::string TempFilename(string path) {
234 static const string suffix("XXXXXX");
235 CHECK(StringHasSuffix(path, suffix));
236 do {
237 string new_suffix;
238 for (unsigned int i = 0; i < suffix.size(); i++) {
239 int r = rand() % (26 * 2 + 10); // [a-zA-Z0-9]
240 if (r < 26)
241 new_suffix.append(1, 'a' + r);
242 else if (r < (26 * 2))
243 new_suffix.append(1, 'A' + r - 26);
244 else
245 new_suffix.append(1, '0' + r - (26 * 2));
246 }
247 CHECK_EQ(new_suffix.size(), suffix.size());
248 path.resize(path.size() - new_suffix.size());
249 path.append(new_suffix);
250 } while (FileExists(path.c_str()));
251 return path;
252}
253
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700254bool MakeTempFile(const std::string& filename_template,
255 std::string* filename,
256 int* fd) {
257 DCHECK(filename || fd);
258 vector<char> buf(filename_template.size() + 1);
259 memcpy(&buf[0], filename_template.data(), filename_template.size());
260 buf[filename_template.size()] = '\0';
261
262 int mkstemp_fd = mkstemp(&buf[0]);
263 TEST_AND_RETURN_FALSE_ERRNO(mkstemp_fd >= 0);
264 if (filename) {
265 *filename = &buf[0];
266 }
267 if (fd) {
268 *fd = mkstemp_fd;
269 } else {
270 close(mkstemp_fd);
271 }
272 return true;
273}
274
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700275bool MakeTempDirectory(const std::string& dirname_template,
276 std::string* dirname) {
277 DCHECK(dirname);
278 vector<char> buf(dirname_template.size() + 1);
279 memcpy(&buf[0], dirname_template.data(), dirname_template.size());
280 buf[dirname_template.size()] = '\0';
281
282 char* return_code = mkdtemp(&buf[0]);
283 TEST_AND_RETURN_FALSE_ERRNO(return_code != NULL);
284 *dirname = &buf[0];
285 return true;
286}
287
adlr@google.com3defe6a2009-12-04 20:57:17 +0000288bool StringHasSuffix(const std::string& str, const std::string& suffix) {
289 if (suffix.size() > str.size())
290 return false;
291 return 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix);
292}
293
294bool StringHasPrefix(const std::string& str, const std::string& prefix) {
295 if (prefix.size() > str.size())
296 return false;
297 return 0 == str.compare(0, prefix.size(), prefix);
298}
299
300const std::string BootDevice() {
301 string proc_cmdline;
302 if (!ReadFileToString("/proc/cmdline", &proc_cmdline))
303 return "";
304 // look for "root=" in the command line
305 string::size_type pos = 0;
306 if (!StringHasPrefix(proc_cmdline, "root=")) {
307 pos = proc_cmdline.find(" root=") + 1;
308 }
309 if (pos == string::npos) {
310 // can't find root=
311 return "";
312 }
313 // at this point, pos is the point in the string where "root=" starts
314 string ret;
315 pos += strlen("root="); // advance to the device name itself
316 while (pos < proc_cmdline.size()) {
317 char c = proc_cmdline[pos];
318 if (c == ' ')
319 break;
320 ret += c;
321 pos++;
322 }
323 return ret;
324 // TODO(adlr): use findfs to figure out UUID= or LABEL= filesystems
325}
326
327bool MountFilesystem(const string& device,
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700328 const string& mountpoint,
329 unsigned long mountflags) {
330 int rc = mount(device.c_str(), mountpoint.c_str(), "ext3", mountflags, NULL);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000331 if (rc < 0) {
332 string msg = ErrnoNumberAsString(errno);
333 LOG(ERROR) << "Unable to mount destination device: " << msg << ". "
334 << device << " on " << mountpoint;
335 return false;
336 }
337 return true;
338}
339
340bool UnmountFilesystem(const string& mountpoint) {
341 TEST_AND_RETURN_FALSE_ERRNO(umount(mountpoint.c_str()) == 0);
342 return true;
343}
344
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700345const char* GetGErrorMessage(const GError* error) {
346 if (!error)
347 return "Unknown error.";
348 return error->message;
349}
350
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800351const char* const kStatefulPartition = "/mnt/stateful_partition";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000352
353} // namespace utils
354
355} // namespace chromeos_update_engine
356