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