donut snapshot
diff --git a/libs/utils/Android.mk b/libs/utils/Android.mk
index f9fb780..9bdd64a 100644
--- a/libs/utils/Android.mk
+++ b/libs/utils/Android.mk
@@ -117,7 +117,8 @@
IPermissionController.cpp \
IServiceManager.cpp \
Unicode.cpp \
- file_backup_helper.cpp
+ BackupData.cpp \
+ BackupHelpers.cpp
ifeq ($(TARGET_SIMULATOR),true)
LOCAL_SRC_FILES += $(hostSources)
diff --git a/libs/utils/Asset.cpp b/libs/utils/Asset.cpp
index 91203dd..23cb72d 100644
--- a/libs/utils/Asset.cpp
+++ b/libs/utils/Asset.cpp
@@ -582,11 +582,14 @@
if ((((size_t)data)&0x3) == 0) {
// We can return this directly if it is aligned on a word
// boundary.
+ LOGV("Returning aligned FileAsset %p (%s).", this,
+ getAssetSource());
return data;
}
// If not aligned on a word boundary, then we need to copy it into
// our own buffer.
- LOGV("Copying FileAsset %p to buffer size %d to make it aligned.", this, (int)mLength);
+ LOGV("Copying FileAsset %p (%s) to buffer size %d to make it aligned.", this,
+ getAssetSource(), (int)mLength);
unsigned char* buf = new unsigned char[mLength];
if (buf == NULL) {
LOGE("alloc of %ld bytes failed\n", (long) mLength);
diff --git a/libs/utils/AssetManager.cpp b/libs/utils/AssetManager.cpp
index 447b801..5a05e6a 100644
--- a/libs/utils/AssetManager.cpp
+++ b/libs/utils/AssetManager.cpp
@@ -395,21 +395,41 @@
const size_t N = mAssetPaths.size();
for (size_t i=0; i<N; i++) {
Asset* ass = NULL;
+ ResTable* sharedRes = NULL;
bool shared = true;
const asset_path& ap = mAssetPaths.itemAt(i);
LOGV("Looking for resource asset in '%s'\n", ap.path.string());
if (ap.type != kFileTypeDirectory) {
- ass = const_cast<AssetManager*>(this)->
- mZipSet.getZipResourceTable(ap.path);
- if (ass == NULL) {
- LOGV("loading resource table %s\n", ap.path.string());
+ if (i == 0) {
+ // The first item is typically the framework resources,
+ // which we want to avoid parsing every time.
+ sharedRes = const_cast<AssetManager*>(this)->
+ mZipSet.getZipResourceTable(ap.path);
+ }
+ if (sharedRes == NULL) {
ass = const_cast<AssetManager*>(this)->
- openNonAssetInPathLocked("resources.arsc",
- Asset::ACCESS_BUFFER,
- ap);
- if (ass != NULL && ass != kExcludedAsset) {
+ mZipSet.getZipResourceTableAsset(ap.path);
+ if (ass == NULL) {
+ LOGV("loading resource table %s\n", ap.path.string());
ass = const_cast<AssetManager*>(this)->
- mZipSet.setZipResourceTable(ap.path, ass);
+ openNonAssetInPathLocked("resources.arsc",
+ Asset::ACCESS_BUFFER,
+ ap);
+ if (ass != NULL && ass != kExcludedAsset) {
+ ass = const_cast<AssetManager*>(this)->
+ mZipSet.setZipResourceTableAsset(ap.path, ass);
+ }
+ }
+
+ if (i == 0 && ass != NULL) {
+ // If this is the first resource table in the asset
+ // manager, then we are going to cache it so that we
+ // can quickly copy it out for others.
+ LOGV("Creating shared resources for %s", ap.path.string());
+ sharedRes = new ResTable();
+ sharedRes->add(ass, (void*)(i+1), false);
+ sharedRes = const_cast<AssetManager*>(this)->
+ mZipSet.setZipResourceTable(ap.path, sharedRes);
}
}
} else {
@@ -420,13 +440,19 @@
ap);
shared = false;
}
- if (ass != NULL && ass != kExcludedAsset) {
+ if ((ass != NULL || sharedRes != NULL) && ass != kExcludedAsset) {
if (rt == NULL) {
mResources = rt = new ResTable();
updateResourceParamsLocked();
}
LOGV("Installing resource asset %p in to table %p\n", ass, mResources);
- rt->add(ass, (void*)(i+1), !shared);
+ if (sharedRes != NULL) {
+ LOGV("Copying existing resources for %s", ap.path.string());
+ rt->add(sharedRes);
+ } else {
+ LOGV("Parsing resources for %s", ap.path.string());
+ rt->add(ass, (void*)(i+1), !shared);
+ }
if (!shared) {
delete ass;
@@ -901,6 +927,60 @@
}
/*
+ * Open a directory in the non-asset namespace.
+ *
+ * An "asset directory" is simply the combination of all files in all
+ * locations, with ".gz" stripped for loose files. With app, locale, and
+ * vendor defined, we have 8 directories and 2 Zip archives to scan.
+ *
+ * Pass in "" for the root dir.
+ */
+AssetDir* AssetManager::openNonAssetDir(void* cookie, const char* dirName)
+{
+ AutoMutex _l(mLock);
+
+ AssetDir* pDir = NULL;
+ SortedVector<AssetDir::FileInfo>* pMergedInfo = NULL;
+
+ LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
+ assert(dirName != NULL);
+
+ //printf("+++ openDir(%s) in '%s'\n", dirName, (const char*) mAssetBase);
+
+ if (mCacheMode != CACHE_OFF && !mCacheValid)
+ loadFileNameCacheLocked();
+
+ pDir = new AssetDir;
+
+ pMergedInfo = new SortedVector<AssetDir::FileInfo>;
+
+ const size_t which = ((size_t)cookie)-1;
+
+ if (which < mAssetPaths.size()) {
+ const asset_path& ap = mAssetPaths.itemAt(which);
+ if (ap.type == kFileTypeRegular) {
+ LOGV("Adding directory %s from zip %s", dirName, ap.path.string());
+ scanAndMergeZipLocked(pMergedInfo, ap, NULL, dirName);
+ } else {
+ LOGV("Adding directory %s from dir %s", dirName, ap.path.string());
+ scanAndMergeDirLocked(pMergedInfo, ap, NULL, dirName);
+ }
+ }
+
+#if 0
+ printf("FILE LIST:\n");
+ for (i = 0; i < (size_t) pMergedInfo->size(); i++) {
+ printf(" %d: (%d) '%s'\n", i,
+ pMergedInfo->itemAt(i).getFileType(),
+ (const char*) pMergedInfo->itemAt(i).getFileName());
+ }
+#endif
+
+ pDir->setFileList(pMergedInfo);
+ return pDir;
+}
+
+/*
* Scan the contents of the specified directory and merge them into the
* "pMergedInfo" vector, removing previous entries if we find "exclude"
* directives.
@@ -1143,6 +1223,7 @@
LOGE("ARGH: name too long?\n");
continue;
}
+ //printf("Comparing %s in %s?\n", nameBuf, dirName.string());
if (dirNameLen == 0 ||
(strncmp(nameBuf, dirName.string(), dirNameLen) == 0 &&
nameBuf[dirNameLen] == '/'))
@@ -1165,7 +1246,7 @@
createZipSourceNameLocked(zipName, dirName, info.getFileName()));
contents.add(info);
- //printf("FOUND: file '%s'\n", (const char*) info.mFileName);
+ //printf("FOUND: file '%s'\n", info.getFileName().string());
} else {
/* this is a subdir; add it if we don't already have it*/
String8 subdirName(cp, nextSlash - cp);
@@ -1181,7 +1262,7 @@
dirs.add(subdirName);
}
- //printf("FOUND: dir '%s'\n", (const char*) subdirName);
+ //printf("FOUND: dir '%s'\n", subdirName.string());
}
}
}
@@ -1455,7 +1536,8 @@
DefaultKeyedVector<String8, wp<AssetManager::SharedZip> > AssetManager::SharedZip::gOpen;
AssetManager::SharedZip::SharedZip(const String8& path, time_t modWhen)
- : mPath(path), mZipFile(NULL), mModWhen(modWhen), mResourceTableAsset(NULL)
+ : mPath(path), mZipFile(NULL), mModWhen(modWhen),
+ mResourceTableAsset(NULL), mResourceTable(NULL)
{
//LOGI("Creating SharedZip %p %s\n", this, (const char*)mPath);
mZipFile = new ZipFileRO;
@@ -1508,6 +1590,25 @@
return mResourceTableAsset;
}
+ResTable* AssetManager::SharedZip::getResourceTable()
+{
+ LOGV("Getting from SharedZip %p resource table %p\n", this, mResourceTable);
+ return mResourceTable;
+}
+
+ResTable* AssetManager::SharedZip::setResourceTable(ResTable* res)
+{
+ {
+ AutoMutex _l(gLock);
+ if (mResourceTable == NULL) {
+ mResourceTable = res;
+ return res;
+ }
+ }
+ delete res;
+ return mResourceTable;
+}
+
bool AssetManager::SharedZip::isUpToDate()
{
time_t modWhen = getFileModDate(mPath.string());
@@ -1517,6 +1618,9 @@
AssetManager::SharedZip::~SharedZip()
{
//LOGI("Destroying SharedZip %p %s\n", this, (const char*)mPath);
+ if (mResourceTable != NULL) {
+ delete mResourceTable;
+ }
if (mResourceTableAsset != NULL) {
delete mResourceTableAsset;
}
@@ -1572,7 +1676,7 @@
return zip->getZip();
}
-Asset* AssetManager::ZipSet::getZipResourceTable(const String8& path)
+Asset* AssetManager::ZipSet::getZipResourceTableAsset(const String8& path)
{
int idx = getIndex(path);
sp<SharedZip> zip = mZipFile[idx];
@@ -1583,7 +1687,7 @@
return zip->getResourceTableAsset();
}
-Asset* AssetManager::ZipSet::setZipResourceTable(const String8& path,
+Asset* AssetManager::ZipSet::setZipResourceTableAsset(const String8& path,
Asset* asset)
{
int idx = getIndex(path);
@@ -1592,6 +1696,26 @@
return zip->setResourceTableAsset(asset);
}
+ResTable* AssetManager::ZipSet::getZipResourceTable(const String8& path)
+{
+ int idx = getIndex(path);
+ sp<SharedZip> zip = mZipFile[idx];
+ if (zip == NULL) {
+ zip = SharedZip::get(path);
+ mZipFile.editItemAt(idx) = zip;
+ }
+ return zip->getResourceTable();
+}
+
+ResTable* AssetManager::ZipSet::setZipResourceTable(const String8& path,
+ ResTable* res)
+{
+ int idx = getIndex(path);
+ sp<SharedZip> zip = mZipFile[idx];
+ // doesn't make sense to call before previously accessing.
+ return zip->setResourceTable(res);
+}
+
/*
* Generate the partial pathname for the specified archive. The caller
* gets to prepend the asset root directory.
diff --git a/libs/utils/BackupData.cpp b/libs/utils/BackupData.cpp
new file mode 100644
index 0000000..cce754a
--- /dev/null
+++ b/libs/utils/BackupData.cpp
@@ -0,0 +1,349 @@
+/*
+ * Copyright (C) 2009 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.
+ */
+
+#define LOG_TAG "backup_data"
+
+#include <utils/BackupHelpers.h>
+#include <utils/ByteOrder.h>
+
+#include <stdio.h>
+#include <unistd.h>
+
+#include <cutils/log.h>
+
+namespace android {
+
+/*
+ * File Format (v1):
+ *
+ * All ints are stored little-endian.
+ *
+ * - An app_header_v1 struct.
+ * - The name of the package, utf-8, null terminated, padded to 4-byte boundary.
+ * - A sequence of zero or more key/value paires (entities), each with
+ * - A entity_header_v1 struct
+ * - The key, utf-8, null terminated, padded to 4-byte boundary.
+ * - The value, padded to 4 byte boundary
+ */
+
+const static int ROUND_UP[4] = { 0, 3, 2, 1 };
+
+static inline size_t
+round_up(size_t n)
+{
+ return n + ROUND_UP[n % 4];
+}
+
+static inline size_t
+padding_extra(size_t n)
+{
+ return ROUND_UP[n % 4];
+}
+
+BackupDataWriter::BackupDataWriter(int fd)
+ :m_fd(fd),
+ m_status(NO_ERROR),
+ m_pos(0),
+ m_entityCount(0)
+{
+}
+
+BackupDataWriter::~BackupDataWriter()
+{
+}
+
+// Pad out anything they've previously written to the next 4 byte boundary.
+status_t
+BackupDataWriter::write_padding_for(int n)
+{
+ ssize_t amt;
+ ssize_t paddingSize;
+
+ paddingSize = padding_extra(n);
+ if (paddingSize > 0) {
+ uint32_t padding = 0xbcbcbcbc;
+ amt = write(m_fd, &padding, paddingSize);
+ if (amt != paddingSize) {
+ m_status = errno;
+ return m_status;
+ }
+ m_pos += amt;
+ }
+ return NO_ERROR;
+}
+
+status_t
+BackupDataWriter::WriteEntityHeader(const String8& key, size_t dataSize)
+{
+ if (m_status != NO_ERROR) {
+ return m_status;
+ }
+
+ ssize_t amt;
+
+ amt = write_padding_for(m_pos);
+ if (amt != 0) {
+ return amt;
+ }
+
+ String8 k;
+ if (m_keyPrefix.length() > 0) {
+ k = m_keyPrefix;
+ k += ":";
+ k += key;
+ } else {
+ k = key;
+ }
+ if (true) {
+ LOGD("Writing entity: prefix='%s' key='%s' dataSize=%d", m_keyPrefix.string(), key.string(),
+ dataSize);
+ }
+
+ entity_header_v1 header;
+ ssize_t keyLen;
+
+ keyLen = k.length();
+
+ header.type = tolel(BACKUP_HEADER_ENTITY_V1);
+ header.keyLen = tolel(keyLen);
+ header.dataSize = tolel(dataSize);
+
+ amt = write(m_fd, &header, sizeof(entity_header_v1));
+ if (amt != sizeof(entity_header_v1)) {
+ m_status = errno;
+ return m_status;
+ }
+ m_pos += amt;
+
+ amt = write(m_fd, k.string(), keyLen+1);
+ if (amt != keyLen+1) {
+ m_status = errno;
+ return m_status;
+ }
+ m_pos += amt;
+
+ amt = write_padding_for(keyLen+1);
+
+ m_entityCount++;
+
+ return amt;
+}
+
+status_t
+BackupDataWriter::WriteEntityData(const void* data, size_t size)
+{
+ if (m_status != NO_ERROR) {
+ return m_status;
+ }
+
+ // We don't write padding here, because they're allowed to call this several
+ // times with smaller buffers. We write it at the end of WriteEntityHeader
+ // instead.
+ ssize_t amt = write(m_fd, data, size);
+ if (amt != (ssize_t)size) {
+ m_status = errno;
+ return m_status;
+ }
+ m_pos += amt;
+ return NO_ERROR;
+}
+
+void
+BackupDataWriter::SetKeyPrefix(const String8& keyPrefix)
+{
+ m_keyPrefix = keyPrefix;
+}
+
+
+BackupDataReader::BackupDataReader(int fd)
+ :m_fd(fd),
+ m_done(false),
+ m_status(NO_ERROR),
+ m_pos(0),
+ m_entityCount(0)
+{
+ memset(&m_header, 0, sizeof(m_header));
+}
+
+BackupDataReader::~BackupDataReader()
+{
+}
+
+status_t
+BackupDataReader::Status()
+{
+ return m_status;
+}
+
+#define CHECK_SIZE(actual, expected) \
+ do { \
+ if ((actual) != (expected)) { \
+ if ((actual) == 0) { \
+ m_status = EIO; \
+ } else { \
+ m_status = errno; \
+ } \
+ return m_status; \
+ } \
+ } while(0)
+#define SKIP_PADDING() \
+ do { \
+ status_t err = skip_padding(); \
+ if (err != NO_ERROR) { \
+ m_status = err; \
+ return err; \
+ } \
+ } while(0)
+
+status_t
+BackupDataReader::ReadNextHeader(bool* done, int* type)
+{
+ *done = m_done;
+ if (m_status != NO_ERROR) {
+ return m_status;
+ }
+
+ int amt;
+
+ // No error checking here, in case we're at the end of the stream. Just let read() fail.
+ skip_padding();
+ amt = read(m_fd, &m_header, sizeof(m_header));
+ *done = m_done = (amt == 0);
+ CHECK_SIZE(amt, sizeof(m_header));
+ m_pos += sizeof(m_header);
+ if (type) {
+ *type = m_header.type;
+ }
+
+ // validate and fix up the fields.
+ m_header.type = fromlel(m_header.type);
+ switch (m_header.type)
+ {
+ case BACKUP_HEADER_ENTITY_V1:
+ {
+ m_header.entity.keyLen = fromlel(m_header.entity.keyLen);
+ if (m_header.entity.keyLen <= 0) {
+ LOGD("Entity header at %d has keyLen<=0: 0x%08x\n", (int)m_pos,
+ (int)m_header.entity.keyLen);
+ m_status = EINVAL;
+ }
+ m_header.entity.dataSize = fromlel(m_header.entity.dataSize);
+ m_entityCount++;
+
+ // read the rest of the header (filename)
+ size_t size = m_header.entity.keyLen;
+ char* buf = m_key.lockBuffer(size);
+ if (buf == NULL) {
+ m_status = ENOMEM;
+ return m_status;
+ }
+ int amt = read(m_fd, buf, size+1);
+ CHECK_SIZE(amt, (int)size+1);
+ m_key.unlockBuffer(size);
+ m_pos += size+1;
+ SKIP_PADDING();
+ m_dataEndPos = m_pos + m_header.entity.dataSize;
+
+ break;
+ }
+ default:
+ LOGD("Chunk header at %d has invalid type: 0x%08x", (int)m_pos, (int)m_header.type);
+ m_status = EINVAL;
+ }
+
+ return m_status;
+}
+
+bool
+BackupDataReader::HasEntities()
+{
+ return m_status == NO_ERROR && m_header.type == BACKUP_HEADER_ENTITY_V1;
+}
+
+status_t
+BackupDataReader::ReadEntityHeader(String8* key, size_t* dataSize)
+{
+ if (m_status != NO_ERROR) {
+ return m_status;
+ }
+ if (m_header.type != BACKUP_HEADER_ENTITY_V1) {
+ return EINVAL;
+ }
+ *key = m_key;
+ *dataSize = m_header.entity.dataSize;
+ return NO_ERROR;
+}
+
+status_t
+BackupDataReader::SkipEntityData()
+{
+ if (m_status != NO_ERROR) {
+ return m_status;
+ }
+ if (m_header.type != BACKUP_HEADER_ENTITY_V1) {
+ return EINVAL;
+ }
+ if (m_header.entity.dataSize > 0) {
+ int pos = lseek(m_fd, m_dataEndPos, SEEK_SET);
+ return pos == -1 ? (int)errno : (int)NO_ERROR;
+ } else {
+ return NO_ERROR;
+ }
+}
+
+ssize_t
+BackupDataReader::ReadEntityData(void* data, size_t size)
+{
+ if (m_status != NO_ERROR) {
+ return -1;
+ }
+ int remaining = m_dataEndPos - m_pos;
+ //LOGD("ReadEntityData size=%d m_pos=0x%x m_dataEndPos=0x%x remaining=%d\n",
+ // size, m_pos, m_dataEndPos, remaining);
+ if (remaining <= 0) {
+ return 0;
+ }
+ if (((int)size) > remaining) {
+ size = remaining;
+ }
+ //LOGD(" reading %d bytes", size);
+ int amt = read(m_fd, data, size);
+ if (amt < 0) {
+ m_status = errno;
+ return -1;
+ }
+ m_pos += amt;
+ return amt;
+}
+
+status_t
+BackupDataReader::skip_padding()
+{
+ ssize_t amt;
+ ssize_t paddingSize;
+
+ paddingSize = padding_extra(m_pos);
+ if (paddingSize > 0) {
+ uint32_t padding;
+ amt = read(m_fd, &padding, paddingSize);
+ CHECK_SIZE(amt, paddingSize);
+ m_pos += amt;
+ }
+ return NO_ERROR;
+}
+
+
+} // namespace android
diff --git a/libs/utils/BackupHelpers.cpp b/libs/utils/BackupHelpers.cpp
new file mode 100644
index 0000000..4ad9b51
--- /dev/null
+++ b/libs/utils/BackupHelpers.cpp
@@ -0,0 +1,1314 @@
+/*
+ * Copyright (C) 2009 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.
+ */
+
+#define LOG_TAG "file_backup_helper"
+
+#include <utils/BackupHelpers.h>
+
+#include <utils/KeyedVector.h>
+#include <utils/ByteOrder.h>
+#include <utils/String8.h>
+
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/uio.h>
+#include <sys/stat.h>
+#include <sys/time.h> // for utimes
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <utime.h>
+#include <fcntl.h>
+#include <zlib.h>
+
+#include <cutils/log.h>
+
+namespace android {
+
+#define MAGIC0 0x70616e53 // Snap
+#define MAGIC1 0x656c6946 // File
+
+/*
+ * File entity data format (v1):
+ *
+ * - 4-byte version number of the metadata, little endian (0x00000001 for v1)
+ * - 12 bytes of metadata
+ * - the file data itself
+ *
+ * i.e. a 16-byte metadata header followed by the raw file data. If the
+ * restore code does not recognize the metadata version, it can still
+ * interpret the file data itself correctly.
+ *
+ * file_metadata_v1:
+ *
+ * - 4 byte version number === 0x00000001 (little endian)
+ * - 4-byte access mode (little-endian)
+ * - undefined (8 bytes)
+ */
+
+struct file_metadata_v1 {
+ int version;
+ int mode;
+ int undefined_1;
+ int undefined_2;
+};
+
+const static int CURRENT_METADATA_VERSION = 1;
+
+#if 1
+#define LOGP(f, x...)
+#else
+#if TEST_BACKUP_HELPERS
+#define LOGP(f, x...) printf(f "\n", x)
+#else
+#define LOGP(x...) LOGD(x)
+#endif
+#endif
+
+const static int ROUND_UP[4] = { 0, 3, 2, 1 };
+
+static inline int
+round_up(int n)
+{
+ return n + ROUND_UP[n % 4];
+}
+
+static int
+read_snapshot_file(int fd, KeyedVector<String8,FileState>* snapshot)
+{
+ int bytesRead = 0;
+ int amt;
+ SnapshotHeader header;
+
+ amt = read(fd, &header, sizeof(header));
+ if (amt != sizeof(header)) {
+ return errno;
+ }
+ bytesRead += amt;
+
+ if (header.magic0 != MAGIC0 || header.magic1 != MAGIC1) {
+ LOGW("read_snapshot_file header.magic0=0x%08x magic1=0x%08x", header.magic0, header.magic1);
+ return 1;
+ }
+
+ for (int i=0; i<header.fileCount; i++) {
+ FileState file;
+ char filenameBuf[128];
+
+ amt = read(fd, &file, sizeof(FileState));
+ if (amt != sizeof(FileState)) {
+ LOGW("read_snapshot_file FileState truncated/error with read at %d bytes\n", bytesRead);
+ return 1;
+ }
+ bytesRead += amt;
+
+ // filename is not NULL terminated, but it is padded
+ int nameBufSize = round_up(file.nameLen);
+ char* filename = nameBufSize <= (int)sizeof(filenameBuf)
+ ? filenameBuf
+ : (char*)malloc(nameBufSize);
+ amt = read(fd, filename, nameBufSize);
+ if (amt == nameBufSize) {
+ snapshot->add(String8(filename, file.nameLen), file);
+ }
+ bytesRead += amt;
+ if (filename != filenameBuf) {
+ free(filename);
+ }
+ if (amt != nameBufSize) {
+ LOGW("read_snapshot_file filename truncated/error with read at %d bytes\n", bytesRead);
+ return 1;
+ }
+ }
+
+ if (header.totalSize != bytesRead) {
+ LOGW("read_snapshot_file length mismatch: header.totalSize=%d bytesRead=%d\n",
+ header.totalSize, bytesRead);
+ return 1;
+ }
+
+ return 0;
+}
+
+static int
+write_snapshot_file(int fd, const KeyedVector<String8,FileRec>& snapshot)
+{
+ int fileCount = 0;
+ int bytesWritten = sizeof(SnapshotHeader);
+ // preflight size
+ const int N = snapshot.size();
+ for (int i=0; i<N; i++) {
+ const FileRec& g = snapshot.valueAt(i);
+ if (!g.deleted) {
+ const String8& name = snapshot.keyAt(i);
+ bytesWritten += sizeof(FileState) + round_up(name.length());
+ fileCount++;
+ }
+ }
+
+ LOGP("write_snapshot_file fd=%d\n", fd);
+
+ int amt;
+ SnapshotHeader header = { MAGIC0, fileCount, MAGIC1, bytesWritten };
+
+ amt = write(fd, &header, sizeof(header));
+ if (amt != sizeof(header)) {
+ LOGW("write_snapshot_file error writing header %s", strerror(errno));
+ return errno;
+ }
+
+ for (int i=0; i<N; i++) {
+ FileRec r = snapshot.valueAt(i);
+ if (!r.deleted) {
+ const String8& name = snapshot.keyAt(i);
+ int nameLen = r.s.nameLen = name.length();
+
+ amt = write(fd, &r.s, sizeof(FileState));
+ if (amt != sizeof(FileState)) {
+ LOGW("write_snapshot_file error writing header %s", strerror(errno));
+ return 1;
+ }
+
+ // filename is not NULL terminated, but it is padded
+ amt = write(fd, name.string(), nameLen);
+ if (amt != nameLen) {
+ LOGW("write_snapshot_file error writing filename %s", strerror(errno));
+ return 1;
+ }
+ int paddingLen = ROUND_UP[nameLen % 4];
+ if (paddingLen != 0) {
+ int padding = 0xabababab;
+ amt = write(fd, &padding, paddingLen);
+ if (amt != paddingLen) {
+ LOGW("write_snapshot_file error writing %d bytes of filename padding %s",
+ paddingLen, strerror(errno));
+ return 1;
+ }
+ }
+ }
+ }
+
+ return 0;
+}
+
+static int
+write_delete_file(BackupDataWriter* dataStream, const String8& key)
+{
+ LOGP("write_delete_file %s\n", key.string());
+ return dataStream->WriteEntityHeader(key, -1);
+}
+
+static int
+write_update_file(BackupDataWriter* dataStream, int fd, int mode, const String8& key,
+ char const* realFilename)
+{
+ LOGP("write_update_file %s (%s) : mode 0%o\n", realFilename, key.string(), mode);
+
+ const int bufsize = 4*1024;
+ int err;
+ int amt;
+ int fileSize;
+ int bytesLeft;
+ file_metadata_v1 metadata;
+
+ char* buf = (char*)malloc(bufsize);
+ int crc = crc32(0L, Z_NULL, 0);
+
+
+ fileSize = lseek(fd, 0, SEEK_END);
+ lseek(fd, 0, SEEK_SET);
+
+ if (sizeof(metadata) != 16) {
+ LOGE("ERROR: metadata block is the wrong size!");
+ }
+
+ bytesLeft = fileSize + sizeof(metadata);
+ err = dataStream->WriteEntityHeader(key, bytesLeft);
+ if (err != 0) {
+ free(buf);
+ return err;
+ }
+
+ // store the file metadata first
+ metadata.version = tolel(CURRENT_METADATA_VERSION);
+ metadata.mode = tolel(mode);
+ metadata.undefined_1 = metadata.undefined_2 = 0;
+ err = dataStream->WriteEntityData(&metadata, sizeof(metadata));
+ if (err != 0) {
+ free(buf);
+ return err;
+ }
+ bytesLeft -= sizeof(metadata); // bytesLeft should == fileSize now
+
+ // now store the file content
+ while ((amt = read(fd, buf, bufsize)) != 0 && bytesLeft > 0) {
+ bytesLeft -= amt;
+ if (bytesLeft < 0) {
+ amt += bytesLeft; // Plus a negative is minus. Don't write more than we promised.
+ }
+ err = dataStream->WriteEntityData(buf, amt);
+ if (err != 0) {
+ free(buf);
+ return err;
+ }
+ }
+ if (bytesLeft != 0) {
+ if (bytesLeft > 0) {
+ // Pad out the space we promised in the buffer. We can't corrupt the buffer,
+ // even though the data we're sending is probably bad.
+ memset(buf, 0, bufsize);
+ while (bytesLeft > 0) {
+ amt = bytesLeft < bufsize ? bytesLeft : bufsize;
+ bytesLeft -= amt;
+ err = dataStream->WriteEntityData(buf, amt);
+ if (err != 0) {
+ free(buf);
+ return err;
+ }
+ }
+ }
+ LOGE("write_update_file size mismatch for %s. expected=%d actual=%d."
+ " You aren't doing proper locking!", realFilename, fileSize, fileSize-bytesLeft);
+ }
+
+ free(buf);
+ return NO_ERROR;
+}
+
+static int
+write_update_file(BackupDataWriter* dataStream, const String8& key, char const* realFilename)
+{
+ int err;
+ struct stat st;
+
+ err = stat(realFilename, &st);
+ if (err < 0) {
+ return errno;
+ }
+
+ int fd = open(realFilename, O_RDONLY);
+ if (fd == -1) {
+ return errno;
+ }
+
+ err = write_update_file(dataStream, fd, st.st_mode, key, realFilename);
+ close(fd);
+ return err;
+}
+
+static int
+compute_crc32(int fd)
+{
+ const int bufsize = 4*1024;
+ int amt;
+
+ char* buf = (char*)malloc(bufsize);
+ int crc = crc32(0L, Z_NULL, 0);
+
+ lseek(fd, 0, SEEK_SET);
+
+ while ((amt = read(fd, buf, bufsize)) != 0) {
+ crc = crc32(crc, (Bytef*)buf, amt);
+ }
+
+ free(buf);
+ return crc;
+}
+
+int
+back_up_files(int oldSnapshotFD, BackupDataWriter* dataStream, int newSnapshotFD,
+ char const* const* files, char const* const* keys, int fileCount)
+{
+ int err;
+ KeyedVector<String8,FileState> oldSnapshot;
+ KeyedVector<String8,FileRec> newSnapshot;
+
+ if (oldSnapshotFD != -1) {
+ err = read_snapshot_file(oldSnapshotFD, &oldSnapshot);
+ if (err != 0) {
+ // On an error, treat this as a full backup.
+ oldSnapshot.clear();
+ }
+ }
+
+ for (int i=0; i<fileCount; i++) {
+ String8 key(keys[i]);
+ FileRec r;
+ char const* file = files[i];
+ r.file = file;
+ struct stat st;
+
+ err = stat(file, &st);
+ if (err != 0) {
+ r.deleted = true;
+ } else {
+ r.deleted = false;
+ r.s.modTime_sec = st.st_mtime;
+ r.s.modTime_nsec = 0; // workaround sim breakage
+ //r.s.modTime_nsec = st.st_mtime_nsec;
+ r.s.mode = st.st_mode;
+ r.s.size = st.st_size;
+ // we compute the crc32 later down below, when we already have the file open.
+
+ if (newSnapshot.indexOfKey(key) >= 0) {
+ LOGP("back_up_files key already in use '%s'", key.string());
+ return -1;
+ }
+ }
+ newSnapshot.add(key, r);
+ }
+
+ int n = 0;
+ int N = oldSnapshot.size();
+ int m = 0;
+
+ while (n<N && m<fileCount) {
+ const String8& p = oldSnapshot.keyAt(n);
+ const String8& q = newSnapshot.keyAt(m);
+ FileRec& g = newSnapshot.editValueAt(m);
+ int cmp = p.compare(q);
+ if (g.deleted || cmp < 0) {
+ // file removed
+ LOGP("file removed: %s", p.string());
+ g.deleted = true; // They didn't mention the file, but we noticed that it's gone.
+ dataStream->WriteEntityHeader(p, -1);
+ n++;
+ }
+ else if (cmp > 0) {
+ // file added
+ LOGP("file added: %s", g.file.string());
+ write_update_file(dataStream, q, g.file.string());
+ m++;
+ }
+ else {
+ // both files exist, check them
+ const FileState& f = oldSnapshot.valueAt(n);
+
+ int fd = open(g.file.string(), O_RDONLY);
+ if (fd < 0) {
+ // We can't open the file. Don't report it as a delete either. Let the
+ // server keep the old version. Maybe they'll be able to deal with it
+ // on restore.
+ LOGP("Unable to open file %s - skipping", g.file.string());
+ } else {
+ g.s.crc32 = compute_crc32(fd);
+
+ LOGP("%s", q.string());
+ LOGP(" new: modTime=%d,%d mode=%04o size=%-3d crc32=0x%08x",
+ f.modTime_sec, f.modTime_nsec, f.mode, f.size, f.crc32);
+ LOGP(" old: modTime=%d,%d mode=%04o size=%-3d crc32=0x%08x",
+ g.s.modTime_sec, g.s.modTime_nsec, g.s.mode, g.s.size, g.s.crc32);
+ if (f.modTime_sec != g.s.modTime_sec || f.modTime_nsec != g.s.modTime_nsec
+ || f.mode != g.s.mode || f.size != g.s.size || f.crc32 != g.s.crc32) {
+ write_update_file(dataStream, fd, g.s.mode, p, g.file.string());
+ }
+
+ close(fd);
+ }
+ n++;
+ m++;
+ }
+ }
+
+ // these were deleted
+ while (n<N) {
+ dataStream->WriteEntityHeader(oldSnapshot.keyAt(n), -1);
+ n++;
+ }
+
+ // these were added
+ while (m<fileCount) {
+ const String8& q = newSnapshot.keyAt(m);
+ FileRec& g = newSnapshot.editValueAt(m);
+ write_update_file(dataStream, q, g.file.string());
+ m++;
+ }
+
+ err = write_snapshot_file(newSnapshotFD, newSnapshot);
+
+ return 0;
+}
+
+#define RESTORE_BUF_SIZE (8*1024)
+
+RestoreHelperBase::RestoreHelperBase()
+{
+ m_buf = malloc(RESTORE_BUF_SIZE);
+ m_loggedUnknownMetadata = false;
+}
+
+RestoreHelperBase::~RestoreHelperBase()
+{
+ free(m_buf);
+}
+
+status_t
+RestoreHelperBase::WriteFile(const String8& filename, BackupDataReader* in)
+{
+ ssize_t err;
+ size_t dataSize;
+ String8 key;
+ int fd;
+ void* buf = m_buf;
+ ssize_t amt;
+ int mode;
+ int crc;
+ struct stat st;
+ FileRec r;
+
+ err = in->ReadEntityHeader(&key, &dataSize);
+ if (err != NO_ERROR) {
+ return err;
+ }
+
+ // Get the metadata block off the head of the file entity and use that to
+ // set up the output file
+ file_metadata_v1 metadata;
+ amt = in->ReadEntityData(&metadata, sizeof(metadata));
+ if (amt != sizeof(metadata)) {
+ LOGW("Could not read metadata for %s -- %ld / %s", filename.string(),
+ (long)amt, strerror(errno));
+ return EIO;
+ }
+ metadata.version = fromlel(metadata.version);
+ metadata.mode = fromlel(metadata.mode);
+ if (metadata.version > CURRENT_METADATA_VERSION) {
+ if (!m_loggedUnknownMetadata) {
+ m_loggedUnknownMetadata = true;
+ LOGW("Restoring file with unsupported metadata version %d (currently %d)",
+ metadata.version, CURRENT_METADATA_VERSION);
+ }
+ }
+ mode = metadata.mode;
+
+ // Write the file and compute the crc
+ crc = crc32(0L, Z_NULL, 0);
+ fd = open(filename.string(), O_CREAT|O_RDWR|O_TRUNC, mode);
+ if (fd == -1) {
+ LOGW("Could not open file %s -- %s", filename.string(), strerror(errno));
+ return errno;
+ }
+
+ while ((amt = in->ReadEntityData(buf, RESTORE_BUF_SIZE)) > 0) {
+ err = write(fd, buf, amt);
+ if (err != amt) {
+ close(fd);
+ LOGW("Error '%s' writing '%s'", strerror(errno), filename.string());
+ return errno;
+ }
+ crc = crc32(crc, (Bytef*)buf, amt);
+ }
+
+ close(fd);
+
+ // Record for the snapshot
+ err = stat(filename.string(), &st);
+ if (err != 0) {
+ LOGW("Error stating file that we just created %s", filename.string());
+ return errno;
+ }
+
+ r.file = filename;
+ r.deleted = false;
+ r.s.modTime_sec = st.st_mtime;
+ r.s.modTime_nsec = 0; // workaround sim breakage
+ //r.s.modTime_nsec = st.st_mtime_nsec;
+ r.s.mode = st.st_mode;
+ r.s.size = st.st_size;
+ r.s.crc32 = crc;
+
+ m_files.add(key, r);
+
+ return NO_ERROR;
+}
+
+status_t
+RestoreHelperBase::WriteSnapshot(int fd)
+{
+ return write_snapshot_file(fd, m_files);;
+}
+
+#if TEST_BACKUP_HELPERS
+
+#define SCRATCH_DIR "/data/backup_helper_test/"
+
+static int
+write_text_file(const char* path, const char* data)
+{
+ int amt;
+ int fd;
+ int len;
+
+ fd = creat(path, 0666);
+ if (fd == -1) {
+ fprintf(stderr, "creat %s failed\n", path);
+ return errno;
+ }
+
+ len = strlen(data);
+ amt = write(fd, data, len);
+ if (amt != len) {
+ fprintf(stderr, "error (%s) writing to file %s\n", strerror(errno), path);
+ return errno;
+ }
+
+ close(fd);
+
+ return 0;
+}
+
+static int
+compare_file(const char* path, const unsigned char* data, int len)
+{
+ int fd;
+ int amt;
+
+ fd = open(path, O_RDONLY);
+ if (fd == -1) {
+ fprintf(stderr, "compare_file error (%s) opening %s\n", strerror(errno), path);
+ return errno;
+ }
+
+ unsigned char* contents = (unsigned char*)malloc(len);
+ if (contents == NULL) {
+ fprintf(stderr, "malloc(%d) failed\n", len);
+ return ENOMEM;
+ }
+
+ bool sizesMatch = true;
+ amt = lseek(fd, 0, SEEK_END);
+ if (amt != len) {
+ fprintf(stderr, "compare_file file length should be %d, was %d\n", len, amt);
+ sizesMatch = false;
+ }
+ lseek(fd, 0, SEEK_SET);
+
+ int readLen = amt < len ? amt : len;
+ amt = read(fd, contents, readLen);
+ if (amt != readLen) {
+ fprintf(stderr, "compare_file read expected %d bytes but got %d\n", len, amt);
+ }
+
+ bool contentsMatch = true;
+ for (int i=0; i<readLen; i++) {
+ if (data[i] != contents[i]) {
+ if (contentsMatch) {
+ fprintf(stderr, "compare_file contents are different: (index, expected, actual)\n");
+ contentsMatch = false;
+ }
+ fprintf(stderr, " [%-2d] %02x %02x\n", i, data[i], contents[i]);
+ }
+ }
+
+ free(contents);
+ return contentsMatch && sizesMatch ? 0 : 1;
+}
+
+int
+backup_helper_test_empty()
+{
+ int err;
+ int fd;
+ KeyedVector<String8,FileRec> snapshot;
+ const char* filename = SCRATCH_DIR "backup_helper_test_empty.snap";
+
+ system("rm -r " SCRATCH_DIR);
+ mkdir(SCRATCH_DIR, 0777);
+
+ // write
+ fd = creat(filename, 0666);
+ if (fd == -1) {
+ fprintf(stderr, "error creating %s\n", filename);
+ return 1;
+ }
+
+ err = write_snapshot_file(fd, snapshot);
+
+ close(fd);
+
+ if (err != 0) {
+ fprintf(stderr, "write_snapshot_file reported error %d (%s)\n", err, strerror(err));
+ return err;
+ }
+
+ static const unsigned char correct_data[] = {
+ 0x53, 0x6e, 0x61, 0x70, 0x00, 0x00, 0x00, 0x00,
+ 0x46, 0x69, 0x6c, 0x65, 0x10, 0x00, 0x00, 0x00
+ };
+
+ err = compare_file(filename, correct_data, sizeof(correct_data));
+ if (err != 0) {
+ return err;
+ }
+
+ // read
+ fd = open(filename, O_RDONLY);
+ if (fd == -1) {
+ fprintf(stderr, "error opening for read %s\n", filename);
+ return 1;
+ }
+
+ KeyedVector<String8,FileState> readSnapshot;
+ err = read_snapshot_file(fd, &readSnapshot);
+ if (err != 0) {
+ fprintf(stderr, "read_snapshot_file failed %d\n", err);
+ return err;
+ }
+
+ if (readSnapshot.size() != 0) {
+ fprintf(stderr, "readSnapshot should be length 0\n");
+ return 1;
+ }
+
+ return 0;
+}
+
+int
+backup_helper_test_four()
+{
+ int err;
+ int fd;
+ KeyedVector<String8,FileRec> snapshot;
+ const char* filename = SCRATCH_DIR "backup_helper_test_four.snap";
+
+ system("rm -r " SCRATCH_DIR);
+ mkdir(SCRATCH_DIR, 0777);
+
+ // write
+ fd = creat(filename, 0666);
+ if (fd == -1) {
+ fprintf(stderr, "error opening %s\n", filename);
+ return 1;
+ }
+
+ String8 filenames[4];
+ FileState states[4];
+ FileRec r;
+ r.deleted = false;
+
+ states[0].modTime_sec = 0xfedcba98;
+ states[0].modTime_nsec = 0xdeadbeef;
+ states[0].mode = 0777; // decimal 511, hex 0x000001ff
+ states[0].size = 0xababbcbc;
+ states[0].crc32 = 0x12345678;
+ states[0].nameLen = -12;
+ r.s = states[0];
+ filenames[0] = String8("bytes_of_padding");
+ snapshot.add(filenames[0], r);
+
+ states[1].modTime_sec = 0x93400031;
+ states[1].modTime_nsec = 0xdeadbeef;
+ states[1].mode = 0666; // decimal 438, hex 0x000001b6
+ states[1].size = 0x88557766;
+ states[1].crc32 = 0x22334422;
+ states[1].nameLen = -1;
+ r.s = states[1];
+ filenames[1] = String8("bytes_of_padding3");
+ snapshot.add(filenames[1], r);
+
+ states[2].modTime_sec = 0x33221144;
+ states[2].modTime_nsec = 0xdeadbeef;
+ states[2].mode = 0744; // decimal 484, hex 0x000001e4
+ states[2].size = 0x11223344;
+ states[2].crc32 = 0x01122334;
+ states[2].nameLen = 0;
+ r.s = states[2];
+ filenames[2] = String8("bytes_of_padding_2");
+ snapshot.add(filenames[2], r);
+
+ states[3].modTime_sec = 0x33221144;
+ states[3].modTime_nsec = 0xdeadbeef;
+ states[3].mode = 0755; // decimal 493, hex 0x000001ed
+ states[3].size = 0x11223344;
+ states[3].crc32 = 0x01122334;
+ states[3].nameLen = 0;
+ r.s = states[3];
+ filenames[3] = String8("bytes_of_padding__1");
+ snapshot.add(filenames[3], r);
+
+ err = write_snapshot_file(fd, snapshot);
+
+ close(fd);
+
+ if (err != 0) {
+ fprintf(stderr, "write_snapshot_file reported error %d (%s)\n", err, strerror(err));
+ return err;
+ }
+
+ static const unsigned char correct_data[] = {
+ // header
+ 0x53, 0x6e, 0x61, 0x70, 0x04, 0x00, 0x00, 0x00,
+ 0x46, 0x69, 0x6c, 0x65, 0xbc, 0x00, 0x00, 0x00,
+
+ // bytes_of_padding
+ 0x98, 0xba, 0xdc, 0xfe, 0xef, 0xbe, 0xad, 0xde,
+ 0xff, 0x01, 0x00, 0x00, 0xbc, 0xbc, 0xab, 0xab,
+ 0x78, 0x56, 0x34, 0x12, 0x10, 0x00, 0x00, 0x00,
+ 0x62, 0x79, 0x74, 0x65, 0x73, 0x5f, 0x6f, 0x66,
+ 0x5f, 0x70, 0x61, 0x64, 0x64, 0x69, 0x6e, 0x67,
+
+ // bytes_of_padding3
+ 0x31, 0x00, 0x40, 0x93, 0xef, 0xbe, 0xad, 0xde,
+ 0xb6, 0x01, 0x00, 0x00, 0x66, 0x77, 0x55, 0x88,
+ 0x22, 0x44, 0x33, 0x22, 0x11, 0x00, 0x00, 0x00,
+ 0x62, 0x79, 0x74, 0x65, 0x73, 0x5f, 0x6f, 0x66,
+ 0x5f, 0x70, 0x61, 0x64, 0x64, 0x69, 0x6e, 0x67,
+ 0x33, 0xab, 0xab, 0xab,
+
+ // bytes of padding2
+ 0x44, 0x11, 0x22, 0x33, 0xef, 0xbe, 0xad, 0xde,
+ 0xe4, 0x01, 0x00, 0x00, 0x44, 0x33, 0x22, 0x11,
+ 0x34, 0x23, 0x12, 0x01, 0x12, 0x00, 0x00, 0x00,
+ 0x62, 0x79, 0x74, 0x65, 0x73, 0x5f, 0x6f, 0x66,
+ 0x5f, 0x70, 0x61, 0x64, 0x64, 0x69, 0x6e, 0x67,
+ 0x5f, 0x32, 0xab, 0xab,
+
+ // bytes of padding3
+ 0x44, 0x11, 0x22, 0x33, 0xef, 0xbe, 0xad, 0xde,
+ 0xed, 0x01, 0x00, 0x00, 0x44, 0x33, 0x22, 0x11,
+ 0x34, 0x23, 0x12, 0x01, 0x13, 0x00, 0x00, 0x00,
+ 0x62, 0x79, 0x74, 0x65, 0x73, 0x5f, 0x6f, 0x66,
+ 0x5f, 0x70, 0x61, 0x64, 0x64, 0x69, 0x6e, 0x67,
+ 0x5f, 0x5f, 0x31, 0xab
+ };
+
+ err = compare_file(filename, correct_data, sizeof(correct_data));
+ if (err != 0) {
+ return err;
+ }
+
+ // read
+ fd = open(filename, O_RDONLY);
+ if (fd == -1) {
+ fprintf(stderr, "error opening for read %s\n", filename);
+ return 1;
+ }
+
+
+ KeyedVector<String8,FileState> readSnapshot;
+ err = read_snapshot_file(fd, &readSnapshot);
+ if (err != 0) {
+ fprintf(stderr, "read_snapshot_file failed %d\n", err);
+ return err;
+ }
+
+ if (readSnapshot.size() != 4) {
+ fprintf(stderr, "readSnapshot should be length 4 is %d\n", readSnapshot.size());
+ return 1;
+ }
+
+ bool matched = true;
+ for (size_t i=0; i<readSnapshot.size(); i++) {
+ const String8& name = readSnapshot.keyAt(i);
+ const FileState state = readSnapshot.valueAt(i);
+
+ if (name != filenames[i] || states[i].modTime_sec != state.modTime_sec
+ || states[i].modTime_nsec != state.modTime_nsec || states[i].mode != state.mode
+ || states[i].size != state.size || states[i].crc32 != states[i].crc32) {
+ fprintf(stderr, "state %d expected={%d/%d, 0x%08x, %04o, 0x%08x, %3d} '%s'\n"
+ " actual={%d/%d, 0x%08x, %04o, 0x%08x, %3d} '%s'\n", i,
+ states[i].modTime_sec, states[i].modTime_nsec, states[i].mode, states[i].size,
+ states[i].crc32, name.length(), filenames[i].string(),
+ state.modTime_sec, state.modTime_nsec, state.mode, state.size, state.crc32,
+ state.nameLen, name.string());
+ matched = false;
+ }
+ }
+
+ return matched ? 0 : 1;
+}
+
+// hexdump -v -e '" " 8/1 " 0x%02x," "\n"' data_writer.data
+const unsigned char DATA_GOLDEN_FILE[] = {
+ 0x44, 0x61, 0x74, 0x61, 0x0b, 0x00, 0x00, 0x00,
+ 0x0c, 0x00, 0x00, 0x00, 0x6e, 0x6f, 0x5f, 0x70,
+ 0x61, 0x64, 0x64, 0x69, 0x6e, 0x67, 0x5f, 0x00,
+ 0x6e, 0x6f, 0x5f, 0x70, 0x61, 0x64, 0x64, 0x69,
+ 0x6e, 0x67, 0x5f, 0x00, 0x44, 0x61, 0x74, 0x61,
+ 0x0c, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00,
+ 0x70, 0x61, 0x64, 0x64, 0x65, 0x64, 0x5f, 0x74,
+ 0x6f, 0x5f, 0x5f, 0x33, 0x00, 0xbc, 0xbc, 0xbc,
+ 0x70, 0x61, 0x64, 0x64, 0x65, 0x64, 0x5f, 0x74,
+ 0x6f, 0x5f, 0x5f, 0x33, 0x00, 0xbc, 0xbc, 0xbc,
+ 0x44, 0x61, 0x74, 0x61, 0x0d, 0x00, 0x00, 0x00,
+ 0x0e, 0x00, 0x00, 0x00, 0x70, 0x61, 0x64, 0x64,
+ 0x65, 0x64, 0x5f, 0x74, 0x6f, 0x5f, 0x32, 0x5f,
+ 0x5f, 0x00, 0xbc, 0xbc, 0x70, 0x61, 0x64, 0x64,
+ 0x65, 0x64, 0x5f, 0x74, 0x6f, 0x5f, 0x32, 0x5f,
+ 0x5f, 0x00, 0xbc, 0xbc, 0x44, 0x61, 0x74, 0x61,
+ 0x0a, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00,
+ 0x70, 0x61, 0x64, 0x64, 0x65, 0x64, 0x5f, 0x74,
+ 0x6f, 0x31, 0x00, 0xbc, 0x70, 0x61, 0x64, 0x64,
+ 0x65, 0x64, 0x5f, 0x74, 0x6f, 0x31, 0x00
+
+};
+const int DATA_GOLDEN_FILE_SIZE = sizeof(DATA_GOLDEN_FILE);
+
+static int
+test_write_header_and_entity(BackupDataWriter& writer, const char* str)
+{
+ int err;
+ String8 text(str);
+
+ err = writer.WriteEntityHeader(text, text.length()+1);
+ if (err != 0) {
+ fprintf(stderr, "WriteEntityHeader failed with %s\n", strerror(err));
+ return err;
+ }
+
+ err = writer.WriteEntityData(text.string(), text.length()+1);
+ if (err != 0) {
+ fprintf(stderr, "write failed for data '%s'\n", text.string());
+ return errno;
+ }
+
+ return err;
+}
+
+int
+backup_helper_test_data_writer()
+{
+ int err;
+ int fd;
+ const char* filename = SCRATCH_DIR "data_writer.data";
+
+ system("rm -r " SCRATCH_DIR);
+ mkdir(SCRATCH_DIR, 0777);
+ mkdir(SCRATCH_DIR "data", 0777);
+
+ fd = creat(filename, 0666);
+ if (fd == -1) {
+ fprintf(stderr, "error creating: %s\n", strerror(errno));
+ return errno;
+ }
+
+ BackupDataWriter writer(fd);
+
+ err = 0;
+ err |= test_write_header_and_entity(writer, "no_padding_");
+ err |= test_write_header_and_entity(writer, "padded_to__3");
+ err |= test_write_header_and_entity(writer, "padded_to_2__");
+ err |= test_write_header_and_entity(writer, "padded_to1");
+
+ close(fd);
+
+ err = compare_file(filename, DATA_GOLDEN_FILE, DATA_GOLDEN_FILE_SIZE);
+ if (err != 0) {
+ return err;
+ }
+
+ return err;
+}
+
+int
+test_read_header_and_entity(BackupDataReader& reader, const char* str)
+{
+ int err;
+ int bufSize = strlen(str)+1;
+ char* buf = (char*)malloc(bufSize);
+ String8 string;
+ int cookie = 0x11111111;
+ size_t actualSize;
+ bool done;
+ int type;
+ ssize_t nRead;
+
+ // printf("\n\n---------- test_read_header_and_entity -- %s\n\n", str);
+
+ err = reader.ReadNextHeader(&done, &type);
+ if (done) {
+ fprintf(stderr, "should not be done yet\n");
+ goto finished;
+ }
+ if (err != 0) {
+ fprintf(stderr, "ReadNextHeader (for app header) failed with %s\n", strerror(err));
+ goto finished;
+ }
+ if (type != BACKUP_HEADER_ENTITY_V1) {
+ err = EINVAL;
+ fprintf(stderr, "type=0x%08x expected 0x%08x\n", type, BACKUP_HEADER_ENTITY_V1);
+ }
+
+ err = reader.ReadEntityHeader(&string, &actualSize);
+ if (err != 0) {
+ fprintf(stderr, "ReadEntityHeader failed with %s\n", strerror(err));
+ goto finished;
+ }
+ if (string != str) {
+ fprintf(stderr, "ReadEntityHeader expected key '%s' got '%s'\n", str, string.string());
+ err = EINVAL;
+ goto finished;
+ }
+ if ((int)actualSize != bufSize) {
+ fprintf(stderr, "ReadEntityHeader expected dataSize 0x%08x got 0x%08x\n", bufSize,
+ actualSize);
+ err = EINVAL;
+ goto finished;
+ }
+
+ nRead = reader.ReadEntityData(buf, bufSize);
+ if (nRead < 0) {
+ err = reader.Status();
+ fprintf(stderr, "ReadEntityData failed with %s\n", strerror(err));
+ goto finished;
+ }
+
+ if (0 != memcmp(buf, str, bufSize)) {
+ fprintf(stderr, "ReadEntityData expected '%s' but got something starting with "
+ "%02x %02x %02x %02x '%c%c%c%c'\n", str, buf[0], buf[1], buf[2], buf[3],
+ buf[0], buf[1], buf[2], buf[3]);
+ err = EINVAL;
+ goto finished;
+ }
+
+ // The next read will confirm whether it got the right amount of data.
+
+finished:
+ if (err != NO_ERROR) {
+ fprintf(stderr, "test_read_header_and_entity failed with %s\n", strerror(err));
+ }
+ free(buf);
+ return err;
+}
+
+int
+backup_helper_test_data_reader()
+{
+ int err;
+ int fd;
+ const char* filename = SCRATCH_DIR "data_reader.data";
+
+ system("rm -r " SCRATCH_DIR);
+ mkdir(SCRATCH_DIR, 0777);
+ mkdir(SCRATCH_DIR "data", 0777);
+
+ fd = creat(filename, 0666);
+ if (fd == -1) {
+ fprintf(stderr, "error creating: %s\n", strerror(errno));
+ return errno;
+ }
+
+ err = write(fd, DATA_GOLDEN_FILE, DATA_GOLDEN_FILE_SIZE);
+ if (err != DATA_GOLDEN_FILE_SIZE) {
+ fprintf(stderr, "Error \"%s\" writing golden file %s\n", strerror(errno), filename);
+ return errno;
+ }
+
+ close(fd);
+
+ fd = open(filename, O_RDONLY);
+ if (fd == -1) {
+ fprintf(stderr, "Error \"%s\" opening golden file %s for read\n", strerror(errno),
+ filename);
+ return errno;
+ }
+
+ {
+ BackupDataReader reader(fd);
+
+ err = 0;
+
+ if (err == NO_ERROR) {
+ err = test_read_header_and_entity(reader, "no_padding_");
+ }
+
+ if (err == NO_ERROR) {
+ err = test_read_header_and_entity(reader, "padded_to__3");
+ }
+
+ if (err == NO_ERROR) {
+ err = test_read_header_and_entity(reader, "padded_to_2__");
+ }
+
+ if (err == NO_ERROR) {
+ err = test_read_header_and_entity(reader, "padded_to1");
+ }
+ }
+
+ close(fd);
+
+ return err;
+}
+
+static int
+get_mod_time(const char* filename, struct timeval times[2])
+{
+ int err;
+ struct stat64 st;
+ err = stat64(filename, &st);
+ if (err != 0) {
+ fprintf(stderr, "stat '%s' failed: %s\n", filename, strerror(errno));
+ return errno;
+ }
+ times[0].tv_sec = st.st_atime;
+ times[1].tv_sec = st.st_mtime;
+
+ // If st_atime is a macro then struct stat64 uses struct timespec
+ // to store the access and modif time values and typically
+ // st_*time_nsec is not defined. In glibc, this is controlled by
+ // __USE_MISC.
+#ifdef __USE_MISC
+#if !defined(st_atime) || defined(st_atime_nsec)
+#error "Check if this __USE_MISC conditional is still needed."
+#endif
+ times[0].tv_usec = st.st_atim.tv_nsec / 1000;
+ times[1].tv_usec = st.st_mtim.tv_nsec / 1000;
+#else
+ times[0].tv_usec = st.st_atime_nsec / 1000;
+ times[1].tv_usec = st.st_mtime_nsec / 1000;
+#endif
+
+ return 0;
+}
+
+int
+backup_helper_test_files()
+{
+ int err;
+ int oldSnapshotFD;
+ int dataStreamFD;
+ int newSnapshotFD;
+
+ system("rm -r " SCRATCH_DIR);
+ mkdir(SCRATCH_DIR, 0777);
+ mkdir(SCRATCH_DIR "data", 0777);
+
+ write_text_file(SCRATCH_DIR "data/b", "b\nbb\n");
+ write_text_file(SCRATCH_DIR "data/c", "c\ncc\n");
+ write_text_file(SCRATCH_DIR "data/d", "d\ndd\n");
+ write_text_file(SCRATCH_DIR "data/e", "e\nee\n");
+ write_text_file(SCRATCH_DIR "data/f", "f\nff\n");
+ write_text_file(SCRATCH_DIR "data/h", "h\nhh\n");
+
+ char const* files_before[] = {
+ SCRATCH_DIR "data/b",
+ SCRATCH_DIR "data/c",
+ SCRATCH_DIR "data/d",
+ SCRATCH_DIR "data/e",
+ SCRATCH_DIR "data/f"
+ };
+
+ char const* keys_before[] = {
+ "data/b",
+ "data/c",
+ "data/d",
+ "data/e",
+ "data/f"
+ };
+
+ dataStreamFD = creat(SCRATCH_DIR "1.data", 0666);
+ if (dataStreamFD == -1) {
+ fprintf(stderr, "error creating: %s\n", strerror(errno));
+ return errno;
+ }
+
+ newSnapshotFD = creat(SCRATCH_DIR "before.snap", 0666);
+ if (newSnapshotFD == -1) {
+ fprintf(stderr, "error creating: %s\n", strerror(errno));
+ return errno;
+ }
+
+ {
+ BackupDataWriter dataStream(dataStreamFD);
+
+ err = back_up_files(-1, &dataStream, newSnapshotFD, files_before, keys_before, 5);
+ if (err != 0) {
+ return err;
+ }
+ }
+
+ close(dataStreamFD);
+ close(newSnapshotFD);
+
+ sleep(3);
+
+ struct timeval d_times[2];
+ struct timeval e_times[2];
+
+ err = get_mod_time(SCRATCH_DIR "data/d", d_times);
+ err |= get_mod_time(SCRATCH_DIR "data/e", e_times);
+ if (err != 0) {
+ return err;
+ }
+
+ write_text_file(SCRATCH_DIR "data/a", "a\naa\n");
+ unlink(SCRATCH_DIR "data/c");
+ write_text_file(SCRATCH_DIR "data/c", "c\ncc\n");
+ write_text_file(SCRATCH_DIR "data/d", "dd\ndd\n");
+ utimes(SCRATCH_DIR "data/d", d_times);
+ write_text_file(SCRATCH_DIR "data/e", "z\nzz\n");
+ utimes(SCRATCH_DIR "data/e", e_times);
+ write_text_file(SCRATCH_DIR "data/g", "g\ngg\n");
+ unlink(SCRATCH_DIR "data/f");
+
+ char const* files_after[] = {
+ SCRATCH_DIR "data/a", // added
+ SCRATCH_DIR "data/b", // same
+ SCRATCH_DIR "data/c", // different mod time
+ SCRATCH_DIR "data/d", // different size (same mod time)
+ SCRATCH_DIR "data/e", // different contents (same mod time, same size)
+ SCRATCH_DIR "data/g" // added
+ };
+
+ char const* keys_after[] = {
+ "data/a", // added
+ "data/b", // same
+ "data/c", // different mod time
+ "data/d", // different size (same mod time)
+ "data/e", // different contents (same mod time, same size)
+ "data/g" // added
+ };
+
+ oldSnapshotFD = open(SCRATCH_DIR "before.snap", O_RDONLY);
+ if (oldSnapshotFD == -1) {
+ fprintf(stderr, "error opening: %s\n", strerror(errno));
+ return errno;
+ }
+
+ dataStreamFD = creat(SCRATCH_DIR "2.data", 0666);
+ if (dataStreamFD == -1) {
+ fprintf(stderr, "error creating: %s\n", strerror(errno));
+ return errno;
+ }
+
+ newSnapshotFD = creat(SCRATCH_DIR "after.snap", 0666);
+ if (newSnapshotFD == -1) {
+ fprintf(stderr, "error creating: %s\n", strerror(errno));
+ return errno;
+ }
+
+ {
+ BackupDataWriter dataStream(dataStreamFD);
+
+ err = back_up_files(oldSnapshotFD, &dataStream, newSnapshotFD, files_after, keys_after, 6);
+ if (err != 0) {
+ return err;
+ }
+}
+
+ close(oldSnapshotFD);
+ close(dataStreamFD);
+ close(newSnapshotFD);
+
+ return 0;
+}
+
+int
+backup_helper_test_null_base()
+{
+ int err;
+ int oldSnapshotFD;
+ int dataStreamFD;
+ int newSnapshotFD;
+
+ system("rm -r " SCRATCH_DIR);
+ mkdir(SCRATCH_DIR, 0777);
+ mkdir(SCRATCH_DIR "data", 0777);
+
+ write_text_file(SCRATCH_DIR "data/a", "a\naa\n");
+
+ char const* files[] = {
+ SCRATCH_DIR "data/a",
+ };
+
+ char const* keys[] = {
+ "a",
+ };
+
+ dataStreamFD = creat(SCRATCH_DIR "null_base.data", 0666);
+ if (dataStreamFD == -1) {
+ fprintf(stderr, "error creating: %s\n", strerror(errno));
+ return errno;
+ }
+
+ newSnapshotFD = creat(SCRATCH_DIR "null_base.snap", 0666);
+ if (newSnapshotFD == -1) {
+ fprintf(stderr, "error creating: %s\n", strerror(errno));
+ return errno;
+ }
+
+ {
+ BackupDataWriter dataStream(dataStreamFD);
+
+ err = back_up_files(-1, &dataStream, newSnapshotFD, files, keys, 1);
+ if (err != 0) {
+ return err;
+ }
+ }
+
+ close(dataStreamFD);
+ close(newSnapshotFD);
+
+ return 0;
+}
+
+int
+backup_helper_test_missing_file()
+{
+ int err;
+ int oldSnapshotFD;
+ int dataStreamFD;
+ int newSnapshotFD;
+
+ system("rm -r " SCRATCH_DIR);
+ mkdir(SCRATCH_DIR, 0777);
+ mkdir(SCRATCH_DIR "data", 0777);
+
+ write_text_file(SCRATCH_DIR "data/b", "b\nbb\n");
+
+ char const* files[] = {
+ SCRATCH_DIR "data/a",
+ SCRATCH_DIR "data/b",
+ SCRATCH_DIR "data/c",
+ };
+
+ char const* keys[] = {
+ "a",
+ "b",
+ "c",
+ };
+
+ dataStreamFD = creat(SCRATCH_DIR "null_base.data", 0666);
+ if (dataStreamFD == -1) {
+ fprintf(stderr, "error creating: %s\n", strerror(errno));
+ return errno;
+ }
+
+ newSnapshotFD = creat(SCRATCH_DIR "null_base.snap", 0666);
+ if (newSnapshotFD == -1) {
+ fprintf(stderr, "error creating: %s\n", strerror(errno));
+ return errno;
+ }
+
+ {
+ BackupDataWriter dataStream(dataStreamFD);
+
+ err = back_up_files(-1, &dataStream, newSnapshotFD, files, keys, 1);
+ if (err != 0) {
+ return err;
+ }
+ }
+
+ close(dataStreamFD);
+ close(newSnapshotFD);
+
+ return 0;
+}
+
+
+#endif // TEST_BACKUP_HELPERS
+
+}
diff --git a/libs/utils/characterData.h b/libs/utils/CharacterData.h
similarity index 100%
rename from libs/utils/characterData.h
rename to libs/utils/CharacterData.h
diff --git a/libs/utils/Parcel.cpp b/libs/utils/Parcel.cpp
index 0f4b647..b0e3750 100644
--- a/libs/utils/Parcel.cpp
+++ b/libs/utils/Parcel.cpp
@@ -409,12 +409,16 @@
mObjects[idx++] = off;
mObjectsSize++;
- const flat_binder_object* flat
+ flat_binder_object* flat
= reinterpret_cast<flat_binder_object*>(mData + off);
acquire_object(proc, *flat, this);
- // take note if the object is a file descriptor
if (flat->type == BINDER_TYPE_FD) {
+ // If this is a file descriptor, we need to dup it so the
+ // new Parcel now owns its own fd, and can declare that we
+ // officially know we have fds.
+ flat->handle = dup(flat->handle);
+ flat->cookie = (void*)1;
mHasFds = mFdsKnown = true;
}
}
@@ -650,28 +654,26 @@
return flatten_binder(ProcessState::self(), val, this);
}
-status_t Parcel::writeNativeHandle(const native_handle& handle)
+status_t Parcel::writeNativeHandle(const native_handle* handle)
{
- if (handle.version != sizeof(native_handle))
+ if (handle->version != sizeof(native_handle))
return BAD_TYPE;
status_t err;
- err = writeInt32(handle.numFds);
+ err = writeInt32(handle->numFds);
if (err != NO_ERROR) return err;
- err = writeInt32(handle.numInts);
+ err = writeInt32(handle->numInts);
if (err != NO_ERROR) return err;
- for (int i=0 ; err==NO_ERROR && i<handle.numFds ; i++)
- err = writeDupFileDescriptor(handle.data[i]);
+ for (int i=0 ; err==NO_ERROR && i<handle->numFds ; i++)
+ err = writeDupFileDescriptor(handle->data[i]);
if (err != NO_ERROR) {
LOGD("write native handle, write dup fd failed");
return err;
}
-
- err = write(handle.data + handle.numFds, sizeof(int)*handle.numInts);
-
+ err = write(handle->data + handle->numFds, sizeof(int)*handle->numInts);
return err;
}
@@ -928,7 +930,7 @@
}
-native_handle* Parcel::readNativeHandle(native_handle* (*alloc)(void*, int, int), void* cookie) const
+native_handle* Parcel::readNativeHandle() const
{
int numFds, numInts;
status_t err;
@@ -937,31 +939,15 @@
err = readInt32(&numInts);
if (err != NO_ERROR) return 0;
- native_handle* h;
- if (alloc == 0) {
- size_t size = sizeof(native_handle) + sizeof(int)*(numFds + numInts);
- h = (native_handle*)malloc(size);
- h->version = sizeof(native_handle);
- h->numFds = numFds;
- h->numInts = numInts;
- } else {
- h = alloc(cookie, numFds, numInts);
- if (h->version != sizeof(native_handle)) {
- return 0;
- }
- }
-
+ native_handle* h = native_handle_create(numFds, numInts);
for (int i=0 ; err==NO_ERROR && i<numFds ; i++) {
h->data[i] = dup(readFileDescriptor());
if (h->data[i] < 0) err = BAD_VALUE;
}
-
err = read(h->data + numFds, sizeof(int)*numInts);
-
if (err != NO_ERROR) {
- if (alloc == 0) {
- free(h);
- }
+ native_handle_close(h);
+ native_handle_delete(h);
h = 0;
}
return h;
diff --git a/libs/utils/ResourceTypes.cpp b/libs/utils/ResourceTypes.cpp
index 3d12dca..109f28d 100644
--- a/libs/utils/ResourceTypes.cpp
+++ b/libs/utils/ResourceTypes.cpp
@@ -544,7 +544,7 @@
return mEventCode;
}
-const int32_t ResXMLParser::getCommentID() const
+int32_t ResXMLParser::getCommentID() const
{
return mCurNode != NULL ? dtohl(mCurNode->comment.index) : -1;
}
@@ -555,12 +555,12 @@
return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
}
-const uint32_t ResXMLParser::getLineNumber() const
+uint32_t ResXMLParser::getLineNumber() const
{
return mCurNode != NULL ? dtohl(mCurNode->lineNumber) : -1;
}
-const int32_t ResXMLParser::getTextID() const
+int32_t ResXMLParser::getTextID() const
{
if (mEventCode == TEXT) {
return dtohl(((const ResXMLTree_cdataExt*)mCurExt)->data.index);
@@ -583,7 +583,7 @@
return BAD_TYPE;
}
-const int32_t ResXMLParser::getNamespacePrefixID() const
+int32_t ResXMLParser::getNamespacePrefixID() const
{
if (mEventCode == START_NAMESPACE || mEventCode == END_NAMESPACE) {
return dtohl(((const ResXMLTree_namespaceExt*)mCurExt)->prefix.index);
@@ -598,7 +598,7 @@
return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
}
-const int32_t ResXMLParser::getNamespaceUriID() const
+int32_t ResXMLParser::getNamespaceUriID() const
{
if (mEventCode == START_NAMESPACE || mEventCode == END_NAMESPACE) {
return dtohl(((const ResXMLTree_namespaceExt*)mCurExt)->uri.index);
@@ -613,7 +613,7 @@
return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
}
-const int32_t ResXMLParser::getElementNamespaceID() const
+int32_t ResXMLParser::getElementNamespaceID() const
{
if (mEventCode == START_TAG) {
return dtohl(((const ResXMLTree_attrExt*)mCurExt)->ns.index);
@@ -630,7 +630,7 @@
return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
}
-const int32_t ResXMLParser::getElementNameID() const
+int32_t ResXMLParser::getElementNameID() const
{
if (mEventCode == START_TAG) {
return dtohl(((const ResXMLTree_attrExt*)mCurExt)->name.index);
@@ -655,7 +655,7 @@
return 0;
}
-const int32_t ResXMLParser::getAttributeNamespaceID(size_t idx) const
+int32_t ResXMLParser::getAttributeNamespaceID(size_t idx) const
{
if (mEventCode == START_TAG) {
const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt;
@@ -678,7 +678,7 @@
return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
}
-const int32_t ResXMLParser::getAttributeNameID(size_t idx) const
+int32_t ResXMLParser::getAttributeNameID(size_t idx) const
{
if (mEventCode == START_TAG) {
const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt;
@@ -701,7 +701,7 @@
return id >= 0 ? mTree.mStrings.stringAt(id, outLen) : NULL;
}
-const uint32_t ResXMLParser::getAttributeNameResID(size_t idx) const
+uint32_t ResXMLParser::getAttributeNameResID(size_t idx) const
{
int32_t id = getAttributeNameID(idx);
if (id >= 0 && (size_t)id < mTree.mNumResIds) {
@@ -710,7 +710,7 @@
return 0;
}
-const int32_t ResXMLParser::getAttributeValueStringID(size_t idx) const
+int32_t ResXMLParser::getAttributeValueStringID(size_t idx) const
{
if (mEventCode == START_TAG) {
const ResXMLTree_attrExt* tag = (const ResXMLTree_attrExt*)mCurExt;
@@ -1136,8 +1136,9 @@
struct ResTable::Header
{
- Header() : ownedData(NULL), header(NULL) { }
+ Header(ResTable* _owner) : owner(_owner), ownedData(NULL), header(NULL) { }
+ ResTable* const owner;
void* ownedData;
const ResTable_header* header;
size_t size;
@@ -1163,8 +1164,8 @@
struct ResTable::Package
{
- Package(const Header* _header, const ResTable_package* _package)
- : header(_header), package(_package) { }
+ Package(ResTable* _owner, const Header* _header, const ResTable_package* _package)
+ : owner(_owner), header(_header), package(_package) { }
~Package()
{
size_t i = types.size();
@@ -1174,10 +1175,14 @@
}
}
+ ResTable* const owner;
const Header* const header;
const ResTable_package* const package;
Vector<Type*> types;
+ ResStringPool typeStrings;
+ ResStringPool keyStrings;
+
const Type* getType(size_t idx) const {
return idx < types.size() ? types[idx] : NULL;
}
@@ -1188,13 +1193,16 @@
// table that defined the package); the ones after are skins on top of it.
struct ResTable::PackageGroup
{
- PackageGroup(const String16& _name, uint32_t _id)
- : name(_name), id(_id), typeCount(0), bags(NULL) { }
+ PackageGroup(ResTable* _owner, const String16& _name, uint32_t _id)
+ : owner(_owner), name(_name), id(_id), typeCount(0), bags(NULL) { }
~PackageGroup() {
clearBagCache();
const size_t N = packages.size();
for (size_t i=0; i<N; i++) {
- delete packages[i];
+ Package* pkg = packages[i];
+ if (pkg->owner == owner) {
+ delete pkg;
+ }
}
}
@@ -1225,15 +1233,17 @@
}
}
+ ResTable* const owner;
String16 const name;
uint32_t const id;
Vector<Package*> packages;
+
+ // This is for finding typeStrings and other common package stuff.
+ Package* basePackage;
- // Taken from the root package.
- ResStringPool typeStrings;
- ResStringPool keyStrings;
+ // For quick access.
size_t typeCount;
-
+
// Computed attribute bags, first indexed by the type and second
// by the entry in that type.
bag_set*** bags;
@@ -1560,11 +1570,36 @@
return add(data, size, cookie, asset, copyData);
}
+status_t ResTable::add(ResTable* src)
+{
+ mError = src->mError;
+ mParams = src->mParams;
+
+ for (size_t i=0; i<src->mHeaders.size(); i++) {
+ mHeaders.add(src->mHeaders[i]);
+ }
+
+ for (size_t i=0; i<src->mPackageGroups.size(); i++) {
+ PackageGroup* srcPg = src->mPackageGroups[i];
+ PackageGroup* pg = new PackageGroup(this, srcPg->name, srcPg->id);
+ for (size_t j=0; j<srcPg->packages.size(); j++) {
+ pg->packages.add(srcPg->packages[j]);
+ }
+ pg->basePackage = srcPg->basePackage;
+ pg->typeCount = srcPg->typeCount;
+ mPackageGroups.add(pg);
+ }
+
+ memcpy(mPackageMap, src->mPackageMap, sizeof(mPackageMap));
+
+ return mError;
+}
+
status_t ResTable::add(const void* data, size_t size, void* cookie,
Asset* asset, bool copyData)
{
if (!data) return NO_ERROR;
- Header* header = new Header;
+ Header* header = new Header(this);
header->index = mHeaders.size();
header->cookie = cookie;
mHeaders.add(header);
@@ -1682,10 +1717,12 @@
N = mHeaders.size();
for (size_t i=0; i<N; i++) {
Header* header = mHeaders[i];
- if (header->ownedData) {
- free(header->ownedData);
+ if (header->owner == this) {
+ if (header->ownedData) {
+ free(header->ownedData);
+ }
+ delete header;
}
- delete header;
}
mPackageGroups.clear();
@@ -1728,8 +1765,8 @@
outName->package = grp->name.string();
outName->packageLen = grp->name.size();
- outName->type = grp->typeStrings.stringAt(t, &outName->typeLen);
- outName->name = grp->keyStrings.stringAt(
+ outName->type = grp->basePackage->typeStrings.stringAt(t, &outName->typeLen);
+ outName->name = grp->basePackage->keyStrings.stringAt(
dtohl(entry->key.index), &outName->nameLen);
return true;
}
@@ -2331,13 +2368,13 @@
continue;
}
- const ssize_t ti = group->typeStrings.indexOfString(type, typeLen);
+ const ssize_t ti = group->basePackage->typeStrings.indexOfString(type, typeLen);
if (ti < 0) {
TABLE_NOISY(printf("Type not found in package %s\n", String8(group->name).string()));
continue;
}
- const ssize_t ei = group->keyStrings.indexOfString(name, nameLen);
+ const ssize_t ei = group->basePackage->keyStrings.indexOfString(name, nameLen);
if (ei < 0) {
TABLE_NOISY(printf("Name not found in package %s\n", String8(group->name).string()));
continue;
@@ -3630,25 +3667,36 @@
PackageGroup* group = NULL;
uint32_t id = dtohl(pkg->id);
if (id != 0 && id < 256) {
+
+ package = new Package(this, header, pkg);
+ if (package == NULL) {
+ return (mError=NO_MEMORY);
+ }
+
size_t idx = mPackageMap[id];
if (idx == 0) {
idx = mPackageGroups.size()+1;
char16_t tmpName[sizeof(pkg->name)/sizeof(char16_t)];
strcpy16_dtoh(tmpName, pkg->name, sizeof(pkg->name)/sizeof(char16_t));
- group = new PackageGroup(String16(tmpName), id);
+ group = new PackageGroup(this, String16(tmpName), id);
if (group == NULL) {
+ delete package;
return (mError=NO_MEMORY);
}
- err = group->typeStrings.setTo(base+dtohl(pkg->typeStrings),
+ err = package->typeStrings.setTo(base+dtohl(pkg->typeStrings),
header->dataEnd-(base+dtohl(pkg->typeStrings)));
if (err != NO_ERROR) {
+ delete group;
+ delete package;
return (mError=err);
}
- err = group->keyStrings.setTo(base+dtohl(pkg->keyStrings),
+ err = package->keyStrings.setTo(base+dtohl(pkg->keyStrings),
header->dataEnd-(base+dtohl(pkg->keyStrings)));
if (err != NO_ERROR) {
+ delete group;
+ delete package;
return (mError=err);
}
@@ -3657,6 +3705,8 @@
if (err < NO_ERROR) {
return (mError=err);
}
+ group->basePackage = package;
+
mPackageMap[id] = (uint8_t)idx;
} else {
group = mPackageGroups.itemAt(idx-1);
@@ -3664,10 +3714,6 @@
return (mError=UNKNOWN_ERROR);
}
}
- package = new Package(header, pkg);
- if (package == NULL) {
- return (mError=NO_MEMORY);
- }
err = group->packages.add(package);
if (err < NO_ERROR) {
return (mError=err);
@@ -3830,9 +3876,88 @@
#define CHAR16_ARRAY_EQ(constant, var, len) \
((len == (sizeof(constant)/sizeof(constant[0]))) && (0 == memcmp((var), (constant), (len))))
-void ResTable::print() const
+void print_complex(uint32_t complex, bool isFraction)
{
- printf("mError=0x%x (%s)\n", mError, strerror(mError));
+ const float MANTISSA_MULT =
+ 1.0f / (1<<Res_value::COMPLEX_MANTISSA_SHIFT);
+ const float RADIX_MULTS[] = {
+ 1.0f*MANTISSA_MULT, 1.0f/(1<<7)*MANTISSA_MULT,
+ 1.0f/(1<<15)*MANTISSA_MULT, 1.0f/(1<<23)*MANTISSA_MULT
+ };
+
+ float value = (complex&(Res_value::COMPLEX_MANTISSA_MASK
+ <<Res_value::COMPLEX_MANTISSA_SHIFT))
+ * RADIX_MULTS[(complex>>Res_value::COMPLEX_RADIX_SHIFT)
+ & Res_value::COMPLEX_RADIX_MASK];
+ printf("%f", value);
+
+ if (!isFraction) {
+ switch ((complex>>Res_value::COMPLEX_UNIT_SHIFT)&Res_value::COMPLEX_UNIT_MASK) {
+ case Res_value::COMPLEX_UNIT_PX: printf("px"); break;
+ case Res_value::COMPLEX_UNIT_DIP: printf("dp"); break;
+ case Res_value::COMPLEX_UNIT_SP: printf("sp"); break;
+ case Res_value::COMPLEX_UNIT_PT: printf("pt"); break;
+ case Res_value::COMPLEX_UNIT_IN: printf("in"); break;
+ case Res_value::COMPLEX_UNIT_MM: printf("mm"); break;
+ default: printf(" (unknown unit)"); break;
+ }
+ } else {
+ switch ((complex>>Res_value::COMPLEX_UNIT_SHIFT)&Res_value::COMPLEX_UNIT_MASK) {
+ case Res_value::COMPLEX_UNIT_FRACTION: printf("%%"); break;
+ case Res_value::COMPLEX_UNIT_FRACTION_PARENT: printf("%%p"); break;
+ default: printf(" (unknown unit)"); break;
+ }
+ }
+}
+
+void ResTable::print_value(const Package* pkg, const Res_value& value) const
+{
+ if (value.dataType == Res_value::TYPE_NULL) {
+ printf("(null)\n");
+ } else if (value.dataType == Res_value::TYPE_REFERENCE) {
+ printf("(reference) 0x%08x\n", value.data);
+ } else if (value.dataType == Res_value::TYPE_ATTRIBUTE) {
+ printf("(attribute) 0x%08x\n", value.data);
+ } else if (value.dataType == Res_value::TYPE_STRING) {
+ size_t len;
+ const char16_t* str = pkg->header->values.stringAt(
+ value.data, &len);
+ if (str == NULL) {
+ printf("(string) null\n");
+ } else {
+ printf("(string) \"%s\"\n",
+ String8(str, len).string());
+ }
+ } else if (value.dataType == Res_value::TYPE_FLOAT) {
+ printf("(float) %g\n", *(const float*)&value.data);
+ } else if (value.dataType == Res_value::TYPE_DIMENSION) {
+ printf("(dimension) ");
+ print_complex(value.data, false);
+ printf("\n");
+ } else if (value.dataType == Res_value::TYPE_FRACTION) {
+ printf("(fraction) ");
+ print_complex(value.data, true);
+ printf("\n");
+ } else if (value.dataType >= Res_value::TYPE_FIRST_COLOR_INT
+ || value.dataType <= Res_value::TYPE_LAST_COLOR_INT) {
+ printf("(color) #%08x\n", value.data);
+ } else if (value.dataType == Res_value::TYPE_INT_BOOLEAN) {
+ printf("(boolean) %s\n", value.data ? "true" : "false");
+ } else if (value.dataType >= Res_value::TYPE_FIRST_INT
+ || value.dataType <= Res_value::TYPE_LAST_INT) {
+ printf("(int) 0x%08x or %d\n", value.data, value.data);
+ } else {
+ printf("(unknown type) t=0x%02x d=0x%08x (s=0x%04x r=0x%02x)\n",
+ (int)value.dataType, (int)value.data,
+ (int)value.size, (int)value.res0);
+ }
+}
+
+void ResTable::print(bool inclValues) const
+{
+ if (mError != 0) {
+ printf("mError=0x%x (%s)\n", mError, strerror(mError));
+ }
#if 0
printf("mParams=%c%c-%c%c,\n",
mParams.language[0], mParams.language[1],
@@ -3883,7 +4008,7 @@
printf(" NON-INTEGER ResTable_type ADDRESS: %p\n", type);
continue;
}
- printf(" config %d lang=%c%c cnt=%c%c orien=%d touch=%d density=%d key=%d infl=%d nav=%d w=%d h=%d\n",
+ printf(" config %d lang=%c%c cnt=%c%c orien=%d touch=%d density=%d key=%d infl=%d nav=%d w=%d h=%d lyt=%d\n",
(int)configIndex,
type->config.language[0] ? type->config.language[0] : '-',
type->config.language[1] ? type->config.language[1] : '-',
@@ -3896,7 +4021,8 @@
type->config.inputFlags,
type->config.navigation,
dtohs(type->config.screenWidth),
- dtohs(type->config.screenHeight));
+ dtohs(type->config.screenHeight),
+ type->config.screenLayout);
size_t entryCount = dtohl(type->entryCount);
uint32_t entriesStart = dtohl(type->entriesStart);
if ((entriesStart&0x3) != 0) {
@@ -3947,32 +4073,60 @@
(void*)(entriesStart + thisOffset));
continue;
}
+
+ uint16_t esize = dtohs(ent->size);
+ if ((esize&0x3) != 0) {
+ printf("NON-INTEGER ResTable_entry SIZE: %p\n", (void*)esize);
+ continue;
+ }
+ if ((thisOffset+esize) > typeSize) {
+ printf("ResTable_entry OUT OF BOUNDS: %p+%p+%p (size is %p)\n",
+ (void*)entriesStart, (void*)thisOffset,
+ (void*)esize, (void*)typeSize);
+ continue;
+ }
+
+ const Res_value* valuePtr = NULL;
+ const ResTable_map_entry* bagPtr = NULL;
+ Res_value value;
if ((dtohs(ent->flags)&ResTable_entry::FLAG_COMPLEX) != 0) {
printf("<bag>");
+ bagPtr = (const ResTable_map_entry*)ent;
} else {
- uint16_t esize = dtohs(ent->size);
- if ((esize&0x3) != 0) {
- printf("NON-INTEGER ResTable_entry SIZE: %p\n", (void*)esize);
- continue;
- }
- if ((thisOffset+esize) > typeSize) {
- printf("ResTable_entry OUT OF BOUNDS: %p+%p+%p (size is %p)\n",
- (void*)entriesStart, (void*)thisOffset,
- (void*)esize, (void*)typeSize);
- continue;
- }
-
- const Res_value* value = (const Res_value*)
+ valuePtr = (const Res_value*)
(((const uint8_t*)ent) + esize);
+ value.copyFrom_dtoh(*valuePtr);
printf("t=0x%02x d=0x%08x (s=0x%04x r=0x%02x)",
- (int)value->dataType, (int)dtohl(value->data),
- (int)dtohs(value->size), (int)value->res0);
+ (int)value.dataType, (int)value.data,
+ (int)value.size, (int)value.res0);
}
if ((dtohs(ent->flags)&ResTable_entry::FLAG_PUBLIC) != 0) {
printf(" (PUBLIC)");
}
printf("\n");
+
+ if (inclValues) {
+ if (valuePtr != NULL) {
+ printf(" ");
+ print_value(pkg, value);
+ } else if (bagPtr != NULL) {
+ const int N = dtohl(bagPtr->count);
+ const ResTable_map* mapPtr = (const ResTable_map*)
+ (((const uint8_t*)ent) + esize);
+ printf(" Parent=0x%08x, Count=%d\n",
+ dtohl(bagPtr->parent.ident), N);
+ for (int i=0; i<N; i++) {
+ printf(" #%i (Key=0x%08x): ",
+ i, dtohl(mapPtr->name.ident));
+ value.copyFrom_dtoh(mapPtr->value);
+ print_value(pkg, value);
+ const size_t size = dtohs(mapPtr->value.size);
+ mapPtr = (ResTable_map*)(((const uint8_t*)mapPtr)
+ + size + sizeof(*mapPtr)-sizeof(mapPtr->value));
+ }
+ }
+ }
}
}
}
diff --git a/libs/utils/Unicode.cpp b/libs/utils/Unicode.cpp
index 33f535f..f92703e 100644
--- a/libs/utils/Unicode.cpp
+++ b/libs/utils/Unicode.cpp
@@ -14,11 +14,11 @@
* limitations under the License.
*/
-#include "utils/AndroidUnicode.h"
-#include "characterData.h"
+#include <utils/AndroidUnicode.h>
+#include "CharacterData.h"
#define LOG_TAG "Unicode"
-#include "utils/Log.h"
+#include <utils/Log.h>
// ICU headers for using macros
#include <unicode/utf16.h>
diff --git a/libs/utils/ZipEntry.cpp b/libs/utils/ZipEntry.cpp
index fbc9e67..96f9fc4 100644
--- a/libs/utils/ZipEntry.cpp
+++ b/libs/utils/ZipEntry.cpp
@@ -20,8 +20,8 @@
#define LOG_TAG "zip"
-#include "utils/ZipEntry.h"
-#include "utils/Log.h"
+#include <utils/ZipEntry.h>
+#include <utils/Log.h>
#include <stdio.h>
#include <string.h>
diff --git a/libs/utils/ZipFile.cpp b/libs/utils/ZipFile.cpp
index 89aa874..6f27d17 100644
--- a/libs/utils/ZipFile.cpp
+++ b/libs/utils/ZipFile.cpp
@@ -20,9 +20,9 @@
#define LOG_TAG "zip"
-#include "utils/ZipFile.h"
-#include "utils/ZipUtils.h"
-#include "utils/Log.h"
+#include <utils/ZipFile.h>
+#include <utils/ZipUtils.h>
+#include <utils/Log.h>
#include <zlib.h>
#define DEF_MEM_LEVEL 8 // normally in zutil.h?
diff --git a/libs/utils/ZipFileCRO.cpp b/libs/utils/ZipFileCRO.cpp
index d312daf..45f6c8b 100644
--- a/libs/utils/ZipFileCRO.cpp
+++ b/libs/utils/ZipFileCRO.cpp
@@ -14,8 +14,8 @@
* limitations under the License.
*/
-#include "utils/ZipFileCRO.h"
-#include "utils/ZipFileRO.h"
+#include <utils/ZipFileCRO.h>
+#include <utils/ZipFileRO.h>
using namespace android;
diff --git a/libs/utils/ZipFileRO.cpp b/libs/utils/ZipFileRO.cpp
index ae8c719..6c701dd 100644
--- a/libs/utils/ZipFileRO.cpp
+++ b/libs/utils/ZipFileRO.cpp
@@ -19,9 +19,9 @@
//
#define LOG_TAG "zipro"
//#define LOG_NDEBUG 0
-#include "utils/ZipFileRO.h"
-#include "utils/Log.h"
-#include "utils/misc.h"
+#include <utils/ZipFileRO.h>
+#include <utils/Log.h>
+#include <utils/misc.h>
#include <zlib.h>
diff --git a/libs/utils/ZipUtils.cpp b/libs/utils/ZipUtils.cpp
index bfbacfe..5df94cb 100644
--- a/libs/utils/ZipUtils.cpp
+++ b/libs/utils/ZipUtils.cpp
@@ -20,9 +20,9 @@
#define LOG_TAG "ziputil"
-#include "utils/ZipUtils.h"
-#include "utils/ZipFileRO.h"
-#include "utils/Log.h"
+#include <utils/ZipUtils.h>
+#include <utils/ZipFileRO.h>
+#include <utils/Log.h>
#include <stdlib.h>
#include <string.h>
diff --git a/libs/utils/file_backup_helper.cpp b/libs/utils/file_backup_helper.cpp
deleted file mode 100644
index 453084a..0000000
--- a/libs/utils/file_backup_helper.cpp
+++ /dev/null
@@ -1,685 +0,0 @@
-#define LOG_TAG "file_backup_helper"
-
-#include <utils/backup_helpers.h>
-
-#include <utils/KeyedVector.h>
-#include <utils/ByteOrder.h>
-#include <utils/String8.h>
-
-#include <errno.h>
-#include <sys/types.h>
-#include <sys/uio.h>
-#include <sys/stat.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <utime.h>
-#include <fcntl.h>
-#include <zlib.h>
-
-#include <cutils/log.h>
-
-using namespace android;
-
-#define MAGIC0 0x70616e53 // Snap
-#define MAGIC1 0x656c6946 // File
-
-#define LOGP(x...) LOGD(x)
-//#define LOGP(x...) printf(x)
-
-struct SnapshotHeader {
- int magic0;
- int fileCount;
- int magic1;
- int totalSize;
-};
-
-struct FileState {
- int modTime_sec;
- int modTime_nsec;
- int size;
- int crc32;
- int nameLen;
-};
-
-const static int ROUND_UP[4] = { 0, 3, 2, 1 };
-
-static inline int
-round_up(int n)
-{
- return n + ROUND_UP[n % 4];
-}
-
-static int
-read_snapshot_file(int fd, KeyedVector<String8,FileState>* snapshot)
-{
- int bytesRead = 0;
- int amt;
- SnapshotHeader header;
-
- amt = read(fd, &header, sizeof(header));
- if (amt != sizeof(header)) {
- return errno;
- }
- bytesRead += amt;
-
- if (header.magic0 != MAGIC0 || header.magic1 != MAGIC1) {
- LOGW("read_snapshot_file header.magic0=0x%08x magic1=0x%08x", header.magic0, header.magic1);
- return 1;
- }
-
- for (int i=0; i<header.fileCount; i++) {
- FileState file;
- char filenameBuf[128];
-
- amt = read(fd, &file, sizeof(file));
- if (amt != sizeof(file)) {
- LOGW("read_snapshot_file FileState truncated/error with read at %d bytes\n", bytesRead);
- return 1;
- }
- bytesRead += amt;
-
- // filename is not NULL terminated, but it is padded
- int nameBufSize = round_up(file.nameLen);
- char* filename = nameBufSize <= (int)sizeof(filenameBuf)
- ? filenameBuf
- : (char*)malloc(nameBufSize);
- amt = read(fd, filename, nameBufSize);
- if (amt == nameBufSize) {
- snapshot->add(String8(filename, file.nameLen), file);
- }
- bytesRead += amt;
- if (filename != filenameBuf) {
- free(filename);
- }
- if (amt != nameBufSize) {
- LOGW("read_snapshot_file filename truncated/error with read at %d bytes\n", bytesRead);
- return 1;
- }
- }
-
- if (header.totalSize != bytesRead) {
- LOGW("read_snapshot_file length mismatch: header.totalSize=%d bytesRead=%d\n",
- header.totalSize, bytesRead);
- return 1;
- }
-
- return 0;
-}
-
-static int
-write_snapshot_file(int fd, const KeyedVector<String8,FileState>& snapshot)
-{
- int bytesWritten = sizeof(SnapshotHeader);
- // preflight size
- const int N = snapshot.size();
- for (int i=0; i<N; i++) {
- const String8& name = snapshot.keyAt(i);
- bytesWritten += sizeof(FileState) + round_up(name.length());
- }
-
- int amt;
- SnapshotHeader header = { MAGIC0, N, MAGIC1, bytesWritten };
-
- amt = write(fd, &header, sizeof(header));
- if (amt != sizeof(header)) {
- LOGW("write_snapshot_file error writing header %s", strerror(errno));
- return errno;
- }
-
- for (int i=0; i<header.fileCount; i++) {
- const String8& name = snapshot.keyAt(i);
- FileState file = snapshot.valueAt(i);
- int nameLen = file.nameLen = name.length();
-
- amt = write(fd, &file, sizeof(file));
- if (amt != sizeof(file)) {
- LOGW("write_snapshot_file error writing header %s", strerror(errno));
- return 1;
- }
-
- // filename is not NULL terminated, but it is padded
- amt = write(fd, name.string(), nameLen);
- if (amt != nameLen) {
- LOGW("write_snapshot_file error writing filename %s", strerror(errno));
- return 1;
- }
- int paddingLen = ROUND_UP[nameLen % 4];
- if (paddingLen != 0) {
- int padding = 0xabababab;
- amt = write(fd, &padding, paddingLen);
- if (amt != paddingLen) {
- LOGW("write_snapshot_file error writing %d bytes of filename padding %s",
- paddingLen, strerror(errno));
- return 1;
- }
- }
- }
-
- return 0;
-}
-
-static int
-write_delete_file(const String8& key)
-{
- LOGP("write_delete_file %s\n", key.string());
- return 0;
-}
-
-static int
-write_update_file(const String8& realFilename, const String8& key)
-{
- LOGP("write_update_file %s (%s)\n", realFilename.string(), key.string());
- return 0;
-}
-
-static int
-compute_crc32(const String8& filename)
-{
- const int bufsize = 4*1024;
- int amt;
-
- int fd = open(filename.string(), O_RDONLY);
- if (fd == -1) {
- return -1;
- }
-
- char* buf = (char*)malloc(bufsize);
- int crc = crc32(0L, Z_NULL, 0);
-
- while ((amt = read(fd, buf, bufsize)) != 0) {
- crc = crc32(crc, (Bytef*)buf, amt);
- }
-
- close(fd);
- free(buf);
-
- return crc;
-}
-
-int
-back_up_files(int oldSnapshotFD, int oldDataStream, int newSnapshotFD,
- char const* fileBase, char const* const* files, int fileCount)
-{
- int err;
- const String8 base(fileBase);
- KeyedVector<String8,FileState> oldSnapshot;
- KeyedVector<String8,FileState> newSnapshot;
-
- if (oldSnapshotFD != -1) {
- err = read_snapshot_file(oldSnapshotFD, &oldSnapshot);
- if (err != 0) {
- // On an error, treat this as a full backup.
- oldSnapshot.clear();
- }
- }
-
- for (int i=0; i<fileCount; i++) {
- String8 name(files[i]);
- FileState s;
- struct stat st;
- String8 realFilename(base);
- realFilename.appendPath(name);
-
- err = stat(realFilename.string(), &st);
- if (err != 0) {
- LOGW("Error stating file %s", realFilename.string());
- continue;
- }
-
- s.modTime_sec = st.st_mtime;
- s.modTime_nsec = 0; // workaround sim breakage
- //s.modTime_nsec = st.st_mtime_nsec;
- s.size = st.st_size;
- s.crc32 = compute_crc32(realFilename);
-
- newSnapshot.add(name, s);
- }
-
- int n = 0;
- int N = oldSnapshot.size();
- int m = 0;
-
- while (n<N && m<fileCount) {
- const String8& p = oldSnapshot.keyAt(n);
- const String8& q = newSnapshot.keyAt(m);
- int cmp = p.compare(q);
- if (cmp > 0) {
- // file added
- String8 realFilename(base);
- realFilename.appendPath(q);
- write_update_file(realFilename, q);
- m++;
- }
- else if (cmp < 0) {
- // file removed
- write_delete_file(p);
- n++;
- }
- else {
- // both files exist, check them
- String8 realFilename(base);
- realFilename.appendPath(q);
- const FileState& f = oldSnapshot.valueAt(n);
- const FileState& g = newSnapshot.valueAt(m);
-
- LOGP("%s\n", q.string());
- LOGP(" new: modTime=%d,%d size=%-3d crc32=0x%08x\n",
- f.modTime_sec, f.modTime_nsec, f.size, f.crc32);
- LOGP(" old: modTime=%d,%d size=%-3d crc32=0x%08x\n",
- g.modTime_sec, g.modTime_nsec, g.size, g.crc32);
- if (f.modTime_sec != g.modTime_sec || f.modTime_nsec != g.modTime_nsec
- || f.size != g.size || f.crc32 != g.crc32) {
- write_update_file(realFilename, p);
- }
- n++;
- m++;
- }
- }
-
- // these were deleted
- while (n<N) {
- write_delete_file(oldSnapshot.keyAt(n));
- n++;
- }
-
- // these were added
- while (m<fileCount) {
- const String8& q = newSnapshot.keyAt(m);
- String8 realFilename(base);
- realFilename.appendPath(q);
- write_update_file(realFilename, q);
- m++;
- }
-
- err = write_snapshot_file(newSnapshotFD, newSnapshot);
-
- return 0;
-}
-
-#if TEST_BACKUP_HELPERS
-
-#define SCRATCH_DIR "/data/backup_helper_test/"
-
-static int
-write_text_file(const char* path, const char* data)
-{
- int amt;
- int fd;
- int len;
-
- fd = creat(path, 0666);
- if (fd == -1) {
- fprintf(stderr, "creat %s failed\n", path);
- return errno;
- }
-
- len = strlen(data);
- amt = write(fd, data, len);
- if (amt != len) {
- fprintf(stderr, "error (%s) writing to file %s\n", strerror(errno), path);
- return errno;
- }
-
- close(fd);
-
- return 0;
-}
-
-static int
-compare_file(const char* path, const unsigned char* data, int len)
-{
- int fd;
- int amt;
-
- fd = open(path, O_RDONLY);
- if (fd == -1) {
- fprintf(stderr, "compare_file error (%s) opening %s\n", strerror(errno), path);
- return errno;
- }
-
- unsigned char* contents = (unsigned char*)malloc(len);
- if (contents == NULL) {
- fprintf(stderr, "malloc(%d) failed\n", len);
- return ENOMEM;
- }
-
- bool sizesMatch = true;
- amt = lseek(fd, 0, SEEK_END);
- if (amt != len) {
- fprintf(stderr, "compare_file file length should be %d, was %d\n", len, amt);
- sizesMatch = false;
- }
- lseek(fd, 0, SEEK_SET);
-
- int readLen = amt < len ? amt : len;
- amt = read(fd, contents, readLen);
- if (amt != readLen) {
- fprintf(stderr, "compare_file read expected %d bytes but got %d\n", len, amt);
- }
-
- bool contentsMatch = true;
- for (int i=0; i<readLen; i++) {
- if (data[i] != contents[i]) {
- if (contentsMatch) {
- fprintf(stderr, "compare_file contents are different: (index, expected, actual)\n");
- contentsMatch = false;
- }
- fprintf(stderr, " [%-2d] %02x %02x\n", i, data[i], contents[i]);
- }
- }
-
- return contentsMatch && sizesMatch ? 0 : 1;
-}
-
-int
-backup_helper_test_empty()
-{
- int err;
- int fd;
- KeyedVector<String8,FileState> snapshot;
- const char* filename = SCRATCH_DIR "backup_helper_test_empty.snap";
-
- system("rm -r " SCRATCH_DIR);
- mkdir(SCRATCH_DIR, 0777);
-
- // write
- fd = creat(filename, 0666);
- if (fd == -1) {
- fprintf(stderr, "error creating %s\n", filename);
- return 1;
- }
-
- err = write_snapshot_file(fd, snapshot);
-
- close(fd);
-
- if (err != 0) {
- fprintf(stderr, "write_snapshot_file reported error %d (%s)\n", err, strerror(err));
- return err;
- }
-
- static const unsigned char correct_data[] = {
- 0x53, 0x6e, 0x61, 0x70, 0x00, 0x00, 0x00, 0x00,
- 0x46, 0x69, 0x6c, 0x65, 0x10, 0x00, 0x00, 0x00
- };
-
- err = compare_file(filename, correct_data, sizeof(correct_data));
- if (err != 0) {
- return err;
- }
-
- // read
- fd = open(filename, O_RDONLY);
- if (fd == -1) {
- fprintf(stderr, "error opening for read %s\n", filename);
- return 1;
- }
-
- KeyedVector<String8,FileState> readSnapshot;
- err = read_snapshot_file(fd, &readSnapshot);
- if (err != 0) {
- fprintf(stderr, "read_snapshot_file failed %d\n", err);
- return err;
- }
-
- if (readSnapshot.size() != 0) {
- fprintf(stderr, "readSnapshot should be length 0\n");
- return 1;
- }
-
- return 0;
-}
-
-int
-backup_helper_test_four()
-{
- int err;
- int fd;
- KeyedVector<String8,FileState> snapshot;
- const char* filename = SCRATCH_DIR "backup_helper_test_four.snap";
-
- system("rm -r " SCRATCH_DIR);
- mkdir(SCRATCH_DIR, 0777);
-
- // write
- fd = creat(filename, 0666);
- if (fd == -1) {
- fprintf(stderr, "error opening %s\n", filename);
- return 1;
- }
-
- String8 filenames[4];
- FileState states[4];
-
- states[0].modTime_sec = 0xfedcba98;
- states[0].modTime_nsec = 0xdeadbeef;
- states[0].size = 0xababbcbc;
- states[0].crc32 = 0x12345678;
- states[0].nameLen = -12;
- filenames[0] = String8("bytes_of_padding");
- snapshot.add(filenames[0], states[0]);
-
- states[1].modTime_sec = 0x93400031;
- states[1].modTime_nsec = 0xdeadbeef;
- states[1].size = 0x88557766;
- states[1].crc32 = 0x22334422;
- states[1].nameLen = -1;
- filenames[1] = String8("bytes_of_padding3");
- snapshot.add(filenames[1], states[1]);
-
- states[2].modTime_sec = 0x33221144;
- states[2].modTime_nsec = 0xdeadbeef;
- states[2].size = 0x11223344;
- states[2].crc32 = 0x01122334;
- states[2].nameLen = 0;
- filenames[2] = String8("bytes_of_padding_2");
- snapshot.add(filenames[2], states[2]);
-
- states[3].modTime_sec = 0x33221144;
- states[3].modTime_nsec = 0xdeadbeef;
- states[3].size = 0x11223344;
- states[3].crc32 = 0x01122334;
- states[3].nameLen = 0;
- filenames[3] = String8("bytes_of_padding__1");
- snapshot.add(filenames[3], states[3]);
-
- err = write_snapshot_file(fd, snapshot);
-
- close(fd);
-
- if (err != 0) {
- fprintf(stderr, "write_snapshot_file reported error %d (%s)\n", err, strerror(err));
- return err;
- }
-
- static const unsigned char correct_data[] = {
- // header
- 0x53, 0x6e, 0x61, 0x70, 0x04, 0x00, 0x00, 0x00,
- 0x46, 0x69, 0x6c, 0x65, 0xac, 0x00, 0x00, 0x00,
-
- // bytes_of_padding
- 0x98, 0xba, 0xdc, 0xfe, 0xef, 0xbe, 0xad, 0xde,
- 0xbc, 0xbc, 0xab, 0xab, 0x78, 0x56, 0x34, 0x12,
- 0x10, 0x00, 0x00, 0x00, 0x62, 0x79, 0x74, 0x65,
- 0x73, 0x5f, 0x6f, 0x66, 0x5f, 0x70, 0x61, 0x64,
- 0x64, 0x69, 0x6e, 0x67,
-
- // bytes_of_padding3
- 0x31, 0x00, 0x40, 0x93, 0xef, 0xbe, 0xad, 0xde,
- 0x66, 0x77, 0x55, 0x88, 0x22, 0x44, 0x33, 0x22,
- 0x11, 0x00, 0x00, 0x00, 0x62, 0x79, 0x74, 0x65,
- 0x73, 0x5f, 0x6f, 0x66, 0x5f, 0x70, 0x61, 0x64,
- 0x64, 0x69, 0x6e, 0x67, 0x33, 0xab, 0xab, 0xab,
-
- // bytes of padding2
- 0x44, 0x11, 0x22, 0x33, 0xef, 0xbe, 0xad, 0xde,
- 0x44, 0x33, 0x22, 0x11, 0x34, 0x23, 0x12, 0x01,
- 0x12, 0x00, 0x00, 0x00, 0x62, 0x79, 0x74, 0x65,
- 0x73, 0x5f, 0x6f, 0x66, 0x5f, 0x70, 0x61, 0x64,
- 0x64, 0x69, 0x6e, 0x67, 0x5f, 0x32, 0xab, 0xab,
-
- // bytes of padding3
- 0x44, 0x11, 0x22, 0x33, 0xef, 0xbe, 0xad, 0xde,
- 0x44, 0x33, 0x22, 0x11, 0x34, 0x23, 0x12, 0x01,
- 0x13, 0x00, 0x00, 0x00, 0x62, 0x79, 0x74, 0x65,
- 0x73, 0x5f, 0x6f, 0x66, 0x5f, 0x70, 0x61, 0x64,
- 0x64, 0x69, 0x6e, 0x67, 0x5f, 0x5f, 0x31, 0xab
- };
-
- err = compare_file(filename, correct_data, sizeof(correct_data));
- if (err != 0) {
- return err;
- }
-
- // read
- fd = open(filename, O_RDONLY);
- if (fd == -1) {
- fprintf(stderr, "error opening for read %s\n", filename);
- return 1;
- }
-
-
- KeyedVector<String8,FileState> readSnapshot;
- err = read_snapshot_file(fd, &readSnapshot);
- if (err != 0) {
- fprintf(stderr, "read_snapshot_file failed %d\n", err);
- return err;
- }
-
- if (readSnapshot.size() != 4) {
- fprintf(stderr, "readSnapshot should be length 4 is %d\n", readSnapshot.size());
- return 1;
- }
-
- bool matched = true;
- for (size_t i=0; i<readSnapshot.size(); i++) {
- const String8& name = readSnapshot.keyAt(i);
- const FileState state = readSnapshot.valueAt(i);
-
- if (name != filenames[i] || states[i].modTime_sec != state.modTime_sec
- || states[i].modTime_nsec != state.modTime_nsec
- || states[i].size != state.size || states[i].crc32 != states[i].crc32) {
- fprintf(stderr, "state %d expected={%d/%d, 0x%08x, 0x%08x, %3d} '%s'\n"
- " actual={%d/%d, 0x%08x, 0x%08x, %3d} '%s'\n", i,
- states[i].modTime_sec, states[i].modTime_nsec, states[i].size, states[i].crc32,
- name.length(), filenames[i].string(),
- state.modTime_sec, state.modTime_nsec, state.size, state.crc32, state.nameLen,
- name.string());
- matched = false;
- }
- }
-
- return matched ? 0 : 1;
-}
-
-static int
-get_mod_time(const char* filename, struct timeval times[2])
-{
- int err;
- struct stat64 st;
- err = stat64(filename, &st);
- if (err != 0) {
- fprintf(stderr, "stat '%s' failed: %s\n", filename, strerror(errno));
- return errno;
- }
- times[0].tv_sec = st.st_atime;
- times[0].tv_usec = st.st_atime_nsec / 1000;
- times[1].tv_sec = st.st_mtime;
- times[1].tv_usec = st.st_mtime_nsec / 1000;
- return 0;
-}
-
-int
-backup_helper_test_files()
-{
- int err;
- int newSnapshotFD;
- int oldSnapshotFD;
-
- system("rm -r " SCRATCH_DIR);
- mkdir(SCRATCH_DIR, 0777);
- mkdir(SCRATCH_DIR "data", 0777);
-
- write_text_file(SCRATCH_DIR "data/b", "b\nbb\n");
- write_text_file(SCRATCH_DIR "data/c", "c\ncc\n");
- write_text_file(SCRATCH_DIR "data/d", "d\ndd\n");
- write_text_file(SCRATCH_DIR "data/e", "e\nee\n");
- write_text_file(SCRATCH_DIR "data/f", "f\nff\n");
- write_text_file(SCRATCH_DIR "data/h", "h\nhh\n");
-
- char const* files_before[] = {
- "data/b",
- "data/c",
- "data/d",
- "data/e",
- "data/f"
- };
-
- newSnapshotFD = creat(SCRATCH_DIR "before.snap", 0666);
- if (newSnapshotFD == -1) {
- fprintf(stderr, "error creating: %s\n", strerror(errno));
- return errno;
- }
-
- err = back_up_files(-1, newSnapshotFD, 0, SCRATCH_DIR, files_before, 5);
- if (err != 0) {
- return err;
- }
-
- close(newSnapshotFD);
-
- sleep(3);
-
- struct timeval d_times[2];
- struct timeval e_times[2];
-
- err = get_mod_time(SCRATCH_DIR "data/d", d_times);
- err |= get_mod_time(SCRATCH_DIR "data/e", e_times);
- if (err != 0) {
- return err;
- }
-
- write_text_file(SCRATCH_DIR "data/a", "a\naa\n");
- unlink(SCRATCH_DIR "data/c");
- write_text_file(SCRATCH_DIR "data/c", "c\ncc\n");
- write_text_file(SCRATCH_DIR "data/d", "dd\ndd\n");
- utimes(SCRATCH_DIR "data/d", d_times);
- write_text_file(SCRATCH_DIR "data/e", "z\nzz\n");
- utimes(SCRATCH_DIR "data/e", e_times);
- write_text_file(SCRATCH_DIR "data/g", "g\ngg\n");
- unlink(SCRATCH_DIR "data/f");
-
- char const* files_after[] = {
- "data/a", // added
- "data/b", // same
- "data/c", // different mod time
- "data/d", // different size (same mod time)
- "data/e", // different contents (same mod time, same size)
- "data/g" // added
- };
-
- oldSnapshotFD = open(SCRATCH_DIR "before.snap", O_RDONLY);
- if (oldSnapshotFD == -1) {
- fprintf(stderr, "error opening: %s\n", strerror(errno));
- return errno;
- }
-
- newSnapshotFD = creat(SCRATCH_DIR "after.snap", 0666);
- if (newSnapshotFD == -1) {
- fprintf(stderr, "error creating: %s\n", strerror(errno));
- return errno;
- }
-
- err = back_up_files(oldSnapshotFD, newSnapshotFD, 0, SCRATCH_DIR, files_after, 6);
- if (err != 0) {
- return err;
- }
-
- close(oldSnapshotFD);
- close(newSnapshotFD);
-
- return 0;
-}
-
-#endif // TEST_BACKUP_HELPERS
diff --git a/libs/utils/futex_synchro.c b/libs/utils/futex_synchro.c
index ba19520..ab48c69 100644
--- a/libs/utils/futex_synchro.c
+++ b/libs/utils/futex_synchro.c
@@ -28,6 +28,7 @@
// This futex glue code is need on desktop linux, but is already part of bionic.
#if !defined(HAVE_FUTEX_WRAPPERS)
+#include <unistd.h>
#include <sys/syscall.h>
typedef unsigned int u32;
#define asmlinkage