summaryrefslogtreecommitdiff
path: root/host/commands/fetcher/install_zip.cc
blob: bd6294f20a3103c015a2752671b5a06a10712314 (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
//
// 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 "install_zip.h"

#include <stdlib.h>

#include <string>
#include <vector>

#include <glog/logging.h>

#include "common/libs/utils/archive.h"
#include "common/libs/utils/subprocess.h"

std::vector<std::string> ExtractImages(const std::string& archive_file,
                                       const std::string& target_directory,
                                       const std::vector<std::string>& images) {
  cvd::Archive archive(archive_file);
  bool extracted =
      images.size() > 0
          ? archive.ExtractFiles(images, target_directory)
          : archive.ExtractAll(target_directory);
  if (!extracted) {
    LOG(ERROR) << "Unable to extract images.";
    return {};
  }

  bool extraction_success = true;
  std::vector<std::string> files =
      images.size() > 0 ? images : archive.Contents();
  for (const auto& file : files) {
    if (file.find(".img") == std::string::npos) {
      continue;
    }
    std::string extracted_file = target_directory + "/" + file;

    std::string file_input, file_output;
    cvd::Command file_cmd("/usr/bin/file");
    file_cmd.AddParameter(extracted_file);
    auto file_ret = cvd::RunWithManagedStdio(std::move(file_cmd), &file_input,
                                             &file_output, nullptr);
    if (file_ret != 0) {
      LOG(ERROR) << "Unable to run file on " << file << ", returned" << file_ret;
      extraction_success = false;
      continue;
    }
    if (file_output.find("Android sparse image,") == std::string::npos) {
      continue;
    }
    std::string inflated_file = extracted_file + ".inflated";
    cvd::Command simg_cmd("/usr/bin/simg2img");
    simg_cmd.AddParameter(extracted_file);
    simg_cmd.AddParameter(inflated_file);
    simg_cmd.RedirectStdIO(cvd::Subprocess::StdIOChannel::kStdOut,
                           cvd::Subprocess::StdIOChannel::kStdErr);
    if (simg_cmd.Start().Wait() != 0) {
      LOG(ERROR) << "Unable to run simg2img on " << file;
      extraction_success = false;
      continue;
    }
    auto rename_ret = rename(inflated_file.c_str(), extracted_file.c_str());
    if (rename_ret != 0) {
      LOG(ERROR) << "Unable to rename deflated version of " << file;
      extraction_success = false;
    }
  }
  for (auto& file : files) {
    file = target_directory + "/" + file;
  }
  return extraction_success ? files : std::vector<std::string>{};
}