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 #include "common/libs/utils/archive.h"
18
19 #include <ostream>
20 #include <string>
21 #include <utility>
22 #include <vector>
23
24 #include <android-base/strings.h>
25 #include <android-base/logging.h>
26
27 #include "common/libs/utils/subprocess.h"
28
29 namespace cuttlefish {
30
Archive(const std::string & file)31 Archive::Archive(const std::string& file) : file(file) {
32 }
33
~Archive()34 Archive::~Archive() {
35 }
36
Contents()37 std::vector<std::string> Archive::Contents() {
38 Command bsdtar_cmd("/usr/bin/bsdtar");
39 bsdtar_cmd.AddParameter("-tf");
40 bsdtar_cmd.AddParameter(file);
41 std::string bsdtar_input, bsdtar_output;
42 auto bsdtar_ret = RunWithManagedStdio(std::move(bsdtar_cmd), &bsdtar_input,
43 &bsdtar_output, nullptr);
44 if (bsdtar_ret != 0) {
45 LOG(ERROR) << "`bsdtar -tf \"" << file << "\"` returned " << bsdtar_ret;
46 }
47 return bsdtar_ret == 0
48 ? android::base::Split(bsdtar_output, "\n")
49 : std::vector<std::string>();
50 }
51
ExtractAll(const std::string & target_directory)52 bool Archive::ExtractAll(const std::string& target_directory) {
53 return ExtractFiles({}, target_directory);
54 }
55
ExtractFiles(const std::vector<std::string> & to_extract,const std::string & target_directory)56 bool Archive::ExtractFiles(const std::vector<std::string>& to_extract,
57 const std::string& target_directory) {
58 Command bsdtar_cmd("/usr/bin/bsdtar");
59 bsdtar_cmd.AddParameter("-x");
60 bsdtar_cmd.AddParameter("-v");
61 bsdtar_cmd.AddParameter("-C");
62 bsdtar_cmd.AddParameter(target_directory);
63 bsdtar_cmd.AddParameter("-f");
64 bsdtar_cmd.AddParameter(file);
65 bsdtar_cmd.AddParameter("-S");
66 for (const auto& extract : to_extract) {
67 bsdtar_cmd.AddParameter(extract);
68 }
69 bsdtar_cmd.RedirectStdIO(Subprocess::StdIOChannel::kStdOut,
70 Subprocess::StdIOChannel::kStdErr);
71 auto bsdtar_ret = bsdtar_cmd.Start().Wait();
72 if (bsdtar_ret != 0) {
73 LOG(ERROR) << "bsdtar extraction on \"" << file << "\" returned " << bsdtar_ret;
74 }
75 return bsdtar_ret == 0;
76 }
77
ExtractToMemory(const std::string & path)78 std::string Archive::ExtractToMemory(const std::string& path) {
79 Command bsdtar_cmd("/usr/bin/bsdtar");
80 bsdtar_cmd.AddParameter("-xf");
81 bsdtar_cmd.AddParameter(file);
82 bsdtar_cmd.AddParameter("-O");
83 bsdtar_cmd.AddParameter(path);
84 std::string stdout_str;
85 auto ret =
86 RunWithManagedStdio(std::move(bsdtar_cmd), nullptr, &stdout_str, nullptr);
87 if (ret != 0) {
88 LOG(ERROR) << "Could not extract \"" << path << "\" from \"" << file
89 << "\" to memory.";
90 return "";
91 }
92 return stdout_str;
93 }
94
95 } // namespace cuttlefish
96