blob: 0c2f05e306b72e2d673991237479ff38b86bc177 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2015 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Alex Deymo2b19cfb2015-03-26 00:35:07 -070016
17#ifndef UPDATE_ENGINE_PAYLOAD_GENERATOR_FILESYSTEM_INTERFACE_H_
18#define UPDATE_ENGINE_PAYLOAD_GENERATOR_FILESYSTEM_INTERFACE_H_
19
20// This class is used to abstract a filesystem and iterate the blocks
21// associated with the files and filesystem structures.
Sen Jiang771f6482018-04-04 17:59:10 -070022// For the purposes of the update payload generation, a filesystem is a
23// formatted partition composed by fixed-size blocks, since that's the interface
24// used in the update payload.
Alex Deymo2b19cfb2015-03-26 00:35:07 -070025
26#include <sys/stat.h>
27#include <sys/types.h>
28#include <unistd.h>
29
30#include <memory>
31#include <string>
32#include <vector>
33
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070034#include <brillo/key_value_store.h>
Amin Hassani3cd4df12017-08-25 11:21:53 -070035#include <puffin/utils.h>
Alex Deymo2b19cfb2015-03-26 00:35:07 -070036
Kelvin Zhang446989a2021-12-08 13:49:07 -080037#include "update_engine/lz4diff/lz4diff_format.h"
Kelvin Zhangcb2dc882021-11-22 09:26:50 -080038#include "update_engine/lz4diff/lz4diff.h"
Alex Deymo2b19cfb2015-03-26 00:35:07 -070039#include "update_engine/update_metadata.pb.h"
40
41namespace chromeos_update_engine {
42
43class FilesystemInterface {
44 public:
45 // This represents a file or pseudo-file in the filesystem. It can include
46 // all sort of files, like symlinks, hardlinks, directories and even a file
47 // entry representing the metadata, free space, journaling data, etc.
Kelvin Zhangcb2dc882021-11-22 09:26:50 -080048
Alex Deymo2b19cfb2015-03-26 00:35:07 -070049 struct File {
Amin Hassani232f8f92019-01-14 16:15:31 -080050 File() { memset(&file_stat, 0, sizeof(file_stat)); }
Alex Deymo2b19cfb2015-03-26 00:35:07 -070051
52 // The stat struct for the file. This is invalid (inode 0) for some
53 // pseudo-files.
Kelvin Zhangcb2dc882021-11-22 09:26:50 -080054 struct stat file_stat = {};
Alex Deymo2b19cfb2015-03-26 00:35:07 -070055
56 // The absolute path to the file inside the filesystem, for example,
57 // "/usr/bin/bash". For pseudo-files, like blocks associated to internal
58 // filesystem tables or free space, the path doesn't start with a /.
59 std::string name;
60
61 // The list of all physical blocks holding the data of this file in
62 // the same order as the logical data. All the block numbers shall be
63 // between 0 and GetBlockCount() - 1. The blocks are encoded in extents,
64 // indicating the starting block, and the number of consecutive blocks.
65 std::vector<Extent> extents;
Amin Hassani3cd4df12017-08-25 11:21:53 -070066
Amin Hassani1a200c12020-02-26 14:47:23 -080067 // If true, the file is already compressed on the disk, so we don't need to
68 // parse it again for deflates. For example, image .gz files inside a
69 // compressed SquashFS image. They might have already been compressed by the
70 // mksquashfs, so we can't really parse the file and look for deflate
71 // compressed parts anymore.
72 bool is_compressed = false;
73
Amin Hassani3cd4df12017-08-25 11:21:53 -070074 // All the deflate locations in the file. These locations are not relative
75 // to the extents. They are relative to the file system itself.
76 std::vector<puffin::BitExtent> deflates;
Kelvin Zhang446989a2021-12-08 13:49:07 -080077
78 CompressedFile compressed_file_info;
Alex Deymo2b19cfb2015-03-26 00:35:07 -070079 };
80
81 virtual ~FilesystemInterface() = default;
82
83 // Returns the size of a block in the filesystem.
84 virtual size_t GetBlockSize() const = 0;
85
86 // Returns the number of blocks in the filesystem.
87 virtual size_t GetBlockCount() const = 0;
88
89 // Stores in |files| the list of files and pseudo-files in the filesystem. See
90 // FileInterface for details. The paths returned by this method shall not
91 // be repeated; but the same block could be present in more than one file as
92 // happens for example with hard-linked files, but not limited to those cases.
93 // Returns whether the function succeeded.
94 virtual bool GetFiles(std::vector<File>* files) const = 0;
95
Alex Deymo2e9533b2015-06-26 20:57:06 -070096 // Load the image settings stored in the filesystem in the
97 // /etc/update_engine.conf file. Returns whether the settings were found.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070098 virtual bool LoadSettings(brillo::KeyValueStore* store) const = 0;
Alex Deymo2e9533b2015-06-26 20:57:06 -070099
Alex Deymo2b19cfb2015-03-26 00:35:07 -0700100 protected:
101 FilesystemInterface() = default;
102
103 private:
104 DISALLOW_COPY_AND_ASSIGN(FilesystemInterface);
105};
106
107} // namespace chromeos_update_engine
108
109#endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_FILESYSTEM_INTERFACE_H_