Andrew de los Reyes | b4025e6 | 2010-02-23 17:47:03 -0800 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 5 | #include "update_engine/payload_generator/extent_mapper.h" |
Andrew de los Reyes | b4025e6 | 2010-02-23 17:47:03 -0800 | [diff] [blame] | 6 | |
| 7 | #include <assert.h> |
| 8 | #include <errno.h> |
| 9 | #include <fcntl.h> |
Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 10 | #include <linux/fs.h> |
Andrew de los Reyes | b4025e6 | 2010-02-23 17:47:03 -0800 | [diff] [blame] | 11 | #include <stdio.h> |
| 12 | #include <string.h> |
Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 13 | #include <sys/ioctl.h> |
| 14 | #include <sys/stat.h> |
| 15 | #include <sys/types.h> |
Andrew de los Reyes | b4025e6 | 2010-02-23 17:47:03 -0800 | [diff] [blame] | 16 | |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 17 | #include <algorithm> |
| 18 | |
Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 19 | #include "update_engine/payload_constants.h" |
Alex Deymo | 5c6c655 | 2015-06-03 19:06:50 +0200 | [diff] [blame^] | 20 | #include "update_engine/payload_generator/extent_utils.h" |
Andrew de los Reyes | b4025e6 | 2010-02-23 17:47:03 -0800 | [diff] [blame] | 21 | #include "update_engine/utils.h" |
| 22 | |
| 23 | using std::string; |
| 24 | using std::vector; |
| 25 | |
| 26 | namespace chromeos_update_engine { |
| 27 | |
| 28 | namespace extent_mapper { |
| 29 | |
| 30 | namespace { |
| 31 | const int kBlockSize = 4096; |
| 32 | } |
| 33 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 34 | bool ExtentsForFileChunkFibmap(const string& path, |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 35 | off_t chunk_offset, |
| 36 | off_t chunk_size, |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 37 | vector<Extent>* out) { |
Andrew de los Reyes | b4025e6 | 2010-02-23 17:47:03 -0800 | [diff] [blame] | 38 | CHECK(out); |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 39 | CHECK_EQ(0, chunk_offset % kBlockSize); |
| 40 | CHECK(chunk_size == -1 || chunk_size >= 0); |
Andrew de los Reyes | b4025e6 | 2010-02-23 17:47:03 -0800 | [diff] [blame] | 41 | struct stat stbuf; |
| 42 | int rc = stat(path.c_str(), &stbuf); |
| 43 | TEST_AND_RETURN_FALSE_ERRNO(rc == 0); |
| 44 | TEST_AND_RETURN_FALSE(S_ISREG(stbuf.st_mode)); |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 45 | |
Andrew de los Reyes | b4025e6 | 2010-02-23 17:47:03 -0800 | [diff] [blame] | 46 | int fd = open(path.c_str(), O_RDONLY, 0); |
| 47 | TEST_AND_RETURN_FALSE_ERRNO(fd >= 0); |
| 48 | ScopedFdCloser fd_closer(&fd); |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 49 | |
Andrew de los Reyes | b4025e6 | 2010-02-23 17:47:03 -0800 | [diff] [blame] | 50 | // Get file size in blocks |
Gabe Black | a77939e | 2014-09-09 23:35:08 -0700 | [diff] [blame] | 51 | off_t file_size = utils::FileSize(fd); |
| 52 | if (file_size < 0) { |
Andrew de los Reyes | b4025e6 | 2010-02-23 17:47:03 -0800 | [diff] [blame] | 53 | return false; |
| 54 | } |
Gabe Black | a77939e | 2014-09-09 23:35:08 -0700 | [diff] [blame] | 55 | CHECK_LE(chunk_offset, file_size); |
| 56 | off_t size = file_size - chunk_offset; |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 57 | if (chunk_size != -1) { |
| 58 | size = std::min(size, chunk_size); |
| 59 | } |
| 60 | const int block_count = (size + kBlockSize - 1) / kBlockSize; |
| 61 | const int start_block = chunk_offset / kBlockSize; |
Andrew de los Reyes | b4025e6 | 2010-02-23 17:47:03 -0800 | [diff] [blame] | 62 | Extent current; |
| 63 | current.set_start_block(0); |
| 64 | current.set_num_blocks(0); |
| 65 | |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 66 | for (int i = start_block; i < start_block + block_count; i++) { |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 67 | unsigned int block32 = i; |
| 68 | rc = ioctl(fd, FIBMAP, &block32); |
Andrew de los Reyes | b4025e6 | 2010-02-23 17:47:03 -0800 | [diff] [blame] | 69 | TEST_AND_RETURN_FALSE_ERRNO(rc == 0); |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 70 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 71 | const uint64_t block = (block32 == 0 ? kSparseHole : block32); |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 72 | |
Alex Deymo | 5c6c655 | 2015-06-03 19:06:50 +0200 | [diff] [blame^] | 73 | AppendBlockToExtents(out, block); |
Andrew de los Reyes | b4025e6 | 2010-02-23 17:47:03 -0800 | [diff] [blame] | 74 | } |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 75 | return true; |
| 76 | } |
Andrew de los Reyes | b4025e6 | 2010-02-23 17:47:03 -0800 | [diff] [blame] | 77 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 78 | bool ExtentsForFileFibmap(const string& path, vector<Extent>* out) { |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 79 | return ExtentsForFileChunkFibmap(path, 0, -1, out); |
| 80 | } |
| 81 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 82 | bool GetFilesystemBlockSize(const string& path, uint32_t* out_blocksize) { |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 83 | int fd = open(path.c_str(), O_RDONLY, 0); |
| 84 | TEST_AND_RETURN_FALSE_ERRNO(fd >= 0); |
| 85 | ScopedFdCloser fd_closer(&fd); |
| 86 | int rc = ioctl(fd, FIGETBSZ, out_blocksize); |
| 87 | TEST_AND_RETURN_FALSE_ERRNO(rc != -1); |
Andrew de los Reyes | b4025e6 | 2010-02-23 17:47:03 -0800 | [diff] [blame] | 88 | return true; |
| 89 | } |
| 90 | |
| 91 | } // namespace extent_mapper |
| 92 | |
| 93 | } // namespace chromeos_update_engine |