blob: f0c045f2b87fc482a49c00bdbcd24bde476dbc3f [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
adlr@google.com3defe6a2009-12-04 20:57:17 +000016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#include "update_engine/common/utils.h"
Darin Petkovf74eb652010-08-04 12:08:38 -070018
Ben Chan9abb7632014-08-07 00:10:53 -070019#include <stdint.h>
20
adlr@google.com3defe6a2009-12-04 20:57:17 +000021#include <dirent.h>
Alex Deymoc1711e22014-08-08 13:16:23 -070022#include <elf.h>
Alex Deymo6f20dd42015-08-18 16:42:46 -070023#include <endian.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000024#include <errno.h>
Andrew de los Reyes970bb282009-12-09 16:34:04 -080025#include <fcntl.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000026#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
Alex Deymoc4acdf42014-05-28 21:07:10 -070029#include <sys/mount.h>
30#include <sys/resource.h>
Kelvin Zhang8933d572021-01-28 10:17:34 -050031#include <sys/sendfile.h>
Alex Deymoc4acdf42014-05-28 21:07:10 -070032#include <sys/stat.h>
33#include <sys/types.h>
Yifan Hongc80de2d2020-02-25 17:13:53 -080034#include <time.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000035#include <unistd.h>
Darin Petkovf74eb652010-08-04 12:08:38 -070036
adlr@google.com3defe6a2009-12-04 20:57:17 +000037#include <algorithm>
Kelvin Zhang49170aa2022-11-28 10:55:16 -080038#include <filesystem>
Alex Vakulenkod2779df2014-06-16 13:19:00 -070039#include <utility>
Chris Sosac1972482013-04-30 22:31:10 -070040#include <vector>
Darin Petkovf74eb652010-08-04 12:08:38 -070041
Colin Cross84fe9da2021-12-21 13:19:14 -080042#include <android-base/strings.h>
Alex Vakulenko4906c1c2014-08-21 13:17:44 -070043#include <base/callback.h>
Ben Chan736fcb52014-05-21 18:28:22 -070044#include <base/files/file_path.h>
Alex Deymo192393b2014-11-10 15:58:38 -080045#include <base/files/file_util.h>
Ben Chan736fcb52014-05-21 18:28:22 -070046#include <base/files/scoped_file.h>
Alex Deymoc00c98a2015-03-17 17:38:00 -070047#include <base/format_macros.h>
Alex Deymo60ca1a72015-06-18 18:19:15 -070048#include <base/location.h>
Mike Frysinger8155d082012-04-06 15:23:18 -040049#include <base/logging.h>
Chris Sosafc661a12013-02-26 14:43:21 -080050#include <base/posix/eintr_wrapper.h>
Will Drewry8f71da82010-08-30 14:07:11 -050051#include <base/rand_util.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070052#include <base/strings/string_number_conversions.h>
53#include <base/strings/string_split.h>
54#include <base/strings/string_util.h>
55#include <base/strings/stringprintf.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070056#include <brillo/data_encoding.h>
Will Drewry8f71da82010-08-30 14:07:11 -050057
Alex Deymo39910dc2015-11-09 17:04:30 -080058#include "update_engine/common/constants.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080059#include "update_engine/common/subprocess.h"
Kelvin Zhange47767a2023-05-16 13:00:58 -070060#ifdef __ANDROID__
Kelvin Zhang32a73a92022-12-19 12:27:41 -080061#include "update_engine/common/platform_constants.h"
Kelvin Zhange47767a2023-05-16 13:00:58 -070062#endif
Alex Deymo39910dc2015-11-09 17:04:30 -080063#include "update_engine/payload_consumer/file_descriptor.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000064
Jay Srinivasan480ddfa2012-06-01 19:15:26 -070065using base::Time;
Gilad Arnold8e3f1262013-01-08 14:59:54 -080066using base::TimeDelta;
adlr@google.com3defe6a2009-12-04 20:57:17 +000067using std::min;
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -070068using std::numeric_limits;
adlr@google.com3defe6a2009-12-04 20:57:17 +000069using std::string;
70using std::vector;
71
72namespace chromeos_update_engine {
73
Ben Chan77a1eba2012-10-07 22:54:55 -070074namespace {
75
76// The following constants control how UnmountFilesystem should retry if
77// umount() fails with an errno EBUSY, i.e. retry 5 times over the course of
78// one second.
79const int kUnmountMaxNumOfRetries = 5;
80const int kUnmountRetryIntervalInMicroseconds = 200 * 1000; // 200 ms
Alex Deymo032e7722014-03-25 17:53:56 -070081
82// Number of bytes to read from a file to attempt to detect its contents. Used
83// in GetFileFormat.
84const int kGetFileFormatMaxHeaderSize = 32;
85
Alex Deymodd132f32015-09-14 19:12:07 -070086// The path to the kernel's boot_id.
87const char kBootIdPath[] = "/proc/sys/kernel/random/boot_id";
88
Alex Deymo5aa1c542015-09-18 01:02:33 -070089// If |path| is absolute, or explicit relative to the current working directory,
90// leaves it as is. Otherwise, uses the system's temp directory, as defined by
91// base::GetTempDir() and prepends it to |path|. On success stores the full
92// temporary path in |template_path| and returns true.
93bool GetTempName(const string& path, base::FilePath* template_path) {
Alex Vakulenko0103c362016-01-20 07:56:15 -080094 if (path[0] == '/' ||
95 base::StartsWith(path, "./", base::CompareCase::SENSITIVE) ||
96 base::StartsWith(path, "../", base::CompareCase::SENSITIVE)) {
Alex Deymo5aa1c542015-09-18 01:02:33 -070097 *template_path = base::FilePath(path);
98 return true;
99 }
100
101 base::FilePath temp_dir;
Sen Jiang9c123462015-11-19 13:16:23 -0800102#ifdef __ANDROID__
Sen Jiangc2b37662019-01-09 16:51:18 -0800103 temp_dir = base::FilePath(constants::kNonVolatileDirectory).Append("tmp");
Alex Deymo32951022016-08-10 17:02:49 -0700104#else
Sen Jiangc2b37662019-01-09 16:51:18 -0800105 TEST_AND_RETURN_FALSE(base::GetTempDir(&temp_dir));
Alex Deymo32951022016-08-10 17:02:49 -0700106#endif // __ANDROID__
Sen Jiang9c123462015-11-19 13:16:23 -0800107 if (!base::PathExists(temp_dir))
108 TEST_AND_RETURN_FALSE(base::CreateDirectory(temp_dir));
Alex Deymo5aa1c542015-09-18 01:02:33 -0700109 *template_path = temp_dir.Append(path);
110 return true;
111}
112
Ben Chan77a1eba2012-10-07 22:54:55 -0700113} // namespace
114
adlr@google.com3defe6a2009-12-04 20:57:17 +0000115namespace utils {
116
Alex Deymo335516c2016-11-28 13:37:06 -0800117bool WriteFile(const char* path, const void* data, size_t data_len) {
118 int fd = HANDLE_EINTR(open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600));
119 TEST_AND_RETURN_FALSE_ERRNO(fd >= 0);
120 ScopedFdCloser fd_closer(&fd);
121 return WriteAll(fd, data, data_len);
Andrew de los Reyes970bb282009-12-09 16:34:04 -0800122}
123
Alex Deymo0d298542016-03-30 18:31:49 -0700124bool ReadAll(
125 int fd, void* buf, size_t count, size_t* out_bytes_read, bool* eof) {
126 char* c_buf = static_cast<char*>(buf);
127 size_t bytes_read = 0;
128 *eof = false;
129 while (bytes_read < count) {
130 ssize_t rc = HANDLE_EINTR(read(fd, c_buf + bytes_read, count - bytes_read));
131 if (rc < 0) {
132 // EAGAIN and EWOULDBLOCK are normal return values when there's no more
133 // input and we are in non-blocking mode.
134 if (errno != EWOULDBLOCK && errno != EAGAIN) {
135 PLOG(ERROR) << "Error reading fd " << fd;
136 *out_bytes_read = bytes_read;
137 return false;
138 }
139 break;
140 } else if (rc == 0) {
141 // A value of 0 means that we reached EOF and there is nothing else to
142 // read from this fd.
143 *eof = true;
144 break;
145 } else {
146 bytes_read += rc;
147 }
148 }
149 *out_bytes_read = bytes_read;
150 return true;
151}
152
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700153bool WriteAll(int fd, const void* buf, size_t count) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700154 const char* c_buf = static_cast<const char*>(buf);
155 ssize_t bytes_written = 0;
156 while (bytes_written < static_cast<ssize_t>(count)) {
157 ssize_t rc = write(fd, c_buf + bytes_written, count - bytes_written);
158 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0);
159 bytes_written += rc;
160 }
161 return true;
162}
163
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700164bool PWriteAll(int fd, const void* buf, size_t count, off_t offset) {
165 const char* c_buf = static_cast<const char*>(buf);
Gilad Arnold780db212012-07-11 13:12:49 -0700166 size_t bytes_written = 0;
167 int num_attempts = 0;
168 while (bytes_written < count) {
169 num_attempts++;
Amin Hassanib2689592019-01-13 17:04:28 -0800170 ssize_t rc = pwrite(fd,
171 c_buf + bytes_written,
172 count - bytes_written,
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700173 offset + bytes_written);
Gilad Arnold780db212012-07-11 13:12:49 -0700174 // TODO(garnold) for debugging failure in chromium-os:31077; to be removed.
175 if (rc < 0) {
176 PLOG(ERROR) << "pwrite error; num_attempts=" << num_attempts
Amin Hassanib2689592019-01-13 17:04:28 -0800177 << " bytes_written=" << bytes_written << " count=" << count
178 << " offset=" << offset;
Gilad Arnold780db212012-07-11 13:12:49 -0700179 }
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700180 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0);
181 bytes_written += rc;
182 }
183 return true;
184}
185
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800186bool WriteAll(FileDescriptor* fd, const void* buf, size_t count) {
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -0800187 const char* c_buf = static_cast<const char*>(buf);
188 ssize_t bytes_written = 0;
189 while (bytes_written < static_cast<ssize_t>(count)) {
190 ssize_t rc = fd->Write(c_buf + bytes_written, count - bytes_written);
191 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0);
192 bytes_written += rc;
193 }
194 return true;
195}
196
Kelvin Zhang4b280242020-11-06 16:07:45 -0500197bool WriteAll(const FileDescriptorPtr& fd,
198 const void* buf,
199 size_t count,
200 off_t offset) {
Allie Wood0c8cf7e2015-04-23 19:24:49 -0700201 TEST_AND_RETURN_FALSE_ERRNO(fd->Seek(offset, SEEK_SET) !=
202 static_cast<off_t>(-1));
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -0800203 return WriteAll(fd, buf, count);
204}
205
Amin Hassanib2689592019-01-13 17:04:28 -0800206bool PReadAll(
207 int fd, void* buf, size_t count, off_t offset, ssize_t* out_bytes_read) {
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700208 char* c_buf = static_cast<char*>(buf);
209 ssize_t bytes_read = 0;
210 while (bytes_read < static_cast<ssize_t>(count)) {
Amin Hassanib2689592019-01-13 17:04:28 -0800211 ssize_t rc =
212 pread(fd, c_buf + bytes_read, count - bytes_read, offset + bytes_read);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700213 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0);
214 if (rc == 0) {
215 break;
216 }
217 bytes_read += rc;
218 }
219 *out_bytes_read = bytes_read;
220 return true;
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700221}
222
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800223bool ReadAll(FileDescriptor* fd,
Kelvin Zhang4b280242020-11-06 16:07:45 -0500224 void* buf,
225 size_t count,
226 off_t offset,
227 ssize_t* out_bytes_read) {
Allie Wood0c8cf7e2015-04-23 19:24:49 -0700228 TEST_AND_RETURN_FALSE_ERRNO(fd->Seek(offset, SEEK_SET) !=
229 static_cast<off_t>(-1));
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -0800230 char* c_buf = static_cast<char*>(buf);
231 ssize_t bytes_read = 0;
232 while (bytes_read < static_cast<ssize_t>(count)) {
233 ssize_t rc = fd->Read(c_buf + bytes_read, count - bytes_read);
234 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0);
235 if (rc == 0) {
236 break;
237 }
238 bytes_read += rc;
239 }
240 *out_bytes_read = bytes_read;
241 return true;
242}
243
Kelvin Zhang1a0ed712022-01-26 16:09:05 -0800244bool PReadAll(FileDescriptor* fd,
Kelvin Zhangc3c0a3a2020-11-09 16:08:13 -0500245 void* buf,
246 size_t count,
247 off_t offset,
248 ssize_t* out_bytes_read) {
249 auto old_off = fd->Seek(0, SEEK_CUR);
250 TEST_AND_RETURN_FALSE_ERRNO(old_off >= 0);
251
252 auto success = ReadAll(fd, buf, count, offset, out_bytes_read);
253 TEST_AND_RETURN_FALSE_ERRNO(fd->Seek(old_off, SEEK_SET) == old_off);
254 return success;
255}
256
257bool PWriteAll(const FileDescriptorPtr& fd,
258 const void* buf,
259 size_t count,
260 off_t offset) {
261 auto old_off = fd->Seek(0, SEEK_CUR);
262 TEST_AND_RETURN_FALSE_ERRNO(old_off >= 0);
263
264 auto success = WriteAll(fd, buf, count, offset);
265 TEST_AND_RETURN_FALSE_ERRNO(fd->Seek(old_off, SEEK_SET) == old_off);
266 return success;
267}
268
Gilad Arnold19a45f02012-07-19 12:36:10 -0700269// Append |nbytes| of content from |buf| to the vector pointed to by either
270// |vec_p| or |str_p|.
Amin Hassanib2689592019-01-13 17:04:28 -0800271static void AppendBytes(const uint8_t* buf,
272 size_t nbytes,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700273 brillo::Blob* vec_p) {
Gilad Arnold19a45f02012-07-19 12:36:10 -0700274 CHECK(buf);
275 CHECK(vec_p);
276 vec_p->insert(vec_p->end(), buf, buf + nbytes);
277}
Amin Hassanib2689592019-01-13 17:04:28 -0800278static void AppendBytes(const uint8_t* buf, size_t nbytes, string* str_p) {
Gilad Arnold19a45f02012-07-19 12:36:10 -0700279 CHECK(buf);
280 CHECK(str_p);
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800281 str_p->append(buf, buf + nbytes);
Gilad Arnold19a45f02012-07-19 12:36:10 -0700282}
283
284// Reads from an open file |fp|, appending the read content to the container
285// pointer to by |out_p|. Returns true upon successful reading all of the
Darin Petkov8e447e02013-04-16 16:23:50 +0200286// file's content, false otherwise. If |size| is not -1, reads up to |size|
287// bytes.
Gilad Arnold19a45f02012-07-19 12:36:10 -0700288template <class T>
Darin Petkov8e447e02013-04-16 16:23:50 +0200289static bool Read(FILE* fp, off_t size, T* out_p) {
Gilad Arnold19a45f02012-07-19 12:36:10 -0700290 CHECK(fp);
Darin Petkov8e447e02013-04-16 16:23:50 +0200291 CHECK(size == -1 || size >= 0);
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800292 uint8_t buf[1024];
Darin Petkov8e447e02013-04-16 16:23:50 +0200293 while (size == -1 || size > 0) {
294 off_t bytes_to_read = sizeof(buf);
295 if (size > 0 && bytes_to_read > size) {
296 bytes_to_read = size;
297 }
298 size_t nbytes = fread(buf, 1, bytes_to_read, fp);
299 if (!nbytes) {
300 break;
301 }
Gilad Arnold19a45f02012-07-19 12:36:10 -0700302 AppendBytes(buf, nbytes, out_p);
Darin Petkov8e447e02013-04-16 16:23:50 +0200303 if (size != -1) {
304 CHECK(size >= static_cast<off_t>(nbytes));
305 size -= nbytes;
306 }
307 }
308 if (ferror(fp)) {
309 return false;
310 }
311 return size == 0 || feof(fp);
Gilad Arnold19a45f02012-07-19 12:36:10 -0700312}
313
Darin Petkov8e447e02013-04-16 16:23:50 +0200314// Opens a file |path| for reading and appends its the contents to a container
315// |out_p|. Starts reading the file from |offset|. If |offset| is beyond the end
316// of the file, returns success. If |size| is not -1, reads up to |size| bytes.
Gilad Arnold19a45f02012-07-19 12:36:10 -0700317template <class T>
Alex Deymof329b932014-10-30 01:37:48 -0700318static bool ReadFileChunkAndAppend(const string& path,
Darin Petkov8e447e02013-04-16 16:23:50 +0200319 off_t offset,
320 off_t size,
321 T* out_p) {
322 CHECK_GE(offset, 0);
323 CHECK(size == -1 || size >= 0);
Ben Chan736fcb52014-05-21 18:28:22 -0700324 base::ScopedFILE fp(fopen(path.c_str(), "r"));
Darin Petkov8e447e02013-04-16 16:23:50 +0200325 if (!fp.get())
adlr@google.com3defe6a2009-12-04 20:57:17 +0000326 return false;
Darin Petkov8e447e02013-04-16 16:23:50 +0200327 if (offset) {
328 // Return success without appending any data if a chunk beyond the end of
329 // the file is requested.
330 if (offset >= FileSize(path)) {
331 return true;
332 }
333 TEST_AND_RETURN_FALSE_ERRNO(fseek(fp.get(), offset, SEEK_SET) == 0);
334 }
335 return Read(fp.get(), size, out_p);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000336}
337
Alex Deymo10875d92014-11-10 21:52:57 -0800338// TODO(deymo): This is only used in unittest, but requires the private
339// Read<string>() defined here. Expose Read<string>() or move to base/ version.
340bool ReadPipe(const string& cmd, string* out_p) {
Gilad Arnold19a45f02012-07-19 12:36:10 -0700341 FILE* fp = popen(cmd.c_str(), "r");
342 if (!fp)
adlr@google.com3defe6a2009-12-04 20:57:17 +0000343 return false;
Darin Petkov8e447e02013-04-16 16:23:50 +0200344 bool success = Read(fp, -1, out_p);
Gilad Arnold19a45f02012-07-19 12:36:10 -0700345 return (success && pclose(fp) >= 0);
346}
347
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700348bool ReadFile(const string& path, brillo::Blob* out_p) {
Darin Petkov8e447e02013-04-16 16:23:50 +0200349 return ReadFileChunkAndAppend(path, 0, -1, out_p);
Gilad Arnold19a45f02012-07-19 12:36:10 -0700350}
351
Darin Petkov8e447e02013-04-16 16:23:50 +0200352bool ReadFile(const string& path, string* out_p) {
353 return ReadFileChunkAndAppend(path, 0, -1, out_p);
Gilad Arnold19a45f02012-07-19 12:36:10 -0700354}
355
Amin Hassanib2689592019-01-13 17:04:28 -0800356bool ReadFileChunk(const string& path,
357 off_t offset,
358 off_t size,
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700359 brillo::Blob* out_p) {
Darin Petkov8e447e02013-04-16 16:23:50 +0200360 return ReadFileChunkAndAppend(path, offset, size, out_p);
361}
362
Gabe Blackb92cd2e2014-09-08 02:47:41 -0700363off_t BlockDevSize(int fd) {
Daniel Zheng3e881aa2022-09-07 22:10:29 +0000364 uint64_t dev_size{};
Gabe Blackb92cd2e2014-09-08 02:47:41 -0700365 int rc = ioctl(fd, BLKGETSIZE64, &dev_size);
366 if (rc == -1) {
367 dev_size = -1;
368 PLOG(ERROR) << "Error running ioctl(BLKGETSIZE64) on " << fd;
369 }
370 return dev_size;
371}
372
Gabe Blackb92cd2e2014-09-08 02:47:41 -0700373off_t FileSize(int fd) {
Daniel Zheng3e881aa2022-09-07 22:10:29 +0000374 struct stat stbuf {};
Gabe Blackb92cd2e2014-09-08 02:47:41 -0700375 int rc = fstat(fd, &stbuf);
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700376 CHECK_EQ(rc, 0);
Gabe Blackb92cd2e2014-09-08 02:47:41 -0700377 if (rc < 0) {
378 PLOG(ERROR) << "Error stat-ing " << fd;
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700379 return rc;
Gabe Blackb92cd2e2014-09-08 02:47:41 -0700380 }
381 if (S_ISREG(stbuf.st_mode))
382 return stbuf.st_size;
383 if (S_ISBLK(stbuf.st_mode))
384 return BlockDevSize(fd);
385 LOG(ERROR) << "Couldn't determine the type of " << fd;
386 return -1;
387}
388
389off_t FileSize(const string& path) {
390 int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC);
391 if (fd == -1) {
392 PLOG(ERROR) << "Error opening " << path;
393 return fd;
394 }
395 off_t size = FileSize(fd);
396 if (size == -1)
397 PLOG(ERROR) << "Error getting file size of " << path;
398 close(fd);
399 return size;
Andrew de los Reyesf4c7ef12010-04-30 10:37:00 -0700400}
401
Kelvin Zhang02fe6622021-11-01 16:37:58 -0700402bool SendFile(int out_fd, int in_fd, size_t count) {
403 off64_t offset = lseek(in_fd, 0, SEEK_CUR);
404 TEST_AND_RETURN_FALSE_ERRNO(offset >= 0);
405 constexpr size_t BUFFER_SIZE = 4096;
406 while (count > 0) {
407 const auto bytes_written =
408 sendfile(out_fd, in_fd, &offset, std::min(count, BUFFER_SIZE));
409 TEST_AND_RETURN_FALSE_ERRNO(bytes_written > 0);
410 count -= bytes_written;
411 }
412 return true;
413}
414
Kelvin Zhangc80d39b2023-10-25 09:30:16 -0700415bool DeleteDirectory(const char* dirname) {
416 const std::string tmpdir = std::string(dirname) + "_deleted";
417 std::filesystem::remove_all(tmpdir);
418 if (rename(dirname, tmpdir.c_str()) != 0) {
419 PLOG(ERROR) << "Failed to rename " << dirname << " to " << tmpdir;
420 return false;
421 }
422 std::filesystem::remove_all(tmpdir);
423 return true;
424}
425
Kelvin Zhang49170aa2022-11-28 10:55:16 -0800426bool FsyncDirectory(const char* dirname) {
427 android::base::unique_fd fd(
428 TEMP_FAILURE_RETRY(open(dirname, O_RDONLY | O_CLOEXEC)));
429 if (fd == -1) {
430 PLOG(ERROR) << "Failed to open " << dirname;
431 return false;
432 }
433 if (fsync(fd) == -1) {
434 if (errno == EROFS || errno == EINVAL) {
435 PLOG(WARNING) << "Skip fsync " << dirname
436 << " on a file system does not support synchronization";
437 } else {
438 PLOG(ERROR) << "Failed to fsync " << dirname;
439 return false;
440 }
441 }
442 return true;
443}
444
445bool WriteStringToFileAtomic(const std::string& path,
446 std::string_view content) {
447 const std::string tmp_path = path + ".tmp";
448 {
449 const int flags = O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC;
450 android::base::unique_fd fd(
451 TEMP_FAILURE_RETRY(open(tmp_path.c_str(), flags, 0644)));
452 if (fd == -1) {
453 PLOG(ERROR) << "Failed to open " << path;
454 return false;
455 }
456 if (!WriteAll(fd.get(), content.data(), content.size())) {
457 PLOG(ERROR) << "Failed to write to fd " << fd;
458 return false;
459 }
460 // rename() without fsync() is not safe. Data could still be living on page
461 // cache. To ensure atomiticity, call fsync()
462 if (fsync(fd) != 0) {
463 PLOG(ERROR) << "Failed to fsync " << tmp_path;
464 }
465 }
466 if (rename(tmp_path.c_str(), path.c_str()) == -1) {
467 PLOG(ERROR) << "rename failed from " << tmp_path << " to " << path;
468 return false;
469 }
470 return FsyncDirectory(std::filesystem::path(path).parent_path().c_str());
471}
472
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800473void HexDumpArray(const uint8_t* const arr, const size_t length) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000474 LOG(INFO) << "Logging array of length: " << length;
475 const unsigned int bytes_per_line = 16;
Andrew de los Reyes08c4e272010-04-15 14:02:17 -0700476 for (uint32_t i = 0; i < length; i += bytes_per_line) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000477 const unsigned int bytes_remaining = length - i;
Amin Hassanib2689592019-01-13 17:04:28 -0800478 const unsigned int bytes_per_this_line =
479 min(bytes_per_line, bytes_remaining);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000480 char header[100];
481 int r = snprintf(header, sizeof(header), "0x%08x : ", i);
482 TEST_AND_RETURN(r == 13);
483 string line = header;
484 for (unsigned int j = 0; j < bytes_per_this_line; j++) {
485 char buf[20];
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800486 uint8_t c = arr[i + j];
adlr@google.com3defe6a2009-12-04 20:57:17 +0000487 r = snprintf(buf, sizeof(buf), "%02x ", static_cast<unsigned int>(c));
488 TEST_AND_RETURN(r == 3);
489 line += buf;
490 }
491 LOG(INFO) << line;
492 }
493}
494
Alex Deymof329b932014-10-30 01:37:48 -0700495bool SplitPartitionName(const string& partition_name,
496 string* out_disk_name,
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800497 int* out_partition_num) {
Amin Hassanib2689592019-01-13 17:04:28 -0800498 if (!base::StartsWith(
499 partition_name, "/dev/", base::CompareCase::SENSITIVE)) {
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800500 LOG(ERROR) << "Invalid partition device name: " << partition_name;
501 return false;
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700502 }
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800503
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700504 size_t last_nondigit_pos = partition_name.find_last_not_of("0123456789");
505 if (last_nondigit_pos == string::npos ||
506 (last_nondigit_pos + 1) == partition_name.size()) {
507 LOG(ERROR) << "Unable to parse partition device name: " << partition_name;
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800508 return false;
509 }
510
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800511 if (out_disk_name) {
512 // Special case for MMC devices which have the following naming scheme:
513 // mmcblk0p2
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700514 size_t disk_name_len = last_nondigit_pos;
Amin Hassanib2689592019-01-13 17:04:28 -0800515 if (partition_name[last_nondigit_pos] != 'p' || last_nondigit_pos == 0 ||
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700516 !isdigit(partition_name[last_nondigit_pos - 1])) {
517 disk_name_len++;
518 }
519 *out_disk_name = partition_name.substr(0, disk_name_len);
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800520 }
521
522 if (out_partition_num) {
Brian Norris7b428f52019-06-26 10:03:35 -0700523 string partition_str = partition_name.substr(last_nondigit_pos + 1);
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800524 *out_partition_num = atoi(partition_str.c_str());
525 }
526 return true;
527}
528
Alex Deymof329b932014-10-30 01:37:48 -0700529string MakePartitionName(const string& disk_name, int partition_num) {
Nam T. Nguyena78b28c2015-03-06 22:30:12 -0800530 if (partition_num < 1) {
531 LOG(ERROR) << "Invalid partition number: " << partition_num;
532 return string();
533 }
534
Alex Vakulenko0103c362016-01-20 07:56:15 -0800535 if (!base::StartsWith(disk_name, "/dev/", base::CompareCase::SENSITIVE)) {
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800536 LOG(ERROR) << "Invalid disk name: " << disk_name;
Alex Deymof329b932014-10-30 01:37:48 -0700537 return string();
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800538 }
539
Alex Deymof329b932014-10-30 01:37:48 -0700540 string partition_name = disk_name;
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700541 if (isdigit(partition_name.back())) {
542 // Special case for devices with names ending with a digit.
543 // Add "p" to separate the disk name from partition number,
544 // e.g. "/dev/loop0p2"
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800545 partition_name += 'p';
546 }
547
Alex Vakulenkof3f85bb2014-03-26 16:36:35 -0700548 partition_name += std::to_string(partition_num);
549
Alex Vakulenko4f5b1442014-02-21 12:19:44 -0800550 return partition_name;
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700551}
552
adlr@google.com3defe6a2009-12-04 20:57:17 +0000553bool FileExists(const char* path) {
Daniel Zheng3e881aa2022-09-07 22:10:29 +0000554 struct stat stbuf {};
adlr@google.com3defe6a2009-12-04 20:57:17 +0000555 return 0 == lstat(path, &stbuf);
556}
557
Darin Petkov30291ed2010-11-12 10:23:06 -0800558bool IsSymlink(const char* path) {
Daniel Zheng3e881aa2022-09-07 22:10:29 +0000559 struct stat stbuf {};
Darin Petkov30291ed2010-11-12 10:23:06 -0800560 return lstat(path, &stbuf) == 0 && S_ISLNK(stbuf.st_mode) != 0;
561}
562
Kelvin Zhangdeb34452021-01-21 11:54:36 -0500563bool IsRegFile(const char* path) {
Daniel Zheng3e881aa2022-09-07 22:10:29 +0000564 struct stat stbuf {};
Kelvin Zhangdeb34452021-01-21 11:54:36 -0500565 return lstat(path, &stbuf) == 0 && S_ISREG(stbuf.st_mode) != 0;
566}
567
Alex Deymof329b932014-10-30 01:37:48 -0700568bool MakeTempFile(const string& base_filename_template,
569 string* filename,
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700570 int* fd) {
Alex Deymo5aa1c542015-09-18 01:02:33 -0700571 base::FilePath filename_template;
572 TEST_AND_RETURN_FALSE(
573 GetTempName(base_filename_template, &filename_template));
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700574 DCHECK(filename || fd);
Alex Deymo5aa1c542015-09-18 01:02:33 -0700575 vector<char> buf(filename_template.value().size() + 1);
Amin Hassanib2689592019-01-13 17:04:28 -0800576 memcpy(buf.data(),
577 filename_template.value().data(),
Alex Deymo5aa1c542015-09-18 01:02:33 -0700578 filename_template.value().size());
579 buf[filename_template.value().size()] = '\0';
Darin Petkov296889c2010-07-23 16:20:54 -0700580
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800581 int mkstemp_fd = mkstemp(buf.data());
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700582 TEST_AND_RETURN_FALSE_ERRNO(mkstemp_fd >= 0);
583 if (filename) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800584 *filename = buf.data();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700585 }
586 if (fd) {
587 *fd = mkstemp_fd;
588 } else {
589 close(mkstemp_fd);
590 }
591 return true;
592}
593
Alex Deymo5fb356c2016-03-25 18:48:22 -0700594bool SetBlockDeviceReadOnly(const string& device, bool read_only) {
595 int fd = HANDLE_EINTR(open(device.c_str(), O_RDONLY | O_CLOEXEC));
596 if (fd < 0) {
597 PLOG(ERROR) << "Opening block device " << device;
598 return false;
599 }
600 ScopedFdCloser fd_closer(&fd);
601 // We take no action if not needed.
Daniel Zheng3e881aa2022-09-07 22:10:29 +0000602 int read_only_flag{};
Alex Deymo5fb356c2016-03-25 18:48:22 -0700603 int expected_flag = read_only ? 1 : 0;
604 int rc = ioctl(fd, BLKROGET, &read_only_flag);
605 // In case of failure reading the setting we will try to set it anyway.
606 if (rc == 0 && read_only_flag == expected_flag)
607 return true;
608
609 rc = ioctl(fd, BLKROSET, &expected_flag);
610 if (rc != 0) {
Amin Hassanib2689592019-01-13 17:04:28 -0800611 PLOG(ERROR) << "Marking block device " << device
612 << " as read_only=" << expected_flag;
Alex Deymo5fb356c2016-03-25 18:48:22 -0700613 return false;
614 }
lin.guifc20b672023-09-01 20:01:35 +0800615
616 /*
617 * Read back the value to check if it is configured successfully.
618 * If fail, use the second method, set the file
619 * /sys/block/<partition_name>/force_ro
620 * to config the read only property.
621 */
622 rc = ioctl(fd, BLKROGET, &read_only_flag);
623 if (rc != 0) {
624 PLOG(ERROR) << "Failed to read back block device read-only value:" << device;
625 return false;
626 }
627 if (read_only_flag == expected_flag) {
628 return true;
629 }
630
631 std::array<char, PATH_MAX> device_name;
632 char *pdevice = realpath(device.c_str(), device_name.data());
633 TEST_AND_RETURN_FALSE_ERRNO(pdevice);
634
635 std::string real_path(pdevice);
636 std::size_t offset = real_path.find_last_of('/');
637 if (offset == std::string::npos){
638 LOG(ERROR) << "Could not find partition name from " << real_path;
639 return false;
640 }
641 const std::string partition_name = real_path.substr(offset + 1);
642
643 std::string force_ro_file = "/sys/block/" + partition_name + "/force_ro";
644 android::base::unique_fd fd_force_ro {
645 HANDLE_EINTR(open(force_ro_file.c_str(), O_WRONLY | O_CLOEXEC))};
646 TEST_AND_RETURN_FALSE_ERRNO(fd_force_ro >= 0);
647
648 rc = write(fd_force_ro, expected_flag ? "1" : "0", 1);
649 TEST_AND_RETURN_FALSE_ERRNO(rc > 0);
650
651 // Read back again
652 rc = ioctl(fd, BLKROGET, &read_only_flag);
653 if (rc != 0) {
654 PLOG(ERROR) << "Failed to read back block device read-only value:" << device;
655 return false;
656 }
657 if (read_only_flag != expected_flag) {
658 LOG(ERROR) << "After modifying force_ro, marking block device " << device
659 << " as read_only=" << expected_flag;
660 return false;
661 }
Alex Deymo5fb356c2016-03-25 18:48:22 -0700662 return true;
663}
664
adlr@google.com3defe6a2009-12-04 20:57:17 +0000665bool MountFilesystem(const string& device,
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700666 const string& mountpoint,
Alex Deymo390efed2016-02-18 11:00:40 -0800667 unsigned long mountflags, // NOLINT(runtime/int)
Alex Deymo14dbd332016-03-01 18:55:54 -0800668 const string& type,
669 const string& fs_mount_options) {
Alex Deymo390efed2016-02-18 11:00:40 -0800670 vector<const char*> fstypes;
671 if (type.empty()) {
Kelvin Zhang4eae81e2021-12-09 17:07:17 -0800672 fstypes = {"ext2", "ext3", "ext4", "squashfs", "erofs"};
Alex Deymo390efed2016-02-18 11:00:40 -0800673 } else {
674 fstypes = {type.c_str()};
675 }
Alex Deymo4c82df32014-11-10 22:25:57 -0800676 for (const char* fstype : fstypes) {
Amin Hassanib2689592019-01-13 17:04:28 -0800677 int rc = mount(device.c_str(),
678 mountpoint.c_str(),
679 fstype,
680 mountflags,
Alex Deymo14dbd332016-03-01 18:55:54 -0800681 fs_mount_options.c_str());
Alex Deymo4c82df32014-11-10 22:25:57 -0800682 if (rc == 0)
683 return true;
684
Amin Hassanib2689592019-01-13 17:04:28 -0800685 PLOG(WARNING) << "Unable to mount destination device " << device << " on "
686 << mountpoint << " as " << fstype;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000687 }
Alex Deymo390efed2016-02-18 11:00:40 -0800688 if (!type.empty()) {
689 LOG(ERROR) << "Unable to mount " << device << " with any supported type";
690 }
Alex Deymo4c82df32014-11-10 22:25:57 -0800691 return false;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000692}
693
694bool UnmountFilesystem(const string& mountpoint) {
Alex Deymo5ba93ad2016-08-22 19:51:59 -0700695 int num_retries = 1;
696 for (;; ++num_retries) {
Ben Chan77a1eba2012-10-07 22:54:55 -0700697 if (umount(mountpoint.c_str()) == 0)
Alex Deymo5ba93ad2016-08-22 19:51:59 -0700698 return true;
699 if (errno != EBUSY || num_retries >= kUnmountMaxNumOfRetries)
Ben Chan77a1eba2012-10-07 22:54:55 -0700700 break;
Alex Deymo8d2bbe32015-06-23 16:28:59 -0700701 usleep(kUnmountRetryIntervalInMicroseconds);
Ben Chan77a1eba2012-10-07 22:54:55 -0700702 }
Alex Deymo5ba93ad2016-08-22 19:51:59 -0700703 if (errno == EINVAL) {
704 LOG(INFO) << "Not a mountpoint: " << mountpoint;
705 return false;
706 }
707 PLOG(WARNING) << "Error unmounting " << mountpoint << " after " << num_retries
708 << " attempts. Lazy unmounting instead, error was";
709 if (umount2(mountpoint.c_str(), MNT_DETACH) != 0) {
710 PLOG(ERROR) << "Lazy unmount failed";
711 return false;
712 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000713 return true;
714}
715
Alex Deymof4116502017-03-22 17:00:31 -0700716bool IsMountpoint(const std::string& mountpoint) {
Daniel Zheng3e881aa2022-09-07 22:10:29 +0000717 struct stat stdir {
718 }, stparent{};
Alex Deymof4116502017-03-22 17:00:31 -0700719
720 // Check whether the passed mountpoint is a directory and the /.. is in the
721 // same device or not. If mountpoint/.. is in a different device it means that
722 // there is a filesystem mounted there. If it is not, but they both point to
723 // the same inode it basically is the special case of /.. pointing to /. This
724 // test doesn't play well with bind mount but that's out of the scope of what
725 // we want to detect here.
726 if (lstat(mountpoint.c_str(), &stdir) != 0) {
727 PLOG(ERROR) << "Error stat'ing " << mountpoint;
728 return false;
729 }
730 if (!S_ISDIR(stdir.st_mode))
731 return false;
732
733 base::FilePath parent(mountpoint);
734 parent = parent.Append("..");
735 if (lstat(parent.value().c_str(), &stparent) != 0) {
736 PLOG(ERROR) << "Error stat'ing " << parent.value();
737 return false;
738 }
739 return S_ISDIR(stparent.st_mode) &&
740 (stparent.st_dev != stdir.st_dev || stparent.st_ino == stdir.st_ino);
741}
742
Alex Deymo032e7722014-03-25 17:53:56 -0700743// Tries to parse the header of an ELF file to obtain a human-readable
744// description of it on the |output| string.
Amin Hassanib2689592019-01-13 17:04:28 -0800745static bool GetFileFormatELF(const uint8_t* buffer,
746 size_t size,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800747 string* output) {
Alex Deymo032e7722014-03-25 17:53:56 -0700748 // 0x00: EI_MAG - ELF magic header, 4 bytes.
Alex Deymoc1711e22014-08-08 13:16:23 -0700749 if (size < SELFMAG || memcmp(buffer, ELFMAG, SELFMAG) != 0)
Alex Deymo032e7722014-03-25 17:53:56 -0700750 return false;
751 *output = "ELF";
752
753 // 0x04: EI_CLASS, 1 byte.
Alex Deymoc1711e22014-08-08 13:16:23 -0700754 if (size < EI_CLASS + 1)
Alex Deymo032e7722014-03-25 17:53:56 -0700755 return true;
Alex Deymoc1711e22014-08-08 13:16:23 -0700756 switch (buffer[EI_CLASS]) {
757 case ELFCLASS32:
Alex Deymo032e7722014-03-25 17:53:56 -0700758 *output += " 32-bit";
759 break;
Alex Deymoc1711e22014-08-08 13:16:23 -0700760 case ELFCLASS64:
Alex Deymo032e7722014-03-25 17:53:56 -0700761 *output += " 64-bit";
762 break;
763 default:
764 *output += " ?-bit";
765 }
766
767 // 0x05: EI_DATA, endianness, 1 byte.
Alex Deymoc1711e22014-08-08 13:16:23 -0700768 if (size < EI_DATA + 1)
Alex Deymo032e7722014-03-25 17:53:56 -0700769 return true;
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800770 uint8_t ei_data = buffer[EI_DATA];
Alex Deymo032e7722014-03-25 17:53:56 -0700771 switch (ei_data) {
Alex Deymoc1711e22014-08-08 13:16:23 -0700772 case ELFDATA2LSB:
Alex Deymo032e7722014-03-25 17:53:56 -0700773 *output += " little-endian";
774 break;
Alex Deymoc1711e22014-08-08 13:16:23 -0700775 case ELFDATA2MSB:
Alex Deymo032e7722014-03-25 17:53:56 -0700776 *output += " big-endian";
777 break;
778 default:
779 *output += " ?-endian";
780 // Don't parse anything after the 0x10 offset if endianness is unknown.
781 return true;
782 }
783
Alex Deymoc1711e22014-08-08 13:16:23 -0700784 const Elf32_Ehdr* hdr = reinterpret_cast<const Elf32_Ehdr*>(buffer);
785 // 0x12: e_machine, 2 byte endianness based on ei_data. The position (0x12)
786 // and size is the same for both 32 and 64 bits.
787 if (size < offsetof(Elf32_Ehdr, e_machine) + sizeof(hdr->e_machine))
Alex Deymo032e7722014-03-25 17:53:56 -0700788 return true;
Daniel Zheng3e881aa2022-09-07 22:10:29 +0000789 uint16_t e_machine{};
Sen Jiang771f6482018-04-04 17:59:10 -0700790 // Fix endianness regardless of the host endianness.
Alex Deymoc1711e22014-08-08 13:16:23 -0700791 if (ei_data == ELFDATA2LSB)
792 e_machine = le16toh(hdr->e_machine);
Alex Deymo032e7722014-03-25 17:53:56 -0700793 else
Alex Deymoc1711e22014-08-08 13:16:23 -0700794 e_machine = be16toh(hdr->e_machine);
Alex Deymo032e7722014-03-25 17:53:56 -0700795
796 switch (e_machine) {
Alex Deymoc1711e22014-08-08 13:16:23 -0700797 case EM_386:
Alex Deymo032e7722014-03-25 17:53:56 -0700798 *output += " x86";
799 break;
Alex Deymoc1711e22014-08-08 13:16:23 -0700800 case EM_MIPS:
801 *output += " mips";
802 break;
803 case EM_ARM:
Alex Deymo032e7722014-03-25 17:53:56 -0700804 *output += " arm";
805 break;
Alex Deymoc1711e22014-08-08 13:16:23 -0700806 case EM_X86_64:
Alex Deymo032e7722014-03-25 17:53:56 -0700807 *output += " x86-64";
808 break;
809 default:
810 *output += " unknown-arch";
811 }
812 return true;
813}
814
815string GetFileFormat(const string& path) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700816 brillo::Blob buffer;
Alex Deymo032e7722014-03-25 17:53:56 -0700817 if (!ReadFileChunkAndAppend(path, 0, kGetFileFormatMaxHeaderSize, &buffer))
818 return "File not found.";
819
820 string result;
821 if (GetFileFormatELF(buffer.data(), buffer.size(), &result))
822 return result;
823
824 return "data";
825}
826
Darin Petkov5c0a8af2010-08-24 13:39:13 -0700827int FuzzInt(int value, unsigned int range) {
828 int min = value - range / 2;
829 int max = value + range - range / 2;
830 return base::RandInt(min, max);
831}
832
Gilad Arnoldd7b513d2012-05-10 14:25:27 -0700833string FormatSecs(unsigned secs) {
Gilad Arnold8e3f1262013-01-08 14:59:54 -0800834 return FormatTimeDelta(TimeDelta::FromSeconds(secs));
Gilad Arnoldd7b513d2012-05-10 14:25:27 -0700835}
836
Gilad Arnold8e3f1262013-01-08 14:59:54 -0800837string FormatTimeDelta(TimeDelta delta) {
David Zeuthen973449e2014-08-18 16:18:23 -0400838 string str;
839
840 // Handle negative durations by prefixing with a minus.
841 if (delta.ToInternalValue() < 0) {
842 delta *= -1;
843 str = "-";
844 }
845
Gilad Arnoldd7b513d2012-05-10 14:25:27 -0700846 // Canonicalize into days, hours, minutes, seconds and microseconds.
847 unsigned days = delta.InDays();
Gilad Arnold8e3f1262013-01-08 14:59:54 -0800848 delta -= TimeDelta::FromDays(days);
Gilad Arnoldd7b513d2012-05-10 14:25:27 -0700849 unsigned hours = delta.InHours();
Gilad Arnold8e3f1262013-01-08 14:59:54 -0800850 delta -= TimeDelta::FromHours(hours);
Gilad Arnoldd7b513d2012-05-10 14:25:27 -0700851 unsigned mins = delta.InMinutes();
Gilad Arnold8e3f1262013-01-08 14:59:54 -0800852 delta -= TimeDelta::FromMinutes(mins);
Gilad Arnoldd7b513d2012-05-10 14:25:27 -0700853 unsigned secs = delta.InSeconds();
Gilad Arnold8e3f1262013-01-08 14:59:54 -0800854 delta -= TimeDelta::FromSeconds(secs);
Gilad Arnoldd7b513d2012-05-10 14:25:27 -0700855 unsigned usecs = delta.InMicroseconds();
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800856
Gilad Arnoldd7b513d2012-05-10 14:25:27 -0700857 if (days)
858 base::StringAppendF(&str, "%ud", days);
859 if (days || hours)
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800860 base::StringAppendF(&str, "%uh", hours);
Gilad Arnoldd7b513d2012-05-10 14:25:27 -0700861 if (days || hours || mins)
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800862 base::StringAppendF(&str, "%um", mins);
Gilad Arnoldd7b513d2012-05-10 14:25:27 -0700863 base::StringAppendF(&str, "%u", secs);
864 if (usecs) {
865 int width = 6;
866 while ((usecs / 10) * 10 == usecs) {
867 usecs /= 10;
868 width--;
869 }
870 base::StringAppendF(&str, ".%0*u", width, usecs);
871 }
872 base::StringAppendF(&str, "s");
Gilad Arnold1ebd8132012-03-05 10:19:29 -0800873 return str;
874}
875
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700876string ToString(const Time utc_time) {
Daniel Zheng3e881aa2022-09-07 22:10:29 +0000877 Time::Exploded exp_time{};
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700878 utc_time.UTCExplode(&exp_time);
Alex Vakulenko75039d72014-03-25 12:36:28 -0700879 return base::StringPrintf("%d/%d/%d %d:%02d:%02d GMT",
Amin Hassanib2689592019-01-13 17:04:28 -0800880 exp_time.month,
881 exp_time.day_of_month,
882 exp_time.year,
883 exp_time.hour,
884 exp_time.minute,
885 exp_time.second);
Jay Srinivasan480ddfa2012-06-01 19:15:26 -0700886}
adlr@google.com3defe6a2009-12-04 20:57:17 +0000887
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700888string ToString(bool b) {
889 return (b ? "true" : "false");
890}
891
Alex Deymo1c656c42013-06-28 11:02:14 -0700892string ToString(DownloadSource source) {
Jay Srinivasan19409b72013-04-12 19:23:36 -0700893 switch (source) {
Amin Hassanib2689592019-01-13 17:04:28 -0800894 case kDownloadSourceHttpsServer:
895 return "HttpsServer";
896 case kDownloadSourceHttpServer:
897 return "HttpServer";
898 case kDownloadSourceHttpPeer:
899 return "HttpPeer";
900 case kNumDownloadSources:
901 return "Unknown";
902 // Don't add a default case to let the compiler warn about newly added
903 // download sources which should be added here.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700904 }
905
906 return "Unknown";
907}
908
Alex Deymo1c656c42013-06-28 11:02:14 -0700909string ToString(PayloadType payload_type) {
910 switch (payload_type) {
Amin Hassanib2689592019-01-13 17:04:28 -0800911 case kPayloadTypeDelta:
912 return "Delta";
913 case kPayloadTypeFull:
914 return "Full";
915 case kPayloadTypeForcedFull:
916 return "ForcedFull";
917 case kNumPayloadTypes:
918 return "Unknown";
919 // Don't add a default case to let the compiler warn about newly added
920 // payload types which should be added here.
Alex Deymo1c656c42013-06-28 11:02:14 -0700921 }
922
923 return "Unknown";
924}
925
David Zeuthena99981f2013-04-29 13:42:47 -0700926ErrorCode GetBaseErrorCode(ErrorCode code) {
Jay Srinivasanf0572052012-10-23 18:12:56 -0700927 // Ignore the higher order bits in the code by applying the mask as
928 // we want the enumerations to be in the small contiguous range
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700929 // with values less than ErrorCode::kUmaReportedMax.
930 ErrorCode base_code = static_cast<ErrorCode>(
931 static_cast<int>(code) & ~static_cast<int>(ErrorCode::kSpecialFlags));
Jay Srinivasanf0572052012-10-23 18:12:56 -0700932
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800933 // Make additional adjustments required for UMA and error classification.
934 // TODO(jaysri): Move this logic to UeErrorCode.cc when we fix
935 // chromium-os:34369.
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700936 if (base_code >= ErrorCode::kOmahaRequestHTTPResponseBase) {
Jay Srinivasanf0572052012-10-23 18:12:56 -0700937 // Since we want to keep the enums to a small value, aggregate all HTTP
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800938 // errors into this one bucket for UMA and error classification purposes.
Jay Srinivasan55f50c22013-01-10 19:24:35 -0800939 LOG(INFO) << "Converting error code " << base_code
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700940 << " to ErrorCode::kOmahaErrorInHTTPResponse";
941 base_code = ErrorCode::kOmahaErrorInHTTPResponse;
Jay Srinivasanf0572052012-10-23 18:12:56 -0700942 }
943
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800944 return base_code;
945}
946
Kelvin Zhangd7191032020-08-11 10:48:16 -0400947string StringVectorToString(const vector<string>& vec_str) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700948 string str = "[";
Amin Hassanib2689592019-01-13 17:04:28 -0800949 for (vector<string>::const_iterator i = vec_str.begin(); i != vec_str.end();
950 ++i) {
Alex Deymof329b932014-10-30 01:37:48 -0700951 if (i != vec_str.begin())
David Zeuthen27a48bc2013-08-06 12:06:29 -0700952 str += ", ";
953 str += '"';
954 str += *i;
955 str += '"';
956 }
957 str += "]";
958 return str;
959}
960
Sen Jiang2703ef42017-03-16 13:36:21 -0700961// The P2P file id should be the same for devices running new version and old
962// version so that they can share it with each other. The hash in the response
963// was base64 encoded, but now that we switched to use "hash_sha256" field which
964// is hex encoded, we have to convert them back to base64 for P2P. However, the
965// base64 encoded hash was base64 encoded here again historically for some
966// reason, so we keep the same behavior here.
967string CalculateP2PFileId(const brillo::Blob& payload_hash,
968 size_t payload_size) {
969 string encoded_hash = brillo::data_encoding::Base64Encode(
970 brillo::data_encoding::Base64Encode(payload_hash));
Alex Deymoc00c98a2015-03-17 17:38:00 -0700971 return base::StringPrintf("cros_update_size_%" PRIuS "_hash_%s",
Alex Vakulenko981a9fb2015-02-09 12:51:24 -0800972 payload_size,
973 encoded_hash.c_str());
David Zeuthen8f191b22013-08-06 12:27:50 -0700974}
975
Kelvin Zhangd7191032020-08-11 10:48:16 -0400976bool ConvertToOmahaInstallDate(Time time, int* out_num_days) {
David Zeuthen639aa362014-02-03 16:23:44 -0800977 time_t unix_time = time.ToTimeT();
978 // Output of: date +"%s" --date="Jan 1, 2007 0:00 PST".
979 const time_t kOmahaEpoch = 1167638400;
Amin Hassanib2689592019-01-13 17:04:28 -0800980 const int64_t kNumSecondsPerWeek = 7 * 24 * 3600;
David Zeuthen639aa362014-02-03 16:23:44 -0800981 const int64_t kNumDaysPerWeek = 7;
982
983 time_t omaha_time = unix_time - kOmahaEpoch;
984
985 if (omaha_time < 0)
986 return false;
987
988 // Note, as per the comment in utils.h we are deliberately not
989 // handling DST correctly.
990
991 int64_t num_weeks_since_omaha_epoch = omaha_time / kNumSecondsPerWeek;
992 *out_num_days = num_weeks_since_omaha_epoch * kNumDaysPerWeek;
993
994 return true;
995}
996
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700997bool GetMinorVersion(const brillo::KeyValueStore& store,
Alex Deymob42b98d2015-07-06 17:42:38 -0700998 uint32_t* minor_version) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700999 string result;
Alex Deymob42b98d2015-07-06 17:42:38 -07001000 if (store.GetString("PAYLOAD_MINOR_VERSION", &result)) {
Allie Wood425aa972015-02-17 14:47:17 -08001001 if (!base::StringToUint(result, minor_version)) {
1002 LOG(ERROR) << "StringToUint failed when parsing delta minor version.";
1003 return false;
1004 }
Allie Wood78750a42015-02-11 15:42:11 -08001005 return true;
1006 }
1007 return false;
1008}
1009
Kelvin Zhang02fe6622021-11-01 16:37:58 -07001010bool ReadExtents(const std::string& path,
1011 const google::protobuf::RepeatedPtrField<Extent>& extents,
1012 brillo::Blob* out_data,
1013 size_t block_size) {
1014 return ReadExtents(path,
1015 {extents.begin(), extents.end()},
1016 out_data,
1017 utils::BlocksInExtents(extents) * block_size,
1018 block_size);
1019}
1020
1021bool WriteExtents(const std::string& path,
1022 const google::protobuf::RepeatedPtrField<Extent>& extents,
1023 const brillo::Blob& data,
1024 size_t block_size) {
1025 EintrSafeFileDescriptor fd;
1026 TEST_AND_RETURN_FALSE(fd.Open(path.c_str(), O_RDWR));
1027 size_t bytes_written = 0;
1028 for (const auto& ext : extents) {
1029 TEST_AND_RETURN_FALSE_ERRNO(
1030 fd.Seek(ext.start_block() * block_size, SEEK_SET));
1031 TEST_AND_RETURN_FALSE_ERRNO(
1032 fd.Write(data.data() + bytes_written, ext.num_blocks() * block_size));
1033 bytes_written += ext.num_blocks() * block_size;
1034 }
1035 return true;
1036}
Kelvin Zhang4eae81e2021-12-09 17:07:17 -08001037bool ReadExtents(const std::string& path,
1038 const vector<Extent>& extents,
1039 brillo::Blob* out_data,
1040 ssize_t out_data_size,
1041 size_t block_size) {
1042 FileDescriptorPtr fd = std::make_shared<EintrSafeFileDescriptor>();
1043 fd->Open(path.c_str(), O_RDONLY);
1044 return ReadExtents(fd, extents, out_data, out_data_size, block_size);
1045}
Kelvin Zhang02fe6622021-11-01 16:37:58 -07001046
Kelvin Zhang4eae81e2021-12-09 17:07:17 -08001047bool ReadExtents(FileDescriptorPtr fd,
1048 const google::protobuf::RepeatedPtrField<Extent>& extents,
1049 brillo::Blob* out_data,
1050 size_t block_size) {
1051 return ReadExtents(fd,
1052 {extents.begin(), extents.end()},
1053 out_data,
1054 utils::BlocksInExtents(extents) * block_size,
1055 block_size);
1056}
1057
1058bool ReadExtents(FileDescriptorPtr fd,
Amin Hassanib2689592019-01-13 17:04:28 -08001059 const vector<Extent>& extents,
1060 brillo::Blob* out_data,
1061 ssize_t out_data_size,
Allie Wood56873452015-03-27 17:48:40 -07001062 size_t block_size) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -07001063 brillo::Blob data(out_data_size);
Allie Wood56873452015-03-27 17:48:40 -07001064 ssize_t bytes_read = 0;
Allie Wood56873452015-03-27 17:48:40 -07001065
Gilad Arnold41e34742015-05-11 11:31:50 -07001066 for (const Extent& extent : extents) {
Allie Wood56873452015-03-27 17:48:40 -07001067 ssize_t bytes_read_this_iteration = 0;
1068 ssize_t bytes = extent.num_blocks() * block_size;
Kelvin Zhang02fe6622021-11-01 16:37:58 -07001069 TEST_LE(bytes_read + bytes, out_data_size);
Allie Wood56873452015-03-27 17:48:40 -07001070 TEST_AND_RETURN_FALSE(utils::PReadAll(fd,
1071 &data[bytes_read],
1072 bytes,
1073 extent.start_block() * block_size,
1074 &bytes_read_this_iteration));
1075 TEST_AND_RETURN_FALSE(bytes_read_this_iteration == bytes);
1076 bytes_read += bytes_read_this_iteration;
1077 }
1078 TEST_AND_RETURN_FALSE(out_data_size == bytes_read);
1079 *out_data = data;
1080 return true;
1081}
1082
Matt Ziegelbaum91ba9be2020-06-10 16:56:40 -04001083bool GetVpdValue(string key, string* result) {
1084 int exit_code = 0;
1085 string value, error;
1086 vector<string> cmd = {"vpd_get_value", key};
1087 if (!chromeos_update_engine::Subprocess::SynchronousExec(
1088 cmd, &exit_code, &value, &error) ||
1089 exit_code) {
1090 LOG(ERROR) << "Failed to get vpd key for " << value
1091 << " with exit code: " << exit_code << " and error: " << error;
1092 return false;
1093 } else if (!error.empty()) {
1094 LOG(INFO) << "vpd_get_value succeeded but with following errors: " << error;
1095 }
1096
1097 base::TrimWhitespaceASCII(value, base::TRIM_ALL, &value);
1098 *result = value;
1099 return true;
1100}
1101
Alex Deymodd132f32015-09-14 19:12:07 -07001102bool GetBootId(string* boot_id) {
1103 TEST_AND_RETURN_FALSE(
1104 base::ReadFileToString(base::FilePath(kBootIdPath), boot_id));
1105 base::TrimWhitespaceASCII(*boot_id, base::TRIM_TRAILING, boot_id);
1106 return true;
1107}
1108
Marton Hunyadya0302682018-05-16 18:52:13 +02001109int VersionPrefix(const std::string& version) {
1110 if (version.empty()) {
1111 return 0;
1112 }
1113 vector<string> tokens = base::SplitString(
1114 version, ".", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
Daniel Zheng3e881aa2022-09-07 22:10:29 +00001115 int value{};
Marton Hunyadya0302682018-05-16 18:52:13 +02001116 if (tokens.empty() || !base::StringToInt(tokens[0], &value))
1117 return -1; // Target version is invalid.
1118 return value;
1119}
1120
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07001121void ParseRollbackKeyVersion(const string& raw_version,
1122 uint16_t* high_version,
1123 uint16_t* low_version) {
1124 DCHECK(high_version);
1125 DCHECK(low_version);
1126 *high_version = numeric_limits<uint16_t>::max();
1127 *low_version = numeric_limits<uint16_t>::max();
1128
1129 vector<string> parts = base::SplitString(
1130 raw_version, ".", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
1131 if (parts.size() != 2) {
1132 // The version string must have exactly one period.
1133 return;
1134 }
1135
Daniel Zheng3e881aa2022-09-07 22:10:29 +00001136 int high{};
1137 int low{};
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -07001138 if (!(base::StringToInt(parts[0], &high) &&
1139 base::StringToInt(parts[1], &low))) {
1140 // Both parts of the version could not be parsed correctly.
1141 return;
1142 }
1143
1144 if (high >= 0 && high < numeric_limits<uint16_t>::max() && low >= 0 &&
1145 low < numeric_limits<uint16_t>::max()) {
1146 *high_version = static_cast<uint16_t>(high);
1147 *low_version = static_cast<uint16_t>(low);
1148 }
1149}
1150
Kyeongkab.Nam500ca132019-06-26 13:48:07 +09001151string GetFilePath(int fd) {
1152 base::FilePath proc("/proc/self/fd/" + std::to_string(fd));
1153 base::FilePath file_name;
1154
1155 if (!base::ReadSymbolicLink(proc, &file_name)) {
1156 return "not found";
1157 }
1158 return file_name.value();
1159}
1160
Yifan Hongc80de2d2020-02-25 17:13:53 -08001161string GetTimeAsString(time_t utime) {
Daniel Zheng3e881aa2022-09-07 22:10:29 +00001162 struct tm tm {};
Yifan Hongc80de2d2020-02-25 17:13:53 -08001163 CHECK_EQ(localtime_r(&utime, &tm), &tm);
1164 char str[16];
1165 CHECK_EQ(strftime(str, sizeof(str), "%Y%m%d-%H%M%S", &tm), 15u);
1166 return str;
1167}
1168
Jae Hoon Kim694eeb02020-06-01 14:24:08 -07001169string GetExclusionName(const string& str_to_convert) {
1170 return base::NumberToString(base::StringPieceHash()(str_to_convert));
1171}
1172
Kelvin Zhang636ba2f2022-02-09 14:40:22 -08001173static bool ParseTimestamp(std::string_view str, int64_t* out) {
1174 if (!base::StringToInt64(base::StringPiece(str.data(), str.size()), out)) {
Kelvin Zhangd7191032020-08-11 10:48:16 -04001175 LOG(WARNING) << "Invalid timestamp: " << str;
1176 return false;
1177 }
1178 return true;
1179}
1180
Kelvin Zhang636ba2f2022-02-09 14:40:22 -08001181ErrorCode IsTimestampNewer(const std::string_view old_version,
1182 const std::string_view new_version) {
Kelvin Zhangd7191032020-08-11 10:48:16 -04001183 if (old_version.empty() || new_version.empty()) {
1184 LOG(WARNING)
1185 << "One of old/new timestamp is empty, permit update anyway. Old: "
1186 << old_version << " New: " << new_version;
Yifan Hong87029332020-09-01 17:20:08 -07001187 return ErrorCode::kSuccess;
Kelvin Zhangd7191032020-08-11 10:48:16 -04001188 }
1189 int64_t old_ver = 0;
Yifan Hong87029332020-09-01 17:20:08 -07001190 if (!ParseTimestamp(old_version, &old_ver)) {
1191 return ErrorCode::kError;
1192 }
Kelvin Zhangd7191032020-08-11 10:48:16 -04001193 int64_t new_ver = 0;
Yifan Hong87029332020-09-01 17:20:08 -07001194 if (!ParseTimestamp(new_version, &new_ver)) {
1195 return ErrorCode::kDownloadManifestParseError;
1196 }
1197 if (old_ver > new_ver) {
1198 return ErrorCode::kPayloadTimestampError;
1199 }
1200 return ErrorCode::kSuccess;
Kelvin Zhangd7191032020-08-11 10:48:16 -04001201}
1202
Kelvin Zhang4bb49202021-07-08 21:39:05 -04001203std::unique_ptr<android::base::MappedFile> GetReadonlyZeroBlock(size_t size) {
1204 android::base::unique_fd fd{HANDLE_EINTR(open("/dev/zero", O_RDONLY))};
1205 return android::base::MappedFile::FromFd(fd, 0, size, PROT_READ);
1206}
1207
Kelvin Zhang3fe49642021-10-04 15:35:02 -07001208std::string_view GetReadonlyZeroString(size_t size) {
1209 // Reserve 512MB of Virtual Address Space. No actual memory will be used.
1210 static auto zero_block = GetReadonlyZeroBlock(1024 * 1024 * 512);
1211 if (size > zero_block->size()) {
1212 auto larger_block = GetReadonlyZeroBlock(size);
1213 zero_block = std::move(larger_block);
1214 }
1215 return {zero_block->data(), size};
1216}
1217
adlr@google.com3defe6a2009-12-04 20:57:17 +00001218} // namespace utils
1219
Kelvin Zhang20982a52021-08-13 12:31:16 -07001220std::string HexEncode(const brillo::Blob& blob) noexcept {
1221 return base::HexEncode(blob.data(), blob.size());
1222}
1223
Kelvin Zhang3fe49642021-10-04 15:35:02 -07001224std::string HexEncode(const std::string_view blob) noexcept {
1225 return base::HexEncode(blob.data(), blob.size());
1226}
1227
Kelvin Zhang760c3342022-02-08 13:41:49 -08001228[[nodiscard]] std::string_view ToStringView(
1229 const std::vector<unsigned char>& blob) noexcept {
1230 return std::string_view{reinterpret_cast<const char*>(blob.data()),
1231 blob.size()};
1232}
1233
1234[[nodiscard]] std::string_view ToStringView(const void* data,
1235 size_t size) noexcept {
1236 return std::string_view(reinterpret_cast<const char*>(data), size);
1237}
1238
adlr@google.com3defe6a2009-12-04 20:57:17 +00001239} // namespace chromeos_update_engine