| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "update_engine/utils.h" | 5 #include "update_engine/utils.h" |
| 6 #include <sys/mount.h> | 6 #include <sys/mount.h> |
| 7 #include <sys/stat.h> | 7 #include <sys/stat.h> |
| 8 #include <sys/types.h> | 8 #include <sys/types.h> |
| 9 #include <dirent.h> | 9 #include <dirent.h> |
| 10 #include <errno.h> | 10 #include <errno.h> |
| 11 #include <fcntl.h> | 11 #include <fcntl.h> |
| 12 #include <stdio.h> | 12 #include <stdio.h> |
| 13 #include <stdlib.h> | 13 #include <stdlib.h> |
| 14 #include <string.h> | 14 #include <string.h> |
| 15 #include <unistd.h> | 15 #include <unistd.h> |
| 16 #include <algorithm> | 16 #include <algorithm> |
| 17 #include "chromeos/obsolete_logging.h" | 17 #include "chromeos/obsolete_logging.h" |
| 18 #include "update_engine/file_writer.h" | 18 #include "update_engine/file_writer.h" |
| 19 #include "update_engine/subprocess.h" |
| 19 | 20 |
| 20 using std::min; | 21 using std::min; |
| 21 using std::string; | 22 using std::string; |
| 22 using std::vector; | 23 using std::vector; |
| 23 | 24 |
| 24 namespace chromeos_update_engine { | 25 namespace chromeos_update_engine { |
| 25 | 26 |
| 26 namespace utils { | 27 namespace utils { |
| 27 | 28 |
| 28 bool WriteFile(const char* path, const char* data, int data_len) { | 29 bool WriteFile(const char* path, const char* data, int data_len) { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 ssize_t rc = pread(fd, c_buf + bytes_read, count - bytes_read, | 67 ssize_t rc = pread(fd, c_buf + bytes_read, count - bytes_read, |
| 67 offset + bytes_read); | 68 offset + bytes_read); |
| 68 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); | 69 TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); |
| 69 if (rc == 0) { | 70 if (rc == 0) { |
| 70 break; | 71 break; |
| 71 } | 72 } |
| 72 bytes_read += rc; | 73 bytes_read += rc; |
| 73 } | 74 } |
| 74 *out_bytes_read = bytes_read; | 75 *out_bytes_read = bytes_read; |
| 75 return true; | 76 return true; |
| 76 | 77 |
| 77 } | 78 } |
| 78 | 79 |
| 79 bool ReadFile(const std::string& path, std::vector<char>* out) { | 80 bool ReadFile(const std::string& path, std::vector<char>* out) { |
| 80 CHECK(out); | 81 CHECK(out); |
| 81 FILE* fp = fopen(path.c_str(), "r"); | 82 FILE* fp = fopen(path.c_str(), "r"); |
| 82 if (!fp) | 83 if (!fp) |
| 83 return false; | 84 return false; |
| 84 const size_t kChunkSize = 1024; | 85 const size_t kChunkSize = 1024; |
| 85 size_t read_size; | 86 size_t read_size; |
| 86 do { | 87 do { |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 return path; | 276 return path; |
| 276 } | 277 } |
| 277 | 278 |
| 278 bool MakeTempFile(const std::string& filename_template, | 279 bool MakeTempFile(const std::string& filename_template, |
| 279 std::string* filename, | 280 std::string* filename, |
| 280 int* fd) { | 281 int* fd) { |
| 281 DCHECK(filename || fd); | 282 DCHECK(filename || fd); |
| 282 vector<char> buf(filename_template.size() + 1); | 283 vector<char> buf(filename_template.size() + 1); |
| 283 memcpy(&buf[0], filename_template.data(), filename_template.size()); | 284 memcpy(&buf[0], filename_template.data(), filename_template.size()); |
| 284 buf[filename_template.size()] = '\0'; | 285 buf[filename_template.size()] = '\0'; |
| 285 | 286 |
| 286 int mkstemp_fd = mkstemp(&buf[0]); | 287 int mkstemp_fd = mkstemp(&buf[0]); |
| 287 TEST_AND_RETURN_FALSE_ERRNO(mkstemp_fd >= 0); | 288 TEST_AND_RETURN_FALSE_ERRNO(mkstemp_fd >= 0); |
| 288 if (filename) { | 289 if (filename) { |
| 289 *filename = &buf[0]; | 290 *filename = &buf[0]; |
| 290 } | 291 } |
| 291 if (fd) { | 292 if (fd) { |
| 292 *fd = mkstemp_fd; | 293 *fd = mkstemp_fd; |
| 293 } else { | 294 } else { |
| 294 close(mkstemp_fd); | 295 close(mkstemp_fd); |
| 295 } | 296 } |
| 296 return true; | 297 return true; |
| 297 } | 298 } |
| 298 | 299 |
| 299 bool MakeTempDirectory(const std::string& dirname_template, | 300 bool MakeTempDirectory(const std::string& dirname_template, |
| 300 std::string* dirname) { | 301 std::string* dirname) { |
| 301 DCHECK(dirname); | 302 DCHECK(dirname); |
| 302 vector<char> buf(dirname_template.size() + 1); | 303 vector<char> buf(dirname_template.size() + 1); |
| 303 memcpy(&buf[0], dirname_template.data(), dirname_template.size()); | 304 memcpy(&buf[0], dirname_template.data(), dirname_template.size()); |
| 304 buf[dirname_template.size()] = '\0'; | 305 buf[dirname_template.size()] = '\0'; |
| 305 | 306 |
| 306 char* return_code = mkdtemp(&buf[0]); | 307 char* return_code = mkdtemp(&buf[0]); |
| 307 TEST_AND_RETURN_FALSE_ERRNO(return_code != NULL); | 308 TEST_AND_RETURN_FALSE_ERRNO(return_code != NULL); |
| 308 *dirname = &buf[0]; | 309 *dirname = &buf[0]; |
| 309 return true; | 310 return true; |
| 310 } | 311 } |
| 311 | 312 |
| 312 bool StringHasSuffix(const std::string& str, const std::string& suffix) { | 313 bool StringHasSuffix(const std::string& str, const std::string& suffix) { |
| 313 if (suffix.size() > str.size()) | 314 if (suffix.size() > str.size()) |
| 314 return false; | 315 return false; |
| 315 return 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix); | 316 return 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 386 *out_bootloader = BootLoader_SYSLINUX; | 387 *out_bootloader = BootLoader_SYSLINUX; |
| 387 return true; | 388 return true; |
| 388 } | 389 } |
| 389 | 390 |
| 390 const char* GetGErrorMessage(const GError* error) { | 391 const char* GetGErrorMessage(const GError* error) { |
| 391 if (!error) | 392 if (!error) |
| 392 return "Unknown error."; | 393 return "Unknown error."; |
| 393 return error->message; | 394 return error->message; |
| 394 } | 395 } |
| 395 | 396 |
| 397 bool Reboot() { |
| 398 vector<string> command; |
| 399 command.push_back("/sbin/shutdown"); |
| 400 command.push_back("-r"); |
| 401 command.push_back("now"); |
| 402 int rc = 0; |
| 403 Subprocess::SynchronousExec(command, &rc); |
| 404 TEST_AND_RETURN_FALSE(rc == 0); |
| 405 return true; |
| 406 } |
| 407 |
| 396 const char* const kStatefulPartition = "/mnt/stateful_partition"; | 408 const char* const kStatefulPartition = "/mnt/stateful_partition"; |
| 397 | 409 |
| 398 } // namespace utils | 410 } // namespace utils |
| 399 | 411 |
| 400 } // namespace chromeos_update_engine | 412 } // namespace chromeos_update_engine |
| 401 | |
| OLD | NEW |