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 <unistd.h>
20 
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 #include <android-base/logging.h>
26 #include <android-base/strings.h>
27 
28 #include "common/libs/utils/subprocess.h"
29 
30 namespace cuttlefish {
31 namespace {
32 
ExtractHelper(std::vector<std::string> & files,const std::string & archive_filepath,const std::string & target_directory,const bool keep_archive)33 Result<std::vector<std::string>> ExtractHelper(
34     std::vector<std::string>& files, const std::string& archive_filepath,
35     const std::string& target_directory, const bool keep_archive) {
36   CF_EXPECT(!files.empty(), "No files extracted from " << archive_filepath);
37 
38   auto it = files.begin();
39   while (it != files.end()) {
40     if (*it == "" || android::base::EndsWith(*it, "/")) {
41       it = files.erase(it);
42     } else {
43       *it = target_directory + "/" + *it;
44       it++;
45     }
46   }
47 
48   if (!keep_archive && unlink(archive_filepath.data()) != 0) {
49     LOG(ERROR) << "Could not delete " << archive_filepath;
50     files.push_back(archive_filepath);
51   }
52 
53   return {files};
54 }
55 
56 }  // namespace
57 
Archive(const std::string & file)58 Archive::Archive(const std::string& file) : file_(file) {}
59 
~Archive()60 Archive::~Archive() {}
61 
Contents()62 std::vector<std::string> Archive::Contents() {
63   Command bsdtar_cmd("/usr/bin/bsdtar");
64   bsdtar_cmd.AddParameter("-tf");
65   bsdtar_cmd.AddParameter(file_);
66   std::string bsdtar_input, bsdtar_output;
67   auto bsdtar_ret = RunWithManagedStdio(std::move(bsdtar_cmd), &bsdtar_input,
68                                              &bsdtar_output, nullptr);
69   if (bsdtar_ret != 0) {
70     LOG(ERROR) << "`bsdtar -tf \"" << file_ << "\"` returned " << bsdtar_ret;
71   }
72   return bsdtar_ret == 0
73       ? android::base::Split(bsdtar_output, "\n")
74       : std::vector<std::string>();
75 }
76 
ExtractAll(const std::string & target_directory)77 bool Archive::ExtractAll(const std::string& target_directory) {
78   return ExtractFiles({}, target_directory);
79 }
80 
ExtractFiles(const std::vector<std::string> & to_extract,const std::string & target_directory)81 bool Archive::ExtractFiles(const std::vector<std::string>& to_extract,
82                            const std::string& target_directory) {
83   Command bsdtar_cmd("/usr/bin/bsdtar");
84   bsdtar_cmd.AddParameter("-x");
85   bsdtar_cmd.AddParameter("-v");
86   bsdtar_cmd.AddParameter("-C");
87   bsdtar_cmd.AddParameter(target_directory);
88   bsdtar_cmd.AddParameter("-f");
89   bsdtar_cmd.AddParameter(file_);
90   bsdtar_cmd.AddParameter("-S");
91   for (const auto& extract : to_extract) {
92     bsdtar_cmd.AddParameter(extract);
93   }
94   bsdtar_cmd.RedirectStdIO(Subprocess::StdIOChannel::kStdOut,
95                            Subprocess::StdIOChannel::kStdErr);
96   auto bsdtar_ret = bsdtar_cmd.Start().Wait();
97   if (bsdtar_ret != 0) {
98     LOG(ERROR) << "bsdtar extraction on \"" << file_ << "\" returned "
99                << bsdtar_ret;
100   }
101   return bsdtar_ret == 0;
102 }
103 
ExtractToMemory(const std::string & path)104 std::string Archive::ExtractToMemory(const std::string& path) {
105   Command bsdtar_cmd("/usr/bin/bsdtar");
106   bsdtar_cmd.AddParameter("-xf");
107   bsdtar_cmd.AddParameter(file_);
108   bsdtar_cmd.AddParameter("-O");
109   bsdtar_cmd.AddParameter(path);
110   std::string stdout_str;
111   auto ret =
112       RunWithManagedStdio(std::move(bsdtar_cmd), nullptr, &stdout_str, nullptr);
113   if (ret != 0) {
114     LOG(ERROR) << "Could not extract \"" << path << "\" from \"" << file_
115                << "\" to memory.";
116     return "";
117   }
118   return stdout_str;
119 }
120 
ExtractImages(const std::string & archive_filepath,const std::string & target_directory,const std::vector<std::string> & images,const bool keep_archive)121 Result<std::vector<std::string>> ExtractImages(
122     const std::string& archive_filepath, const std::string& target_directory,
123     const std::vector<std::string>& images, const bool keep_archive) {
124   Archive archive(archive_filepath);
125   CF_EXPECT(archive.ExtractFiles(images, target_directory),
126             "Could not extract images from \"" << archive_filepath << "\" to \""
127                                                << target_directory << "\"");
128 
129   std::vector<std::string> files = images;
130   return ExtractHelper(files, archive_filepath, target_directory, keep_archive);
131 }
132 
ExtractImage(const std::string & archive_filepath,const std::string & target_directory,const std::string & image,const bool keep_archive)133 Result<std::string> ExtractImage(const std::string& archive_filepath,
134                                  const std::string& target_directory,
135                                  const std::string& image,
136                                  const bool keep_archive) {
137   std::vector<std::string> result = CF_EXPECT(
138       ExtractImages(archive_filepath, target_directory, {image}, keep_archive));
139   return {result.front()};
140 }
141 
ExtractArchiveContents(const std::string & archive_filepath,const std::string & target_directory,const bool keep_archive)142 Result<std::vector<std::string>> ExtractArchiveContents(
143     const std::string& archive_filepath, const std::string& target_directory,
144     const bool keep_archive) {
145   Archive archive(archive_filepath);
146   CF_EXPECT(archive.ExtractAll(target_directory),
147             "Could not extract \"" << archive_filepath << "\" to \""
148                                    << target_directory << "\"");
149 
150   std::vector<std::string> files = archive.Contents();
151   return ExtractHelper(files, archive_filepath, target_directory, keep_archive);
152 }
153 
154 } // namespace cuttlefish
155