Michael Bestas | 15a104f | 2019-09-19 21:42:12 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 "otautil/ziputil.h" |
| 18 | |
| 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <utime.h> |
| 22 | |
| 23 | #include <string> |
| 24 | |
| 25 | #include <android-base/logging.h> |
| 26 | #include <android-base/unique_fd.h> |
| 27 | #include <selinux/label.h> |
| 28 | #include <selinux/selinux.h> |
| 29 | #include <ziparchive/zip_archive.h> |
| 30 | |
| 31 | #include "otautil/dirutil.h" |
| 32 | |
| 33 | static constexpr mode_t UNZIP_DIRMODE = 0755; |
| 34 | static constexpr mode_t UNZIP_FILEMODE = 0644; |
| 35 | |
| 36 | bool ExtractPackageRecursive(ZipArchiveHandle zip, const std::string& zip_path, |
| 37 | const std::string& dest_path, const struct utimbuf* timestamp, |
| 38 | struct selabel_handle* sehnd) { |
| 39 | if (!zip_path.empty() && zip_path[0] == '/') { |
| 40 | LOG(ERROR) << "ExtractPackageRecursive(): zip_path must be a relative path " << zip_path; |
| 41 | return false; |
| 42 | } |
| 43 | if (dest_path.empty() || dest_path[0] != '/') { |
| 44 | LOG(ERROR) << "ExtractPackageRecursive(): dest_path must be an absolute path " << dest_path; |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | void* cookie; |
| 49 | std::string target_dir(dest_path); |
| 50 | if (dest_path.back() != '/') { |
| 51 | target_dir += '/'; |
| 52 | } |
| 53 | std::string prefix_path(zip_path); |
| 54 | if (!zip_path.empty() && zip_path.back() != '/') { |
| 55 | prefix_path += '/'; |
| 56 | } |
| 57 | |
| 58 | int ret = StartIteration(zip, &cookie, prefix_path, ""); |
| 59 | if (ret != 0) { |
| 60 | LOG(ERROR) << "failed to start iterating zip entries."; |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | std::unique_ptr<void, decltype(&EndIteration)> guard(cookie, EndIteration); |
| 65 | ZipEntry entry; |
| 66 | std::string name; |
| 67 | int extractCount = 0; |
| 68 | while (Next(cookie, &entry, &name) == 0) { |
| 69 | CHECK_LE(prefix_path.size(), name.size()); |
| 70 | std::string path = target_dir + name.substr(prefix_path.size()); |
| 71 | // Skip dir. |
| 72 | if (path.back() == '/') { |
| 73 | continue; |
| 74 | } |
| 75 | |
| 76 | if (mkdir_recursively(path.c_str(), UNZIP_DIRMODE, true, sehnd, timestamp) != 0) { |
| 77 | LOG(ERROR) << "failed to create dir for " << path; |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | char* secontext = NULL; |
| 82 | if (sehnd) { |
| 83 | selabel_lookup(sehnd, &secontext, path.c_str(), UNZIP_FILEMODE); |
| 84 | setfscreatecon(secontext); |
| 85 | } |
| 86 | android::base::unique_fd fd(open(path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, UNZIP_FILEMODE)); |
| 87 | if (fd == -1) { |
| 88 | PLOG(ERROR) << "Can't create target file \"" << path << "\""; |
| 89 | return false; |
| 90 | } |
| 91 | if (secontext) { |
| 92 | freecon(secontext); |
| 93 | setfscreatecon(NULL); |
| 94 | } |
| 95 | |
| 96 | int err = ExtractEntryToFile(zip, &entry, fd); |
| 97 | if (err != 0) { |
| 98 | LOG(ERROR) << "Error extracting \"" << path << "\" : " << ErrorCodeString(err); |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | if (fsync(fd) != 0) { |
| 103 | PLOG(ERROR) << "Error syncing file descriptor when extracting \"" << path << "\""; |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | if (timestamp != nullptr && utime(path.c_str(), timestamp)) { |
| 108 | PLOG(ERROR) << "Error touching \"" << path << "\""; |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | LOG(INFO) << "Extracted file \"" << path << "\""; |
| 113 | ++extractCount; |
| 114 | } |
| 115 | |
| 116 | LOG(INFO) << "Extracted " << extractCount << " file(s)"; |
| 117 | return true; |
| 118 | } |