summaryrefslogtreecommitdiffstats
path: root/updater/include/updater/target_files.h
blob: 860d47a3596c47ea641ffc1001fa27bc9fba84e5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <map>
#include <string>
#include <string_view>
#include <vector>

#include <android-base/file.h>
#include <ziparchive/zip_archive.h>

// This class represents the mount information for each line in a fstab file.
class FstabInfo {
 public:
  FstabInfo(std::string blockdev_name, std::string mount_point, std::string fs_type)
      : blockdev_name(std::move(blockdev_name)),
        mount_point(std::move(mount_point)),
        fs_type(std::move(fs_type)) {}

  std::string blockdev_name;
  std::string mount_point;
  std::string fs_type;
};

// This class parses a target file from a zip file or an extracted directory. It also provides the
// function to read the its content for simulation.
class TargetFile {
 public:
  TargetFile(std::string path, bool extracted_input)
      : path_(std::move(path)), extracted_input_(extracted_input) {}

  // Opens the input target file (or extracted directory) and parses the misc_info.txt.
  bool Open();
  // Parses the build properties in all possible locations and save them in |props_map|
  bool GetBuildProps(std::map<std::string, std::string, std::less<>>* props_map) const;
  // Parses the fstab and save the information about each partition to mount into |fstab_info_list|.
  bool ParseFstabInfo(std::vector<FstabInfo>* fstab_info_list) const;
  // Returns true if the given entry exists in the target file.
  bool EntryExists(const std::string_view name) const;
  // Extracts the image file |entry_name|. Returns true on success.
  bool ExtractImage(const std::string_view entry_name, const FstabInfo& fstab_info,
                    const std::string_view work_dir, TemporaryFile* image_file) const;

 private:
  // Wrapper functions to read the entry from either the zipped target-file, or the extracted input
  // directory.
  bool ReadEntryToString(const std::string_view name, std::string* content) const;
  bool ExtractEntryToTempFile(const std::string_view name, TemporaryFile* temp_file) const;

  std::string path_;      // Path to the zipped target-file or an extracted directory.
  bool extracted_input_;  // True if the target-file has been extracted.
  ZipArchiveHandle handle_{ nullptr };

  // The properties under META/misc_info.txt
  std::map<std::string, std::string, std::less<>> misc_info_;
};