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   std::string bsdtar_output;
97   int bsdtar_ret = RunWithManagedStdio(std::move(bsdtar_cmd), nullptr, nullptr,
98                                        &bsdtar_output);
99   LOG(DEBUG) << bsdtar_output;
100   if (bsdtar_ret != 0) {
101     LOG(ERROR) << "bsdtar extraction on \"" << file_ << "\" returned "
102                << bsdtar_ret;
103   }
104   return bsdtar_ret == 0;
105 }
106 
ExtractToMemory(const std::string & path)107 std::string Archive::ExtractToMemory(const std::string& path) {
108   Command bsdtar_cmd("/usr/bin/bsdtar");
109   bsdtar_cmd.AddParameter("-xf");
110   bsdtar_cmd.AddParameter(file_);
111   bsdtar_cmd.AddParameter("-O");
112   bsdtar_cmd.AddParameter(path);
113   std::string stdout_str;
114   auto ret =
115       RunWithManagedStdio(std::move(bsdtar_cmd), nullptr, &stdout_str, nullptr);
116   if (ret != 0) {
117     LOG(ERROR) << "Could not extract \"" << path << "\" from \"" << file_
118                << "\" to memory.";
119     return "";
120   }
121   return stdout_str;
122 }
123 
ExtractImages(const std::string & archive_filepath,const std::string & target_directory,const std::vector<std::string> & images,const bool keep_archive)124 Result<std::vector<std::string>> ExtractImages(
125     const std::string& archive_filepath, const std::string& target_directory,
126     const std::vector<std::string>& images, const bool keep_archive) {
127   Archive archive(archive_filepath);
128   CF_EXPECT(archive.ExtractFiles(images, target_directory),
129             "Could not extract images from \"" << archive_filepath << "\" to \""
130                                                << target_directory << "\"");
131 
132   std::vector<std::string> files = images;
133   return ExtractHelper(files, archive_filepath, target_directory, keep_archive);
134 }
135 
ExtractImage(const std::string & archive_filepath,const std::string & target_directory,const std::string & image,const bool keep_archive)136 Result<std::string> ExtractImage(const std::string& archive_filepath,
137                                  const std::string& target_directory,
138                                  const std::string& image,
139                                  const bool keep_archive) {
140   std::vector<std::string> result = CF_EXPECT(
141       ExtractImages(archive_filepath, target_directory, {image}, keep_archive));
142   return {result.front()};
143 }
144 
ExtractArchiveContents(const std::string & archive_filepath,const std::string & target_directory,const bool keep_archive)145 Result<std::vector<std::string>> ExtractArchiveContents(
146     const std::string& archive_filepath, const std::string& target_directory,
147     const bool keep_archive) {
148   Archive archive(archive_filepath);
149   CF_EXPECT(archive.ExtractAll(target_directory),
150             "Could not extract \"" << archive_filepath << "\" to \""
151                                    << target_directory << "\"");
152 
153   std::vector<std::string> files = archive.Contents();
154   return ExtractHelper(files, archive_filepath, target_directory, keep_archive);
155 }
156 
157 } // namespace cuttlefish
158