blob: b19e6fc112ea29b2b74397970fd6da3e87965720 [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
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700198string RootDevice(const string& partition_device) {
199 CHECK(!partition_device.empty());
200 string::const_iterator it = --partition_device.end();
201 for (; it >= partition_device.begin(); --it) {
202 if (!isdigit(*it))
203 break;
204 }
205 // Some devices contain a p before the partitions. For example:
206 // /dev/mmc0p4 should be shortened to /dev/mmc0.
207 if (*it == 'p')
208 --it;
209 return string(partition_device.begin(), it + 1);
210}
211
212string PartitionNumber(const string& partition_device) {
213 CHECK(!partition_device.empty());
214 string::const_iterator it = --partition_device.end();
215 for (; it >= partition_device.begin(); --it) {
216 if (!isdigit(*it))
217 break;
218 }
219 return string(it + 1, partition_device.end());
220}
221
adlr@google.com3defe6a2009-12-04 20:57:17 +0000222std::string ErrnoNumberAsString(int err) {
223 char buf[100];
224 buf[0] = '\0';
225 return strerror_r(err, buf, sizeof(buf));
226}
227
228std::string NormalizePath(const std::string& path, bool strip_trailing_slash) {
229 string ret;
230 bool last_insert_was_slash = false;
231 for (string::const_iterator it = path.begin(); it != path.end(); ++it) {
232 if (*it == '/') {
233 if (last_insert_was_slash)
234 continue;
235 last_insert_was_slash = true;
236 } else {
237 last_insert_was_slash = false;
238 }
239 ret.push_back(*it);
240 }
241 if (strip_trailing_slash && last_insert_was_slash) {
242 string::size_type last_non_slash = ret.find_last_not_of('/');
243 if (last_non_slash != string::npos) {
244 ret.resize(last_non_slash + 1);
245 } else {
246 ret = "";
247 }
248 }
249 return ret;
250}
251
252bool FileExists(const char* path) {
253 struct stat stbuf;
254 return 0 == lstat(path, &stbuf);
255}
256
257std::string TempFilename(string path) {
258 static const string suffix("XXXXXX");
259 CHECK(StringHasSuffix(path, suffix));
260 do {
261 string new_suffix;
262 for (unsigned int i = 0; i < suffix.size(); i++) {
263 int r = rand() % (26 * 2 + 10); // [a-zA-Z0-9]
264 if (r < 26)
265 new_suffix.append(1, 'a' + r);
266 else if (r < (26 * 2))
267 new_suffix.append(1, 'A' + r - 26);
268 else
269 new_suffix.append(1, '0' + r - (26 * 2));
270 }
271 CHECK_EQ(new_suffix.size(), suffix.size());
272 path.resize(path.size() - new_suffix.size());
273 path.append(new_suffix);
274 } while (FileExists(path.c_str()));
275 return path;
276}
277
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700278bool MakeTempFile(const std::string& filename_template,
279 std::string* filename,
280 int* fd) {
281 DCHECK(filename || fd);
282 vector<char> buf(filename_template.size() + 1);
283 memcpy(&buf[0], filename_template.data(), filename_template.size());
284 buf[filename_template.size()] = '\0';
285
286 int mkstemp_fd = mkstemp(&buf[0]);
287 TEST_AND_RETURN_FALSE_ERRNO(mkstemp_fd >= 0);
288 if (filename) {
289 *filename = &buf[0];
290 }
291 if (fd) {
292 *fd = mkstemp_fd;
293 } else {
294 close(mkstemp_fd);
295 }
296 return true;
297}
298
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700299bool MakeTempDirectory(const std::string& dirname_template,
300 std::string* dirname) {
301 DCHECK(dirname);
302 vector<char> buf(dirname_template.size() + 1);
303 memcpy(&buf[0], dirname_template.data(), dirname_template.size());
304 buf[dirname_template.size()] = '\0';
305
306 char* return_code = mkdtemp(&buf[0]);
307 TEST_AND_RETURN_FALSE_ERRNO(return_code != NULL);
308 *dirname = &buf[0];
309 return true;
310}
311
adlr@google.com3defe6a2009-12-04 20:57:17 +0000312bool StringHasSuffix(const std::string& str, const std::string& suffix) {
313 if (suffix.size() > str.size())
314 return false;
315 return 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix);
316}
317
318bool StringHasPrefix(const std::string& str, const std::string& prefix) {
319 if (prefix.size() > str.size())
320 return false;
321 return 0 == str.compare(0, prefix.size(), prefix);
322}
323
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700324const string BootDevice() {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000325 string proc_cmdline;
326 if (!ReadFileToString("/proc/cmdline", &proc_cmdline))
327 return "";
328 // look for "root=" in the command line
329 string::size_type pos = 0;
330 if (!StringHasPrefix(proc_cmdline, "root=")) {
331 pos = proc_cmdline.find(" root=") + 1;
332 }
333 if (pos == string::npos) {
334 // can't find root=
335 return "";
336 }
337 // at this point, pos is the point in the string where "root=" starts
338 string ret;
339 pos += strlen("root="); // advance to the device name itself
340 while (pos < proc_cmdline.size()) {
341 char c = proc_cmdline[pos];
342 if (c == ' ')
343 break;
344 ret += c;
345 pos++;
346 }
347 return ret;
348 // TODO(adlr): use findfs to figure out UUID= or LABEL= filesystems
349}
350
Andrew de los Reyesf9185172010-05-03 11:07:05 -0700351const string BootKernelDevice(const std::string& boot_device) {
352 // Currntly this assumes the last digit of the boot device is
353 // 3, 5, or 7, and changes it to 2, 4, or 6, respectively, to
354 // get the kernel device.
355 string ret = boot_device;
356 if (ret.empty())
357 return ret;
358 char last_char = ret[ret.size() - 1];
359 if (last_char == '3' || last_char == '5' || last_char == '7') {
360 ret[ret.size() - 1] = last_char - 1;
361 return ret;
362 }
363 return "";
364}
365
adlr@google.com3defe6a2009-12-04 20:57:17 +0000366bool MountFilesystem(const string& device,
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700367 const string& mountpoint,
368 unsigned long mountflags) {
369 int rc = mount(device.c_str(), mountpoint.c_str(), "ext3", mountflags, NULL);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000370 if (rc < 0) {
371 string msg = ErrnoNumberAsString(errno);
372 LOG(ERROR) << "Unable to mount destination device: " << msg << ". "
373 << device << " on " << mountpoint;
374 return false;
375 }
376 return true;
377}
378
379bool UnmountFilesystem(const string& mountpoint) {
380 TEST_AND_RETURN_FALSE_ERRNO(umount(mountpoint.c_str()) == 0);
381 return true;
382}
383
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700384bool GetBootloader(BootLoader* out_bootloader) {
385 // For now, hardcode to syslinux.
386 *out_bootloader = BootLoader_SYSLINUX;
387 return true;
388}
389
Andrew de los Reyesc7020782010-04-28 10:46:04 -0700390const char* GetGErrorMessage(const GError* error) {
391 if (!error)
392 return "Unknown error.";
393 return error->message;
394}
395
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800396const char* const kStatefulPartition = "/mnt/stateful_partition";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000397
398} // namespace utils
399
400} // namespace chromeos_update_engine
401