blob: 690942ac73dc9d17d8b75bdf06a870683d61b0c0 [file] [log] [blame]
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -07001/*
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 "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
bigbiffd58ba182020-03-23 10:02:29 -040031#include "otautil/dirutil.h"
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070032
33static constexpr mode_t UNZIP_DIRMODE = 0755;
34static constexpr mode_t UNZIP_FILEMODE = 0644;
35
36bool 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 }
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070057
bigbiff83298f52021-10-13 19:24:42 -040058 const std::string zip_prefix(prefix_path.c_str());
sekaiacga202b5a2021-12-11 09:28:55 +080059 int ret = StartIteration(zip, &cookie, zip_prefix);
bigbiff83298f52021-10-13 19:24:42 -040060
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070061 if (ret != 0) {
62 LOG(ERROR) << "failed to start iterating zip entries.";
63 return false;
64 }
65
66 std::unique_ptr<void, decltype(&EndIteration)> guard(cookie, EndIteration);
67 ZipEntry entry;
bigbiff673c7ae2020-12-02 19:44:56 -050068 std::string name;
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070069 int extractCount = 0;
70 while (Next(cookie, &entry, &name) == 0) {
bigbiff673c7ae2020-12-02 19:44:56 -050071 std::string entry_name(name.c_str(), name.c_str() + name.size());
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070072 CHECK_LE(prefix_path.size(), entry_name.size());
73 std::string path = target_dir + entry_name.substr(prefix_path.size());
74 // Skip dir.
75 if (path.back() == '/') {
76 continue;
77 }
78 //TODO(b/31917448) handle the symlink.
79
80 if (dirCreateHierarchy(path.c_str(), UNZIP_DIRMODE, timestamp, true, sehnd) != 0) {
81 LOG(ERROR) << "failed to create dir for " << path;
82 return false;
83 }
84
85 char *secontext = NULL;
86 if (sehnd) {
87 selabel_lookup(sehnd, &secontext, path.c_str(), UNZIP_FILEMODE);
88 setfscreatecon(secontext);
89 }
90 android::base::unique_fd fd(open(path.c_str(), O_CREAT|O_WRONLY|O_TRUNC, UNZIP_FILEMODE));
91 if (fd == -1) {
92 PLOG(ERROR) << "Can't create target file \"" << path << "\"";
93 return false;
94 }
95 if (secontext) {
96 freecon(secontext);
97 setfscreatecon(NULL);
98 }
99
100 int err = ExtractEntryToFile(zip, &entry, fd);
101 if (err != 0) {
102 LOG(ERROR) << "Error extracting \"" << path << "\" : " << ErrorCodeString(err);
103 return false;
104 }
105
106 if (fsync(fd) != 0) {
107 PLOG(ERROR) << "Error syncing file descriptor when extracting \"" << path << "\"";
108 return false;
109 }
110
111 if (timestamp != nullptr && utime(path.c_str(), timestamp)) {
112 PLOG(ERROR) << "Error touching \"" << path << "\"";
113 return false;
114 }
115
116 LOG(INFO) << "Extracted file \"" << path << "\"";
117 ++extractCount;
118 }
119
120 LOG(INFO) << "Extracted " << extractCount << " file(s)";
121 return true;
122}