OLD | NEW |
1 // Copyright (c) 2009 The Chromium OS 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 | 6 |
7 #include <sys/mount.h> | 7 #include <sys/mount.h> |
8 #include <sys/resource.h> | 8 #include <sys/resource.h> |
9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
10 #include <sys/types.h> | 10 #include <sys/types.h> |
11 #include <dirent.h> | 11 #include <dirent.h> |
12 #include <errno.h> | 12 #include <errno.h> |
13 #include <fcntl.h> | 13 #include <fcntl.h> |
14 #include <stdio.h> | 14 #include <stdio.h> |
15 #include <stdlib.h> | 15 #include <stdlib.h> |
16 #include <string.h> | 16 #include <string.h> |
17 #include <unistd.h> | 17 #include <unistd.h> |
18 | 18 |
19 #include <algorithm> | 19 #include <algorithm> |
20 | 20 |
| 21 #include <base/eintr_wrapper.h> |
21 #include <base/file_path.h> | 22 #include <base/file_path.h> |
22 #include <base/file_util.h> | 23 #include <base/file_util.h> |
23 #include <base/rand_util.h> | 24 #include <base/rand_util.h> |
24 #include <base/string_util.h> | 25 #include <base/string_util.h> |
25 #include <base/logging.h> | 26 #include <base/logging.h> |
26 #include <rootdev/rootdev.h> | 27 #include <rootdev/rootdev.h> |
27 | 28 |
28 #include "update_engine/file_writer.h" | 29 #include "update_engine/file_writer.h" |
29 #include "update_engine/omaha_request_params.h" | 30 #include "update_engine/omaha_request_params.h" |
30 #include "update_engine/subprocess.h" | 31 #include "update_engine/subprocess.h" |
(...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
414 return false; | 415 return false; |
415 } | 416 } |
416 return true; | 417 return true; |
417 } | 418 } |
418 | 419 |
419 bool UnmountFilesystem(const string& mountpoint) { | 420 bool UnmountFilesystem(const string& mountpoint) { |
420 TEST_AND_RETURN_FALSE_ERRNO(umount(mountpoint.c_str()) == 0); | 421 TEST_AND_RETURN_FALSE_ERRNO(umount(mountpoint.c_str()) == 0); |
421 return true; | 422 return true; |
422 } | 423 } |
423 | 424 |
| 425 bool GetFilesystemSize(const std::string& device, |
| 426 int* out_block_count, |
| 427 int* out_block_size) { |
| 428 int fd = HANDLE_EINTR(open(device.c_str(), O_RDONLY)); |
| 429 TEST_AND_RETURN_FALSE(fd >= 0); |
| 430 ScopedFdCloser fd_closer(&fd); |
| 431 return GetFilesystemSizeFromFD(fd, out_block_count, out_block_size); |
| 432 } |
| 433 |
| 434 bool GetFilesystemSizeFromFD(int fd, |
| 435 int* out_block_count, |
| 436 int* out_block_size) { |
| 437 TEST_AND_RETURN_FALSE(fd >= 0); |
| 438 |
| 439 // Determine the ext3 filesystem size by directly reading the block count and |
| 440 // block size information from the superblock. See include/linux/ext3_fs.h for |
| 441 // more details on the structure. |
| 442 ssize_t kBufferSize = 16 * sizeof(uint32_t); |
| 443 char buffer[kBufferSize]; |
| 444 const int kSuperblockOffset = 1024; |
| 445 if (HANDLE_EINTR(pread(fd, buffer, kBufferSize, kSuperblockOffset)) != |
| 446 kBufferSize) { |
| 447 PLOG(ERROR) << "Unable to determine file system size:"; |
| 448 return false; |
| 449 } |
| 450 uint32_t block_count; // ext3_fs.h: ext3_super_block.s_blocks_count |
| 451 uint32_t log_block_size; // ext3_fs.h: ext3_super_block.s_log_block_size |
| 452 uint16_t magic; // ext3_fs.h: ext3_super_block.s_magic |
| 453 memcpy(&block_count, &buffer[1 * sizeof(int32_t)], sizeof(block_count)); |
| 454 memcpy(&log_block_size, &buffer[6 * sizeof(int32_t)], sizeof(log_block_size)); |
| 455 memcpy(&magic, &buffer[14 * sizeof(int32_t)], sizeof(magic)); |
| 456 block_count = le32toh(block_count); |
| 457 const int kExt3MinBlockLogSize = 10; // ext3_fs.h: EXT3_MIN_BLOCK_LOG_SIZE |
| 458 log_block_size = le32toh(log_block_size) + kExt3MinBlockLogSize; |
| 459 magic = le16toh(magic); |
| 460 |
| 461 // Sanity check the parameters. |
| 462 const uint16_t kExt3SuperMagic = 0xef53; // ext3_fs.h: EXT3_SUPER_MAGIC |
| 463 TEST_AND_RETURN_FALSE(magic == kExt3SuperMagic); |
| 464 const int kExt3MinBlockSize = 1024; // ext3_fs.h: EXT3_MIN_BLOCK_SIZE |
| 465 const int kExt3MaxBlockSize = 4096; // ext3_fs.h: EXT3_MAX_BLOCK_SIZE |
| 466 int block_size = 1 << log_block_size; |
| 467 TEST_AND_RETURN_FALSE(block_size >= kExt3MinBlockSize && |
| 468 block_size <= kExt3MaxBlockSize); |
| 469 TEST_AND_RETURN_FALSE(block_count > 0); |
| 470 |
| 471 if (out_block_count) { |
| 472 *out_block_count = block_count; |
| 473 } |
| 474 if (out_block_size) { |
| 475 *out_block_size = block_size; |
| 476 } |
| 477 return true; |
| 478 } |
| 479 |
424 bool GetBootloader(BootLoader* out_bootloader) { | 480 bool GetBootloader(BootLoader* out_bootloader) { |
425 // For now, hardcode to syslinux. | 481 // For now, hardcode to syslinux. |
426 *out_bootloader = BootLoader_SYSLINUX; | 482 *out_bootloader = BootLoader_SYSLINUX; |
427 return true; | 483 return true; |
428 } | 484 } |
429 | 485 |
430 const char* GetGErrorMessage(const GError* error) { | 486 const char* GetGErrorMessage(const GError* error) { |
431 if (!error) | 487 if (!error) |
432 return "Unknown error."; | 488 return "Unknown error."; |
433 return error->message; | 489 return error->message; |
(...skipping 26 matching lines...) Expand all Loading... |
460 int min = value - range / 2; | 516 int min = value - range / 2; |
461 int max = value + range - range / 2; | 517 int max = value + range - range / 2; |
462 return base::RandInt(min, max); | 518 return base::RandInt(min, max); |
463 } | 519 } |
464 | 520 |
465 const char* const kStatefulPartition = "/mnt/stateful_partition"; | 521 const char* const kStatefulPartition = "/mnt/stateful_partition"; |
466 | 522 |
467 } // namespace utils | 523 } // namespace utils |
468 | 524 |
469 } // namespace chromeos_update_engine | 525 } // namespace chromeos_update_engine |
OLD | NEW |