blob: 05acc1545dbf1eb2d0c70d90a73b5a40921a1c10 [file] [log] [blame]
Darin Petkov296889c2010-07-23 16:20:54 -07001// Copyright (c) 2009 The Chromium OS Authors. All rights reserved.
adlr@google.com3defe6a2009-12-04 20:57:17 +00002// 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"
Darin Petkovf74eb652010-08-04 12:08:38 -07006
adlr@google.com3defe6a2009-12-04 20:57:17 +00007#include <sys/mount.h>
8#include <sys/stat.h>
9#include <sys/types.h>
10#include <dirent.h>
11#include <errno.h>
Andrew de los Reyes970bb282009-12-09 16:34:04 -080012#include <fcntl.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000013#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16#include <unistd.h>
Darin Petkovf74eb652010-08-04 12:08:38 -070017
adlr@google.com3defe6a2009-12-04 20:57:17 +000018#include <algorithm>
Darin Petkovf74eb652010-08-04 12:08:38 -070019
20#include "base/file_path.h"
21#include "base/file_util.h"
22#include "base/string_util.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000023#include "chromeos/obsolete_logging.h"
Andrew de los Reyes970bb282009-12-09 16:34:04 -080024#include "update_engine/file_writer.h"
Darin Petkov33d30642010-08-04 10:18:57 -070025#include "update_engine/omaha_request_params.h"
Darin Petkov296889c2010-07-23 16:20:54 -070026#include "update_engine/subprocess.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000027
28using std::min;
29using std::string;
30using std::vector;
31
32namespace chromeos_update_engine {
33
34namespace utils {
35
Darin Petkov33d30642010-08-04 10:18:57 -070036bool IsOfficialBuild() {
37 OmahaRequestDeviceParams params;
38 if (!params.Init("", "")) {
39 return true;
40 }
41 return params.app_track != "buildbot-build" &&
42 params.app_track != "developer-build";
43}
44
Andrew de los Reyes970bb282009-12-09 16:34:04 -080045bool WriteFile(const char* path, const char* data, int data_len) {
46 DirectFileWriter writer;
47 TEST_AND_RETURN_FALSE_ERRNO(0 == writer.Open(path,
48 O_WRONLY | O_CREAT | O_TRUNC,
49 0666));
50 ScopedFileWriterCloser closer(&writer);
51 TEST_AND_RETURN_FALSE_ERRNO(data_len == writer.Write(data, data_len));
52 return true;
53}
54
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070055bool WriteAll(int fd, const void* buf, size_t count) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070056 const char* c_buf = static_cast<const char*>(buf);
57 ssize_t bytes_written = 0;
58 while (bytes_written < static_cast<ssize_t>(count)) {
59 ssize_t rc = write(fd, c_buf + bytes_written, count - bytes_written);
60 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0);
61 bytes_written += rc;
62 }
63 return true;
64}
65
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070066bool PWriteAll(int fd, const void* buf, size_t count, off_t offset) {
67 const char* c_buf = static_cast<const char*>(buf);
68 ssize_t bytes_written = 0;
69 while (bytes_written < static_cast<ssize_t>(count)) {
70 ssize_t rc = pwrite(fd, c_buf + bytes_written, count - bytes_written,
71 offset + bytes_written);
72 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0);
73 bytes_written += rc;
74 }
75 return true;
76}
77
78bool PReadAll(int fd, void* buf, size_t count, off_t offset,
79 ssize_t* out_bytes_read) {
80 char* c_buf = static_cast<char*>(buf);
81 ssize_t bytes_read = 0;
82 while (bytes_read < static_cast<ssize_t>(count)) {
83 ssize_t rc = pread(fd, c_buf + bytes_read, count - bytes_read,
84 offset + bytes_read);
85 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0);
86 if (rc == 0) {
87 break;
88 }
89 bytes_read += rc;
90 }
91 *out_bytes_read = bytes_read;
92 return true;
Darin Petkov296889c2010-07-23 16:20:54 -070093
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070094}
95
adlr@google.com3defe6a2009-12-04 20:57:17 +000096bool ReadFile(const std::string& path, std::vector<char>* out) {
97 CHECK(out);
98 FILE* fp = fopen(path.c_str(), "r");
99 if (!fp)
100 return false;
101 const size_t kChunkSize = 1024;
102 size_t read_size;
103 do {
104 char buf[kChunkSize];
105 read_size = fread(buf, 1, kChunkSize, fp);
106 if (read_size == 0)
107 break;
108 out->insert(out->end(), buf, buf + read_size);
109 } while (read_size == kChunkSize);
110 bool success = !ferror(fp);
111 TEST_AND_RETURN_FALSE_ERRNO(fclose(fp) == 0);
112 return success;
113}
114
115bool ReadFileToString(const std::string& path, std::string* out) {
116 vector<char> data;
117 bool success = ReadFile(path, &data);
118 if (!success) {
119 return false;
120 }
121 (*out) = string(&data[0], data.size());
122 return true;
123}
124
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700125off_t FileSize(const string& path) {
126 struct stat stbuf;
127 int rc = stat(path.c_str(), &stbuf);
128 CHECK_EQ(rc, 0);
129 if (rc < 0)
130 return rc;
131 return stbuf.st_size;
132}
133
adlr@google.com3defe6a2009-12-04 20:57:17 +0000134void HexDumpArray(const unsigned char* const arr, const size_t length) {
135 const unsigned char* const char_arr =
136 reinterpret_cast<const unsigned char* const>(arr);
137 LOG(INFO) << "Logging array of length: " << length;
138 const unsigned int bytes_per_line = 16;
Andrew de los Reyes08c4e272010-04-15 14:02:17 -0700139 for (uint32_t i = 0; i < length; i += bytes_per_line) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000140 const unsigned int bytes_remaining = length - i;
141 const unsigned int bytes_per_this_line = min(bytes_per_line,
142 bytes_remaining);
143 char header[100];
144 int r = snprintf(header, sizeof(header), "0x%08x : ", i);
145 TEST_AND_RETURN(r == 13);
146 string line = header;
147 for (unsigned int j = 0; j < bytes_per_this_line; j++) {
148 char buf[20];
149 unsigned char c = char_arr[i + j];
150 r = snprintf(buf, sizeof(buf), "%02x ", static_cast<unsigned int>(c));
151 TEST_AND_RETURN(r == 3);
152 line += buf;
153 }
154 LOG(INFO) << line;
155 }
156}
157
158namespace {
159class ScopedDirCloser {
160 public:
161 explicit ScopedDirCloser(DIR** dir) : dir_(dir) {}
162 ~ScopedDirCloser() {
163 if (dir_ && *dir_) {
164 int r = closedir(*dir_);
165 TEST_AND_RETURN_ERRNO(r == 0);
166 *dir_ = NULL;
167 dir_ = NULL;
168 }
169 }
170 private:
171 DIR** dir_;
172};
173} // namespace {}
174
175bool RecursiveUnlinkDir(const std::string& path) {
176 struct stat stbuf;
177 int r = lstat(path.c_str(), &stbuf);
178 TEST_AND_RETURN_FALSE_ERRNO((r == 0) || (errno == ENOENT));
179 if ((r < 0) && (errno == ENOENT))
180 // path request is missing. that's fine.
181 return true;
182 if (!S_ISDIR(stbuf.st_mode)) {
183 TEST_AND_RETURN_FALSE_ERRNO((unlink(path.c_str()) == 0) ||
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700184 (errno == ENOENT));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000185 // success or path disappeared before we could unlink.
186 return true;
187 }
188 {
189 // We have a dir, unlink all children, then delete dir
190 DIR *dir = opendir(path.c_str());
191 TEST_AND_RETURN_FALSE_ERRNO(dir);
192 ScopedDirCloser dir_closer(&dir);
193 struct dirent dir_entry;
194 struct dirent *dir_entry_p;
195 int err = 0;
196 while ((err = readdir_r(dir, &dir_entry, &dir_entry_p)) == 0) {
197 if (dir_entry_p == NULL) {
198 // end of stream reached
199 break;
200 }
201 // Skip . and ..
202 if (!strcmp(dir_entry_p->d_name, ".") ||
203 !strcmp(dir_entry_p->d_name, ".."))
204 continue;
205 TEST_AND_RETURN_FALSE(RecursiveUnlinkDir(path + "/" +
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700206 dir_entry_p->d_name));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000207 }
208 TEST_AND_RETURN_FALSE(err == 0);
209 }
210 // unlink dir
211 TEST_AND_RETURN_FALSE_ERRNO((rmdir(path.c_str()) == 0) || (errno == ENOENT));
212 return true;
213}
214
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700215string RootDevice(const string& partition_device) {
Darin Petkovf74eb652010-08-04 12:08:38 -0700216 FilePath device_path(partition_device);
217 if (device_path.DirName().value() != "/dev") {
218 return "";
219 }
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700220 string::const_iterator it = --partition_device.end();
221 for (; it >= partition_device.begin(); --it) {
222 if (!isdigit(*it))
223 break;
224 }
225 // Some devices contain a p before the partitions. For example:
226 // /dev/mmc0p4 should be shortened to /dev/mmc0.
227 if (*it == 'p')
228 --it;
229 return string(partition_device.begin(), it + 1);
230}
231
232string PartitionNumber(const string& partition_device) {
233 CHECK(!partition_device.empty());
234 string::const_iterator it = --partition_device.end();
235 for (; it >= partition_device.begin(); --it) {
236 if (!isdigit(*it))
237 break;
238 }
239 return string(it + 1, partition_device.end());
240}
241
Darin Petkovf74eb652010-08-04 12:08:38 -0700242string SysfsBlockDevice(const string& device) {
243 FilePath device_path(device);
244 if (device_path.DirName().value() != "/dev") {
245 return "";
246 }
247 return FilePath("/sys/block").Append(device_path.BaseName()).value();
248}
249
250bool IsRemovableDevice(const std::string& device) {
251 string sysfs_block = SysfsBlockDevice(device);
252 string removable;
253 if (sysfs_block.empty() ||
254 !file_util::ReadFileToString(FilePath(sysfs_block).Append("removable"),
255 &removable)) {
256 return false;
257 }
258 TrimWhitespaceASCII(removable, TRIM_ALL, &removable);
259 return removable == "1";
260}
261
adlr@google.com3defe6a2009-12-04 20:57:17 +0000262std::string ErrnoNumberAsString(int err) {
263 char buf[100];
264 buf[0] = '\0';
265 return strerror_r(err, buf, sizeof(buf));
266}
267
268std::string NormalizePath(const std::string& path, bool strip_trailing_slash) {
269 string ret;
270 bool last_insert_was_slash = false;
271 for (string::const_iterator it = path.begin(); it != path.end(); ++it) {
272 if (*it == '/') {
273 if (last_insert_was_slash)
274 continue;
275 last_insert_was_slash = true;
276 } else {
277 last_insert_was_slash = false;
278 }
279 ret.push_back(*it);
280 }
281 if (strip_trailing_slash && last_insert_was_slash) {
282 string::size_type last_non_slash = ret.find_last_not_of('/');
283 if (last_non_slash != string::npos) {
284 ret.resize(last_non_slash + 1);
285 } else {
286 ret = "";
287 }
288 }
289 return ret;
290}
291
292bool FileExists(const char* path) {
293 struct stat stbuf;
294 return 0 == lstat(path, &stbuf);
295}
296
297std::string TempFilename(string path) {
298 static const string suffix("XXXXXX");
299 CHECK(StringHasSuffix(path, suffix));
300 do {
301 string new_suffix;
302 for (unsigned int i = 0; i < suffix.size(); i++) {
303 int r = rand() % (26 * 2 + 10); // [a-zA-Z0-9]
304 if (r < 26)
305 new_suffix.append(1, 'a' + r);
306 else if (r < (26 * 2))
307 new_suffix.append(1, 'A' + r - 26);
308 else
309 new_suffix.append(1, '0' + r - (26 * 2));
310 }
311 CHECK_EQ(new_suffix.size(), suffix.size());
312 path.resize(path.size() - new_suffix.size());
313 path.append(new_suffix);
314 } while (FileExists(path.c_str()));
315 return path;
316}
317
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700318bool MakeTempFile(const std::string& filename_template,
319 std::string* filename,
320 int* fd) {
321 DCHECK(filename || fd);
322 vector<char> buf(filename_template.size() + 1);
323 memcpy(&buf[0], filename_template.data(), filename_template.size());
324 buf[filename_template.size()] = '\0';
Darin Petkov296889c2010-07-23 16:20:54 -0700325
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700326 int mkstemp_fd = mkstemp(&buf[0]);
327 TEST_AND_RETURN_FALSE_ERRNO(mkstemp_fd >= 0);
328 if (filename) {
329 *filename = &buf[0];
330 }
331 if (fd) {
332 *fd = mkstemp_fd;
333 } else {
334 close(mkstemp_fd);
335 }
336 return true;
337}
338
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700339bool MakeTempDirectory(const std::string& dirname_template,
340 std::string* dirname) {
341 DCHECK(dirname);
342 vector<char> buf(dirname_template.size() + 1);
343 memcpy(&buf[0], dirname_template.data(), dirname_template.size());
344 buf[dirname_template.size()] = '\0';
Darin Petkov296889c2010-07-23 16:20:54 -0700345
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700346 char* return_code = mkdtemp(&buf[0]);
347 TEST_AND_RETURN_FALSE_ERRNO(return_code != NULL);
348 *dirname = &buf[0];
349 return true;
350}
351
adlr@google.com3defe6a2009-12-04 20:57:17 +0000352bool StringHasSuffix(const std::string& str, const std::string& suffix) {
353 if (suffix.size() > str.size())
354 return false;
355 return 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix);
356}
357
358bool StringHasPrefix(const std::string& str, const std::string& prefix) {
359 if (prefix.size() > str.size())
360 return false;
361 return 0 == str.compare(0, prefix.size(), prefix);
362}
363
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700364const string BootDevice() {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000365 string proc_cmdline;
366 if (!ReadFileToString("/proc/cmdline", &proc_cmdline))
367 return "";
368 // look for "root=" in the command line
369 string::size_type pos = 0;
370 if (!StringHasPrefix(proc_cmdline, "root=")) {
371 pos = proc_cmdline.find(" root=") + 1;
372 }
373 if (pos == string::npos) {
374 // can't find root=
375 return "";
376 }
377 // at this point, pos is the point in the string where "root=" starts
378 string ret;
379 pos += strlen("root="); // advance to the device name itself
380 while (pos < proc_cmdline.size()) {
381 char c = proc_cmdline[pos];
382 if (c == ' ')
383 break;
384 ret += c;
385 pos++;
386 }
387 return ret;
388 // TODO(adlr): use findfs to figure out UUID= or LABEL= filesystems
389}
390
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700391const string BootKernelDevice(const std::string& boot_device) {
392 // Currntly this assumes the last digit of the boot device is
393 // 3, 5, or 7, and changes it to 2, 4, or 6, respectively, to
394 // get the kernel device.
395 string ret = boot_device;
396 if (ret.empty())
397 return ret;
398 char last_char = ret[ret.size() - 1];
399 if (last_char == '3' || last_char == '5' || last_char == '7') {
400 ret[ret.size() - 1] = last_char - 1;
401 return ret;
402 }
403 return "";
404}
405
adlr@google.com3defe6a2009-12-04 20:57:17 +0000406bool MountFilesystem(const string& device,
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700407 const string& mountpoint,
408 unsigned long mountflags) {
409 int rc = mount(device.c_str(), mountpoint.c_str(), "ext3", mountflags, NULL);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000410 if (rc < 0) {
411 string msg = ErrnoNumberAsString(errno);
412 LOG(ERROR) << "Unable to mount destination device: " << msg << ". "
413 << device << " on " << mountpoint;
414 return false;
415 }
416 return true;
417}
418
419bool UnmountFilesystem(const string& mountpoint) {
420 TEST_AND_RETURN_FALSE_ERRNO(umount(mountpoint.c_str()) == 0);
421 return true;
422}
423
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700424bool GetBootloader(BootLoader* out_bootloader) {
425 // For now, hardcode to syslinux.
426 *out_bootloader = BootLoader_SYSLINUX;
427 return true;
428}
429
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700430const char* GetGErrorMessage(const GError* error) {
431 if (!error)
432 return "Unknown error.";
433 return error->message;
434}
435
Darin Petkov296889c2010-07-23 16:20:54 -0700436bool Reboot() {
437 vector<string> command;
438 command.push_back("/sbin/shutdown");
439 command.push_back("-r");
440 command.push_back("now");
441 int rc = 0;
442 Subprocess::SynchronousExec(command, &rc);
443 TEST_AND_RETURN_FALSE(rc == 0);
444 return true;
445}
446
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800447const char* const kStatefulPartition = "/mnt/stateful_partition";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000448
449} // namespace utils
450
451} // namespace chromeos_update_engine