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