blob: a7f4b28c8404c37981b3e0ea166a721febcdfb50 [file] [log] [blame]
Brian Carlstromb0460ea2011-07-29 10:08:05 -07001/*
2 * Copyright (C) 2008 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
David Sehr1979c642018-04-26 14:41:18 -070017#include "zip_archive.h"
Brian Carlstromb0460ea2011-07-29 10:08:05 -070018
19#include <fcntl.h>
Ian Rogers8d31bbd2013-10-13 10:44:14 -070020#include <stdio.h>
Andreas Gampe0dfc3152017-04-24 07:58:06 -070021#include <sys/mman.h> // For the PROT_* and MAP_* constants.
Brian Carlstromb0460ea2011-07-29 10:08:05 -070022#include <sys/stat.h>
23#include <sys/types.h>
24#include <unistd.h>
Ian Rogers700a4022014-05-19 16:49:03 -070025#include <vector>
Brian Carlstromb0460ea2011-07-29 10:08:05 -070026
Igor Murashkin271a0f82017-02-14 21:14:17 +000027#include "android-base/stringprintf.h"
Andreas Gampe0c2d3e52017-07-03 12:50:44 -070028#include "ziparchive/zip_archive.h"
29
David Sehr1979c642018-04-26 14:41:18 -070030#include "bit_utils.h"
31#include "unix_file/fd_file.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070032
Brian Carlstromb0460ea2011-07-29 10:08:05 -070033namespace art {
34
Igor Murashkin271a0f82017-02-14 21:14:17 +000035// Log file contents and mmap info when mapping entries directly.
36static constexpr const bool kDebugZipMapDirectly = false;
37
38using android::base::StringPrintf;
39
Brian Carlstromb0460ea2011-07-29 10:08:05 -070040uint32_t ZipEntry::GetUncompressedLength() {
Narayan Kamath92572be2013-11-28 14:06:24 +000041 return zip_entry_->uncompressed_length;
Brian Carlstromb0460ea2011-07-29 10:08:05 -070042}
43
44uint32_t ZipEntry::GetCrc32() {
Narayan Kamath92572be2013-11-28 14:06:24 +000045 return zip_entry_->crc32;
Brian Carlstromb0460ea2011-07-29 10:08:05 -070046}
47
Igor Murashkin271a0f82017-02-14 21:14:17 +000048bool ZipEntry::IsUncompressed() {
49 return zip_entry_->method == kCompressStored;
50}
51
Nicolas Geoffrayf3075272018-01-08 12:41:19 +000052bool ZipEntry::IsAlignedTo(size_t alignment) const {
Igor Murashkin271a0f82017-02-14 21:14:17 +000053 DCHECK(IsPowerOfTwo(alignment)) << alignment;
54 return IsAlignedParam(zip_entry_->offset, static_cast<int>(alignment));
55}
56
Mathieu Chartier661974a2014-01-09 11:23:53 -080057ZipEntry::~ZipEntry() {
58 delete zip_entry_;
59}
Brian Carlstromb0460ea2011-07-29 10:08:05 -070060
Ian Rogers8d31bbd2013-10-13 10:44:14 -070061bool ZipEntry::ExtractToFile(File& file, std::string* error_msg) {
Narayan Kamath92572be2013-11-28 14:06:24 +000062 const int32_t error = ExtractEntryToFile(handle_, zip_entry_, file.Fd());
63 if (error) {
64 *error_msg = std::string(ErrorCodeString(error));
Brian Carlstrom89521892011-12-07 22:05:07 -080065 return false;
66 }
67
Narayan Kamath92572be2013-11-28 14:06:24 +000068 return true;
Brian Carlstromb0460ea2011-07-29 10:08:05 -070069}
70
Vladimir Markoc34bebf2018-08-16 16:12:49 +010071MemMap ZipEntry::ExtractToMemMap(const char* zip_filename,
72 const char* entry_filename,
73 std::string* error_msg) {
Brian Carlstrom4922e9d2013-07-09 17:18:47 -070074 std::string name(entry_filename);
75 name += " extracted in memory from ";
Brian Carlstrom0aa504b2014-05-23 02:47:28 -070076 name += zip_filename;
Vladimir Markoc34bebf2018-08-16 16:12:49 +010077 MemMap map = MemMap::MapAnonymous(name.c_str(),
Vladimir Markoc34bebf2018-08-16 16:12:49 +010078 GetUncompressedLength(),
79 PROT_READ | PROT_WRITE,
Vladimir Marko11306592018-10-26 14:22:59 +010080 /*low_4gb=*/ false,
Vladimir Markoc34bebf2018-08-16 16:12:49 +010081 error_msg);
82 if (!map.IsValid()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070083 DCHECK(!error_msg->empty());
Vladimir Markoc34bebf2018-08-16 16:12:49 +010084 return MemMap::Invalid();
Brian Carlstrom4922e9d2013-07-09 17:18:47 -070085 }
86
Vladimir Markoc34bebf2018-08-16 16:12:49 +010087 const int32_t error = ExtractToMemory(handle_, zip_entry_, map.Begin(), map.Size());
Narayan Kamath92572be2013-11-28 14:06:24 +000088 if (error) {
89 *error_msg = std::string(ErrorCodeString(error));
Vladimir Markoc34bebf2018-08-16 16:12:49 +010090 return MemMap::Invalid();
Brian Carlstrom4922e9d2013-07-09 17:18:47 -070091 }
92
Vladimir Markoc34bebf2018-08-16 16:12:49 +010093 return map;
Brian Carlstrom4922e9d2013-07-09 17:18:47 -070094}
95
Vladimir Markoc34bebf2018-08-16 16:12:49 +010096MemMap ZipEntry::MapDirectlyFromFile(const char* zip_filename, std::string* error_msg) {
Igor Murashkin271a0f82017-02-14 21:14:17 +000097 const int zip_fd = GetFileDescriptor(handle_);
98 const char* entry_filename = entry_name_.c_str();
99
100 // Should not happen since we don't have a memory ZipArchive constructor.
101 // However the underlying ZipArchive isn't required to have an FD,
102 // so check to be sure.
103 CHECK_GE(zip_fd, 0) <<
104 StringPrintf("Cannot map '%s' (in zip '%s') directly because the zip archive "
105 "is not file backed.",
106 entry_filename,
107 zip_filename);
108
109 if (!IsUncompressed()) {
110 *error_msg = StringPrintf("Cannot map '%s' (in zip '%s') directly because it is compressed.",
111 entry_filename,
112 zip_filename);
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100113 return MemMap::Invalid();
Igor Murashkin271a0f82017-02-14 21:14:17 +0000114 } else if (zip_entry_->uncompressed_length != zip_entry_->compressed_length) {
115 *error_msg = StringPrintf("Cannot map '%s' (in zip '%s') directly because "
116 "entry has bad size (%u != %u).",
117 entry_filename,
118 zip_filename,
119 zip_entry_->uncompressed_length,
120 zip_entry_->compressed_length);
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100121 return MemMap::Invalid();
Igor Murashkin271a0f82017-02-14 21:14:17 +0000122 }
123
124 std::string name(entry_filename);
125 name += " mapped directly in memory from ";
126 name += zip_filename;
127
128 const off_t offset = zip_entry_->offset;
129
130 if (kDebugZipMapDirectly) {
131 LOG(INFO) << "zip_archive: " << "make mmap of " << name << " @ offset = " << offset;
132 }
133
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100134 MemMap map =
Vladimir Markoc09cd052018-08-23 16:36:36 +0100135 MemMap::MapFile(GetUncompressedLength(), // Byte count
136 PROT_READ | PROT_WRITE,
137 MAP_PRIVATE,
138 zip_fd,
139 offset,
Vladimir Marko11306592018-10-26 14:22:59 +0100140 /*low_4gb=*/ false,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100141 name.c_str(),
142 error_msg);
Igor Murashkin271a0f82017-02-14 21:14:17 +0000143
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100144 if (!map.IsValid()) {
Igor Murashkin271a0f82017-02-14 21:14:17 +0000145 DCHECK(!error_msg->empty());
146 }
147
148 if (kDebugZipMapDirectly) {
149 // Dump contents of file, same format as using this shell command:
150 // $> od -j <offset> -t x1 <zip_filename>
151 static constexpr const int kMaxDumpChars = 15;
152 lseek(zip_fd, 0, SEEK_SET);
153
154 int count = offset + kMaxDumpChars;
155
156 std::string tmp;
157 char buf;
158
159 // Dump file contents.
160 int i = 0;
161 while (read(zip_fd, &buf, 1) > 0 && i < count) {
162 tmp += StringPrintf("%3d ", (unsigned int)buf);
163 ++i;
164 }
165
166 LOG(INFO) << "map_fd raw bytes starting at 0";
167 LOG(INFO) << "" << tmp;
168 LOG(INFO) << "---------------------------";
169
170 // Dump map contents.
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100171 if (map.IsValid()) {
Igor Murashkin271a0f82017-02-14 21:14:17 +0000172 tmp = "";
173
174 count = kMaxDumpChars;
175
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100176 uint8_t* begin = map.Begin();
Igor Murashkin271a0f82017-02-14 21:14:17 +0000177 for (i = 0; i < count; ++i) {
178 tmp += StringPrintf("%3d ", (unsigned int)begin[i]);
179 }
180
181 LOG(INFO) << "map address " << StringPrintf("%p", begin);
182 LOG(INFO) << "map first " << kMaxDumpChars << " chars:";
183 LOG(INFO) << tmp;
184 }
185 }
186
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100187 return map;
Igor Murashkin271a0f82017-02-14 21:14:17 +0000188}
189
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100190MemMap ZipEntry::MapDirectlyOrExtract(const char* zip_filename,
191 const char* entry_filename,
Colin Cross2b41cca2018-11-16 22:43:41 -0800192 std::string* error_msg,
193 size_t alignment) {
194 if (IsUncompressed() && IsAlignedTo(alignment) && GetFileDescriptor(handle_) >= 0) {
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100195 std::string local_error_msg;
196 MemMap ret = MapDirectlyFromFile(zip_filename, &local_error_msg);
197 if (ret.IsValid()) {
Mathieu Chartier792111c2018-02-15 13:02:15 -0800198 return ret;
199 }
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100200 // Fall back to extraction for the failure case.
Mathieu Chartier792111c2018-02-15 13:02:15 -0800201 }
Mathieu Chartier792111c2018-02-15 13:02:15 -0800202 return ExtractToMemMap(zip_filename, entry_filename, error_msg);
203}
204
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800205static void SetCloseOnExec(int fd) {
206 // This dance is more portable than Linux's O_CLOEXEC open(2) flag.
207 int flags = fcntl(fd, F_GETFD);
208 if (flags == -1) {
209 PLOG(WARNING) << "fcntl(" << fd << ", F_GETFD) failed";
210 return;
211 }
212 int rc = fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
213 if (rc == -1) {
214 PLOG(WARNING) << "fcntl(" << fd << ", F_SETFD, " << flags << ") failed";
215 return;
216 }
217}
218
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700219ZipArchive* ZipArchive::Open(const char* filename, std::string* error_msg) {
220 DCHECK(filename != nullptr);
Narayan Kamath92572be2013-11-28 14:06:24 +0000221
222 ZipArchiveHandle handle;
223 const int32_t error = OpenArchive(filename, &handle);
224 if (error) {
225 *error_msg = std::string(ErrorCodeString(error));
226 CloseArchive(handle);
227 return nullptr;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700228 }
Narayan Kamath92572be2013-11-28 14:06:24 +0000229
230 SetCloseOnExec(GetFileDescriptor(handle));
231 return new ZipArchive(handle);
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700232}
233
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700234ZipArchive* ZipArchive::OpenFromFd(int fd, const char* filename, std::string* error_msg) {
Narayan Kamath92572be2013-11-28 14:06:24 +0000235 DCHECK(filename != nullptr);
236 DCHECK_GT(fd, 0);
237
238 ZipArchiveHandle handle;
239 const int32_t error = OpenArchiveFd(fd, filename, &handle);
240 if (error) {
241 *error_msg = std::string(ErrorCodeString(error));
242 CloseArchive(handle);
243 return nullptr;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700244 }
Narayan Kamath92572be2013-11-28 14:06:24 +0000245
246 SetCloseOnExec(GetFileDescriptor(handle));
247 return new ZipArchive(handle);
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700248}
249
Narayan Kamath92572be2013-11-28 14:06:24 +0000250ZipEntry* ZipArchive::Find(const char* name, std::string* error_msg) const {
251 DCHECK(name != nullptr);
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700252
Narayan Kamath92572be2013-11-28 14:06:24 +0000253 // Resist the urge to delete the space. <: is a bigraph sequence.
Ian Rogers700a4022014-05-19 16:49:03 -0700254 std::unique_ptr< ::ZipEntry> zip_entry(new ::ZipEntry);
Yusuke Sato64db62d2015-06-25 14:56:49 -0700255 const int32_t error = FindEntry(handle_, ZipString(name), zip_entry.get());
Narayan Kamath92572be2013-11-28 14:06:24 +0000256 if (error) {
257 *error_msg = std::string(ErrorCodeString(error));
258 return nullptr;
Kenny Root72fcca22013-09-19 09:25:34 -0700259 }
260
Igor Murashkin271a0f82017-02-14 21:14:17 +0000261 return new ZipEntry(handle_, zip_entry.release(), name);
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700262}
263
Vladimir Marko9bdf1082016-01-21 12:15:52 +0000264ZipArchive::~ZipArchive() {
265 CloseArchive(handle_);
266}
267
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700268} // namespace art