blob: 69a59cc054204bfefea8eddd960c9cbf9d934ee5 [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
adlr@google.com3defe6a2009-12-04 20:57:17 +000038bool ReadFile(const std::string& path, std::vector<char>* out) {
39 CHECK(out);
40 FILE* fp = fopen(path.c_str(), "r");
41 if (!fp)
42 return false;
43 const size_t kChunkSize = 1024;
44 size_t read_size;
45 do {
46 char buf[kChunkSize];
47 read_size = fread(buf, 1, kChunkSize, fp);
48 if (read_size == 0)
49 break;
50 out->insert(out->end(), buf, buf + read_size);
51 } while (read_size == kChunkSize);
52 bool success = !ferror(fp);
53 TEST_AND_RETURN_FALSE_ERRNO(fclose(fp) == 0);
54 return success;
55}
56
57bool ReadFileToString(const std::string& path, std::string* out) {
58 vector<char> data;
59 bool success = ReadFile(path, &data);
60 if (!success) {
61 return false;
62 }
63 (*out) = string(&data[0], data.size());
64 return true;
65}
66
67void HexDumpArray(const unsigned char* const arr, const size_t length) {
68 const unsigned char* const char_arr =
69 reinterpret_cast<const unsigned char* const>(arr);
70 LOG(INFO) << "Logging array of length: " << length;
71 const unsigned int bytes_per_line = 16;
72 for (size_t i = 0; i < length; i += bytes_per_line) {
73 const unsigned int bytes_remaining = length - i;
74 const unsigned int bytes_per_this_line = min(bytes_per_line,
75 bytes_remaining);
76 char header[100];
77 int r = snprintf(header, sizeof(header), "0x%08x : ", i);
78 TEST_AND_RETURN(r == 13);
79 string line = header;
80 for (unsigned int j = 0; j < bytes_per_this_line; j++) {
81 char buf[20];
82 unsigned char c = char_arr[i + j];
83 r = snprintf(buf, sizeof(buf), "%02x ", static_cast<unsigned int>(c));
84 TEST_AND_RETURN(r == 3);
85 line += buf;
86 }
87 LOG(INFO) << line;
88 }
89}
90
91namespace {
92class ScopedDirCloser {
93 public:
94 explicit ScopedDirCloser(DIR** dir) : dir_(dir) {}
95 ~ScopedDirCloser() {
96 if (dir_ && *dir_) {
97 int r = closedir(*dir_);
98 TEST_AND_RETURN_ERRNO(r == 0);
99 *dir_ = NULL;
100 dir_ = NULL;
101 }
102 }
103 private:
104 DIR** dir_;
105};
106} // namespace {}
107
108bool RecursiveUnlinkDir(const std::string& path) {
109 struct stat stbuf;
110 int r = lstat(path.c_str(), &stbuf);
111 TEST_AND_RETURN_FALSE_ERRNO((r == 0) || (errno == ENOENT));
112 if ((r < 0) && (errno == ENOENT))
113 // path request is missing. that's fine.
114 return true;
115 if (!S_ISDIR(stbuf.st_mode)) {
116 TEST_AND_RETURN_FALSE_ERRNO((unlink(path.c_str()) == 0) ||
117 (errno == ENOENT));
118 // success or path disappeared before we could unlink.
119 return true;
120 }
121 {
122 // We have a dir, unlink all children, then delete dir
123 DIR *dir = opendir(path.c_str());
124 TEST_AND_RETURN_FALSE_ERRNO(dir);
125 ScopedDirCloser dir_closer(&dir);
126 struct dirent dir_entry;
127 struct dirent *dir_entry_p;
128 int err = 0;
129 while ((err = readdir_r(dir, &dir_entry, &dir_entry_p)) == 0) {
130 if (dir_entry_p == NULL) {
131 // end of stream reached
132 break;
133 }
134 // Skip . and ..
135 if (!strcmp(dir_entry_p->d_name, ".") ||
136 !strcmp(dir_entry_p->d_name, ".."))
137 continue;
138 TEST_AND_RETURN_FALSE(RecursiveUnlinkDir(path + "/" +
139 dir_entry_p->d_name));
140 }
141 TEST_AND_RETURN_FALSE(err == 0);
142 }
143 // unlink dir
144 TEST_AND_RETURN_FALSE_ERRNO((rmdir(path.c_str()) == 0) || (errno == ENOENT));
145 return true;
146}
147
148std::string ErrnoNumberAsString(int err) {
149 char buf[100];
150 buf[0] = '\0';
151 return strerror_r(err, buf, sizeof(buf));
152}
153
154std::string NormalizePath(const std::string& path, bool strip_trailing_slash) {
155 string ret;
156 bool last_insert_was_slash = false;
157 for (string::const_iterator it = path.begin(); it != path.end(); ++it) {
158 if (*it == '/') {
159 if (last_insert_was_slash)
160 continue;
161 last_insert_was_slash = true;
162 } else {
163 last_insert_was_slash = false;
164 }
165 ret.push_back(*it);
166 }
167 if (strip_trailing_slash && last_insert_was_slash) {
168 string::size_type last_non_slash = ret.find_last_not_of('/');
169 if (last_non_slash != string::npos) {
170 ret.resize(last_non_slash + 1);
171 } else {
172 ret = "";
173 }
174 }
175 return ret;
176}
177
178bool FileExists(const char* path) {
179 struct stat stbuf;
180 return 0 == lstat(path, &stbuf);
181}
182
183std::string TempFilename(string path) {
184 static const string suffix("XXXXXX");
185 CHECK(StringHasSuffix(path, suffix));
186 do {
187 string new_suffix;
188 for (unsigned int i = 0; i < suffix.size(); i++) {
189 int r = rand() % (26 * 2 + 10); // [a-zA-Z0-9]
190 if (r < 26)
191 new_suffix.append(1, 'a' + r);
192 else if (r < (26 * 2))
193 new_suffix.append(1, 'A' + r - 26);
194 else
195 new_suffix.append(1, '0' + r - (26 * 2));
196 }
197 CHECK_EQ(new_suffix.size(), suffix.size());
198 path.resize(path.size() - new_suffix.size());
199 path.append(new_suffix);
200 } while (FileExists(path.c_str()));
201 return path;
202}
203
204bool StringHasSuffix(const std::string& str, const std::string& suffix) {
205 if (suffix.size() > str.size())
206 return false;
207 return 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix);
208}
209
210bool StringHasPrefix(const std::string& str, const std::string& prefix) {
211 if (prefix.size() > str.size())
212 return false;
213 return 0 == str.compare(0, prefix.size(), prefix);
214}
215
216const std::string BootDevice() {
217 string proc_cmdline;
218 if (!ReadFileToString("/proc/cmdline", &proc_cmdline))
219 return "";
220 // look for "root=" in the command line
221 string::size_type pos = 0;
222 if (!StringHasPrefix(proc_cmdline, "root=")) {
223 pos = proc_cmdline.find(" root=") + 1;
224 }
225 if (pos == string::npos) {
226 // can't find root=
227 return "";
228 }
229 // at this point, pos is the point in the string where "root=" starts
230 string ret;
231 pos += strlen("root="); // advance to the device name itself
232 while (pos < proc_cmdline.size()) {
233 char c = proc_cmdline[pos];
234 if (c == ' ')
235 break;
236 ret += c;
237 pos++;
238 }
239 return ret;
240 // TODO(adlr): use findfs to figure out UUID= or LABEL= filesystems
241}
242
243bool MountFilesystem(const string& device,
244 const string& mountpoint) {
245 int rc = mount(device.c_str(), mountpoint.c_str(), "ext3", 0, NULL);
246 if (rc < 0) {
247 string msg = ErrnoNumberAsString(errno);
248 LOG(ERROR) << "Unable to mount destination device: " << msg << ". "
249 << device << " on " << mountpoint;
250 return false;
251 }
252 return true;
253}
254
255bool UnmountFilesystem(const string& mountpoint) {
256 TEST_AND_RETURN_FALSE_ERRNO(umount(mountpoint.c_str()) == 0);
257 return true;
258}
259
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800260const char* const kStatefulPartition = "/mnt/stateful_partition";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000261
262} // namespace utils
263
264} // namespace chromeos_update_engine
265