Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 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.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 16 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #include "update_engine/common/utils.h" |
Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 18 | |
Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 19 | #include <stdint.h> |
| 20 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 21 | #include <dirent.h> |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 22 | #include <elf.h> |
Alex Deymo | 6f20dd4 | 2015-08-18 16:42:46 -0700 | [diff] [blame] | 23 | #include <endian.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 24 | #include <errno.h> |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 25 | #include <fcntl.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
Alex Deymo | c4acdf4 | 2014-05-28 21:07:10 -0700 | [diff] [blame] | 29 | #include <sys/mount.h> |
| 30 | #include <sys/resource.h> |
Kelvin Zhang | 8933d57 | 2021-01-28 10:17:34 -0500 | [diff] [blame] | 31 | #include <sys/sendfile.h> |
Alex Deymo | c4acdf4 | 2014-05-28 21:07:10 -0700 | [diff] [blame] | 32 | #include <sys/stat.h> |
| 33 | #include <sys/types.h> |
Yifan Hong | c80de2d | 2020-02-25 17:13:53 -0800 | [diff] [blame] | 34 | #include <time.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 35 | #include <unistd.h> |
Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 36 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 37 | #include <algorithm> |
Kelvin Zhang | 49170aa | 2022-11-28 10:55:16 -0800 | [diff] [blame] | 38 | #include <filesystem> |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 39 | #include <utility> |
Chris Sosa | c197248 | 2013-04-30 22:31:10 -0700 | [diff] [blame] | 40 | #include <vector> |
Darin Petkov | f74eb65 | 2010-08-04 12:08:38 -0700 | [diff] [blame] | 41 | |
Colin Cross | 84fe9da | 2021-12-21 13:19:14 -0800 | [diff] [blame] | 42 | #include <android-base/strings.h> |
Alex Vakulenko | 4906c1c | 2014-08-21 13:17:44 -0700 | [diff] [blame] | 43 | #include <base/callback.h> |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 44 | #include <base/files/file_path.h> |
Alex Deymo | 192393b | 2014-11-10 15:58:38 -0800 | [diff] [blame] | 45 | #include <base/files/file_util.h> |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 46 | #include <base/files/scoped_file.h> |
Alex Deymo | c00c98a | 2015-03-17 17:38:00 -0700 | [diff] [blame] | 47 | #include <base/format_macros.h> |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 48 | #include <base/location.h> |
Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame] | 49 | #include <base/logging.h> |
Chris Sosa | fc661a1 | 2013-02-26 14:43:21 -0800 | [diff] [blame] | 50 | #include <base/posix/eintr_wrapper.h> |
Will Drewry | 8f71da8 | 2010-08-30 14:07:11 -0500 | [diff] [blame] | 51 | #include <base/rand_util.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 52 | #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 Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 56 | #include <brillo/data_encoding.h> |
Will Drewry | 8f71da8 | 2010-08-30 14:07:11 -0500 | [diff] [blame] | 57 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 58 | #include "update_engine/common/constants.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 59 | #include "update_engine/common/subprocess.h" |
Kelvin Zhang | e47767a | 2023-05-16 13:00:58 -0700 | [diff] [blame] | 60 | #ifdef __ANDROID__ |
Kelvin Zhang | 32a73a9 | 2022-12-19 12:27:41 -0800 | [diff] [blame] | 61 | #include "update_engine/common/platform_constants.h" |
Kelvin Zhang | e47767a | 2023-05-16 13:00:58 -0700 | [diff] [blame] | 62 | #endif |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 63 | #include "update_engine/payload_consumer/file_descriptor.h" |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 64 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 65 | using base::Time; |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 66 | using base::TimeDelta; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 67 | using std::min; |
Zentaro Kavanagh | 0ff621c | 2018-07-13 13:06:56 -0700 | [diff] [blame] | 68 | using std::numeric_limits; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 69 | using std::string; |
| 70 | using std::vector; |
| 71 | |
| 72 | namespace chromeos_update_engine { |
| 73 | |
Ben Chan | 77a1eba | 2012-10-07 22:54:55 -0700 | [diff] [blame] | 74 | namespace { |
| 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. |
| 79 | const int kUnmountMaxNumOfRetries = 5; |
| 80 | const int kUnmountRetryIntervalInMicroseconds = 200 * 1000; // 200 ms |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 81 | |
| 82 | // Number of bytes to read from a file to attempt to detect its contents. Used |
| 83 | // in GetFileFormat. |
| 84 | const int kGetFileFormatMaxHeaderSize = 32; |
| 85 | |
Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 86 | // The path to the kernel's boot_id. |
| 87 | const char kBootIdPath[] = "/proc/sys/kernel/random/boot_id"; |
| 88 | |
Kelvin Zhang | fab31ad | 2024-03-26 12:56:08 -0700 | [diff] [blame] | 89 | } // namespace |
Alex Deymo | 5aa1c54 | 2015-09-18 01:02:33 -0700 | [diff] [blame] | 90 | // If |path| is absolute, or explicit relative to the current working directory, |
| 91 | // leaves it as is. Otherwise, uses the system's temp directory, as defined by |
| 92 | // base::GetTempDir() and prepends it to |path|. On success stores the full |
| 93 | // temporary path in |template_path| and returns true. |
| 94 | bool GetTempName(const string& path, base::FilePath* template_path) { |
Alex Vakulenko | 0103c36 | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 95 | if (path[0] == '/' || |
| 96 | base::StartsWith(path, "./", base::CompareCase::SENSITIVE) || |
| 97 | base::StartsWith(path, "../", base::CompareCase::SENSITIVE)) { |
Alex Deymo | 5aa1c54 | 2015-09-18 01:02:33 -0700 | [diff] [blame] | 98 | *template_path = base::FilePath(path); |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | base::FilePath temp_dir; |
Sen Jiang | 9c12346 | 2015-11-19 13:16:23 -0800 | [diff] [blame] | 103 | #ifdef __ANDROID__ |
Sen Jiang | c2b3766 | 2019-01-09 16:51:18 -0800 | [diff] [blame] | 104 | temp_dir = base::FilePath(constants::kNonVolatileDirectory).Append("tmp"); |
Alex Deymo | 3295102 | 2016-08-10 17:02:49 -0700 | [diff] [blame] | 105 | #else |
Sen Jiang | c2b3766 | 2019-01-09 16:51:18 -0800 | [diff] [blame] | 106 | TEST_AND_RETURN_FALSE(base::GetTempDir(&temp_dir)); |
Alex Deymo | 3295102 | 2016-08-10 17:02:49 -0700 | [diff] [blame] | 107 | #endif // __ANDROID__ |
Sen Jiang | 9c12346 | 2015-11-19 13:16:23 -0800 | [diff] [blame] | 108 | if (!base::PathExists(temp_dir)) |
| 109 | TEST_AND_RETURN_FALSE(base::CreateDirectory(temp_dir)); |
Alex Deymo | 5aa1c54 | 2015-09-18 01:02:33 -0700 | [diff] [blame] | 110 | *template_path = temp_dir.Append(path); |
| 111 | return true; |
| 112 | } |
| 113 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 114 | namespace utils { |
| 115 | |
Alex Deymo | 335516c | 2016-11-28 13:37:06 -0800 | [diff] [blame] | 116 | bool WriteFile(const char* path, const void* data, size_t data_len) { |
| 117 | int fd = HANDLE_EINTR(open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600)); |
| 118 | TEST_AND_RETURN_FALSE_ERRNO(fd >= 0); |
| 119 | ScopedFdCloser fd_closer(&fd); |
| 120 | return WriteAll(fd, data, data_len); |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 123 | bool ReadAll( |
| 124 | int fd, void* buf, size_t count, size_t* out_bytes_read, bool* eof) { |
| 125 | char* c_buf = static_cast<char*>(buf); |
| 126 | size_t bytes_read = 0; |
| 127 | *eof = false; |
| 128 | while (bytes_read < count) { |
| 129 | ssize_t rc = HANDLE_EINTR(read(fd, c_buf + bytes_read, count - bytes_read)); |
| 130 | if (rc < 0) { |
| 131 | // EAGAIN and EWOULDBLOCK are normal return values when there's no more |
| 132 | // input and we are in non-blocking mode. |
| 133 | if (errno != EWOULDBLOCK && errno != EAGAIN) { |
| 134 | PLOG(ERROR) << "Error reading fd " << fd; |
| 135 | *out_bytes_read = bytes_read; |
| 136 | return false; |
| 137 | } |
| 138 | break; |
| 139 | } else if (rc == 0) { |
| 140 | // A value of 0 means that we reached EOF and there is nothing else to |
| 141 | // read from this fd. |
| 142 | *eof = true; |
| 143 | break; |
| 144 | } else { |
| 145 | bytes_read += rc; |
| 146 | } |
| 147 | } |
| 148 | *out_bytes_read = bytes_read; |
| 149 | return true; |
| 150 | } |
| 151 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 152 | bool WriteAll(int fd, const void* buf, size_t count) { |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 153 | const char* c_buf = static_cast<const char*>(buf); |
| 154 | ssize_t bytes_written = 0; |
| 155 | while (bytes_written < static_cast<ssize_t>(count)) { |
| 156 | ssize_t rc = write(fd, c_buf + bytes_written, count - bytes_written); |
| 157 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 158 | bytes_written += rc; |
| 159 | } |
| 160 | return true; |
| 161 | } |
| 162 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 163 | bool PWriteAll(int fd, const void* buf, size_t count, off_t offset) { |
| 164 | const char* c_buf = static_cast<const char*>(buf); |
Gilad Arnold | 780db21 | 2012-07-11 13:12:49 -0700 | [diff] [blame] | 165 | size_t bytes_written = 0; |
| 166 | int num_attempts = 0; |
| 167 | while (bytes_written < count) { |
| 168 | num_attempts++; |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 169 | ssize_t rc = pwrite(fd, |
| 170 | c_buf + bytes_written, |
| 171 | count - bytes_written, |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 172 | offset + bytes_written); |
Gilad Arnold | 780db21 | 2012-07-11 13:12:49 -0700 | [diff] [blame] | 173 | // TODO(garnold) for debugging failure in chromium-os:31077; to be removed. |
| 174 | if (rc < 0) { |
| 175 | PLOG(ERROR) << "pwrite error; num_attempts=" << num_attempts |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 176 | << " bytes_written=" << bytes_written << " count=" << count |
| 177 | << " offset=" << offset; |
Gilad Arnold | 780db21 | 2012-07-11 13:12:49 -0700 | [diff] [blame] | 178 | } |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 179 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 180 | bytes_written += rc; |
| 181 | } |
| 182 | return true; |
| 183 | } |
| 184 | |
Kelvin Zhang | 1a0ed71 | 2022-01-26 16:09:05 -0800 | [diff] [blame] | 185 | bool WriteAll(FileDescriptor* fd, const void* buf, size_t count) { |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 186 | const char* c_buf = static_cast<const char*>(buf); |
| 187 | ssize_t bytes_written = 0; |
| 188 | while (bytes_written < static_cast<ssize_t>(count)) { |
| 189 | ssize_t rc = fd->Write(c_buf + bytes_written, count - bytes_written); |
| 190 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 191 | bytes_written += rc; |
| 192 | } |
| 193 | return true; |
| 194 | } |
| 195 | |
Kelvin Zhang | 4b28024 | 2020-11-06 16:07:45 -0500 | [diff] [blame] | 196 | bool WriteAll(const FileDescriptorPtr& fd, |
| 197 | const void* buf, |
| 198 | size_t count, |
| 199 | off_t offset) { |
Allie Wood | 0c8cf7e | 2015-04-23 19:24:49 -0700 | [diff] [blame] | 200 | TEST_AND_RETURN_FALSE_ERRNO(fd->Seek(offset, SEEK_SET) != |
| 201 | static_cast<off_t>(-1)); |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 202 | return WriteAll(fd, buf, count); |
| 203 | } |
| 204 | |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 205 | bool PReadAll( |
| 206 | int fd, void* buf, size_t count, off_t offset, ssize_t* out_bytes_read) { |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 207 | char* c_buf = static_cast<char*>(buf); |
| 208 | ssize_t bytes_read = 0; |
| 209 | while (bytes_read < static_cast<ssize_t>(count)) { |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 210 | ssize_t rc = |
| 211 | pread(fd, c_buf + bytes_read, count - bytes_read, offset + bytes_read); |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 212 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 213 | if (rc == 0) { |
| 214 | break; |
| 215 | } |
| 216 | bytes_read += rc; |
| 217 | } |
| 218 | *out_bytes_read = bytes_read; |
| 219 | return true; |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Kelvin Zhang | 1a0ed71 | 2022-01-26 16:09:05 -0800 | [diff] [blame] | 222 | bool ReadAll(FileDescriptor* fd, |
Kelvin Zhang | 4b28024 | 2020-11-06 16:07:45 -0500 | [diff] [blame] | 223 | void* buf, |
| 224 | size_t count, |
| 225 | off_t offset, |
| 226 | ssize_t* out_bytes_read) { |
Allie Wood | 0c8cf7e | 2015-04-23 19:24:49 -0700 | [diff] [blame] | 227 | TEST_AND_RETURN_FALSE_ERRNO(fd->Seek(offset, SEEK_SET) != |
| 228 | static_cast<off_t>(-1)); |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 229 | char* c_buf = static_cast<char*>(buf); |
| 230 | ssize_t bytes_read = 0; |
| 231 | while (bytes_read < static_cast<ssize_t>(count)) { |
| 232 | ssize_t rc = fd->Read(c_buf + bytes_read, count - bytes_read); |
| 233 | TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 234 | if (rc == 0) { |
| 235 | break; |
| 236 | } |
| 237 | bytes_read += rc; |
| 238 | } |
| 239 | *out_bytes_read = bytes_read; |
| 240 | return true; |
| 241 | } |
| 242 | |
Kelvin Zhang | 1a0ed71 | 2022-01-26 16:09:05 -0800 | [diff] [blame] | 243 | bool PReadAll(FileDescriptor* fd, |
Kelvin Zhang | c3c0a3a | 2020-11-09 16:08:13 -0500 | [diff] [blame] | 244 | void* buf, |
| 245 | size_t count, |
| 246 | off_t offset, |
| 247 | ssize_t* out_bytes_read) { |
| 248 | auto old_off = fd->Seek(0, SEEK_CUR); |
| 249 | TEST_AND_RETURN_FALSE_ERRNO(old_off >= 0); |
| 250 | |
| 251 | auto success = ReadAll(fd, buf, count, offset, out_bytes_read); |
| 252 | TEST_AND_RETURN_FALSE_ERRNO(fd->Seek(old_off, SEEK_SET) == old_off); |
| 253 | return success; |
| 254 | } |
| 255 | |
| 256 | bool PWriteAll(const FileDescriptorPtr& fd, |
| 257 | const void* buf, |
| 258 | size_t count, |
| 259 | off_t offset) { |
| 260 | auto old_off = fd->Seek(0, SEEK_CUR); |
| 261 | TEST_AND_RETURN_FALSE_ERRNO(old_off >= 0); |
| 262 | |
| 263 | auto success = WriteAll(fd, buf, count, offset); |
| 264 | TEST_AND_RETURN_FALSE_ERRNO(fd->Seek(old_off, SEEK_SET) == old_off); |
| 265 | return success; |
| 266 | } |
| 267 | |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 268 | // Append |nbytes| of content from |buf| to the vector pointed to by either |
| 269 | // |vec_p| or |str_p|. |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 270 | static void AppendBytes(const uint8_t* buf, |
| 271 | size_t nbytes, |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 272 | brillo::Blob* vec_p) { |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 273 | CHECK(buf); |
| 274 | CHECK(vec_p); |
| 275 | vec_p->insert(vec_p->end(), buf, buf + nbytes); |
| 276 | } |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 277 | static void AppendBytes(const uint8_t* buf, size_t nbytes, string* str_p) { |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 278 | CHECK(buf); |
| 279 | CHECK(str_p); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 280 | str_p->append(buf, buf + nbytes); |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | // Reads from an open file |fp|, appending the read content to the container |
| 284 | // pointer to by |out_p|. Returns true upon successful reading all of the |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 285 | // file's content, false otherwise. If |size| is not -1, reads up to |size| |
| 286 | // bytes. |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 287 | template <class T> |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 288 | static bool Read(FILE* fp, off_t size, T* out_p) { |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 289 | CHECK(fp); |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 290 | CHECK(size == -1 || size >= 0); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 291 | uint8_t buf[1024]; |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 292 | while (size == -1 || size > 0) { |
| 293 | off_t bytes_to_read = sizeof(buf); |
| 294 | if (size > 0 && bytes_to_read > size) { |
| 295 | bytes_to_read = size; |
| 296 | } |
| 297 | size_t nbytes = fread(buf, 1, bytes_to_read, fp); |
| 298 | if (!nbytes) { |
| 299 | break; |
| 300 | } |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 301 | AppendBytes(buf, nbytes, out_p); |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 302 | if (size != -1) { |
| 303 | CHECK(size >= static_cast<off_t>(nbytes)); |
| 304 | size -= nbytes; |
| 305 | } |
| 306 | } |
| 307 | if (ferror(fp)) { |
| 308 | return false; |
| 309 | } |
| 310 | return size == 0 || feof(fp); |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 313 | // Opens a file |path| for reading and appends its the contents to a container |
| 314 | // |out_p|. Starts reading the file from |offset|. If |offset| is beyond the end |
| 315 | // of the file, returns success. If |size| is not -1, reads up to |size| bytes. |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 316 | template <class T> |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 317 | static bool ReadFileChunkAndAppend(const string& path, |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 318 | off_t offset, |
| 319 | off_t size, |
| 320 | T* out_p) { |
| 321 | CHECK_GE(offset, 0); |
| 322 | CHECK(size == -1 || size >= 0); |
Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 323 | base::ScopedFILE fp(fopen(path.c_str(), "r")); |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 324 | if (!fp.get()) |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 325 | return false; |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 326 | if (offset) { |
| 327 | // Return success without appending any data if a chunk beyond the end of |
| 328 | // the file is requested. |
| 329 | if (offset >= FileSize(path)) { |
| 330 | return true; |
| 331 | } |
| 332 | TEST_AND_RETURN_FALSE_ERRNO(fseek(fp.get(), offset, SEEK_SET) == 0); |
| 333 | } |
| 334 | return Read(fp.get(), size, out_p); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 337 | // TODO(deymo): This is only used in unittest, but requires the private |
| 338 | // Read<string>() defined here. Expose Read<string>() or move to base/ version. |
| 339 | bool ReadPipe(const string& cmd, string* out_p) { |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 340 | FILE* fp = popen(cmd.c_str(), "r"); |
| 341 | if (!fp) |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 342 | return false; |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 343 | bool success = Read(fp, -1, out_p); |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 344 | return (success && pclose(fp) >= 0); |
| 345 | } |
| 346 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 347 | bool ReadFile(const string& path, brillo::Blob* out_p) { |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 348 | return ReadFileChunkAndAppend(path, 0, -1, out_p); |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 351 | bool ReadFile(const string& path, string* out_p) { |
| 352 | return ReadFileChunkAndAppend(path, 0, -1, out_p); |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 355 | bool ReadFileChunk(const string& path, |
| 356 | off_t offset, |
| 357 | off_t size, |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 358 | brillo::Blob* out_p) { |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 359 | return ReadFileChunkAndAppend(path, offset, size, out_p); |
| 360 | } |
| 361 | |
Gabe Black | b92cd2e | 2014-09-08 02:47:41 -0700 | [diff] [blame] | 362 | off_t BlockDevSize(int fd) { |
Daniel Zheng | 3e881aa | 2022-09-07 22:10:29 +0000 | [diff] [blame] | 363 | uint64_t dev_size{}; |
Gabe Black | b92cd2e | 2014-09-08 02:47:41 -0700 | [diff] [blame] | 364 | int rc = ioctl(fd, BLKGETSIZE64, &dev_size); |
| 365 | if (rc == -1) { |
| 366 | dev_size = -1; |
| 367 | PLOG(ERROR) << "Error running ioctl(BLKGETSIZE64) on " << fd; |
| 368 | } |
| 369 | return dev_size; |
| 370 | } |
| 371 | |
Gabe Black | b92cd2e | 2014-09-08 02:47:41 -0700 | [diff] [blame] | 372 | off_t FileSize(int fd) { |
Daniel Zheng | 3e881aa | 2022-09-07 22:10:29 +0000 | [diff] [blame] | 373 | struct stat stbuf {}; |
Gabe Black | b92cd2e | 2014-09-08 02:47:41 -0700 | [diff] [blame] | 374 | int rc = fstat(fd, &stbuf); |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 375 | CHECK_EQ(rc, 0); |
Gabe Black | b92cd2e | 2014-09-08 02:47:41 -0700 | [diff] [blame] | 376 | if (rc < 0) { |
| 377 | PLOG(ERROR) << "Error stat-ing " << fd; |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 378 | return rc; |
Gabe Black | b92cd2e | 2014-09-08 02:47:41 -0700 | [diff] [blame] | 379 | } |
| 380 | if (S_ISREG(stbuf.st_mode)) |
| 381 | return stbuf.st_size; |
| 382 | if (S_ISBLK(stbuf.st_mode)) |
| 383 | return BlockDevSize(fd); |
| 384 | LOG(ERROR) << "Couldn't determine the type of " << fd; |
| 385 | return -1; |
| 386 | } |
| 387 | |
| 388 | off_t FileSize(const string& path) { |
| 389 | int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC); |
| 390 | if (fd == -1) { |
| 391 | PLOG(ERROR) << "Error opening " << path; |
| 392 | return fd; |
| 393 | } |
| 394 | off_t size = FileSize(fd); |
| 395 | if (size == -1) |
| 396 | PLOG(ERROR) << "Error getting file size of " << path; |
| 397 | close(fd); |
| 398 | return size; |
Andrew de los Reyes | f4c7ef1 | 2010-04-30 10:37:00 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Kelvin Zhang | 02fe662 | 2021-11-01 16:37:58 -0700 | [diff] [blame] | 401 | bool SendFile(int out_fd, int in_fd, size_t count) { |
| 402 | off64_t offset = lseek(in_fd, 0, SEEK_CUR); |
| 403 | TEST_AND_RETURN_FALSE_ERRNO(offset >= 0); |
| 404 | constexpr size_t BUFFER_SIZE = 4096; |
| 405 | while (count > 0) { |
| 406 | const auto bytes_written = |
| 407 | sendfile(out_fd, in_fd, &offset, std::min(count, BUFFER_SIZE)); |
| 408 | TEST_AND_RETURN_FALSE_ERRNO(bytes_written > 0); |
| 409 | count -= bytes_written; |
| 410 | } |
| 411 | return true; |
| 412 | } |
| 413 | |
Kelvin Zhang | c80d39b | 2023-10-25 09:30:16 -0700 | [diff] [blame] | 414 | bool DeleteDirectory(const char* dirname) { |
Daniel Zheng | 6a00fe9 | 2024-04-09 14:44:21 -0700 | [diff] [blame^] | 415 | if (!std::filesystem::exists(dirname)) { |
| 416 | return true; |
| 417 | } |
Kelvin Zhang | c80d39b | 2023-10-25 09:30:16 -0700 | [diff] [blame] | 418 | const std::string tmpdir = std::string(dirname) + "_deleted"; |
| 419 | std::filesystem::remove_all(tmpdir); |
| 420 | if (rename(dirname, tmpdir.c_str()) != 0) { |
| 421 | PLOG(ERROR) << "Failed to rename " << dirname << " to " << tmpdir; |
| 422 | return false; |
| 423 | } |
| 424 | std::filesystem::remove_all(tmpdir); |
| 425 | return true; |
| 426 | } |
| 427 | |
Kelvin Zhang | 49170aa | 2022-11-28 10:55:16 -0800 | [diff] [blame] | 428 | bool FsyncDirectory(const char* dirname) { |
| 429 | android::base::unique_fd fd( |
| 430 | TEMP_FAILURE_RETRY(open(dirname, O_RDONLY | O_CLOEXEC))); |
| 431 | if (fd == -1) { |
| 432 | PLOG(ERROR) << "Failed to open " << dirname; |
| 433 | return false; |
| 434 | } |
| 435 | if (fsync(fd) == -1) { |
| 436 | if (errno == EROFS || errno == EINVAL) { |
| 437 | PLOG(WARNING) << "Skip fsync " << dirname |
| 438 | << " on a file system does not support synchronization"; |
| 439 | } else { |
| 440 | PLOG(ERROR) << "Failed to fsync " << dirname; |
| 441 | return false; |
| 442 | } |
| 443 | } |
| 444 | return true; |
| 445 | } |
| 446 | |
| 447 | bool WriteStringToFileAtomic(const std::string& path, |
| 448 | std::string_view content) { |
| 449 | const std::string tmp_path = path + ".tmp"; |
| 450 | { |
| 451 | const int flags = O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC; |
| 452 | android::base::unique_fd fd( |
| 453 | TEMP_FAILURE_RETRY(open(tmp_path.c_str(), flags, 0644))); |
| 454 | if (fd == -1) { |
| 455 | PLOG(ERROR) << "Failed to open " << path; |
| 456 | return false; |
| 457 | } |
| 458 | if (!WriteAll(fd.get(), content.data(), content.size())) { |
| 459 | PLOG(ERROR) << "Failed to write to fd " << fd; |
| 460 | return false; |
| 461 | } |
| 462 | // rename() without fsync() is not safe. Data could still be living on page |
| 463 | // cache. To ensure atomiticity, call fsync() |
| 464 | if (fsync(fd) != 0) { |
| 465 | PLOG(ERROR) << "Failed to fsync " << tmp_path; |
| 466 | } |
| 467 | } |
| 468 | if (rename(tmp_path.c_str(), path.c_str()) == -1) { |
| 469 | PLOG(ERROR) << "rename failed from " << tmp_path << " to " << path; |
| 470 | return false; |
| 471 | } |
| 472 | return FsyncDirectory(std::filesystem::path(path).parent_path().c_str()); |
| 473 | } |
| 474 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 475 | void HexDumpArray(const uint8_t* const arr, const size_t length) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 476 | LOG(INFO) << "Logging array of length: " << length; |
| 477 | const unsigned int bytes_per_line = 16; |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 478 | for (uint32_t i = 0; i < length; i += bytes_per_line) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 479 | const unsigned int bytes_remaining = length - i; |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 480 | const unsigned int bytes_per_this_line = |
| 481 | min(bytes_per_line, bytes_remaining); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 482 | char header[100]; |
| 483 | int r = snprintf(header, sizeof(header), "0x%08x : ", i); |
| 484 | TEST_AND_RETURN(r == 13); |
| 485 | string line = header; |
| 486 | for (unsigned int j = 0; j < bytes_per_this_line; j++) { |
| 487 | char buf[20]; |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 488 | uint8_t c = arr[i + j]; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 489 | r = snprintf(buf, sizeof(buf), "%02x ", static_cast<unsigned int>(c)); |
| 490 | TEST_AND_RETURN(r == 3); |
| 491 | line += buf; |
| 492 | } |
| 493 | LOG(INFO) << line; |
| 494 | } |
| 495 | } |
| 496 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 497 | bool SplitPartitionName(const string& partition_name, |
| 498 | string* out_disk_name, |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 499 | int* out_partition_num) { |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 500 | if (!base::StartsWith( |
| 501 | partition_name, "/dev/", base::CompareCase::SENSITIVE)) { |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 502 | LOG(ERROR) << "Invalid partition device name: " << partition_name; |
| 503 | return false; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 504 | } |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 505 | |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 506 | size_t last_nondigit_pos = partition_name.find_last_not_of("0123456789"); |
| 507 | if (last_nondigit_pos == string::npos || |
| 508 | (last_nondigit_pos + 1) == partition_name.size()) { |
| 509 | LOG(ERROR) << "Unable to parse partition device name: " << partition_name; |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 510 | return false; |
| 511 | } |
| 512 | |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 513 | if (out_disk_name) { |
| 514 | // Special case for MMC devices which have the following naming scheme: |
| 515 | // mmcblk0p2 |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 516 | size_t disk_name_len = last_nondigit_pos; |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 517 | if (partition_name[last_nondigit_pos] != 'p' || last_nondigit_pos == 0 || |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 518 | !isdigit(partition_name[last_nondigit_pos - 1])) { |
| 519 | disk_name_len++; |
| 520 | } |
| 521 | *out_disk_name = partition_name.substr(0, disk_name_len); |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | if (out_partition_num) { |
Brian Norris | 7b428f5 | 2019-06-26 10:03:35 -0700 | [diff] [blame] | 525 | string partition_str = partition_name.substr(last_nondigit_pos + 1); |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 526 | *out_partition_num = atoi(partition_str.c_str()); |
| 527 | } |
| 528 | return true; |
| 529 | } |
| 530 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 531 | string MakePartitionName(const string& disk_name, int partition_num) { |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 532 | if (partition_num < 1) { |
| 533 | LOG(ERROR) << "Invalid partition number: " << partition_num; |
| 534 | return string(); |
| 535 | } |
| 536 | |
Alex Vakulenko | 0103c36 | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 537 | if (!base::StartsWith(disk_name, "/dev/", base::CompareCase::SENSITIVE)) { |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 538 | LOG(ERROR) << "Invalid disk name: " << disk_name; |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 539 | return string(); |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 540 | } |
| 541 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 542 | string partition_name = disk_name; |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 543 | if (isdigit(partition_name.back())) { |
| 544 | // Special case for devices with names ending with a digit. |
| 545 | // Add "p" to separate the disk name from partition number, |
| 546 | // e.g. "/dev/loop0p2" |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 547 | partition_name += 'p'; |
| 548 | } |
| 549 | |
Alex Vakulenko | f3f85bb | 2014-03-26 16:36:35 -0700 | [diff] [blame] | 550 | partition_name += std::to_string(partition_num); |
| 551 | |
Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 552 | return partition_name; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 553 | } |
| 554 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 555 | bool FileExists(const char* path) { |
Daniel Zheng | 3e881aa | 2022-09-07 22:10:29 +0000 | [diff] [blame] | 556 | struct stat stbuf {}; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 557 | return 0 == lstat(path, &stbuf); |
| 558 | } |
| 559 | |
Darin Petkov | 30291ed | 2010-11-12 10:23:06 -0800 | [diff] [blame] | 560 | bool IsSymlink(const char* path) { |
Daniel Zheng | 3e881aa | 2022-09-07 22:10:29 +0000 | [diff] [blame] | 561 | struct stat stbuf {}; |
Darin Petkov | 30291ed | 2010-11-12 10:23:06 -0800 | [diff] [blame] | 562 | return lstat(path, &stbuf) == 0 && S_ISLNK(stbuf.st_mode) != 0; |
| 563 | } |
| 564 | |
Kelvin Zhang | deb3445 | 2021-01-21 11:54:36 -0500 | [diff] [blame] | 565 | bool IsRegFile(const char* path) { |
Daniel Zheng | 3e881aa | 2022-09-07 22:10:29 +0000 | [diff] [blame] | 566 | struct stat stbuf {}; |
Kelvin Zhang | deb3445 | 2021-01-21 11:54:36 -0500 | [diff] [blame] | 567 | return lstat(path, &stbuf) == 0 && S_ISREG(stbuf.st_mode) != 0; |
| 568 | } |
| 569 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 570 | bool MakeTempFile(const string& base_filename_template, |
| 571 | string* filename, |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 572 | int* fd) { |
Alex Deymo | 5aa1c54 | 2015-09-18 01:02:33 -0700 | [diff] [blame] | 573 | base::FilePath filename_template; |
| 574 | TEST_AND_RETURN_FALSE( |
| 575 | GetTempName(base_filename_template, &filename_template)); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 576 | DCHECK(filename || fd); |
Alex Deymo | 5aa1c54 | 2015-09-18 01:02:33 -0700 | [diff] [blame] | 577 | vector<char> buf(filename_template.value().size() + 1); |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 578 | memcpy(buf.data(), |
| 579 | filename_template.value().data(), |
Alex Deymo | 5aa1c54 | 2015-09-18 01:02:33 -0700 | [diff] [blame] | 580 | filename_template.value().size()); |
| 581 | buf[filename_template.value().size()] = '\0'; |
Darin Petkov | 296889c | 2010-07-23 16:20:54 -0700 | [diff] [blame] | 582 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 583 | int mkstemp_fd = mkstemp(buf.data()); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 584 | TEST_AND_RETURN_FALSE_ERRNO(mkstemp_fd >= 0); |
| 585 | if (filename) { |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 586 | *filename = buf.data(); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 587 | } |
| 588 | if (fd) { |
| 589 | *fd = mkstemp_fd; |
| 590 | } else { |
| 591 | close(mkstemp_fd); |
| 592 | } |
| 593 | return true; |
| 594 | } |
| 595 | |
Alex Deymo | 5fb356c | 2016-03-25 18:48:22 -0700 | [diff] [blame] | 596 | bool SetBlockDeviceReadOnly(const string& device, bool read_only) { |
| 597 | int fd = HANDLE_EINTR(open(device.c_str(), O_RDONLY | O_CLOEXEC)); |
| 598 | if (fd < 0) { |
| 599 | PLOG(ERROR) << "Opening block device " << device; |
| 600 | return false; |
| 601 | } |
| 602 | ScopedFdCloser fd_closer(&fd); |
| 603 | // We take no action if not needed. |
Daniel Zheng | 3e881aa | 2022-09-07 22:10:29 +0000 | [diff] [blame] | 604 | int read_only_flag{}; |
Alex Deymo | 5fb356c | 2016-03-25 18:48:22 -0700 | [diff] [blame] | 605 | int expected_flag = read_only ? 1 : 0; |
| 606 | int rc = ioctl(fd, BLKROGET, &read_only_flag); |
| 607 | // In case of failure reading the setting we will try to set it anyway. |
| 608 | if (rc == 0 && read_only_flag == expected_flag) |
| 609 | return true; |
| 610 | |
| 611 | rc = ioctl(fd, BLKROSET, &expected_flag); |
| 612 | if (rc != 0) { |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 613 | PLOG(ERROR) << "Marking block device " << device |
| 614 | << " as read_only=" << expected_flag; |
Alex Deymo | 5fb356c | 2016-03-25 18:48:22 -0700 | [diff] [blame] | 615 | return false; |
| 616 | } |
lin.gui | fc20b67 | 2023-09-01 20:01:35 +0800 | [diff] [blame] | 617 | |
| 618 | /* |
| 619 | * Read back the value to check if it is configured successfully. |
| 620 | * If fail, use the second method, set the file |
| 621 | * /sys/block/<partition_name>/force_ro |
| 622 | * to config the read only property. |
| 623 | */ |
| 624 | rc = ioctl(fd, BLKROGET, &read_only_flag); |
| 625 | if (rc != 0) { |
Daniel Zheng | 6a00fe9 | 2024-04-09 14:44:21 -0700 | [diff] [blame^] | 626 | PLOG(ERROR) << "Failed to read back block device read-only value:" |
| 627 | << device; |
lin.gui | fc20b67 | 2023-09-01 20:01:35 +0800 | [diff] [blame] | 628 | return false; |
| 629 | } |
| 630 | if (read_only_flag == expected_flag) { |
| 631 | return true; |
| 632 | } |
| 633 | |
| 634 | std::array<char, PATH_MAX> device_name; |
Daniel Zheng | 6a00fe9 | 2024-04-09 14:44:21 -0700 | [diff] [blame^] | 635 | char* pdevice = realpath(device.c_str(), device_name.data()); |
lin.gui | fc20b67 | 2023-09-01 20:01:35 +0800 | [diff] [blame] | 636 | TEST_AND_RETURN_FALSE_ERRNO(pdevice); |
| 637 | |
| 638 | std::string real_path(pdevice); |
| 639 | std::size_t offset = real_path.find_last_of('/'); |
Daniel Zheng | 6a00fe9 | 2024-04-09 14:44:21 -0700 | [diff] [blame^] | 640 | if (offset == std::string::npos) { |
lin.gui | fc20b67 | 2023-09-01 20:01:35 +0800 | [diff] [blame] | 641 | LOG(ERROR) << "Could not find partition name from " << real_path; |
| 642 | return false; |
| 643 | } |
| 644 | const std::string partition_name = real_path.substr(offset + 1); |
| 645 | |
| 646 | std::string force_ro_file = "/sys/block/" + partition_name + "/force_ro"; |
Daniel Zheng | 6a00fe9 | 2024-04-09 14:44:21 -0700 | [diff] [blame^] | 647 | android::base::unique_fd fd_force_ro{ |
| 648 | HANDLE_EINTR(open(force_ro_file.c_str(), O_WRONLY | O_CLOEXEC))}; |
lin.gui | fc20b67 | 2023-09-01 20:01:35 +0800 | [diff] [blame] | 649 | TEST_AND_RETURN_FALSE_ERRNO(fd_force_ro >= 0); |
| 650 | |
| 651 | rc = write(fd_force_ro, expected_flag ? "1" : "0", 1); |
| 652 | TEST_AND_RETURN_FALSE_ERRNO(rc > 0); |
| 653 | |
| 654 | // Read back again |
| 655 | rc = ioctl(fd, BLKROGET, &read_only_flag); |
| 656 | if (rc != 0) { |
Daniel Zheng | 6a00fe9 | 2024-04-09 14:44:21 -0700 | [diff] [blame^] | 657 | PLOG(ERROR) << "Failed to read back block device read-only value:" |
| 658 | << device; |
lin.gui | fc20b67 | 2023-09-01 20:01:35 +0800 | [diff] [blame] | 659 | return false; |
| 660 | } |
| 661 | if (read_only_flag != expected_flag) { |
| 662 | LOG(ERROR) << "After modifying force_ro, marking block device " << device |
Daniel Zheng | 6a00fe9 | 2024-04-09 14:44:21 -0700 | [diff] [blame^] | 663 | << " as read_only=" << expected_flag; |
lin.gui | fc20b67 | 2023-09-01 20:01:35 +0800 | [diff] [blame] | 664 | return false; |
| 665 | } |
Alex Deymo | 5fb356c | 2016-03-25 18:48:22 -0700 | [diff] [blame] | 666 | return true; |
| 667 | } |
| 668 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 669 | bool MountFilesystem(const string& device, |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 670 | const string& mountpoint, |
Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 671 | unsigned long mountflags, // NOLINT(runtime/int) |
Alex Deymo | 14dbd33 | 2016-03-01 18:55:54 -0800 | [diff] [blame] | 672 | const string& type, |
| 673 | const string& fs_mount_options) { |
Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 674 | vector<const char*> fstypes; |
| 675 | if (type.empty()) { |
Kelvin Zhang | 4eae81e | 2021-12-09 17:07:17 -0800 | [diff] [blame] | 676 | fstypes = {"ext2", "ext3", "ext4", "squashfs", "erofs"}; |
Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 677 | } else { |
| 678 | fstypes = {type.c_str()}; |
| 679 | } |
Alex Deymo | 4c82df3 | 2014-11-10 22:25:57 -0800 | [diff] [blame] | 680 | for (const char* fstype : fstypes) { |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 681 | int rc = mount(device.c_str(), |
| 682 | mountpoint.c_str(), |
| 683 | fstype, |
| 684 | mountflags, |
Alex Deymo | 14dbd33 | 2016-03-01 18:55:54 -0800 | [diff] [blame] | 685 | fs_mount_options.c_str()); |
Alex Deymo | 4c82df3 | 2014-11-10 22:25:57 -0800 | [diff] [blame] | 686 | if (rc == 0) |
| 687 | return true; |
| 688 | |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 689 | PLOG(WARNING) << "Unable to mount destination device " << device << " on " |
| 690 | << mountpoint << " as " << fstype; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 691 | } |
Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 692 | if (!type.empty()) { |
| 693 | LOG(ERROR) << "Unable to mount " << device << " with any supported type"; |
| 694 | } |
Alex Deymo | 4c82df3 | 2014-11-10 22:25:57 -0800 | [diff] [blame] | 695 | return false; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | bool UnmountFilesystem(const string& mountpoint) { |
Alex Deymo | 5ba93ad | 2016-08-22 19:51:59 -0700 | [diff] [blame] | 699 | int num_retries = 1; |
| 700 | for (;; ++num_retries) { |
Ben Chan | 77a1eba | 2012-10-07 22:54:55 -0700 | [diff] [blame] | 701 | if (umount(mountpoint.c_str()) == 0) |
Alex Deymo | 5ba93ad | 2016-08-22 19:51:59 -0700 | [diff] [blame] | 702 | return true; |
| 703 | if (errno != EBUSY || num_retries >= kUnmountMaxNumOfRetries) |
Ben Chan | 77a1eba | 2012-10-07 22:54:55 -0700 | [diff] [blame] | 704 | break; |
Alex Deymo | 8d2bbe3 | 2015-06-23 16:28:59 -0700 | [diff] [blame] | 705 | usleep(kUnmountRetryIntervalInMicroseconds); |
Ben Chan | 77a1eba | 2012-10-07 22:54:55 -0700 | [diff] [blame] | 706 | } |
Alex Deymo | 5ba93ad | 2016-08-22 19:51:59 -0700 | [diff] [blame] | 707 | if (errno == EINVAL) { |
| 708 | LOG(INFO) << "Not a mountpoint: " << mountpoint; |
| 709 | return false; |
| 710 | } |
| 711 | PLOG(WARNING) << "Error unmounting " << mountpoint << " after " << num_retries |
| 712 | << " attempts. Lazy unmounting instead, error was"; |
| 713 | if (umount2(mountpoint.c_str(), MNT_DETACH) != 0) { |
| 714 | PLOG(ERROR) << "Lazy unmount failed"; |
| 715 | return false; |
| 716 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 717 | return true; |
| 718 | } |
| 719 | |
Alex Deymo | f411650 | 2017-03-22 17:00:31 -0700 | [diff] [blame] | 720 | bool IsMountpoint(const std::string& mountpoint) { |
Daniel Zheng | 3e881aa | 2022-09-07 22:10:29 +0000 | [diff] [blame] | 721 | struct stat stdir { |
| 722 | }, stparent{}; |
Alex Deymo | f411650 | 2017-03-22 17:00:31 -0700 | [diff] [blame] | 723 | |
| 724 | // Check whether the passed mountpoint is a directory and the /.. is in the |
| 725 | // same device or not. If mountpoint/.. is in a different device it means that |
| 726 | // there is a filesystem mounted there. If it is not, but they both point to |
| 727 | // the same inode it basically is the special case of /.. pointing to /. This |
| 728 | // test doesn't play well with bind mount but that's out of the scope of what |
| 729 | // we want to detect here. |
| 730 | if (lstat(mountpoint.c_str(), &stdir) != 0) { |
| 731 | PLOG(ERROR) << "Error stat'ing " << mountpoint; |
| 732 | return false; |
| 733 | } |
| 734 | if (!S_ISDIR(stdir.st_mode)) |
| 735 | return false; |
| 736 | |
| 737 | base::FilePath parent(mountpoint); |
| 738 | parent = parent.Append(".."); |
| 739 | if (lstat(parent.value().c_str(), &stparent) != 0) { |
| 740 | PLOG(ERROR) << "Error stat'ing " << parent.value(); |
| 741 | return false; |
| 742 | } |
| 743 | return S_ISDIR(stparent.st_mode) && |
| 744 | (stparent.st_dev != stdir.st_dev || stparent.st_ino == stdir.st_ino); |
| 745 | } |
| 746 | |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 747 | // Tries to parse the header of an ELF file to obtain a human-readable |
| 748 | // description of it on the |output| string. |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 749 | static bool GetFileFormatELF(const uint8_t* buffer, |
| 750 | size_t size, |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 751 | string* output) { |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 752 | // 0x00: EI_MAG - ELF magic header, 4 bytes. |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 753 | if (size < SELFMAG || memcmp(buffer, ELFMAG, SELFMAG) != 0) |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 754 | return false; |
| 755 | *output = "ELF"; |
| 756 | |
| 757 | // 0x04: EI_CLASS, 1 byte. |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 758 | if (size < EI_CLASS + 1) |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 759 | return true; |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 760 | switch (buffer[EI_CLASS]) { |
| 761 | case ELFCLASS32: |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 762 | *output += " 32-bit"; |
| 763 | break; |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 764 | case ELFCLASS64: |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 765 | *output += " 64-bit"; |
| 766 | break; |
| 767 | default: |
| 768 | *output += " ?-bit"; |
| 769 | } |
| 770 | |
| 771 | // 0x05: EI_DATA, endianness, 1 byte. |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 772 | if (size < EI_DATA + 1) |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 773 | return true; |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 774 | uint8_t ei_data = buffer[EI_DATA]; |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 775 | switch (ei_data) { |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 776 | case ELFDATA2LSB: |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 777 | *output += " little-endian"; |
| 778 | break; |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 779 | case ELFDATA2MSB: |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 780 | *output += " big-endian"; |
| 781 | break; |
| 782 | default: |
| 783 | *output += " ?-endian"; |
| 784 | // Don't parse anything after the 0x10 offset if endianness is unknown. |
| 785 | return true; |
| 786 | } |
| 787 | |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 788 | const Elf32_Ehdr* hdr = reinterpret_cast<const Elf32_Ehdr*>(buffer); |
| 789 | // 0x12: e_machine, 2 byte endianness based on ei_data. The position (0x12) |
| 790 | // and size is the same for both 32 and 64 bits. |
| 791 | if (size < offsetof(Elf32_Ehdr, e_machine) + sizeof(hdr->e_machine)) |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 792 | return true; |
Daniel Zheng | 3e881aa | 2022-09-07 22:10:29 +0000 | [diff] [blame] | 793 | uint16_t e_machine{}; |
Sen Jiang | 771f648 | 2018-04-04 17:59:10 -0700 | [diff] [blame] | 794 | // Fix endianness regardless of the host endianness. |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 795 | if (ei_data == ELFDATA2LSB) |
| 796 | e_machine = le16toh(hdr->e_machine); |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 797 | else |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 798 | e_machine = be16toh(hdr->e_machine); |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 799 | |
| 800 | switch (e_machine) { |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 801 | case EM_386: |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 802 | *output += " x86"; |
| 803 | break; |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 804 | case EM_MIPS: |
| 805 | *output += " mips"; |
| 806 | break; |
| 807 | case EM_ARM: |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 808 | *output += " arm"; |
| 809 | break; |
Alex Deymo | c1711e2 | 2014-08-08 13:16:23 -0700 | [diff] [blame] | 810 | case EM_X86_64: |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 811 | *output += " x86-64"; |
| 812 | break; |
| 813 | default: |
| 814 | *output += " unknown-arch"; |
| 815 | } |
| 816 | return true; |
| 817 | } |
| 818 | |
| 819 | string GetFileFormat(const string& path) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 820 | brillo::Blob buffer; |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 821 | if (!ReadFileChunkAndAppend(path, 0, kGetFileFormatMaxHeaderSize, &buffer)) |
| 822 | return "File not found."; |
| 823 | |
| 824 | string result; |
| 825 | if (GetFileFormatELF(buffer.data(), buffer.size(), &result)) |
| 826 | return result; |
| 827 | |
| 828 | return "data"; |
| 829 | } |
| 830 | |
Darin Petkov | 5c0a8af | 2010-08-24 13:39:13 -0700 | [diff] [blame] | 831 | int FuzzInt(int value, unsigned int range) { |
| 832 | int min = value - range / 2; |
| 833 | int max = value + range - range / 2; |
| 834 | return base::RandInt(min, max); |
| 835 | } |
| 836 | |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 837 | string FormatSecs(unsigned secs) { |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 838 | return FormatTimeDelta(TimeDelta::FromSeconds(secs)); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 839 | } |
| 840 | |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 841 | string FormatTimeDelta(TimeDelta delta) { |
David Zeuthen | 973449e | 2014-08-18 16:18:23 -0400 | [diff] [blame] | 842 | string str; |
| 843 | |
| 844 | // Handle negative durations by prefixing with a minus. |
| 845 | if (delta.ToInternalValue() < 0) { |
| 846 | delta *= -1; |
| 847 | str = "-"; |
| 848 | } |
| 849 | |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 850 | // Canonicalize into days, hours, minutes, seconds and microseconds. |
| 851 | unsigned days = delta.InDays(); |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 852 | delta -= TimeDelta::FromDays(days); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 853 | unsigned hours = delta.InHours(); |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 854 | delta -= TimeDelta::FromHours(hours); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 855 | unsigned mins = delta.InMinutes(); |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 856 | delta -= TimeDelta::FromMinutes(mins); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 857 | unsigned secs = delta.InSeconds(); |
Gilad Arnold | 8e3f126 | 2013-01-08 14:59:54 -0800 | [diff] [blame] | 858 | delta -= TimeDelta::FromSeconds(secs); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 859 | unsigned usecs = delta.InMicroseconds(); |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 860 | |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 861 | if (days) |
| 862 | base::StringAppendF(&str, "%ud", days); |
| 863 | if (days || hours) |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 864 | base::StringAppendF(&str, "%uh", hours); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 865 | if (days || hours || mins) |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 866 | base::StringAppendF(&str, "%um", mins); |
Gilad Arnold | d7b513d | 2012-05-10 14:25:27 -0700 | [diff] [blame] | 867 | base::StringAppendF(&str, "%u", secs); |
| 868 | if (usecs) { |
| 869 | int width = 6; |
| 870 | while ((usecs / 10) * 10 == usecs) { |
| 871 | usecs /= 10; |
| 872 | width--; |
| 873 | } |
| 874 | base::StringAppendF(&str, ".%0*u", width, usecs); |
| 875 | } |
| 876 | base::StringAppendF(&str, "s"); |
Gilad Arnold | 1ebd813 | 2012-03-05 10:19:29 -0800 | [diff] [blame] | 877 | return str; |
| 878 | } |
| 879 | |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 880 | string ToString(const Time utc_time) { |
Daniel Zheng | 3e881aa | 2022-09-07 22:10:29 +0000 | [diff] [blame] | 881 | Time::Exploded exp_time{}; |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 882 | utc_time.UTCExplode(&exp_time); |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 883 | return base::StringPrintf("%d/%d/%d %d:%02d:%02d GMT", |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 884 | exp_time.month, |
| 885 | exp_time.day_of_month, |
| 886 | exp_time.year, |
| 887 | exp_time.hour, |
| 888 | exp_time.minute, |
| 889 | exp_time.second); |
Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 890 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 891 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 892 | string ToString(bool b) { |
| 893 | return (b ? "true" : "false"); |
| 894 | } |
| 895 | |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 896 | string ToString(DownloadSource source) { |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 897 | switch (source) { |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 898 | case kDownloadSourceHttpsServer: |
| 899 | return "HttpsServer"; |
| 900 | case kDownloadSourceHttpServer: |
| 901 | return "HttpServer"; |
| 902 | case kDownloadSourceHttpPeer: |
| 903 | return "HttpPeer"; |
| 904 | case kNumDownloadSources: |
| 905 | return "Unknown"; |
| 906 | // Don't add a default case to let the compiler warn about newly added |
| 907 | // download sources which should be added here. |
Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | return "Unknown"; |
| 911 | } |
| 912 | |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 913 | string ToString(PayloadType payload_type) { |
| 914 | switch (payload_type) { |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 915 | case kPayloadTypeDelta: |
| 916 | return "Delta"; |
| 917 | case kPayloadTypeFull: |
| 918 | return "Full"; |
| 919 | case kPayloadTypeForcedFull: |
| 920 | return "ForcedFull"; |
| 921 | case kNumPayloadTypes: |
| 922 | return "Unknown"; |
| 923 | // Don't add a default case to let the compiler warn about newly added |
| 924 | // payload types which should be added here. |
Alex Deymo | 1c656c4 | 2013-06-28 11:02:14 -0700 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | return "Unknown"; |
| 928 | } |
| 929 | |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 930 | ErrorCode GetBaseErrorCode(ErrorCode code) { |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 931 | // Ignore the higher order bits in the code by applying the mask as |
| 932 | // we want the enumerations to be in the small contiguous range |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 933 | // with values less than ErrorCode::kUmaReportedMax. |
| 934 | ErrorCode base_code = static_cast<ErrorCode>( |
| 935 | static_cast<int>(code) & ~static_cast<int>(ErrorCode::kSpecialFlags)); |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 936 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 937 | // Make additional adjustments required for UMA and error classification. |
| 938 | // TODO(jaysri): Move this logic to UeErrorCode.cc when we fix |
| 939 | // chromium-os:34369. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 940 | if (base_code >= ErrorCode::kOmahaRequestHTTPResponseBase) { |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 941 | // Since we want to keep the enums to a small value, aggregate all HTTP |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 942 | // errors into this one bucket for UMA and error classification purposes. |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 943 | LOG(INFO) << "Converting error code " << base_code |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 944 | << " to ErrorCode::kOmahaErrorInHTTPResponse"; |
| 945 | base_code = ErrorCode::kOmahaErrorInHTTPResponse; |
Jay Srinivasan | f057205 | 2012-10-23 18:12:56 -0700 | [diff] [blame] | 946 | } |
| 947 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 948 | return base_code; |
| 949 | } |
| 950 | |
Kelvin Zhang | d719103 | 2020-08-11 10:48:16 -0400 | [diff] [blame] | 951 | string StringVectorToString(const vector<string>& vec_str) { |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 952 | string str = "["; |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 953 | for (vector<string>::const_iterator i = vec_str.begin(); i != vec_str.end(); |
| 954 | ++i) { |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 955 | if (i != vec_str.begin()) |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 956 | str += ", "; |
| 957 | str += '"'; |
| 958 | str += *i; |
| 959 | str += '"'; |
| 960 | } |
| 961 | str += "]"; |
| 962 | return str; |
| 963 | } |
| 964 | |
Sen Jiang | 2703ef4 | 2017-03-16 13:36:21 -0700 | [diff] [blame] | 965 | // The P2P file id should be the same for devices running new version and old |
| 966 | // version so that they can share it with each other. The hash in the response |
| 967 | // was base64 encoded, but now that we switched to use "hash_sha256" field which |
| 968 | // is hex encoded, we have to convert them back to base64 for P2P. However, the |
| 969 | // base64 encoded hash was base64 encoded here again historically for some |
| 970 | // reason, so we keep the same behavior here. |
| 971 | string CalculateP2PFileId(const brillo::Blob& payload_hash, |
| 972 | size_t payload_size) { |
| 973 | string encoded_hash = brillo::data_encoding::Base64Encode( |
| 974 | brillo::data_encoding::Base64Encode(payload_hash)); |
Alex Deymo | c00c98a | 2015-03-17 17:38:00 -0700 | [diff] [blame] | 975 | return base::StringPrintf("cros_update_size_%" PRIuS "_hash_%s", |
Alex Vakulenko | 981a9fb | 2015-02-09 12:51:24 -0800 | [diff] [blame] | 976 | payload_size, |
| 977 | encoded_hash.c_str()); |
David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 978 | } |
| 979 | |
Kelvin Zhang | d719103 | 2020-08-11 10:48:16 -0400 | [diff] [blame] | 980 | bool ConvertToOmahaInstallDate(Time time, int* out_num_days) { |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 981 | time_t unix_time = time.ToTimeT(); |
| 982 | // Output of: date +"%s" --date="Jan 1, 2007 0:00 PST". |
| 983 | const time_t kOmahaEpoch = 1167638400; |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 984 | const int64_t kNumSecondsPerWeek = 7 * 24 * 3600; |
David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 985 | const int64_t kNumDaysPerWeek = 7; |
| 986 | |
| 987 | time_t omaha_time = unix_time - kOmahaEpoch; |
| 988 | |
| 989 | if (omaha_time < 0) |
| 990 | return false; |
| 991 | |
| 992 | // Note, as per the comment in utils.h we are deliberately not |
| 993 | // handling DST correctly. |
| 994 | |
| 995 | int64_t num_weeks_since_omaha_epoch = omaha_time / kNumSecondsPerWeek; |
| 996 | *out_num_days = num_weeks_since_omaha_epoch * kNumDaysPerWeek; |
| 997 | |
| 998 | return true; |
| 999 | } |
| 1000 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1001 | bool GetMinorVersion(const brillo::KeyValueStore& store, |
Alex Deymo | b42b98d | 2015-07-06 17:42:38 -0700 | [diff] [blame] | 1002 | uint32_t* minor_version) { |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1003 | string result; |
Alex Deymo | b42b98d | 2015-07-06 17:42:38 -0700 | [diff] [blame] | 1004 | if (store.GetString("PAYLOAD_MINOR_VERSION", &result)) { |
Allie Wood | 425aa97 | 2015-02-17 14:47:17 -0800 | [diff] [blame] | 1005 | if (!base::StringToUint(result, minor_version)) { |
| 1006 | LOG(ERROR) << "StringToUint failed when parsing delta minor version."; |
| 1007 | return false; |
| 1008 | } |
Allie Wood | 78750a4 | 2015-02-11 15:42:11 -0800 | [diff] [blame] | 1009 | return true; |
| 1010 | } |
| 1011 | return false; |
| 1012 | } |
| 1013 | |
Kelvin Zhang | 02fe662 | 2021-11-01 16:37:58 -0700 | [diff] [blame] | 1014 | bool ReadExtents(const std::string& path, |
| 1015 | const google::protobuf::RepeatedPtrField<Extent>& extents, |
| 1016 | brillo::Blob* out_data, |
| 1017 | size_t block_size) { |
| 1018 | return ReadExtents(path, |
| 1019 | {extents.begin(), extents.end()}, |
| 1020 | out_data, |
| 1021 | utils::BlocksInExtents(extents) * block_size, |
| 1022 | block_size); |
| 1023 | } |
| 1024 | |
| 1025 | bool WriteExtents(const std::string& path, |
| 1026 | const google::protobuf::RepeatedPtrField<Extent>& extents, |
| 1027 | const brillo::Blob& data, |
| 1028 | size_t block_size) { |
| 1029 | EintrSafeFileDescriptor fd; |
| 1030 | TEST_AND_RETURN_FALSE(fd.Open(path.c_str(), O_RDWR)); |
| 1031 | size_t bytes_written = 0; |
| 1032 | for (const auto& ext : extents) { |
| 1033 | TEST_AND_RETURN_FALSE_ERRNO( |
| 1034 | fd.Seek(ext.start_block() * block_size, SEEK_SET)); |
| 1035 | TEST_AND_RETURN_FALSE_ERRNO( |
| 1036 | fd.Write(data.data() + bytes_written, ext.num_blocks() * block_size)); |
| 1037 | bytes_written += ext.num_blocks() * block_size; |
| 1038 | } |
| 1039 | return true; |
| 1040 | } |
Kelvin Zhang | 4eae81e | 2021-12-09 17:07:17 -0800 | [diff] [blame] | 1041 | bool ReadExtents(const std::string& path, |
| 1042 | const vector<Extent>& extents, |
| 1043 | brillo::Blob* out_data, |
| 1044 | ssize_t out_data_size, |
| 1045 | size_t block_size) { |
| 1046 | FileDescriptorPtr fd = std::make_shared<EintrSafeFileDescriptor>(); |
| 1047 | fd->Open(path.c_str(), O_RDONLY); |
| 1048 | return ReadExtents(fd, extents, out_data, out_data_size, block_size); |
| 1049 | } |
Kelvin Zhang | 02fe662 | 2021-11-01 16:37:58 -0700 | [diff] [blame] | 1050 | |
Kelvin Zhang | 4eae81e | 2021-12-09 17:07:17 -0800 | [diff] [blame] | 1051 | bool ReadExtents(FileDescriptorPtr fd, |
| 1052 | const google::protobuf::RepeatedPtrField<Extent>& extents, |
| 1053 | brillo::Blob* out_data, |
| 1054 | size_t block_size) { |
| 1055 | return ReadExtents(fd, |
| 1056 | {extents.begin(), extents.end()}, |
| 1057 | out_data, |
| 1058 | utils::BlocksInExtents(extents) * block_size, |
| 1059 | block_size); |
| 1060 | } |
| 1061 | |
| 1062 | bool ReadExtents(FileDescriptorPtr fd, |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 1063 | const vector<Extent>& extents, |
| 1064 | brillo::Blob* out_data, |
| 1065 | ssize_t out_data_size, |
Allie Wood | 5687345 | 2015-03-27 17:48:40 -0700 | [diff] [blame] | 1066 | size_t block_size) { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 1067 | brillo::Blob data(out_data_size); |
Allie Wood | 5687345 | 2015-03-27 17:48:40 -0700 | [diff] [blame] | 1068 | ssize_t bytes_read = 0; |
Allie Wood | 5687345 | 2015-03-27 17:48:40 -0700 | [diff] [blame] | 1069 | |
Gilad Arnold | 41e3474 | 2015-05-11 11:31:50 -0700 | [diff] [blame] | 1070 | for (const Extent& extent : extents) { |
Allie Wood | 5687345 | 2015-03-27 17:48:40 -0700 | [diff] [blame] | 1071 | ssize_t bytes_read_this_iteration = 0; |
| 1072 | ssize_t bytes = extent.num_blocks() * block_size; |
Kelvin Zhang | 02fe662 | 2021-11-01 16:37:58 -0700 | [diff] [blame] | 1073 | TEST_LE(bytes_read + bytes, out_data_size); |
Allie Wood | 5687345 | 2015-03-27 17:48:40 -0700 | [diff] [blame] | 1074 | TEST_AND_RETURN_FALSE(utils::PReadAll(fd, |
| 1075 | &data[bytes_read], |
| 1076 | bytes, |
| 1077 | extent.start_block() * block_size, |
| 1078 | &bytes_read_this_iteration)); |
| 1079 | TEST_AND_RETURN_FALSE(bytes_read_this_iteration == bytes); |
| 1080 | bytes_read += bytes_read_this_iteration; |
| 1081 | } |
| 1082 | TEST_AND_RETURN_FALSE(out_data_size == bytes_read); |
| 1083 | *out_data = data; |
| 1084 | return true; |
| 1085 | } |
| 1086 | |
Matt Ziegelbaum | 91ba9be | 2020-06-10 16:56:40 -0400 | [diff] [blame] | 1087 | bool GetVpdValue(string key, string* result) { |
| 1088 | int exit_code = 0; |
| 1089 | string value, error; |
| 1090 | vector<string> cmd = {"vpd_get_value", key}; |
| 1091 | if (!chromeos_update_engine::Subprocess::SynchronousExec( |
| 1092 | cmd, &exit_code, &value, &error) || |
| 1093 | exit_code) { |
| 1094 | LOG(ERROR) << "Failed to get vpd key for " << value |
| 1095 | << " with exit code: " << exit_code << " and error: " << error; |
| 1096 | return false; |
| 1097 | } else if (!error.empty()) { |
| 1098 | LOG(INFO) << "vpd_get_value succeeded but with following errors: " << error; |
| 1099 | } |
| 1100 | |
| 1101 | base::TrimWhitespaceASCII(value, base::TRIM_ALL, &value); |
| 1102 | *result = value; |
| 1103 | return true; |
| 1104 | } |
| 1105 | |
Alex Deymo | dd132f3 | 2015-09-14 19:12:07 -0700 | [diff] [blame] | 1106 | bool GetBootId(string* boot_id) { |
| 1107 | TEST_AND_RETURN_FALSE( |
| 1108 | base::ReadFileToString(base::FilePath(kBootIdPath), boot_id)); |
| 1109 | base::TrimWhitespaceASCII(*boot_id, base::TRIM_TRAILING, boot_id); |
| 1110 | return true; |
| 1111 | } |
| 1112 | |
Marton Hunyady | a030268 | 2018-05-16 18:52:13 +0200 | [diff] [blame] | 1113 | int VersionPrefix(const std::string& version) { |
| 1114 | if (version.empty()) { |
| 1115 | return 0; |
| 1116 | } |
| 1117 | vector<string> tokens = base::SplitString( |
| 1118 | version, ".", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL); |
Daniel Zheng | 3e881aa | 2022-09-07 22:10:29 +0000 | [diff] [blame] | 1119 | int value{}; |
Marton Hunyady | a030268 | 2018-05-16 18:52:13 +0200 | [diff] [blame] | 1120 | if (tokens.empty() || !base::StringToInt(tokens[0], &value)) |
| 1121 | return -1; // Target version is invalid. |
| 1122 | return value; |
| 1123 | } |
| 1124 | |
Zentaro Kavanagh | 0ff621c | 2018-07-13 13:06:56 -0700 | [diff] [blame] | 1125 | void ParseRollbackKeyVersion(const string& raw_version, |
| 1126 | uint16_t* high_version, |
| 1127 | uint16_t* low_version) { |
| 1128 | DCHECK(high_version); |
| 1129 | DCHECK(low_version); |
| 1130 | *high_version = numeric_limits<uint16_t>::max(); |
| 1131 | *low_version = numeric_limits<uint16_t>::max(); |
| 1132 | |
| 1133 | vector<string> parts = base::SplitString( |
| 1134 | raw_version, ".", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 1135 | if (parts.size() != 2) { |
| 1136 | // The version string must have exactly one period. |
| 1137 | return; |
| 1138 | } |
| 1139 | |
Daniel Zheng | 3e881aa | 2022-09-07 22:10:29 +0000 | [diff] [blame] | 1140 | int high{}; |
| 1141 | int low{}; |
Zentaro Kavanagh | 0ff621c | 2018-07-13 13:06:56 -0700 | [diff] [blame] | 1142 | if (!(base::StringToInt(parts[0], &high) && |
| 1143 | base::StringToInt(parts[1], &low))) { |
| 1144 | // Both parts of the version could not be parsed correctly. |
| 1145 | return; |
| 1146 | } |
| 1147 | |
| 1148 | if (high >= 0 && high < numeric_limits<uint16_t>::max() && low >= 0 && |
| 1149 | low < numeric_limits<uint16_t>::max()) { |
| 1150 | *high_version = static_cast<uint16_t>(high); |
| 1151 | *low_version = static_cast<uint16_t>(low); |
| 1152 | } |
| 1153 | } |
| 1154 | |
Kyeongkab.Nam | 500ca13 | 2019-06-26 13:48:07 +0900 | [diff] [blame] | 1155 | string GetFilePath(int fd) { |
| 1156 | base::FilePath proc("/proc/self/fd/" + std::to_string(fd)); |
| 1157 | base::FilePath file_name; |
| 1158 | |
| 1159 | if (!base::ReadSymbolicLink(proc, &file_name)) { |
| 1160 | return "not found"; |
| 1161 | } |
| 1162 | return file_name.value(); |
| 1163 | } |
| 1164 | |
Yifan Hong | c80de2d | 2020-02-25 17:13:53 -0800 | [diff] [blame] | 1165 | string GetTimeAsString(time_t utime) { |
Daniel Zheng | 3e881aa | 2022-09-07 22:10:29 +0000 | [diff] [blame] | 1166 | struct tm tm {}; |
Yifan Hong | c80de2d | 2020-02-25 17:13:53 -0800 | [diff] [blame] | 1167 | CHECK_EQ(localtime_r(&utime, &tm), &tm); |
| 1168 | char str[16]; |
| 1169 | CHECK_EQ(strftime(str, sizeof(str), "%Y%m%d-%H%M%S", &tm), 15u); |
| 1170 | return str; |
| 1171 | } |
| 1172 | |
Jae Hoon Kim | 694eeb0 | 2020-06-01 14:24:08 -0700 | [diff] [blame] | 1173 | string GetExclusionName(const string& str_to_convert) { |
| 1174 | return base::NumberToString(base::StringPieceHash()(str_to_convert)); |
| 1175 | } |
| 1176 | |
Kelvin Zhang | 636ba2f | 2022-02-09 14:40:22 -0800 | [diff] [blame] | 1177 | static bool ParseTimestamp(std::string_view str, int64_t* out) { |
| 1178 | if (!base::StringToInt64(base::StringPiece(str.data(), str.size()), out)) { |
Kelvin Zhang | d719103 | 2020-08-11 10:48:16 -0400 | [diff] [blame] | 1179 | LOG(WARNING) << "Invalid timestamp: " << str; |
| 1180 | return false; |
| 1181 | } |
| 1182 | return true; |
| 1183 | } |
| 1184 | |
Kelvin Zhang | 636ba2f | 2022-02-09 14:40:22 -0800 | [diff] [blame] | 1185 | ErrorCode IsTimestampNewer(const std::string_view old_version, |
| 1186 | const std::string_view new_version) { |
Kelvin Zhang | d719103 | 2020-08-11 10:48:16 -0400 | [diff] [blame] | 1187 | if (old_version.empty() || new_version.empty()) { |
| 1188 | LOG(WARNING) |
| 1189 | << "One of old/new timestamp is empty, permit update anyway. Old: " |
| 1190 | << old_version << " New: " << new_version; |
Yifan Hong | 8702933 | 2020-09-01 17:20:08 -0700 | [diff] [blame] | 1191 | return ErrorCode::kSuccess; |
Kelvin Zhang | d719103 | 2020-08-11 10:48:16 -0400 | [diff] [blame] | 1192 | } |
| 1193 | int64_t old_ver = 0; |
Yifan Hong | 8702933 | 2020-09-01 17:20:08 -0700 | [diff] [blame] | 1194 | if (!ParseTimestamp(old_version, &old_ver)) { |
| 1195 | return ErrorCode::kError; |
| 1196 | } |
Kelvin Zhang | d719103 | 2020-08-11 10:48:16 -0400 | [diff] [blame] | 1197 | int64_t new_ver = 0; |
Yifan Hong | 8702933 | 2020-09-01 17:20:08 -0700 | [diff] [blame] | 1198 | if (!ParseTimestamp(new_version, &new_ver)) { |
| 1199 | return ErrorCode::kDownloadManifestParseError; |
| 1200 | } |
| 1201 | if (old_ver > new_ver) { |
| 1202 | return ErrorCode::kPayloadTimestampError; |
| 1203 | } |
| 1204 | return ErrorCode::kSuccess; |
Kelvin Zhang | d719103 | 2020-08-11 10:48:16 -0400 | [diff] [blame] | 1205 | } |
| 1206 | |
Kelvin Zhang | 4bb4920 | 2021-07-08 21:39:05 -0400 | [diff] [blame] | 1207 | std::unique_ptr<android::base::MappedFile> GetReadonlyZeroBlock(size_t size) { |
| 1208 | android::base::unique_fd fd{HANDLE_EINTR(open("/dev/zero", O_RDONLY))}; |
| 1209 | return android::base::MappedFile::FromFd(fd, 0, size, PROT_READ); |
| 1210 | } |
| 1211 | |
Kelvin Zhang | 3fe4964 | 2021-10-04 15:35:02 -0700 | [diff] [blame] | 1212 | std::string_view GetReadonlyZeroString(size_t size) { |
| 1213 | // Reserve 512MB of Virtual Address Space. No actual memory will be used. |
| 1214 | static auto zero_block = GetReadonlyZeroBlock(1024 * 1024 * 512); |
| 1215 | if (size > zero_block->size()) { |
| 1216 | auto larger_block = GetReadonlyZeroBlock(size); |
| 1217 | zero_block = std::move(larger_block); |
| 1218 | } |
| 1219 | return {zero_block->data(), size}; |
| 1220 | } |
| 1221 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 1222 | } // namespace utils |
| 1223 | |
Kelvin Zhang | 20982a5 | 2021-08-13 12:31:16 -0700 | [diff] [blame] | 1224 | std::string HexEncode(const brillo::Blob& blob) noexcept { |
| 1225 | return base::HexEncode(blob.data(), blob.size()); |
| 1226 | } |
| 1227 | |
Kelvin Zhang | 3fe4964 | 2021-10-04 15:35:02 -0700 | [diff] [blame] | 1228 | std::string HexEncode(const std::string_view blob) noexcept { |
| 1229 | return base::HexEncode(blob.data(), blob.size()); |
| 1230 | } |
| 1231 | |
Kelvin Zhang | 760c334 | 2022-02-08 13:41:49 -0800 | [diff] [blame] | 1232 | [[nodiscard]] std::string_view ToStringView( |
| 1233 | const std::vector<unsigned char>& blob) noexcept { |
| 1234 | return std::string_view{reinterpret_cast<const char*>(blob.data()), |
| 1235 | blob.size()}; |
| 1236 | } |
| 1237 | |
| 1238 | [[nodiscard]] std::string_view ToStringView(const void* data, |
| 1239 | size_t size) noexcept { |
| 1240 | return std::string_view(reinterpret_cast<const char*>(data), size); |
| 1241 | } |
| 1242 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 1243 | } // namespace chromeos_update_engine |