1 /* 2 * Copyright (C) 2019 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 #pragma once 18 19 #include <map> 20 #include <string> 21 #include <string_view> 22 #include <vector> 23 24 #include <android-base/file.h> 25 #include <ziparchive/zip_archive.h> 26 27 // This class represents the mount information for each line in a fstab file. 28 class FstabInfo { 29 public: FstabInfo(std::string blockdev_name,std::string mount_point,std::string fs_type)30 FstabInfo(std::string blockdev_name, std::string mount_point, std::string fs_type) 31 : blockdev_name(std::move(blockdev_name)), 32 mount_point(std::move(mount_point)), 33 fs_type(std::move(fs_type)) {} 34 35 std::string blockdev_name; 36 std::string mount_point; 37 std::string fs_type; 38 }; 39 40 // This class parses a target file from a zip file or an extracted directory. It also provides the 41 // function to read the its content for simulation. 42 class TargetFile { 43 public: TargetFile(std::string path,bool extracted_input)44 TargetFile(std::string path, bool extracted_input) 45 : path_(std::move(path)), extracted_input_(extracted_input) {} 46 47 // Opens the input target file (or extracted directory) and parses the misc_info.txt. 48 bool Open(); 49 // Parses the build properties in all possible locations and save them in |props_map| 50 bool GetBuildProps(std::map<std::string, std::string, std::less<>>* props_map) const; 51 // Parses the fstab and save the information about each partition to mount into |fstab_info_list|. 52 bool ParseFstabInfo(std::vector<FstabInfo>* fstab_info_list) const; 53 // Returns true if the given entry exists in the target file. 54 bool EntryExists(const std::string_view name) const; 55 // Extracts the image file |entry_name|. Returns true on success. 56 bool ExtractImage(const std::string_view entry_name, const FstabInfo& fstab_info, 57 const std::string_view work_dir, TemporaryFile* image_file) const; 58 59 private: 60 // Wrapper functions to read the entry from either the zipped target-file, or the extracted input 61 // directory. 62 bool ReadEntryToString(const std::string_view name, std::string* content) const; 63 bool ExtractEntryToTempFile(const std::string_view name, TemporaryFile* temp_file) const; 64 65 std::string path_; // Path to the zipped target-file or an extracted directory. 66 bool extracted_input_; // True if the target-file has been extracted. 67 ZipArchiveHandle handle_{ nullptr }; 68 69 // The properties under META/misc_info.txt 70 std::map<std::string, std::string, std::less<>> misc_info_; 71 }; 72