Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 1 | /* |
| 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 Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 20 | #include <stdio.h> |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 21 | #include <sys/stat.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <unistd.h> |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 24 | #include <vector> |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 25 | |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 26 | #include "base/unix_file/fd_file.h" |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 27 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 28 | namespace art { |
| 29 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 30 | uint32_t ZipEntry::GetUncompressedLength() { |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 31 | return zip_entry_->uncompressed_length; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | uint32_t ZipEntry::GetCrc32() { |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 35 | return zip_entry_->crc32; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 38 | ZipEntry::~ZipEntry() { |
| 39 | delete zip_entry_; |
| 40 | } |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 41 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 42 | bool ZipEntry::ExtractToFile(File& file, std::string* error_msg) { |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 43 | const int32_t error = ExtractEntryToFile(handle_, zip_entry_, file.Fd()); |
| 44 | if (error) { |
| 45 | *error_msg = std::string(ErrorCodeString(error)); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 46 | return false; |
| 47 | } |
| 48 | |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 49 | return true; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Brian Carlstrom | 0aa504b | 2014-05-23 02:47:28 -0700 | [diff] [blame] | 52 | MemMap* ZipEntry::ExtractToMemMap(const char* zip_filename, const char* entry_filename, |
| 53 | std::string* error_msg) { |
Brian Carlstrom | 4922e9d | 2013-07-09 17:18:47 -0700 | [diff] [blame] | 54 | std::string name(entry_filename); |
| 55 | name += " extracted in memory from "; |
Brian Carlstrom | 0aa504b | 2014-05-23 02:47:28 -0700 | [diff] [blame] | 56 | name += zip_filename; |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 57 | std::unique_ptr<MemMap> map(MemMap::MapAnonymous(name.c_str(), |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 58 | nullptr, GetUncompressedLength(), |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 59 | PROT_READ | PROT_WRITE, false, false, |
| 60 | error_msg)); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 61 | if (map.get() == nullptr) { |
| 62 | DCHECK(!error_msg->empty()); |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 63 | return nullptr; |
Brian Carlstrom | 4922e9d | 2013-07-09 17:18:47 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 66 | const int32_t error = ExtractToMemory(handle_, zip_entry_, |
| 67 | map->Begin(), map->Size()); |
| 68 | if (error) { |
| 69 | *error_msg = std::string(ErrorCodeString(error)); |
| 70 | return nullptr; |
Brian Carlstrom | 4922e9d | 2013-07-09 17:18:47 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | return map.release(); |
| 74 | } |
| 75 | |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 76 | static void SetCloseOnExec(int fd) { |
| 77 | // This dance is more portable than Linux's O_CLOEXEC open(2) flag. |
| 78 | int flags = fcntl(fd, F_GETFD); |
| 79 | if (flags == -1) { |
| 80 | PLOG(WARNING) << "fcntl(" << fd << ", F_GETFD) failed"; |
| 81 | return; |
| 82 | } |
| 83 | int rc = fcntl(fd, F_SETFD, flags | FD_CLOEXEC); |
| 84 | if (rc == -1) { |
| 85 | PLOG(WARNING) << "fcntl(" << fd << ", F_SETFD, " << flags << ") failed"; |
| 86 | return; |
| 87 | } |
| 88 | } |
| 89 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 90 | ZipArchive* ZipArchive::Open(const char* filename, std::string* error_msg) { |
| 91 | DCHECK(filename != nullptr); |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 92 | |
| 93 | ZipArchiveHandle handle; |
| 94 | const int32_t error = OpenArchive(filename, &handle); |
| 95 | if (error) { |
| 96 | *error_msg = std::string(ErrorCodeString(error)); |
| 97 | CloseArchive(handle); |
| 98 | return nullptr; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 99 | } |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 100 | |
| 101 | SetCloseOnExec(GetFileDescriptor(handle)); |
| 102 | return new ZipArchive(handle); |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 105 | ZipArchive* ZipArchive::OpenFromFd(int fd, const char* filename, std::string* error_msg) { |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 106 | DCHECK(filename != nullptr); |
| 107 | DCHECK_GT(fd, 0); |
| 108 | |
| 109 | ZipArchiveHandle handle; |
| 110 | const int32_t error = OpenArchiveFd(fd, filename, &handle); |
| 111 | if (error) { |
| 112 | *error_msg = std::string(ErrorCodeString(error)); |
| 113 | CloseArchive(handle); |
| 114 | return nullptr; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 115 | } |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 116 | |
| 117 | SetCloseOnExec(GetFileDescriptor(handle)); |
| 118 | return new ZipArchive(handle); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 121 | ZipEntry* ZipArchive::Find(const char* name, std::string* error_msg) const { |
| 122 | DCHECK(name != nullptr); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 123 | |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 124 | // Resist the urge to delete the space. <: is a bigraph sequence. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 125 | std::unique_ptr< ::ZipEntry> zip_entry(new ::ZipEntry); |
Yusuke Sato | 64db62d | 2015-06-25 14:56:49 -0700 | [diff] [blame] | 126 | const int32_t error = FindEntry(handle_, ZipString(name), zip_entry.get()); |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 127 | if (error) { |
| 128 | *error_msg = std::string(ErrorCodeString(error)); |
| 129 | return nullptr; |
Kenny Root | 72fcca2 | 2013-09-19 09:25:34 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 132 | return new ZipEntry(handle_, zip_entry.release()); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 135 | ZipArchive::~ZipArchive() { |
| 136 | CloseArchive(handle_); |
| 137 | } |
| 138 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 139 | } // namespace art |