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 | |
| 5 | #include "update_engine/extent_mapper.h" |
| 6 | |
| 7 | #include <sys/ioctl.h> |
| 8 | #include <sys/types.h> |
| 9 | #include <sys/stat.h> |
| 10 | |
| 11 | #include <assert.h> |
| 12 | #include <errno.h> |
| 13 | #include <fcntl.h> |
| 14 | #include <stdio.h> |
| 15 | #include <string.h> |
| 16 | |
| 17 | #include <linux/fs.h> |
| 18 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 19 | #include "update_engine/graph_types.h" |
| 20 | #include "update_engine/graph_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 | |
| 34 | bool ExtentsForFileFibmap(const std::string& path, std::vector<Extent>* out) { |
| 35 | CHECK(out); |
Andrew de los Reyes | b4025e6 | 2010-02-23 17:47:03 -0800 | [diff] [blame] | 36 | struct stat stbuf; |
| 37 | int rc = stat(path.c_str(), &stbuf); |
| 38 | TEST_AND_RETURN_FALSE_ERRNO(rc == 0); |
| 39 | TEST_AND_RETURN_FALSE(S_ISREG(stbuf.st_mode)); |
| 40 | |
| 41 | int fd = open(path.c_str(), O_RDONLY, 0); |
| 42 | TEST_AND_RETURN_FALSE_ERRNO(fd >= 0); |
| 43 | ScopedFdCloser fd_closer(&fd); |
| 44 | |
| 45 | // Get file size in blocks |
| 46 | rc = fstat(fd, &stbuf); |
| 47 | if (rc < 0) { |
| 48 | perror("fstat"); |
| 49 | return false; |
| 50 | } |
| 51 | const int block_count = (stbuf.st_size + kBlockSize - 1) / kBlockSize; |
| 52 | Extent current; |
| 53 | current.set_start_block(0); |
| 54 | current.set_num_blocks(0); |
| 55 | |
| 56 | for (int i = 0; i < block_count; i++) { |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 57 | unsigned int block32 = i; |
| 58 | rc = ioctl(fd, FIBMAP, &block32); |
Andrew de los Reyes | b4025e6 | 2010-02-23 17:47:03 -0800 | [diff] [blame] | 59 | TEST_AND_RETURN_FALSE_ERRNO(rc == 0); |
| 60 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 61 | const uint64_t block = (block32 == 0 ? kSparseHole : block32); |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 62 | |
| 63 | graph_utils::AppendBlockToExtents(out, block); |
Andrew de los Reyes | b4025e6 | 2010-02-23 17:47:03 -0800 | [diff] [blame] | 64 | } |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 65 | return true; |
| 66 | } |
Andrew de los Reyes | b4025e6 | 2010-02-23 17:47:03 -0800 | [diff] [blame] | 67 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 68 | bool GetFilesystemBlockSize(const std::string& path, uint32_t* out_blocksize) { |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 69 | int fd = open(path.c_str(), O_RDONLY, 0); |
| 70 | TEST_AND_RETURN_FALSE_ERRNO(fd >= 0); |
| 71 | ScopedFdCloser fd_closer(&fd); |
| 72 | int rc = ioctl(fd, FIGETBSZ, out_blocksize); |
| 73 | TEST_AND_RETURN_FALSE_ERRNO(rc != -1); |
Andrew de los Reyes | b4025e6 | 2010-02-23 17:47:03 -0800 | [diff] [blame] | 74 | return true; |
| 75 | } |
| 76 | |
| 77 | } // namespace extent_mapper |
| 78 | |
| 79 | } // namespace chromeos_update_engine |