OLD | NEW |
(Empty) | |
| 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 #include <sys/ioctl.h> |
| 7 #include <sys/types.h> |
| 8 #include <sys/stat.h> |
| 9 #include <assert.h> |
| 10 #include <errno.h> |
| 11 #include <fcntl.h> |
| 12 #include <stdio.h> |
| 13 #include <string.h> |
| 14 #include <linux/fs.h> |
| 15 #include "update_engine/utils.h" |
| 16 |
| 17 using std::string; |
| 18 using std::vector; |
| 19 |
| 20 namespace chromeos_update_engine { |
| 21 |
| 22 namespace { |
| 23 const int kBlockSize = 4096; |
| 24 } |
| 25 |
| 26 bool ExtentsForFileFibmap(const std::string& path, std::vector<Extent>* out) { |
| 27 CHECK(out); |
| 28 // TODO(adlr): verify path is a file |
| 29 struct stat stbuf; |
| 30 int rc = stat(path.c_str(), &stbuf); |
| 31 TEST_AND_RETURN_FALSE_ERRNO(rc == 0); |
| 32 TEST_AND_RETURN_FALSE(S_ISREG(stbuf.st_mode)); |
| 33 |
| 34 int fd = open(path.c_str(), O_RDONLY, 0); |
| 35 TEST_AND_RETURN_FALSE_ERRNO(fd >= 0); |
| 36 ScopedFdCloser fd_closer(&fd); |
| 37 |
| 38 // Get file size in blocks |
| 39 rc = fstat(fd, &stbuf); |
| 40 if (rc < 0) { |
| 41 perror("fstat"); |
| 42 return false; |
| 43 } |
| 44 const int block_count = (stbuf.st_size + kBlockSize - 1) / kBlockSize; |
| 45 Extent current; |
| 46 current.set_start_block(0); |
| 47 current.set_num_blocks(0); |
| 48 |
| 49 for (int i = 0; i < block_count; i++) { |
| 50 unsigned int block = i; |
| 51 rc = ioctl(fd, FIBMAP, &block); |
| 52 TEST_AND_RETURN_FALSE_ERRNO(rc == 0); |
| 53 |
| 54 // Add next block to extents |
| 55 if (current.num_blocks() == 0) { |
| 56 // We're starting a new extent |
| 57 current.set_start_block(block); |
| 58 current.set_num_blocks(1); |
| 59 continue; |
| 60 } |
| 61 if ((current.start_block() + current.num_blocks()) == block) { |
| 62 // We're continuing the last extent |
| 63 current.set_num_blocks(current.num_blocks() + 1); |
| 64 continue; |
| 65 } |
| 66 // We're starting a new extent and keeping the current one |
| 67 out->push_back(current); |
| 68 current.set_start_block(block); |
| 69 current.set_num_blocks(1); |
| 70 continue; |
| 71 } |
| 72 |
| 73 if (current.num_blocks() > 0) |
| 74 out->push_back(current); |
| 75 |
| 76 return true; |
| 77 } |
| 78 |
| 79 } // namespace chromeos_update_engine |
OLD | NEW |