blob: ace463fe65ad10487e9a10a070875789a3ef54b4 [file] [log] [blame]
Andrew de los Reyesb4025e62010-02-23 17:47:03 -08001// 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 Deymo161c4a12014-05-16 15:56:21 -07005#include "update_engine/payload_generator/extent_mapper.h"
Andrew de los Reyesb4025e62010-02-23 17:47:03 -08006
7#include <assert.h>
8#include <errno.h>
9#include <fcntl.h>
Alex Deymo161c4a12014-05-16 15:56:21 -070010#include <linux/fs.h>
Andrew de los Reyesb4025e62010-02-23 17:47:03 -080011#include <stdio.h>
12#include <string.h>
Alex Deymo161c4a12014-05-16 15:56:21 -070013#include <sys/ioctl.h>
14#include <sys/stat.h>
15#include <sys/types.h>
Andrew de los Reyesb4025e62010-02-23 17:47:03 -080016
Alex Vakulenko072359c2014-07-18 11:41:07 -070017#include <algorithm>
18
Alex Deymo161c4a12014-05-16 15:56:21 -070019#include "update_engine/payload_constants.h"
20#include "update_engine/payload_generator/graph_types.h"
21#include "update_engine/payload_generator/graph_utils.h"
Andrew de los Reyesb4025e62010-02-23 17:47:03 -080022#include "update_engine/utils.h"
23
24using std::string;
25using std::vector;
26
27namespace chromeos_update_engine {
28
29namespace extent_mapper {
30
31namespace {
32const int kBlockSize = 4096;
33}
34
Darin Petkov8e447e02013-04-16 16:23:50 +020035bool ExtentsForFileChunkFibmap(const std::string& path,
36 off_t chunk_offset,
37 off_t chunk_size,
38 std::vector<Extent>* out) {
Andrew de los Reyesb4025e62010-02-23 17:47:03 -080039 CHECK(out);
Darin Petkov8e447e02013-04-16 16:23:50 +020040 CHECK_EQ(0, chunk_offset % kBlockSize);
41 CHECK(chunk_size == -1 || chunk_size >= 0);
Andrew de los Reyesb4025e62010-02-23 17:47:03 -080042 struct stat stbuf;
43 int rc = stat(path.c_str(), &stbuf);
44 TEST_AND_RETURN_FALSE_ERRNO(rc == 0);
45 TEST_AND_RETURN_FALSE(S_ISREG(stbuf.st_mode));
Darin Petkov8e447e02013-04-16 16:23:50 +020046
Andrew de los Reyesb4025e62010-02-23 17:47:03 -080047 int fd = open(path.c_str(), O_RDONLY, 0);
48 TEST_AND_RETURN_FALSE_ERRNO(fd >= 0);
49 ScopedFdCloser fd_closer(&fd);
Darin Petkov8e447e02013-04-16 16:23:50 +020050
Andrew de los Reyesb4025e62010-02-23 17:47:03 -080051 // Get file size in blocks
52 rc = fstat(fd, &stbuf);
53 if (rc < 0) {
54 perror("fstat");
55 return false;
56 }
Darin Petkov8e447e02013-04-16 16:23:50 +020057 CHECK_LE(chunk_offset, stbuf.st_size);
58 off_t size = stbuf.st_size - chunk_offset;
59 if (chunk_size != -1) {
60 size = std::min(size, chunk_size);
61 }
62 const int block_count = (size + kBlockSize - 1) / kBlockSize;
63 const int start_block = chunk_offset / kBlockSize;
Andrew de los Reyesb4025e62010-02-23 17:47:03 -080064 Extent current;
65 current.set_start_block(0);
66 current.set_num_blocks(0);
67
Darin Petkov8e447e02013-04-16 16:23:50 +020068 for (int i = start_block; i < start_block + block_count; i++) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070069 unsigned int block32 = i;
70 rc = ioctl(fd, FIBMAP, &block32);
Andrew de los Reyesb4025e62010-02-23 17:47:03 -080071 TEST_AND_RETURN_FALSE_ERRNO(rc == 0);
Darin Petkov8e447e02013-04-16 16:23:50 +020072
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070073 const uint64_t block = (block32 == 0 ? kSparseHole : block32);
Darin Petkov8e447e02013-04-16 16:23:50 +020074
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070075 graph_utils::AppendBlockToExtents(out, block);
Andrew de los Reyesb4025e62010-02-23 17:47:03 -080076 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070077 return true;
78}
Andrew de los Reyesb4025e62010-02-23 17:47:03 -080079
Darin Petkov8e447e02013-04-16 16:23:50 +020080bool ExtentsForFileFibmap(const std::string& path, std::vector<Extent>* out) {
81 return ExtentsForFileChunkFibmap(path, 0, -1, out);
82}
83
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070084bool GetFilesystemBlockSize(const std::string& path, uint32_t* out_blocksize) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070085 int fd = open(path.c_str(), O_RDONLY, 0);
86 TEST_AND_RETURN_FALSE_ERRNO(fd >= 0);
87 ScopedFdCloser fd_closer(&fd);
88 int rc = ioctl(fd, FIGETBSZ, out_blocksize);
89 TEST_AND_RETURN_FALSE_ERRNO(rc != -1);
Andrew de los Reyesb4025e62010-02-23 17:47:03 -080090 return true;
91}
92
93} // namespace extent_mapper
94
95} // namespace chromeos_update_engine