blob: ffab674be10d97b70c0a4dca8144933a66cc697b [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
17#include "zip_archive.h"
18
19#include <fcntl.h>
Ian Rogers8d31bbd2013-10-13 10:44:14 -070020#include <stdio.h>
Brian Carlstromb0460ea2011-07-29 10:08:05 -070021#include <sys/stat.h>
22#include <sys/types.h>
23#include <unistd.h>
Ian Rogers700a4022014-05-19 16:49:03 -070024#include <vector>
Brian Carlstromb0460ea2011-07-29 10:08:05 -070025
Ian Rogers8d31bbd2013-10-13 10:44:14 -070026#include "base/stringprintf.h"
Elliott Hughes76160052012-12-12 16:31:20 -080027#include "base/unix_file/fd_file.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070028
Brian Carlstromb0460ea2011-07-29 10:08:05 -070029namespace art {
30
Brian Carlstromb0460ea2011-07-29 10:08:05 -070031uint32_t ZipEntry::GetUncompressedLength() {
Narayan Kamath92572be2013-11-28 14:06:24 +000032 return zip_entry_->uncompressed_length;
Brian Carlstromb0460ea2011-07-29 10:08:05 -070033}
34
35uint32_t ZipEntry::GetCrc32() {
Narayan Kamath92572be2013-11-28 14:06:24 +000036 return zip_entry_->crc32;
Brian Carlstromb0460ea2011-07-29 10:08:05 -070037}
38
Mathieu Chartier661974a2014-01-09 11:23:53 -080039ZipEntry::~ZipEntry() {
40 delete zip_entry_;
41}
Brian Carlstromb0460ea2011-07-29 10:08:05 -070042
Ian Rogers8d31bbd2013-10-13 10:44:14 -070043bool ZipEntry::ExtractToFile(File& file, std::string* error_msg) {
Narayan Kamath92572be2013-11-28 14:06:24 +000044 const int32_t error = ExtractEntryToFile(handle_, zip_entry_, file.Fd());
45 if (error) {
46 *error_msg = std::string(ErrorCodeString(error));
Brian Carlstrom89521892011-12-07 22:05:07 -080047 return false;
48 }
49
Narayan Kamath92572be2013-11-28 14:06:24 +000050 return true;
Brian Carlstromb0460ea2011-07-29 10:08:05 -070051}
52
Brian Carlstrom0aa504b2014-05-23 02:47:28 -070053MemMap* ZipEntry::ExtractToMemMap(const char* zip_filename, const char* entry_filename,
54 std::string* error_msg) {
Brian Carlstrom4922e9d2013-07-09 17:18:47 -070055 std::string name(entry_filename);
56 name += " extracted in memory from ";
Brian Carlstrom0aa504b2014-05-23 02:47:28 -070057 name += zip_filename;
Ian Rogers700a4022014-05-19 16:49:03 -070058 std::unique_ptr<MemMap> map(MemMap::MapAnonymous(name.c_str(),
Brian Carlstrom0aa504b2014-05-23 02:47:28 -070059 NULL, GetUncompressedLength(),
Vladimir Marko5c42c292015-02-25 12:02:49 +000060 PROT_READ | PROT_WRITE, false, false,
61 error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -070062 if (map.get() == nullptr) {
63 DCHECK(!error_msg->empty());
Narayan Kamath92572be2013-11-28 14:06:24 +000064 return nullptr;
Brian Carlstrom4922e9d2013-07-09 17:18:47 -070065 }
66
Narayan Kamath92572be2013-11-28 14:06:24 +000067 const int32_t error = ExtractToMemory(handle_, zip_entry_,
68 map->Begin(), map->Size());
69 if (error) {
70 *error_msg = std::string(ErrorCodeString(error));
71 return nullptr;
Brian Carlstrom4922e9d2013-07-09 17:18:47 -070072 }
73
74 return map.release();
75}
76
Elliott Hughesad6c9c32012-01-19 17:39:12 -080077static void SetCloseOnExec(int fd) {
78 // This dance is more portable than Linux's O_CLOEXEC open(2) flag.
79 int flags = fcntl(fd, F_GETFD);
80 if (flags == -1) {
81 PLOG(WARNING) << "fcntl(" << fd << ", F_GETFD) failed";
82 return;
83 }
84 int rc = fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
85 if (rc == -1) {
86 PLOG(WARNING) << "fcntl(" << fd << ", F_SETFD, " << flags << ") failed";
87 return;
88 }
89}
90
Ian Rogers8d31bbd2013-10-13 10:44:14 -070091ZipArchive* ZipArchive::Open(const char* filename, std::string* error_msg) {
92 DCHECK(filename != nullptr);
Narayan Kamath92572be2013-11-28 14:06:24 +000093
94 ZipArchiveHandle handle;
95 const int32_t error = OpenArchive(filename, &handle);
96 if (error) {
97 *error_msg = std::string(ErrorCodeString(error));
98 CloseArchive(handle);
99 return nullptr;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700100 }
Narayan Kamath92572be2013-11-28 14:06:24 +0000101
102 SetCloseOnExec(GetFileDescriptor(handle));
103 return new ZipArchive(handle);
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700104}
105
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700106ZipArchive* ZipArchive::OpenFromFd(int fd, const char* filename, std::string* error_msg) {
Narayan Kamath92572be2013-11-28 14:06:24 +0000107 DCHECK(filename != nullptr);
108 DCHECK_GT(fd, 0);
109
110 ZipArchiveHandle handle;
111 const int32_t error = OpenArchiveFd(fd, filename, &handle);
112 if (error) {
113 *error_msg = std::string(ErrorCodeString(error));
114 CloseArchive(handle);
115 return nullptr;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700116 }
Narayan Kamath92572be2013-11-28 14:06:24 +0000117
118 SetCloseOnExec(GetFileDescriptor(handle));
119 return new ZipArchive(handle);
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700120}
121
Narayan Kamath92572be2013-11-28 14:06:24 +0000122ZipEntry* ZipArchive::Find(const char* name, std::string* error_msg) const {
123 DCHECK(name != nullptr);
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700124
Narayan Kamath92572be2013-11-28 14:06:24 +0000125 // Resist the urge to delete the space. <: is a bigraph sequence.
Ian Rogers700a4022014-05-19 16:49:03 -0700126 std::unique_ptr< ::ZipEntry> zip_entry(new ::ZipEntry);
Piotr Jastrzebskid57a84a2014-08-13 07:50:03 +0100127 const int32_t error = FindEntry(handle_, ZipEntryName(name), zip_entry.get());
Narayan Kamath92572be2013-11-28 14:06:24 +0000128 if (error) {
129 *error_msg = std::string(ErrorCodeString(error));
130 return nullptr;
Kenny Root72fcca22013-09-19 09:25:34 -0700131 }
132
Narayan Kamath92572be2013-11-28 14:06:24 +0000133 return new ZipEntry(handle_, zip_entry.release());
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700134}
135
136} // namespace art