blob: 9e666192ee82f1b17a1293c7cfb2b837a96c80eb [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 Deymo161c4a12014-05-16 15:56:21 -070017#include "update_engine/payload_constants.h"
18#include "update_engine/payload_generator/graph_types.h"
19#include "update_engine/payload_generator/graph_utils.h"
Andrew de los Reyesb4025e62010-02-23 17:47:03 -080020#include "update_engine/utils.h"
21
22using std::string;
23using std::vector;
24
25namespace chromeos_update_engine {
26
27namespace extent_mapper {
28
29namespace {
30const int kBlockSize = 4096;
31}
32
Darin Petkov8e447e02013-04-16 16:23:50 +020033bool ExtentsForFileChunkFibmap(const std::string& path,
34 off_t chunk_offset,
35 off_t chunk_size,
36 std::vector<Extent>* out) {
Andrew de los Reyesb4025e62010-02-23 17:47:03 -080037 CHECK(out);
Darin Petkov8e447e02013-04-16 16:23:50 +020038 CHECK_EQ(0, chunk_offset % kBlockSize);
39 CHECK(chunk_size == -1 || chunk_size >= 0);
Andrew de los Reyesb4025e62010-02-23 17:47:03 -080040 struct stat stbuf;
41 int rc = stat(path.c_str(), &stbuf);
42 TEST_AND_RETURN_FALSE_ERRNO(rc == 0);
43 TEST_AND_RETURN_FALSE(S_ISREG(stbuf.st_mode));
Darin Petkov8e447e02013-04-16 16:23:50 +020044
Andrew de los Reyesb4025e62010-02-23 17:47:03 -080045 int fd = open(path.c_str(), O_RDONLY, 0);
46 TEST_AND_RETURN_FALSE_ERRNO(fd >= 0);
47 ScopedFdCloser fd_closer(&fd);
Darin Petkov8e447e02013-04-16 16:23:50 +020048
Andrew de los Reyesb4025e62010-02-23 17:47:03 -080049 // Get file size in blocks
50 rc = fstat(fd, &stbuf);
51 if (rc < 0) {
52 perror("fstat");
53 return false;
54 }
Darin Petkov8e447e02013-04-16 16:23:50 +020055 CHECK_LE(chunk_offset, stbuf.st_size);
56 off_t size = stbuf.st_size - chunk_offset;
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 Reyesb4025e62010-02-23 17:47:03 -080062 Extent current;
63 current.set_start_block(0);
64 current.set_num_blocks(0);
65
Darin Petkov8e447e02013-04-16 16:23:50 +020066 for (int i = start_block; i < start_block + block_count; i++) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070067 unsigned int block32 = i;
68 rc = ioctl(fd, FIBMAP, &block32);
Andrew de los Reyesb4025e62010-02-23 17:47:03 -080069 TEST_AND_RETURN_FALSE_ERRNO(rc == 0);
Darin Petkov8e447e02013-04-16 16:23:50 +020070
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070071 const uint64_t block = (block32 == 0 ? kSparseHole : block32);
Darin Petkov8e447e02013-04-16 16:23:50 +020072
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070073 graph_utils::AppendBlockToExtents(out, block);
Andrew de los Reyesb4025e62010-02-23 17:47:03 -080074 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070075 return true;
76}
Andrew de los Reyesb4025e62010-02-23 17:47:03 -080077
Darin Petkov8e447e02013-04-16 16:23:50 +020078bool ExtentsForFileFibmap(const std::string& path, std::vector<Extent>* out) {
79 return ExtentsForFileChunkFibmap(path, 0, -1, out);
80}
81
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070082bool GetFilesystemBlockSize(const std::string& path, uint32_t* out_blocksize) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070083 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 Reyesb4025e62010-02-23 17:47:03 -080088 return true;
89}
90
91} // namespace extent_mapper
92
93} // namespace chromeos_update_engine