Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 75c4b32e authored by Tianjie Xu's avatar Tianjie Xu Committed by android-build-merger
Browse files

Merge "Implement the TargetFile and BuildInfo"

am: 9b76970e

Change-Id: I2d188eae7305bb82c7407bcbb04282b65768dde1
parents a927a168 9b76970e
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -128,6 +128,7 @@ cc_library_host_static {
    ],

    srcs: [
        "build_info.cpp",
        "simulator_runtime.cpp",
        "target_files.cpp",
    ],
@@ -135,6 +136,7 @@ cc_library_host_static {
    static_libs: [
        "libupdater_core",
        "libfstab",
        "libc++fs",
    ],

    target: {
+2 −1
Original line number Diff line number Diff line
@@ -136,7 +136,8 @@ LOCAL_STATIC_LIBRARIES := \
    $(TARGET_RECOVERY_UPDATER_HOST_LIBS) \
    $(TARGET_RECOVERY_UPDATER_HOST_EXTRA_LIBS) \
    $(updater_common_static_libraries) \
    libfstab
    libfstab \
    libc++fs

LOCAL_MODULE_CLASS := EXECUTABLES
inc := $(call local-generated-sources-dir)/register.inc

updater/build_info.cpp

0 → 100644
+126 −0
Original line number Diff line number Diff line
/*
 * 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.
 */

#include "updater/build_info.h"

#include <set>
#include <vector>

#include <android-base/logging.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>

#include "updater/target_files.h"

bool BuildInfo::ParseTargetFile(const std::string_view target_file_path, bool extracted_input) {
  TargetFile target_file(std::string(target_file_path), extracted_input);
  if (!target_file.Open()) {
    return false;
  }

  if (!target_file.GetBuildProps(&build_props_)) {
    return false;
  }

  std::vector<FstabInfo> fstab_info_list;
  if (!target_file.ParseFstabInfo(&fstab_info_list)) {
    return false;
  }

  for (const auto& fstab_info : fstab_info_list) {
    for (const auto& directory : { "IMAGES", "RADIO" }) {
      std::string entry_name = directory + fstab_info.mount_point + ".img";
      if (!target_file.EntryExists(entry_name)) {
        LOG(WARNING) << "Failed to find the image entry in the target file: " << entry_name;
        continue;
      }

      temp_files_.emplace_back(work_dir_);
      auto& image_file = temp_files_.back();
      if (!target_file.ExtractImage(entry_name, fstab_info, work_dir_, &image_file)) {
        LOG(ERROR) << "Failed to set up source image files.";
        return false;
      }

      LOG(INFO) << "Mounted " << fstab_info.mount_point << "\nMapping: " << fstab_info.blockdev_name
                << " to " << image_file.path;

      blockdev_map_.emplace(
          fstab_info.blockdev_name,
          FakeBlockDevice(fstab_info.blockdev_name, fstab_info.mount_point, image_file.path));
      break;
    }
  }

  return true;
}

std::string BuildInfo::GetProperty(const std::string_view key,
                                   const std::string_view default_value) const {
  // The logic to parse the ro.product properties should be in line with the generation script.
  // More details in common.py BuildInfo.GetBuildProp.
  // TODO(xunchang) handle the oem property and the source order defined in
  // ro.product.property_source_order
  const std::set<std::string, std::less<>> ro_product_props = {
    "ro.product.brand", "ro.product.device", "ro.product.manufacturer", "ro.product.model",
    "ro.product.name"
  };
  const std::vector<std::string> source_order = {
    "product", "product_services", "odm", "vendor", "system",
  };
  if (ro_product_props.find(key) != ro_product_props.end()) {
    std::string_view key_suffix(key);
    CHECK(android::base::ConsumePrefix(&key_suffix, "ro.product"));
    for (const auto& source : source_order) {
      std::string resolved_key = "ro.product." + source + std::string(key_suffix);
      if (auto entry = build_props_.find(resolved_key); entry != build_props_.end()) {
        return entry->second;
      }
    }
    LOG(WARNING) << "Failed to find property: " << key;
    return std::string(default_value);
  } else if (key == "ro.build.fingerprint") {
    // clang-format off
    return android::base::StringPrintf("%s/%s/%s:%s/%s/%s:%s/%s",
        GetProperty("ro.product.brand", "").c_str(),
        GetProperty("ro.product.name", "").c_str(),
        GetProperty("ro.product.device", "").c_str(),
        GetProperty("ro.build.version.release", "").c_str(),
        GetProperty("ro.build.id", "").c_str(),
        GetProperty("ro.build.version.incremental", "").c_str(),
        GetProperty("ro.build.type", "").c_str(),
        GetProperty("ro.build.tags", "").c_str());
    // clang-format on
  }

  auto entry = build_props_.find(key);
  if (entry == build_props_.end()) {
    LOG(WARNING) << "Failed to find property: " << key;
    return std::string(default_value);
  }

  return entry->second;
}

std::string BuildInfo::FindBlockDeviceName(const std::string_view name) const {
  auto entry = blockdev_map_.find(name);
  if (entry == blockdev_map_.end()) {
    LOG(WARNING) << "Failed to find path to block device " << name;
    return "";
  }

  return entry->second.mounted_file_path;
}
+63 −0
Original line number Diff line number Diff line
/*
 * 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 <list>
#include <map>
#include <string>
#include <string_view>

#include <android-base/file.h>

// This class serves as the aggregation of the fake block device information during update
// simulation on host. In specific, it has the name of the block device, its mount point, and the
// path to the temporary file that fakes this block device.
class FakeBlockDevice {
 public:
  FakeBlockDevice(std::string block_device, std::string mount_point, std::string temp_file_path)
      : blockdev_name(std::move(block_device)),
        mount_point(std::move(mount_point)),
        mounted_file_path(std::move(temp_file_path)) {}

  std::string blockdev_name;
  std::string mount_point;
  std::string mounted_file_path;  // path to the temp file that mocks the block device
};

// This class stores the information of the source build. For example, it creates and maintains
// the temporary files to simulate the block devices on host. Therefore, the simulator runtime can
// query the information and run the update on host.
class BuildInfo {
 public:
  explicit BuildInfo(const std::string_view work_dir) : work_dir_(work_dir) {}
  // Returns the value of the build properties.
  std::string GetProperty(const std::string_view key, const std::string_view default_value) const;
  // Returns the path to the mock block device.
  std::string FindBlockDeviceName(const std::string_view name) const;
  // Parses the given target-file, initializes the build properties and extracts the images.
  bool ParseTargetFile(const std::string_view target_file_path, bool extracted_input);

 private:
  // A map to store the system properties during simulation.
  std::map<std::string, std::string, std::less<>> build_props_;
  // A map from the blockdev_name to the FakeBlockDevice object, which contains the path to the
  // temporary file.
  std::map<std::string, FakeBlockDevice, std::less<>> blockdev_map_;

  std::list<TemporaryFile> temp_files_;
  std::string work_dir_;  // A temporary directory to store the extracted image files
};
+3 −3
Original line number Diff line number Diff line
@@ -24,11 +24,11 @@
#include <vector>

#include "edify/updater_runtime_interface.h"
#include "updater/target_files.h"
#include "updater/build_info.h"

class SimulatorRuntime : public UpdaterRuntimeInterface {
 public:
  explicit SimulatorRuntime(TargetFiles* source) : source_(source) {}
  explicit SimulatorRuntime(BuildInfo* source) : source_(source) {}

  bool IsSimulator() const override {
    return true;
@@ -53,6 +53,6 @@ class SimulatorRuntime : public UpdaterRuntimeInterface {
 private:
  std::string FindBlockDeviceName(const std::string_view name) const override;

  TargetFiles* source_;
  BuildInfo* source_;
  std::map<std::string, std::string, std::less<>> mounted_partitions_;
};
Loading