Amin Hassani | 924183b | 2017-09-27 14:50:59 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2017 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 | // |
| 16 | |
| 17 | #include "update_engine/payload_generator/deflate_utils.h" |
| 18 | |
| 19 | #include <algorithm> |
| 20 | #include <string> |
| 21 | #include <utility> |
| 22 | |
| 23 | #include <base/files/file_util.h> |
| 24 | #include <base/logging.h> |
Amin Hassani | 924183b | 2017-09-27 14:50:59 -0700 | [diff] [blame] | 25 | |
| 26 | #include "update_engine/common/utils.h" |
| 27 | #include "update_engine/payload_generator/delta_diff_generator.h" |
| 28 | #include "update_engine/payload_generator/extent_ranges.h" |
Kelvin Zhang | 7949fce | 2024-03-12 10:17:40 -0700 | [diff] [blame] | 29 | #include "update_engine/payload_generator/extent_utils.h" |
Amin Hassani | 924183b | 2017-09-27 14:50:59 -0700 | [diff] [blame] | 30 | #include "update_engine/payload_generator/squashfs_filesystem.h" |
| 31 | #include "update_engine/update_metadata.pb.h" |
| 32 | |
Amin Hassani | 3cd4df1 | 2017-08-25 11:21:53 -0700 | [diff] [blame] | 33 | using puffin::BitExtent; |
| 34 | using puffin::ByteExtent; |
Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 35 | using std::string; |
| 36 | using std::vector; |
Amin Hassani | 924183b | 2017-09-27 14:50:59 -0700 | [diff] [blame] | 37 | |
| 38 | namespace chromeos_update_engine { |
| 39 | namespace deflate_utils { |
| 40 | namespace { |
| 41 | |
Kelvin Zhang | 7949fce | 2024-03-12 10:17:40 -0700 | [diff] [blame] | 42 | constexpr std::ostream& operator<<(std::ostream& out, |
| 43 | const puffin::BitExtent& ext) { |
| 44 | out << "BitExtent(" << ext.offset << "," << ext.length << ")"; |
| 45 | return out; |
| 46 | } |
| 47 | |
Amin Hassani | 924183b | 2017-09-27 14:50:59 -0700 | [diff] [blame] | 48 | // The minimum size for a squashfs image to be processed. |
| 49 | const uint64_t kMinimumSquashfsImageSize = 1 * 1024 * 1024; // bytes |
| 50 | |
| 51 | // TODO(*): Optimize this so we don't have to read all extents into memory in |
| 52 | // case it is large. |
| 53 | bool CopyExtentsToFile(const string& in_path, |
Andrew | 9d5a61d | 2020-03-26 13:40:37 -0700 | [diff] [blame] | 54 | const vector<Extent>& extents, |
Amin Hassani | 924183b | 2017-09-27 14:50:59 -0700 | [diff] [blame] | 55 | const string& out_path, |
| 56 | size_t block_size) { |
Amin Hassani | d8b67f4 | 2017-12-06 13:47:52 -0800 | [diff] [blame] | 57 | brillo::Blob data(utils::BlocksInExtents(extents) * block_size); |
Amin Hassani | 924183b | 2017-09-27 14:50:59 -0700 | [diff] [blame] | 58 | TEST_AND_RETURN_FALSE( |
| 59 | utils::ReadExtents(in_path, extents, &data, data.size(), block_size)); |
| 60 | TEST_AND_RETURN_FALSE( |
| 61 | utils::WriteFile(out_path.c_str(), data.data(), data.size())); |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | bool IsSquashfsImage(const string& part_path, |
| 66 | const FilesystemInterface::File& file) { |
| 67 | // Only check for files with img postfix. |
Kelvin Zhang | 0c18424 | 2024-10-25 11:19:27 -0700 | [diff] [blame^] | 68 | if (android::base::EndsWith(file.name, ".img") && |
Amin Hassani | d8b67f4 | 2017-12-06 13:47:52 -0800 | [diff] [blame] | 69 | utils::BlocksInExtents(file.extents) >= |
| 70 | kMinimumSquashfsImageSize / kBlockSize) { |
Amin Hassani | 924183b | 2017-09-27 14:50:59 -0700 | [diff] [blame] | 71 | brillo::Blob super_block; |
| 72 | TEST_AND_RETURN_FALSE( |
| 73 | utils::ReadFileChunk(part_path, |
| 74 | file.extents[0].start_block() * kBlockSize, |
| 75 | 100, |
| 76 | &super_block)); |
| 77 | return SquashfsFilesystem::IsSquashfsImage(super_block); |
| 78 | } |
| 79 | return false; |
| 80 | } |
| 81 | |
Håkan Kvist | e4d414e | 2019-06-28 08:05:06 +0200 | [diff] [blame] | 82 | bool IsRegularFile(const FilesystemInterface::File& file) { |
| 83 | // If inode is 0, then stat information is invalid for some psuedo files |
| 84 | if (file.file_stat.st_ino != 0 && |
| 85 | (file.file_stat.st_mode & S_IFMT) == S_IFREG) { |
| 86 | return true; |
| 87 | } |
| 88 | return false; |
| 89 | } |
| 90 | |
Amin Hassani | 924183b | 2017-09-27 14:50:59 -0700 | [diff] [blame] | 91 | // Realigns subfiles |files| of a splitted file |file| into its correct |
| 92 | // positions. This can be used for squashfs, zip, apk, etc. |
| 93 | bool RealignSplittedFiles(const FilesystemInterface::File& file, |
| 94 | vector<FilesystemInterface::File>* files) { |
| 95 | // We have to shift all the Extents in |files|, based on the Extents of the |
| 96 | // |file| itself. |
| 97 | size_t num_blocks = 0; |
| 98 | for (auto& in_file : *files) { // We need to modify so no constant. |
| 99 | TEST_AND_RETURN_FALSE( |
| 100 | ShiftExtentsOverExtents(file.extents, &in_file.extents)); |
Amin Hassani | 3cd4df1 | 2017-08-25 11:21:53 -0700 | [diff] [blame] | 101 | TEST_AND_RETURN_FALSE( |
| 102 | ShiftBitExtentsOverExtents(file.extents, &in_file.deflates)); |
| 103 | |
Amin Hassani | 924183b | 2017-09-27 14:50:59 -0700 | [diff] [blame] | 104 | in_file.name = file.name + "/" + in_file.name; |
Amin Hassani | d8b67f4 | 2017-12-06 13:47:52 -0800 | [diff] [blame] | 105 | num_blocks += utils::BlocksInExtents(in_file.extents); |
Amin Hassani | 924183b | 2017-09-27 14:50:59 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | // Check that all files in |in_files| cover the entire image. |
Amin Hassani | d8b67f4 | 2017-12-06 13:47:52 -0800 | [diff] [blame] | 109 | TEST_AND_RETURN_FALSE(utils::BlocksInExtents(file.extents) == num_blocks); |
Amin Hassani | 924183b | 2017-09-27 14:50:59 -0700 | [diff] [blame] | 110 | return true; |
| 111 | } |
| 112 | |
Amin Hassani | 3cd4df1 | 2017-08-25 11:21:53 -0700 | [diff] [blame] | 113 | bool IsBitExtentInExtent(const Extent& extent, const BitExtent& bit_extent) { |
| 114 | return (bit_extent.offset / 8) >= (extent.start_block() * kBlockSize) && |
| 115 | ((bit_extent.offset + bit_extent.length + 7) / 8) <= |
| 116 | ((extent.start_block() + extent.num_blocks()) * kBlockSize); |
| 117 | } |
| 118 | |
Sen Jiang | 23bae40 | 2018-11-13 11:27:29 -0800 | [diff] [blame] | 119 | // Returns whether the given file |name| has an extension listed in |
| 120 | // |extensions|. |
Kelvin Zhang | 0af0122 | 2021-10-05 13:36:47 -0700 | [diff] [blame] | 121 | |
| 122 | } // namespace |
| 123 | |
Kelvin Zhang | cb2dc88 | 2021-11-22 09:26:50 -0800 | [diff] [blame] | 124 | constexpr base::StringPiece ToStringPiece(std::string_view s) { |
| 125 | return base::StringPiece(s.data(), s.length()); |
| 126 | } |
| 127 | |
| 128 | bool IsFileExtensions( |
| 129 | const std::string_view name, |
| 130 | const std::initializer_list<std::string_view>& extensions) { |
| 131 | return any_of(extensions.begin(), |
| 132 | extensions.end(), |
| 133 | [name = ToStringPiece(name)](const auto& ext) { |
Kelvin Zhang | 0c18424 | 2024-10-25 11:19:27 -0700 | [diff] [blame^] | 134 | return android::base::EndsWith(ToLower(name), ToLower(ext)); |
Kelvin Zhang | cb2dc88 | 2021-11-22 09:26:50 -0800 | [diff] [blame] | 135 | }); |
Sen Jiang | 23bae40 | 2018-11-13 11:27:29 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Amin Hassani | 3cd4df1 | 2017-08-25 11:21:53 -0700 | [diff] [blame] | 138 | ByteExtent ExpandToByteExtent(const BitExtent& extent) { |
| 139 | uint64_t offset = extent.offset / 8; |
| 140 | uint64_t length = ((extent.offset + extent.length + 7) / 8) - offset; |
| 141 | return {offset, length}; |
| 142 | } |
| 143 | |
Amin Hassani | 924183b | 2017-09-27 14:50:59 -0700 | [diff] [blame] | 144 | bool ShiftExtentsOverExtents(const vector<Extent>& base_extents, |
| 145 | vector<Extent>* over_extents) { |
Amin Hassani | d8b67f4 | 2017-12-06 13:47:52 -0800 | [diff] [blame] | 146 | if (utils::BlocksInExtents(base_extents) < |
| 147 | utils::BlocksInExtents(*over_extents)) { |
Amin Hassani | 924183b | 2017-09-27 14:50:59 -0700 | [diff] [blame] | 148 | LOG(ERROR) << "over_extents have more blocks than base_extents! Invalid!"; |
| 149 | return false; |
| 150 | } |
| 151 | for (size_t idx = 0; idx < over_extents->size(); idx++) { |
| 152 | auto over_ext = &over_extents->at(idx); |
| 153 | auto gap_blocks = base_extents[0].start_block(); |
| 154 | auto last_end_block = base_extents[0].start_block(); |
| 155 | for (auto base_ext : base_extents) { // We need to modify |base_ext|, so we |
| 156 | // use copy. |
| 157 | gap_blocks += base_ext.start_block() - last_end_block; |
| 158 | last_end_block = base_ext.start_block() + base_ext.num_blocks(); |
| 159 | base_ext.set_start_block(base_ext.start_block() - gap_blocks); |
| 160 | if (over_ext->start_block() >= base_ext.start_block() && |
| 161 | over_ext->start_block() < |
| 162 | base_ext.start_block() + base_ext.num_blocks()) { |
| 163 | if (over_ext->start_block() + over_ext->num_blocks() <= |
| 164 | base_ext.start_block() + base_ext.num_blocks()) { |
| 165 | // |over_ext| is inside |base_ext|, increase its start block. |
| 166 | over_ext->set_start_block(over_ext->start_block() + gap_blocks); |
| 167 | } else { |
| 168 | // |over_ext| spills over this |base_ext|, split it into two. |
| 169 | auto new_blocks = base_ext.start_block() + base_ext.num_blocks() - |
| 170 | over_ext->start_block(); |
| 171 | vector<Extent> new_extents = { |
| 172 | ExtentForRange(gap_blocks + over_ext->start_block(), new_blocks), |
| 173 | ExtentForRange(over_ext->start_block() + new_blocks, |
| 174 | over_ext->num_blocks() - new_blocks)}; |
| 175 | *over_ext = new_extents[0]; |
| 176 | over_extents->insert(std::next(over_extents->begin(), idx + 1), |
| 177 | new_extents[1]); |
| 178 | } |
| 179 | break; // We processed |over_ext|, so break the loop; |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | return true; |
| 184 | } |
| 185 | |
Amin Hassani | 3cd4df1 | 2017-08-25 11:21:53 -0700 | [diff] [blame] | 186 | bool ShiftBitExtentsOverExtents(const vector<Extent>& base_extents, |
| 187 | vector<BitExtent>* over_extents) { |
| 188 | if (over_extents->empty()) { |
| 189 | return true; |
| 190 | } |
| 191 | |
| 192 | // This check is needed to make sure the number of bytes in |over_extents| |
| 193 | // does not exceed |base_extents|. |
| 194 | auto last_extent = ExpandToByteExtent(over_extents->back()); |
Kelvin Zhang | cb2dc88 | 2021-11-22 09:26:50 -0800 | [diff] [blame] | 195 | TEST_LE(last_extent.offset + last_extent.length, |
| 196 | utils::BlocksInExtents(base_extents) * kBlockSize); |
Amin Hassani | 3cd4df1 | 2017-08-25 11:21:53 -0700 | [diff] [blame] | 197 | |
| 198 | for (auto o_ext = over_extents->begin(); o_ext != over_extents->end();) { |
| 199 | size_t gap_blocks = base_extents[0].start_block(); |
| 200 | size_t last_end_block = base_extents[0].start_block(); |
| 201 | bool o_ext_processed = false; |
| 202 | for (auto b_ext : base_extents) { // We need to modify |b_ext|, so we copy. |
| 203 | gap_blocks += b_ext.start_block() - last_end_block; |
| 204 | last_end_block = b_ext.start_block() + b_ext.num_blocks(); |
| 205 | b_ext.set_start_block(b_ext.start_block() - gap_blocks); |
| 206 | auto byte_o_ext = ExpandToByteExtent(*o_ext); |
| 207 | if (byte_o_ext.offset >= b_ext.start_block() * kBlockSize && |
| 208 | byte_o_ext.offset < |
| 209 | (b_ext.start_block() + b_ext.num_blocks()) * kBlockSize) { |
| 210 | if ((byte_o_ext.offset + byte_o_ext.length) <= |
| 211 | (b_ext.start_block() + b_ext.num_blocks()) * kBlockSize) { |
| 212 | // |o_ext| is inside |b_ext|, increase its start block. |
| 213 | o_ext->offset += gap_blocks * kBlockSize * 8; |
| 214 | ++o_ext; |
| 215 | } else { |
| 216 | // |o_ext| spills over this |b_ext|, remove it. |
| 217 | o_ext = over_extents->erase(o_ext); |
| 218 | } |
| 219 | o_ext_processed = true; |
| 220 | break; // We processed o_ext, so break the loop; |
| 221 | } |
| 222 | } |
| 223 | TEST_AND_RETURN_FALSE(o_ext_processed); |
| 224 | } |
| 225 | return true; |
| 226 | } |
| 227 | |
| 228 | vector<BitExtent> FindDeflates(const vector<Extent>& extents, |
| 229 | const vector<BitExtent>& in_deflates) { |
| 230 | vector<BitExtent> result; |
| 231 | // TODO(ahassani): Replace this with binary_search style search. |
| 232 | for (const auto& deflate : in_deflates) { |
| 233 | for (const auto& extent : extents) { |
| 234 | if (IsBitExtentInExtent(extent, deflate)) { |
| 235 | result.push_back(deflate); |
| 236 | break; |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | return result; |
| 241 | } |
| 242 | |
| 243 | bool CompactDeflates(const vector<Extent>& extents, |
| 244 | const vector<BitExtent>& in_deflates, |
| 245 | vector<BitExtent>* out_deflates) { |
| 246 | size_t bytes_passed = 0; |
| 247 | out_deflates->reserve(in_deflates.size()); |
| 248 | for (const auto& extent : extents) { |
| 249 | size_t gap_bytes = extent.start_block() * kBlockSize - bytes_passed; |
| 250 | for (const auto& deflate : in_deflates) { |
| 251 | if (IsBitExtentInExtent(extent, deflate)) { |
| 252 | out_deflates->emplace_back(deflate.offset - (gap_bytes * 8), |
| 253 | deflate.length); |
| 254 | } |
| 255 | } |
| 256 | bytes_passed += extent.num_blocks() * kBlockSize; |
| 257 | } |
| 258 | |
| 259 | // All given |in_deflates| items should've been inside one of the extents in |
| 260 | // |extents|. |
Kelvin Zhang | 7949fce | 2024-03-12 10:17:40 -0700 | [diff] [blame] | 261 | TEST_EQ(in_deflates.size(), out_deflates->size()); |
| 262 | Dedup(out_deflates); |
Amin Hassani | 3cd4df1 | 2017-08-25 11:21:53 -0700 | [diff] [blame] | 263 | |
| 264 | // Make sure all outgoing deflates are ordered and non-overlapping. |
| 265 | auto result = std::adjacent_find(out_deflates->begin(), |
| 266 | out_deflates->end(), |
| 267 | [](const BitExtent& a, const BitExtent& b) { |
| 268 | return (a.offset + a.length) > b.offset; |
| 269 | }); |
Kelvin Zhang | 7949fce | 2024-03-12 10:17:40 -0700 | [diff] [blame] | 270 | if (result != out_deflates->end()) { |
| 271 | LOG(ERROR) << "out_deflate is overlapped " << (*result) << ", " |
| 272 | << *(++result); |
| 273 | return false; |
| 274 | } |
Amin Hassani | 3cd4df1 | 2017-08-25 11:21:53 -0700 | [diff] [blame] | 275 | return true; |
| 276 | } |
| 277 | |
| 278 | bool FindAndCompactDeflates(const vector<Extent>& extents, |
| 279 | const vector<BitExtent>& in_deflates, |
| 280 | vector<BitExtent>* out_deflates) { |
| 281 | auto found_deflates = FindDeflates(extents, in_deflates); |
| 282 | TEST_AND_RETURN_FALSE(CompactDeflates(extents, found_deflates, out_deflates)); |
| 283 | return true; |
| 284 | } |
| 285 | |
Kelvin Zhang | cb2dc88 | 2021-11-22 09:26:50 -0800 | [diff] [blame] | 286 | bool DeflatePreprocessFileData(const std::string_view filename, |
| 287 | const brillo::Blob& data, |
| 288 | vector<puffin::BitExtent>* deflates) { |
| 289 | bool is_zip = IsFileExtensions( |
| 290 | filename, {".apk", ".zip", ".jar", ".zvoice", ".apex", "capex"}); |
| 291 | bool is_gzip = IsFileExtensions(filename, {".gz", ".gzip", ".tgz"}); |
| 292 | if (is_zip) { |
| 293 | if (!puffin::LocateDeflatesInZipArchive(data, deflates)) { |
| 294 | LOG(ERROR) << "Failed to locate deflates in zip file " << filename; |
| 295 | deflates->clear(); |
| 296 | return false; |
| 297 | } |
| 298 | } else if (is_gzip) { |
| 299 | if (!puffin::LocateDeflatesInGzip(data, deflates)) { |
| 300 | LOG(ERROR) << "Failed to locate deflates in gzip file " << filename; |
| 301 | deflates->clear(); |
| 302 | return false; |
| 303 | } |
| 304 | } |
| 305 | return true; |
| 306 | } |
| 307 | |
Sen Jiang | ce39e67 | 2018-11-28 16:43:00 -0800 | [diff] [blame] | 308 | bool PreprocessPartitionFiles(const PartitionConfig& part, |
| 309 | vector<FilesystemInterface::File>* result_files, |
| 310 | bool extract_deflates) { |
Amin Hassani | 924183b | 2017-09-27 14:50:59 -0700 | [diff] [blame] | 311 | // Get the file system files. |
| 312 | vector<FilesystemInterface::File> tmp_files; |
| 313 | part.fs_interface->GetFiles(&tmp_files); |
| 314 | result_files->reserve(tmp_files.size()); |
| 315 | |
Tianjie Xu | 1a7bb2c | 2018-01-22 17:56:57 -0800 | [diff] [blame] | 316 | for (auto& file : tmp_files) { |
Håkan Kvist | e4d414e | 2019-06-28 08:05:06 +0200 | [diff] [blame] | 317 | auto is_regular_file = IsRegularFile(file); |
| 318 | |
| 319 | if (is_regular_file && IsSquashfsImage(part.path, file)) { |
Amin Hassani | 924183b | 2017-09-27 14:50:59 -0700 | [diff] [blame] | 320 | // Read the image into a file. |
| 321 | base::FilePath path; |
| 322 | TEST_AND_RETURN_FALSE(base::CreateTemporaryFile(&path)); |
| 323 | ScopedPathUnlinker old_unlinker(path.value()); |
| 324 | TEST_AND_RETURN_FALSE( |
| 325 | CopyExtentsToFile(part.path, file.extents, path.value(), kBlockSize)); |
| 326 | // Test if it is actually a Squashfs file. |
Kelvin Zhang | ed9b208 | 2023-05-25 13:58:19 -0700 | [diff] [blame] | 327 | auto sqfs = |
| 328 | SquashfsFilesystem::CreateFromFile(path.value(), extract_deflates); |
Amin Hassani | 924183b | 2017-09-27 14:50:59 -0700 | [diff] [blame] | 329 | if (sqfs) { |
| 330 | // It is an squashfs file. Get its files to replace with itself. |
| 331 | vector<FilesystemInterface::File> files; |
| 332 | sqfs->GetFiles(&files); |
| 333 | |
Amin Hassani | 3cd4df1 | 2017-08-25 11:21:53 -0700 | [diff] [blame] | 334 | // Replace squashfs file with its files only if |files| has at least two |
| 335 | // files or if it has some deflates (since it is better to replace it to |
| 336 | // take advantage of the deflates.) |
| 337 | if (files.size() > 1 || |
| 338 | (files.size() == 1 && !files[0].deflates.empty())) { |
Amin Hassani | 924183b | 2017-09-27 14:50:59 -0700 | [diff] [blame] | 339 | TEST_AND_RETURN_FALSE(RealignSplittedFiles(file, &files)); |
| 340 | result_files->insert(result_files->end(), files.begin(), files.end()); |
| 341 | continue; |
| 342 | } |
| 343 | } else { |
| 344 | LOG(WARNING) << "We thought file: " << file.name |
| 345 | << " was a Squashfs file, but it was not."; |
| 346 | } |
| 347 | } |
Tianjie Xu | 1a7bb2c | 2018-01-22 17:56:57 -0800 | [diff] [blame] | 348 | |
Tianjie | 55abd3c | 2020-06-19 00:22:59 -0700 | [diff] [blame] | 349 | if (is_regular_file && extract_deflates && !file.is_compressed) { |
Sen Jiang | 23bae40 | 2018-11-13 11:27:29 -0800 | [diff] [blame] | 350 | // Search for deflates if the file is in zip or gzip format. |
| 351 | // .zvoice files may eventually move out of rootfs. If that happens, |
| 352 | // remove ".zvoice" (crbug.com/782918). |
Sen Jiang | 812e9d1 | 2018-11-21 16:40:37 -0800 | [diff] [blame] | 353 | bool is_zip = IsFileExtensions( |
Kelvin Zhang | dc1f258 | 2021-09-13 16:43:41 -0700 | [diff] [blame] | 354 | file.name, {".apk", ".zip", ".jar", ".zvoice", ".apex", "capex"}); |
Sen Jiang | 23bae40 | 2018-11-13 11:27:29 -0800 | [diff] [blame] | 355 | bool is_gzip = IsFileExtensions(file.name, {".gz", ".gzip", ".tgz"}); |
| 356 | if (is_zip || is_gzip) { |
| 357 | brillo::Blob data; |
| 358 | TEST_AND_RETURN_FALSE(utils::ReadExtents( |
| 359 | part.path, |
| 360 | file.extents, |
| 361 | &data, |
| 362 | kBlockSize * utils::BlocksInExtents(file.extents), |
| 363 | kBlockSize)); |
Kelvin Zhang | 6fbf594 | 2021-12-09 14:06:25 -0800 | [diff] [blame] | 364 | // |data| read from disk always has size multiple of kBlockSize. So it |
| 365 | // might contain trailing garbage data and confuse the gzip/zip |
| 366 | // processors. Trim them. |
| 367 | if (file.file_stat.st_size > 0 && |
| 368 | static_cast<size_t>(file.file_stat.st_size) < data.size()) { |
| 369 | data.resize(file.file_stat.st_size); |
| 370 | } |
Sen Jiang | 23bae40 | 2018-11-13 11:27:29 -0800 | [diff] [blame] | 371 | vector<puffin::BitExtent> deflates; |
Kelvin Zhang | cb2dc88 | 2021-11-22 09:26:50 -0800 | [diff] [blame] | 372 | if (!DeflatePreprocessFileData(file.name, data, &deflates)) { |
| 373 | LOG(ERROR) << "Failed to preprocess deflate data in partition " |
| 374 | << part.name; |
| 375 | return false; |
Sen Jiang | 23bae40 | 2018-11-13 11:27:29 -0800 | [diff] [blame] | 376 | } |
| 377 | // Shift the deflate's extent to the offset starting from the beginning |
| 378 | // of the current partition; and the delta processor will align the |
| 379 | // extents in a continuous buffer later. |
| 380 | TEST_AND_RETURN_FALSE( |
| 381 | ShiftBitExtentsOverExtents(file.extents, &deflates)); |
| 382 | file.deflates = std::move(deflates); |
| 383 | } |
Tianjie Xu | 1a7bb2c | 2018-01-22 17:56:57 -0800 | [diff] [blame] | 384 | } |
| 385 | |
Amin Hassani | 924183b | 2017-09-27 14:50:59 -0700 | [diff] [blame] | 386 | result_files->push_back(file); |
| 387 | } |
Amin Hassani | 924183b | 2017-09-27 14:50:59 -0700 | [diff] [blame] | 388 | return true; |
| 389 | } |
| 390 | |
| 391 | } // namespace deflate_utils |
| 392 | } // namespace chromeos_update_engine |