blob: 1cfd8bbf306320b017ea583c42aa8da78f035791 [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
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 Reyesb10320d2010-03-31 16:44:44 -070019#include "update_engine/graph_types.h"
20#include "update_engine/graph_utils.h"
Andrew de los Reyesb4025e62010-02-23 17:47:03 -080021#include "update_engine/utils.h"
22
23using std::string;
24using std::vector;
25
26namespace chromeos_update_engine {
27
28namespace extent_mapper {
29
30namespace {
31const int kBlockSize = 4096;
32}
33
Darin Petkov8e447e02013-04-16 16:23:50 +020034bool ExtentsForFileChunkFibmap(const std::string& path,
35 off_t chunk_offset,
36 off_t chunk_size,
37 std::vector<Extent>* out) {
Andrew de los Reyesb4025e62010-02-23 17:47:03 -080038 CHECK(out);
Darin Petkov8e447e02013-04-16 16:23:50 +020039 CHECK_EQ(0, chunk_offset % kBlockSize);
40 CHECK(chunk_size == -1 || chunk_size >= 0);
Andrew de los Reyesb4025e62010-02-23 17:47:03 -080041 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 Petkov8e447e02013-04-16 16:23:50 +020045
Andrew de los Reyesb4025e62010-02-23 17:47:03 -080046 int fd = open(path.c_str(), O_RDONLY, 0);
47 TEST_AND_RETURN_FALSE_ERRNO(fd >= 0);
48 ScopedFdCloser fd_closer(&fd);
Darin Petkov8e447e02013-04-16 16:23:50 +020049
Andrew de los Reyesb4025e62010-02-23 17:47:03 -080050 // Get file size in blocks
51 rc = fstat(fd, &stbuf);
52 if (rc < 0) {
53 perror("fstat");
54 return false;
55 }
Darin Petkov8e447e02013-04-16 16:23:50 +020056 CHECK_LE(chunk_offset, stbuf.st_size);
57 off_t size = stbuf.st_size - chunk_offset;
58 if (chunk_size != -1) {
59 size = std::min(size, chunk_size);
60 }
61 const int block_count = (size + kBlockSize - 1) / kBlockSize;
62 const int start_block = chunk_offset / kBlockSize;
Andrew de los Reyesb4025e62010-02-23 17:47:03 -080063 Extent current;
64 current.set_start_block(0);
65 current.set_num_blocks(0);
66
Darin Petkov8e447e02013-04-16 16:23:50 +020067 for (int i = start_block; i < start_block + block_count; i++) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070068 unsigned int block32 = i;
69 rc = ioctl(fd, FIBMAP, &block32);
Andrew de los Reyesb4025e62010-02-23 17:47:03 -080070 TEST_AND_RETURN_FALSE_ERRNO(rc == 0);
Darin Petkov8e447e02013-04-16 16:23:50 +020071
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070072 const uint64_t block = (block32 == 0 ? kSparseHole : block32);
Darin Petkov8e447e02013-04-16 16:23:50 +020073
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070074 graph_utils::AppendBlockToExtents(out, block);
Andrew de los Reyesb4025e62010-02-23 17:47:03 -080075 }
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070076 return true;
77}
Andrew de los Reyesb4025e62010-02-23 17:47:03 -080078
Darin Petkov8e447e02013-04-16 16:23:50 +020079bool ExtentsForFileFibmap(const std::string& path, std::vector<Extent>* out) {
80 return ExtentsForFileChunkFibmap(path, 0, -1, out);
81}
82
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070083bool GetFilesystemBlockSize(const std::string& path, uint32_t* out_blocksize) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070084 int fd = open(path.c_str(), O_RDONLY, 0);
85 TEST_AND_RETURN_FALSE_ERRNO(fd >= 0);
86 ScopedFdCloser fd_closer(&fd);
87 int rc = ioctl(fd, FIGETBSZ, out_blocksize);
88 TEST_AND_RETURN_FALSE_ERRNO(rc != -1);
Andrew de los Reyesb4025e62010-02-23 17:47:03 -080089 return true;
90}
91
92} // namespace extent_mapper
93
94} // namespace chromeos_update_engine