2013-05-06 20:20:34 -07:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
//
|
|
|
|
// Read-only access to Zip archives, with minimal heap allocation.
|
|
|
|
//
|
|
|
|
#define LOG_TAG "zipro"
|
|
|
|
//#define LOG_NDEBUG 0
|
|
|
|
#include <androidfw/ZipFileRO.h>
|
|
|
|
#include <utils/Log.h>
|
|
|
|
#include <utils/Compat.h>
|
|
|
|
#include <utils/misc.h>
|
|
|
|
#include <utils/threads.h>
|
2013-12-03 13:16:03 +00:00
|
|
|
#include <ziparchive/zip_archive.h>
|
2013-05-06 20:20:34 -07:00
|
|
|
|
|
|
|
#include <zlib.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
using namespace android;
|
|
|
|
|
2013-12-03 13:16:03 +00:00
|
|
|
class _ZipEntryRO {
|
|
|
|
public:
|
|
|
|
ZipEntry entry;
|
2019-06-14 15:28:38 -07:00
|
|
|
std::string_view name;
|
2013-12-03 13:16:03 +00:00
|
|
|
void *cookie;
|
2013-05-06 20:20:34 -07:00
|
|
|
|
2014-08-13 07:50:20 +01:00
|
|
|
_ZipEntryRO() : cookie(NULL) {}
|
2013-05-06 20:20:34 -07:00
|
|
|
|
2014-08-08 12:52:39 +01:00
|
|
|
~_ZipEntryRO() {
|
|
|
|
EndIteration(cookie);
|
|
|
|
}
|
|
|
|
|
2013-12-03 13:16:03 +00:00
|
|
|
private:
|
|
|
|
_ZipEntryRO(const _ZipEntryRO& other);
|
|
|
|
_ZipEntryRO& operator=(const _ZipEntryRO& other);
|
|
|
|
};
|
2013-05-06 20:20:34 -07:00
|
|
|
|
2013-12-03 13:16:03 +00:00
|
|
|
ZipFileRO::~ZipFileRO() {
|
|
|
|
CloseArchive(mHandle);
|
Fully implement "install" and "install-write" in PackageManagerShellCommand.
We can use the new mechanism to ask the calling shell to open
a file in order to implement the rest of these commands, allowing
you to give the path to an apk to install. That API is thus
extended to allow you to open readable files, not just opening
file for writing.
Doing this however means we no longer can pass a file path to
AssetManager for the apk to parse, we only have an already open
fd for that. Extending AssetManager to allow adding apks from
fds is not that hard, however, since the underlying zip library
already supports this.
This main thing this changes is in AssetManager.cpp where we
retrieve the open zip file for a particular apk that has been
added. This used to look up the zip file by path every time
it was needed, but that won't work anymore now that we can have
things added by fd. Instead, we keep track of each opened zip
in the AssetManager, so we can just directly retrieve it from
the asset_path representing the item that was added. As a
side-effect, this means for normal paths we no longer need to
look up by name, but just have the opened zip file directly
accessible. (This is probably good, but it does mean that we
no longer run the logic of seeing if the zip file's timestamp
has changed and re-opening it if it has. We probably shouldn't
be relying on that for an active AssetManager anyway, and maybe
it is even good that we don't allow the zip file to change
under it?)
A follow-up change will finally remove the Pm.java implementation
and turn the pm "command" into a simple shell script that runs
cmd package.
Test: manual
Change-Id: Ie103e3bdaa5b706796cc329254f2638151a3924f
2017-10-30 14:19:32 -07:00
|
|
|
if (mFileName != NULL) {
|
|
|
|
free(mFileName);
|
|
|
|
}
|
2013-05-06 20:20:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open the specified file read-only. We memory-map the entire thing and
|
|
|
|
* close the file before returning.
|
|
|
|
*/
|
2013-12-03 13:16:03 +00:00
|
|
|
/* static */ ZipFileRO* ZipFileRO::open(const char* zipFileName)
|
2013-05-06 20:20:34 -07:00
|
|
|
{
|
2013-12-03 13:16:03 +00:00
|
|
|
ZipArchiveHandle handle;
|
|
|
|
const int32_t error = OpenArchive(zipFileName, &handle);
|
|
|
|
if (error) {
|
|
|
|
ALOGW("Error opening archive %s: %s", zipFileName, ErrorCodeString(error));
|
2015-05-11 15:45:36 +01:00
|
|
|
CloseArchive(handle);
|
2013-12-03 13:16:03 +00:00
|
|
|
return NULL;
|
2013-05-06 20:20:34 -07:00
|
|
|
}
|
|
|
|
|
2013-12-03 13:16:03 +00:00
|
|
|
return new ZipFileRO(handle, strdup(zipFileName));
|
|
|
|
}
|
2013-05-06 20:20:34 -07:00
|
|
|
|
|
|
|
|
Fully implement "install" and "install-write" in PackageManagerShellCommand.
We can use the new mechanism to ask the calling shell to open
a file in order to implement the rest of these commands, allowing
you to give the path to an apk to install. That API is thus
extended to allow you to open readable files, not just opening
file for writing.
Doing this however means we no longer can pass a file path to
AssetManager for the apk to parse, we only have an already open
fd for that. Extending AssetManager to allow adding apks from
fds is not that hard, however, since the underlying zip library
already supports this.
This main thing this changes is in AssetManager.cpp where we
retrieve the open zip file for a particular apk that has been
added. This used to look up the zip file by path every time
it was needed, but that won't work anymore now that we can have
things added by fd. Instead, we keep track of each opened zip
in the AssetManager, so we can just directly retrieve it from
the asset_path representing the item that was added. As a
side-effect, this means for normal paths we no longer need to
look up by name, but just have the opened zip file directly
accessible. (This is probably good, but it does mean that we
no longer run the logic of seeing if the zip file's timestamp
has changed and re-opening it if it has. We probably shouldn't
be relying on that for an active AssetManager anyway, and maybe
it is even good that we don't allow the zip file to change
under it?)
A follow-up change will finally remove the Pm.java implementation
and turn the pm "command" into a simple shell script that runs
cmd package.
Test: manual
Change-Id: Ie103e3bdaa5b706796cc329254f2638151a3924f
2017-10-30 14:19:32 -07:00
|
|
|
/* static */ ZipFileRO* ZipFileRO::openFd(int fd, const char* debugFileName,
|
|
|
|
bool assume_ownership)
|
|
|
|
{
|
|
|
|
ZipArchiveHandle handle;
|
|
|
|
const int32_t error = OpenArchiveFd(fd, debugFileName, &handle, assume_ownership);
|
|
|
|
if (error) {
|
|
|
|
ALOGW("Error opening archive fd %d %s: %s", fd, debugFileName, ErrorCodeString(error));
|
|
|
|
CloseArchive(handle);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new ZipFileRO(handle, strdup(debugFileName));
|
|
|
|
}
|
|
|
|
|
2013-12-03 13:16:03 +00:00
|
|
|
ZipEntryRO ZipFileRO::findEntryByName(const char* entryName) const
|
|
|
|
{
|
|
|
|
_ZipEntryRO* data = new _ZipEntryRO;
|
2014-08-13 07:50:20 +01:00
|
|
|
|
2019-06-14 15:28:38 -07:00
|
|
|
data->name = entryName;
|
2014-08-13 07:50:20 +01:00
|
|
|
|
2019-05-03 22:42:31 -07:00
|
|
|
const int32_t error = FindEntry(mHandle, entryName, &(data->entry));
|
2013-12-03 13:16:03 +00:00
|
|
|
if (error) {
|
|
|
|
delete data;
|
|
|
|
return NULL;
|
2013-05-06 20:20:34 -07:00
|
|
|
}
|
|
|
|
|
2013-12-03 13:16:03 +00:00
|
|
|
return (ZipEntryRO) data;
|
2013-05-06 20:20:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-12-03 13:16:03 +00:00
|
|
|
* Get the useful fields from the zip entry.
|
|
|
|
*
|
|
|
|
* Returns "false" if the offsets to the fields or the contents of the fields
|
|
|
|
* appear to be bogus.
|
2013-05-06 20:20:34 -07:00
|
|
|
*/
|
2015-06-16 12:02:57 +01:00
|
|
|
bool ZipFileRO::getEntryInfo(ZipEntryRO entry, uint16_t* pMethod,
|
|
|
|
uint32_t* pUncompLen, uint32_t* pCompLen, off64_t* pOffset,
|
|
|
|
uint32_t* pModWhen, uint32_t* pCrc32) const
|
2013-05-06 20:20:34 -07:00
|
|
|
{
|
2013-12-03 13:16:03 +00:00
|
|
|
const _ZipEntryRO* zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
|
|
|
|
const ZipEntry& ze = zipEntry->entry;
|
2013-05-06 20:20:34 -07:00
|
|
|
|
2013-12-03 13:16:03 +00:00
|
|
|
if (pMethod != NULL) {
|
|
|
|
*pMethod = ze.method;
|
2013-05-06 20:20:34 -07:00
|
|
|
}
|
2013-12-03 13:16:03 +00:00
|
|
|
if (pUncompLen != NULL) {
|
|
|
|
*pUncompLen = ze.uncompressed_length;
|
2013-05-06 20:20:34 -07:00
|
|
|
}
|
2013-12-03 13:16:03 +00:00
|
|
|
if (pCompLen != NULL) {
|
|
|
|
*pCompLen = ze.compressed_length;
|
2013-05-06 20:20:34 -07:00
|
|
|
}
|
2013-12-03 13:16:03 +00:00
|
|
|
if (pOffset != NULL) {
|
|
|
|
*pOffset = ze.offset;
|
2013-05-06 20:20:34 -07:00
|
|
|
}
|
2013-12-03 13:16:03 +00:00
|
|
|
if (pModWhen != NULL) {
|
|
|
|
*pModWhen = ze.mod_time;
|
2013-05-06 20:20:34 -07:00
|
|
|
}
|
2013-12-03 13:16:03 +00:00
|
|
|
if (pCrc32 != NULL) {
|
|
|
|
*pCrc32 = ze.crc32;
|
2013-05-06 20:20:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-19 17:18:07 -07:00
|
|
|
bool ZipFileRO::startIteration(void** cookie) {
|
|
|
|
return startIteration(cookie, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ZipFileRO::startIteration(void** cookie, const char* prefix, const char* suffix)
|
2013-05-06 20:20:34 -07:00
|
|
|
{
|
2013-12-03 13:16:03 +00:00
|
|
|
_ZipEntryRO* ze = new _ZipEntryRO;
|
2015-06-19 17:18:07 -07:00
|
|
|
int32_t error = StartIteration(mHandle, &(ze->cookie),
|
2019-05-08 12:12:39 -07:00
|
|
|
prefix ? prefix : "", suffix ? suffix : "");
|
2013-12-03 13:16:03 +00:00
|
|
|
if (error) {
|
Fully implement "install" and "install-write" in PackageManagerShellCommand.
We can use the new mechanism to ask the calling shell to open
a file in order to implement the rest of these commands, allowing
you to give the path to an apk to install. That API is thus
extended to allow you to open readable files, not just opening
file for writing.
Doing this however means we no longer can pass a file path to
AssetManager for the apk to parse, we only have an already open
fd for that. Extending AssetManager to allow adding apks from
fds is not that hard, however, since the underlying zip library
already supports this.
This main thing this changes is in AssetManager.cpp where we
retrieve the open zip file for a particular apk that has been
added. This used to look up the zip file by path every time
it was needed, but that won't work anymore now that we can have
things added by fd. Instead, we keep track of each opened zip
in the AssetManager, so we can just directly retrieve it from
the asset_path representing the item that was added. As a
side-effect, this means for normal paths we no longer need to
look up by name, but just have the opened zip file directly
accessible. (This is probably good, but it does mean that we
no longer run the logic of seeing if the zip file's timestamp
has changed and re-opening it if it has. We probably shouldn't
be relying on that for an active AssetManager anyway, and maybe
it is even good that we don't allow the zip file to change
under it?)
A follow-up change will finally remove the Pm.java implementation
and turn the pm "command" into a simple shell script that runs
cmd package.
Test: manual
Change-Id: Ie103e3bdaa5b706796cc329254f2638151a3924f
2017-10-30 14:19:32 -07:00
|
|
|
ALOGW("Could not start iteration over %s: %s", mFileName != NULL ? mFileName : "<null>",
|
|
|
|
ErrorCodeString(error));
|
2013-12-03 13:16:03 +00:00
|
|
|
delete ze;
|
|
|
|
return false;
|
2013-05-06 20:20:34 -07:00
|
|
|
}
|
|
|
|
|
2013-12-03 13:16:03 +00:00
|
|
|
*cookie = ze;
|
|
|
|
return true;
|
2013-05-06 20:20:34 -07:00
|
|
|
}
|
|
|
|
|
2013-12-03 13:16:03 +00:00
|
|
|
ZipEntryRO ZipFileRO::nextEntry(void* cookie)
|
2013-05-06 20:20:34 -07:00
|
|
|
{
|
2013-12-03 13:16:03 +00:00
|
|
|
_ZipEntryRO* ze = reinterpret_cast<_ZipEntryRO*>(cookie);
|
|
|
|
int32_t error = Next(ze->cookie, &(ze->entry), &(ze->name));
|
|
|
|
if (error) {
|
|
|
|
if (error != -1) {
|
Fully implement "install" and "install-write" in PackageManagerShellCommand.
We can use the new mechanism to ask the calling shell to open
a file in order to implement the rest of these commands, allowing
you to give the path to an apk to install. That API is thus
extended to allow you to open readable files, not just opening
file for writing.
Doing this however means we no longer can pass a file path to
AssetManager for the apk to parse, we only have an already open
fd for that. Extending AssetManager to allow adding apks from
fds is not that hard, however, since the underlying zip library
already supports this.
This main thing this changes is in AssetManager.cpp where we
retrieve the open zip file for a particular apk that has been
added. This used to look up the zip file by path every time
it was needed, but that won't work anymore now that we can have
things added by fd. Instead, we keep track of each opened zip
in the AssetManager, so we can just directly retrieve it from
the asset_path representing the item that was added. As a
side-effect, this means for normal paths we no longer need to
look up by name, but just have the opened zip file directly
accessible. (This is probably good, but it does mean that we
no longer run the logic of seeing if the zip file's timestamp
has changed and re-opening it if it has. We probably shouldn't
be relying on that for an active AssetManager anyway, and maybe
it is even good that we don't allow the zip file to change
under it?)
A follow-up change will finally remove the Pm.java implementation
and turn the pm "command" into a simple shell script that runs
cmd package.
Test: manual
Change-Id: Ie103e3bdaa5b706796cc329254f2638151a3924f
2017-10-30 14:19:32 -07:00
|
|
|
ALOGW("Error iteration over %s: %s", mFileName != NULL ? mFileName : "<null>",
|
|
|
|
ErrorCodeString(error));
|
2013-05-06 20:20:34 -07:00
|
|
|
}
|
2013-12-03 13:16:03 +00:00
|
|
|
return NULL;
|
2013-05-06 20:20:34 -07:00
|
|
|
}
|
|
|
|
|
2013-12-03 13:16:03 +00:00
|
|
|
return &(ze->entry);
|
2013-05-06 20:20:34 -07:00
|
|
|
}
|
|
|
|
|
2013-12-03 13:16:03 +00:00
|
|
|
void ZipFileRO::endIteration(void* cookie)
|
2013-05-06 20:20:34 -07:00
|
|
|
{
|
2013-12-03 13:16:03 +00:00
|
|
|
delete reinterpret_cast<_ZipEntryRO*>(cookie);
|
2013-05-06 20:20:34 -07:00
|
|
|
}
|
|
|
|
|
2013-12-03 13:16:03 +00:00
|
|
|
void ZipFileRO::releaseEntry(ZipEntryRO entry) const
|
2013-05-06 20:20:34 -07:00
|
|
|
{
|
2013-12-03 13:16:03 +00:00
|
|
|
delete reinterpret_cast<_ZipEntryRO*>(entry);
|
2013-05-06 20:20:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy the entry's filename to the buffer.
|
|
|
|
*/
|
2015-06-16 12:02:57 +01:00
|
|
|
int ZipFileRO::getEntryFileName(ZipEntryRO entry, char* buffer, size_t bufLen)
|
2013-05-06 20:20:34 -07:00
|
|
|
const
|
|
|
|
{
|
2013-12-03 13:16:03 +00:00
|
|
|
const _ZipEntryRO* zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
|
2019-06-14 15:28:38 -07:00
|
|
|
const uint16_t requiredSize = zipEntry->name.length() + 1;
|
2013-05-06 20:20:34 -07:00
|
|
|
|
2013-12-03 13:16:03 +00:00
|
|
|
if (bufLen < requiredSize) {
|
|
|
|
ALOGW("Buffer too short, requires %d bytes for entry name", requiredSize);
|
|
|
|
return requiredSize;
|
|
|
|
}
|
|
|
|
|
2019-06-14 15:28:38 -07:00
|
|
|
memcpy(buffer, zipEntry->name.data(), requiredSize - 1);
|
2013-12-03 13:16:03 +00:00
|
|
|
buffer[requiredSize - 1] = '\0';
|
2013-05-06 20:20:34 -07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a new FileMap object that spans the data in "entry".
|
|
|
|
*/
|
|
|
|
FileMap* ZipFileRO::createEntryFileMap(ZipEntryRO entry) const
|
|
|
|
{
|
2013-12-03 13:16:03 +00:00
|
|
|
const _ZipEntryRO *zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
|
|
|
|
const ZipEntry& ze = zipEntry->entry;
|
|
|
|
int fd = GetFileDescriptor(mHandle);
|
|
|
|
size_t actualLen = 0;
|
2013-08-21 10:40:16 -07:00
|
|
|
|
2013-12-03 13:16:03 +00:00
|
|
|
if (ze.method == kCompressStored) {
|
|
|
|
actualLen = ze.uncompressed_length;
|
2013-08-21 10:40:16 -07:00
|
|
|
} else {
|
2013-12-03 13:16:03 +00:00
|
|
|
actualLen = ze.compressed_length;
|
2013-08-21 10:40:16 -07:00
|
|
|
}
|
2013-05-06 20:20:34 -07:00
|
|
|
|
2013-12-03 13:16:03 +00:00
|
|
|
FileMap* newMap = new FileMap();
|
|
|
|
if (!newMap->create(mFileName, fd, ze.offset, actualLen, true)) {
|
2015-02-23 15:47:54 +00:00
|
|
|
delete newMap;
|
2013-05-06 20:20:34 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return newMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Uncompress an entry, in its entirety, into the provided output buffer.
|
|
|
|
*
|
|
|
|
* This doesn't verify the data's CRC, which might be useful for
|
|
|
|
* uncompressed data. The caller should be able to manage it.
|
|
|
|
*/
|
2013-12-03 13:16:03 +00:00
|
|
|
bool ZipFileRO::uncompressEntry(ZipEntryRO entry, void* buffer, size_t size) const
|
2013-05-06 20:20:34 -07:00
|
|
|
{
|
2013-12-03 13:16:03 +00:00
|
|
|
_ZipEntryRO *zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
|
|
|
|
const int32_t error = ExtractToMemory(mHandle, &(zipEntry->entry),
|
|
|
|
(uint8_t*) buffer, size);
|
|
|
|
if (error) {
|
|
|
|
ALOGW("ExtractToMemory failed with %s", ErrorCodeString(error));
|
2013-08-21 10:40:16 -07:00
|
|
|
return false;
|
|
|
|
}
|
2013-05-06 20:20:34 -07:00
|
|
|
|
2013-12-03 13:16:03 +00:00
|
|
|
return true;
|
2013-05-06 20:20:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Uncompress an entry, in its entirety, to an open file descriptor.
|
|
|
|
*
|
|
|
|
* This doesn't verify the data's CRC, but probably should.
|
|
|
|
*/
|
|
|
|
bool ZipFileRO::uncompressEntry(ZipEntryRO entry, int fd) const
|
|
|
|
{
|
2013-12-03 13:16:03 +00:00
|
|
|
_ZipEntryRO *zipEntry = reinterpret_cast<_ZipEntryRO*>(entry);
|
|
|
|
const int32_t error = ExtractEntryToFile(mHandle, &(zipEntry->entry), fd);
|
|
|
|
if (error) {
|
|
|
|
ALOGW("ExtractToMemory failed with %s", ErrorCodeString(error));
|
2013-08-21 10:40:16 -07:00
|
|
|
return false;
|
|
|
|
}
|
2013-05-06 20:20:34 -07:00
|
|
|
|
2013-12-03 13:16:03 +00:00
|
|
|
return true;
|
2013-05-06 20:20:34 -07:00
|
|
|
}
|