Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 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 Deymo | 2b19cfb | 2015-03-26 00:35:07 -0700 | [diff] [blame] | 16 | |
| 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 Jiang | 771f648 | 2018-04-04 17:59:10 -0700 | [diff] [blame] | 22 | // 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 Deymo | 2b19cfb | 2015-03-26 00:35:07 -0700 | [diff] [blame] | 25 | |
| 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 Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 34 | #include <brillo/key_value_store.h> |
Amin Hassani | 3cd4df1 | 2017-08-25 11:21:53 -0700 | [diff] [blame] | 35 | #include <puffin/utils.h> |
Alex Deymo | 2b19cfb | 2015-03-26 00:35:07 -0700 | [diff] [blame] | 36 | |
Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame^] | 37 | #include "update_engine/lz4diff/lz4diff_format.h" |
Alex Deymo | 2b19cfb | 2015-03-26 00:35:07 -0700 | [diff] [blame] | 38 | #include "update_engine/update_metadata.pb.h" |
| 39 | |
| 40 | namespace chromeos_update_engine { |
| 41 | |
| 42 | class FilesystemInterface { |
| 43 | public: |
| 44 | // This represents a file or pseudo-file in the filesystem. It can include |
| 45 | // all sort of files, like symlinks, hardlinks, directories and even a file |
| 46 | // entry representing the metadata, free space, journaling data, etc. |
| 47 | struct File { |
Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 48 | File() { memset(&file_stat, 0, sizeof(file_stat)); } |
Alex Deymo | 2b19cfb | 2015-03-26 00:35:07 -0700 | [diff] [blame] | 49 | |
| 50 | // The stat struct for the file. This is invalid (inode 0) for some |
| 51 | // pseudo-files. |
| 52 | struct stat file_stat; |
| 53 | |
| 54 | // The absolute path to the file inside the filesystem, for example, |
| 55 | // "/usr/bin/bash". For pseudo-files, like blocks associated to internal |
| 56 | // filesystem tables or free space, the path doesn't start with a /. |
| 57 | std::string name; |
| 58 | |
| 59 | // The list of all physical blocks holding the data of this file in |
| 60 | // the same order as the logical data. All the block numbers shall be |
| 61 | // between 0 and GetBlockCount() - 1. The blocks are encoded in extents, |
| 62 | // indicating the starting block, and the number of consecutive blocks. |
| 63 | std::vector<Extent> extents; |
Amin Hassani | 3cd4df1 | 2017-08-25 11:21:53 -0700 | [diff] [blame] | 64 | |
Amin Hassani | 1a200c1 | 2020-02-26 14:47:23 -0800 | [diff] [blame] | 65 | // If true, the file is already compressed on the disk, so we don't need to |
| 66 | // parse it again for deflates. For example, image .gz files inside a |
| 67 | // compressed SquashFS image. They might have already been compressed by the |
| 68 | // mksquashfs, so we can't really parse the file and look for deflate |
| 69 | // compressed parts anymore. |
| 70 | bool is_compressed = false; |
| 71 | |
Amin Hassani | 3cd4df1 | 2017-08-25 11:21:53 -0700 | [diff] [blame] | 72 | // All the deflate locations in the file. These locations are not relative |
| 73 | // to the extents. They are relative to the file system itself. |
| 74 | std::vector<puffin::BitExtent> deflates; |
Kelvin Zhang | 446989a | 2021-12-08 13:49:07 -0800 | [diff] [blame^] | 75 | |
| 76 | CompressedFile compressed_file_info; |
Alex Deymo | 2b19cfb | 2015-03-26 00:35:07 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | virtual ~FilesystemInterface() = default; |
| 80 | |
| 81 | // Returns the size of a block in the filesystem. |
| 82 | virtual size_t GetBlockSize() const = 0; |
| 83 | |
| 84 | // Returns the number of blocks in the filesystem. |
| 85 | virtual size_t GetBlockCount() const = 0; |
| 86 | |
| 87 | // Stores in |files| the list of files and pseudo-files in the filesystem. See |
| 88 | // FileInterface for details. The paths returned by this method shall not |
| 89 | // be repeated; but the same block could be present in more than one file as |
| 90 | // happens for example with hard-linked files, but not limited to those cases. |
| 91 | // Returns whether the function succeeded. |
| 92 | virtual bool GetFiles(std::vector<File>* files) const = 0; |
| 93 | |
Alex Deymo | 2e9533b | 2015-06-26 20:57:06 -0700 | [diff] [blame] | 94 | // Load the image settings stored in the filesystem in the |
| 95 | // /etc/update_engine.conf file. Returns whether the settings were found. |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 96 | virtual bool LoadSettings(brillo::KeyValueStore* store) const = 0; |
Alex Deymo | 2e9533b | 2015-06-26 20:57:06 -0700 | [diff] [blame] | 97 | |
Alex Deymo | 2b19cfb | 2015-03-26 00:35:07 -0700 | [diff] [blame] | 98 | protected: |
| 99 | FilesystemInterface() = default; |
| 100 | |
| 101 | private: |
| 102 | DISALLOW_COPY_AND_ASSIGN(FilesystemInterface); |
| 103 | }; |
| 104 | |
| 105 | } // namespace chromeos_update_engine |
| 106 | |
| 107 | #endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_FILESYSTEM_INTERFACE_H_ |