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 | |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 19 | #include <vector> |
| 20 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 21 | #include <fcntl.h> |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 22 | #include <stdio.h> |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <unistd.h> |
| 26 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 27 | #include "base/stringprintf.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 28 | #include "base/unix_file/fd_file.h" |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 29 | #include "UniquePtr.h" |
| 30 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 31 | namespace art { |
| 32 | |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 33 | uint32_t ZipEntry::GetUncompressedLength() { |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 34 | return zip_entry_->uncompressed_length; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | uint32_t ZipEntry::GetCrc32() { |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 38 | return zip_entry_->crc32; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame^] | 41 | ZipEntry::~ZipEntry() { |
| 42 | delete zip_entry_; |
| 43 | } |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 44 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 45 | bool ZipEntry::ExtractToFile(File& file, std::string* error_msg) { |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 46 | const int32_t error = ExtractEntryToFile(handle_, zip_entry_, file.Fd()); |
| 47 | if (error) { |
| 48 | *error_msg = std::string(ErrorCodeString(error)); |
Brian Carlstrom | 8952189 | 2011-12-07 22:05:07 -0800 | [diff] [blame] | 49 | return false; |
| 50 | } |
| 51 | |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 52 | return true; |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 55 | MemMap* ZipEntry::ExtractToMemMap(const char* entry_filename, std::string* error_msg) { |
Brian Carlstrom | 4922e9d | 2013-07-09 17:18:47 -0700 | [diff] [blame] | 56 | std::string name(entry_filename); |
| 57 | name += " extracted in memory from "; |
| 58 | name += entry_filename; |
| 59 | UniquePtr<MemMap> map(MemMap::MapAnonymous(name.c_str(), |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 60 | NULL, GetUncompressedLength(), |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 61 | PROT_READ | PROT_WRITE, false, error_msg)); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 62 | if (map.get() == nullptr) { |
| 63 | DCHECK(!error_msg->empty()); |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 64 | return nullptr; |
Brian Carlstrom | 4922e9d | 2013-07-09 17:18:47 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 67 | 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 Carlstrom | 4922e9d | 2013-07-09 17:18:47 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | return map.release(); |
| 75 | } |
| 76 | |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 77 | static 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 Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 91 | ZipArchive* ZipArchive::Open(const char* filename, std::string* error_msg) { |
| 92 | DCHECK(filename != nullptr); |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 93 | |
| 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 Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 100 | } |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 101 | |
| 102 | SetCloseOnExec(GetFileDescriptor(handle)); |
| 103 | return new ZipArchive(handle); |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 106 | ZipArchive* ZipArchive::OpenFromFd(int fd, const char* filename, std::string* error_msg) { |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 107 | 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 Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 116 | } |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 117 | |
| 118 | SetCloseOnExec(GetFileDescriptor(handle)); |
| 119 | return new ZipArchive(handle); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 122 | ZipEntry* ZipArchive::Find(const char* name, std::string* error_msg) const { |
| 123 | DCHECK(name != nullptr); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 124 | |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 125 | // Resist the urge to delete the space. <: is a bigraph sequence. |
| 126 | UniquePtr< ::ZipEntry> zip_entry(new ::ZipEntry); |
| 127 | const int32_t error = FindEntry(handle_, name, zip_entry.get()); |
| 128 | if (error) { |
| 129 | *error_msg = std::string(ErrorCodeString(error)); |
| 130 | return nullptr; |
Kenny Root | 72fcca2 | 2013-09-19 09:25:34 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Narayan Kamath | 92572be | 2013-11-28 14:06:24 +0000 | [diff] [blame] | 133 | return new ZipEntry(handle_, zip_entry.release()); |
Brian Carlstrom | b0460ea | 2011-07-29 10:08:05 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | } // namespace art |