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 #include "install_zip.h"
17 
18 #include <stdlib.h>
19 
20 #include <string>
21 #include <vector>
22 
23 #include <android-base/strings.h>
24 #include <android-base/logging.h>
25 
26 #include "common/libs/utils/archive.h"
27 #include "common/libs/utils/subprocess.h"
28 
ExtractImages(const std::string & archive_file,const std::string & target_directory,const std::vector<std::string> & images)29 std::vector<std::string> ExtractImages(const std::string& archive_file,
30                                        const std::string& target_directory,
31                                        const std::vector<std::string>& images) {
32   cuttlefish::Archive archive(archive_file);
33   bool extracted =
34       images.size() > 0
35           ? archive.ExtractFiles(images, target_directory)
36           : archive.ExtractAll(target_directory);
37   if (!extracted) {
38     LOG(ERROR) << "Unable to extract images.";
39     return {};
40   }
41 
42   std::vector<std::string> files =
43       images.size() > 0 ? std::move(images) : archive.Contents();
44   auto it = files.begin();
45   while (it != files.end()) {
46     if (*it == "" || android::base::EndsWith(*it, "/")) {
47       it = files.erase(it);
48     } else {
49       *it = target_directory + "/" + *it;
50       it++;
51     }
52   }
53   return files;
54 }
55