Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 84b6292c authored by Mike Lockwood's avatar Mike Lockwood
Browse files

move libandroidfw to frameworks/native

Change-Id: Ic5b8a2742c7141156ab0f00ca29097bfe92bce60
parent 92a6f301
Loading
Loading
Loading
Loading

include/androidfw/Asset.h

deleted100644 → 0
+0 −322
Original line number Diff line number Diff line
/*
 * Copyright (C) 2006 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.
 */

//
// Class providing access to a read-only asset.  Asset objects are NOT
// thread-safe, and should not be shared across threads.
//
#ifndef __LIBS_ASSET_H
#define __LIBS_ASSET_H

#include <stdio.h>
#include <sys/types.h>

#include <utils/Compat.h>
#include <utils/Errors.h>
#include <utils/FileMap.h>
#include <utils/String8.h>

namespace android {

/*
 * Instances of this class provide read-only operations on a byte stream.
 *
 * Access may be optimized for streaming, random, or whole buffer modes.  All
 * operations are supported regardless of how the file was opened, but some
 * things will be less efficient.  [pass that in??]
 *
 * "Asset" is the base class for all types of assets.  The classes below
 * provide most of the implementation.  The AssetManager uses one of the
 * static "create" functions defined here to create a new instance.
 */
class Asset {
public:
    virtual ~Asset(void);

    static int32_t getGlobalCount();
    static String8 getAssetAllocations();
    
    /* used when opening an asset */
    typedef enum AccessMode {
        ACCESS_UNKNOWN = 0,

        /* read chunks, and seek forward and backward */
        ACCESS_RANDOM,

        /* read sequentially, with an occasional forward seek */
        ACCESS_STREAMING,

        /* caller plans to ask for a read-only buffer with all data */
        ACCESS_BUFFER,
    } AccessMode;

    /*
     * Read data from the current offset.  Returns the actual number of
     * bytes read, 0 on EOF, or -1 on error.
     */
    virtual ssize_t read(void* buf, size_t count) = 0;

    /*
     * Seek to the specified offset.  "whence" uses the same values as
     * lseek/fseek.  Returns the new position on success, or (off64_t) -1
     * on failure.
     */
    virtual off64_t seek(off64_t offset, int whence) = 0;

    /*
     * Close the asset, freeing all associated resources.
     */
    virtual void close(void) = 0;

    /*
     * Get a pointer to a buffer with the entire contents of the file.
     */
    virtual const void* getBuffer(bool wordAligned) = 0;

    /*
     * Get the total amount of data that can be read.
     */
    virtual off64_t getLength(void) const = 0;

    /*
     * Get the total amount of data that can be read from the current position.
     */
    virtual off64_t getRemainingLength(void) const = 0;

    /*
     * Open a new file descriptor that can be used to read this asset.
     * Returns -1 if you can not use the file descriptor (for example if the
     * asset is compressed).
     */
    virtual int openFileDescriptor(off64_t* outStart, off64_t* outLength) const = 0;

    /*
     * Return whether this asset's buffer is allocated in RAM (not mmapped).
     * Note: not virtual so it is safe to call even when being destroyed.
     */
    virtual bool isAllocated(void) const { return false; }

    /*
     * Get a string identifying the asset's source.  This might be a full
     * path, it might be a colon-separated list of identifiers.
     *
     * This is NOT intended to be used for anything except debug output.
     * DO NOT try to parse this or use it to open a file.
     */
    const char* getAssetSource(void) const { return mAssetSource.string(); }

protected:
    Asset(void);        // constructor; only invoked indirectly

    /* handle common seek() housekeeping */
    off64_t handleSeek(off64_t offset, int whence, off64_t curPosn, off64_t maxPosn);

    /* set the asset source string */
    void setAssetSource(const String8& path) { mAssetSource = path; }

    AccessMode getAccessMode(void) const { return mAccessMode; }

private:
    /* these operations are not implemented */
    Asset(const Asset& src);
    Asset& operator=(const Asset& src);

    /* AssetManager needs access to our "create" functions */
    friend class AssetManager;

    /*
     * Create the asset from a named file on disk.
     */
    static Asset* createFromFile(const char* fileName, AccessMode mode);

    /*
     * Create the asset from a named, compressed file on disk (e.g. ".gz").
     */
    static Asset* createFromCompressedFile(const char* fileName,
        AccessMode mode);

#if 0
    /*
     * Create the asset from a segment of an open file.  This will fail
     * if "offset" and "length" don't fit within the bounds of the file.
     *
     * The asset takes ownership of the file descriptor.
     */
    static Asset* createFromFileSegment(int fd, off64_t offset, size_t length,
        AccessMode mode);

    /*
     * Create from compressed data.  "fd" should be seeked to the start of
     * the compressed data.  This could be inside a gzip file or part of a
     * Zip archive.
     *
     * The asset takes ownership of the file descriptor.
     *
     * This may not verify the validity of the compressed data until first
     * use.
     */
    static Asset* createFromCompressedData(int fd, off64_t offset,
        int compressionMethod, size_t compressedLength,
        size_t uncompressedLength, AccessMode mode);
#endif

    /*
     * Create the asset from a memory-mapped file segment.
     *
     * The asset takes ownership of the FileMap.
     */
    static Asset* createFromUncompressedMap(FileMap* dataMap, AccessMode mode);

    /*
     * Create the asset from a memory-mapped file segment with compressed
     * data.  "method" is a Zip archive compression method constant.
     *
     * The asset takes ownership of the FileMap.
     */
    static Asset* createFromCompressedMap(FileMap* dataMap, int method,
        size_t uncompressedLen, AccessMode mode);


    /*
     * Create from a reference-counted chunk of shared memory.
     */
    // TODO

    AccessMode  mAccessMode;        // how the asset was opened
    String8    mAssetSource;       // debug string
    
    Asset*		mNext;				// linked list.
    Asset*		mPrev;
};


/*
 * ===========================================================================
 *
 * Innards follow.  Do not use these classes directly.
 */

/*
 * An asset based on an uncompressed file on disk.  It may encompass the
 * entire file or just a piece of it.  Access is through fread/fseek.
 */
class _FileAsset : public Asset {
public:
    _FileAsset(void);
    virtual ~_FileAsset(void);

    /*
     * Use a piece of an already-open file.
     *
     * On success, the object takes ownership of "fd".
     */
    status_t openChunk(const char* fileName, int fd, off64_t offset, size_t length);

    /*
     * Use a memory-mapped region.
     *
     * On success, the object takes ownership of "dataMap".
     */
    status_t openChunk(FileMap* dataMap);

    /*
     * Standard Asset interfaces.
     */
    virtual ssize_t read(void* buf, size_t count);
    virtual off64_t seek(off64_t offset, int whence);
    virtual void close(void);
    virtual const void* getBuffer(bool wordAligned);
    virtual off64_t getLength(void) const { return mLength; }
    virtual off64_t getRemainingLength(void) const { return mLength-mOffset; }
    virtual int openFileDescriptor(off64_t* outStart, off64_t* outLength) const;
    virtual bool isAllocated(void) const { return mBuf != NULL; }

private:
    off64_t     mStart;         // absolute file offset of start of chunk
    off64_t     mLength;        // length of the chunk
    off64_t     mOffset;        // current local offset, 0 == mStart
    FILE*       mFp;            // for read/seek
    char*       mFileName;      // for opening

    /*
     * To support getBuffer() we either need to read the entire thing into
     * a buffer or memory-map it.  For small files it's probably best to
     * just read them in.
     */
    enum { kReadVsMapThreshold = 4096 };

    FileMap*    mMap;           // for memory map
    unsigned char* mBuf;        // for read
    
    const void* ensureAlignment(FileMap* map);
};


/*
 * An asset based on compressed data in a file.
 */
class _CompressedAsset : public Asset {
public:
    _CompressedAsset(void);
    virtual ~_CompressedAsset(void);

    /*
     * Use a piece of an already-open file.
     *
     * On success, the object takes ownership of "fd".
     */
    status_t openChunk(int fd, off64_t offset, int compressionMethod,
        size_t uncompressedLen, size_t compressedLen);

    /*
     * Use a memory-mapped region.
     *
     * On success, the object takes ownership of "fd".
     */
    status_t openChunk(FileMap* dataMap, int compressionMethod,
        size_t uncompressedLen);

    /*
     * Standard Asset interfaces.
     */
    virtual ssize_t read(void* buf, size_t count);
    virtual off64_t seek(off64_t offset, int whence);
    virtual void close(void);
    virtual const void* getBuffer(bool wordAligned);
    virtual off64_t getLength(void) const { return mUncompressedLen; }
    virtual off64_t getRemainingLength(void) const { return mUncompressedLen-mOffset; }
    virtual int openFileDescriptor(off64_t* outStart, off64_t* outLength) const { return -1; }
    virtual bool isAllocated(void) const { return mBuf != NULL; }

private:
    off64_t     mStart;         // offset to start of compressed data
    off64_t     mCompressedLen; // length of the compressed data
    off64_t     mUncompressedLen; // length of the uncompressed data
    off64_t     mOffset;        // current offset, 0 == start of uncomp data

    FileMap*    mMap;           // for memory-mapped input
    int         mFd;            // for file input

    class StreamingZipInflater* mZipInflater;  // for streaming large compressed assets

    unsigned char*  mBuf;       // for getBuffer()
};

// need: shared mmap version?

}; // namespace android

#endif // __LIBS_ASSET_H

include/androidfw/AssetDir.h

deleted100644 → 0
+0 −145
Original line number Diff line number Diff line
/*
 * Copyright (C) 2006 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.
 */

//
// Access a chunk of the asset hierarchy as if it were a single directory.
//
#ifndef __LIBS_ASSETDIR_H
#define __LIBS_ASSETDIR_H

#include <androidfw/misc.h>
#include <utils/String8.h>
#include <utils/Vector.h>
#include <utils/SortedVector.h>
#include <sys/types.h>

namespace android {

/*
 * This provides vector-style access to a directory.  We do this rather
 * than modeling opendir/readdir access because it's simpler and the
 * nature of the operation requires us to have all data on hand anyway.
 *
 * The list of files will be sorted in ascending order by ASCII value.
 *
 * The contents are populated by our friend, the AssetManager.
 */
class AssetDir {
public:
    AssetDir(void)
        : mFileInfo(NULL)
        {}
    virtual ~AssetDir(void) {
        delete mFileInfo;
    }

    /*
     * Vector-style access.
     */
    size_t getFileCount(void) { return mFileInfo->size(); }
    const String8& getFileName(int idx) {
        return mFileInfo->itemAt(idx).getFileName();
    }
    const String8& getSourceName(int idx) {
        return mFileInfo->itemAt(idx).getSourceName();
    }

    /*
     * Get the type of a file (usually regular or directory).
     */
    FileType getFileType(int idx) {
        return mFileInfo->itemAt(idx).getFileType();
    }

private:
    /* these operations are not implemented */
    AssetDir(const AssetDir& src);
    const AssetDir& operator=(const AssetDir& src);

    friend class AssetManager;

    /*
     * This holds information about files in the asset hierarchy.
     */
    class FileInfo {
    public:
        FileInfo(void) {}
        FileInfo(const String8& path)      // useful for e.g. svect.indexOf
            : mFileName(path), mFileType(kFileTypeUnknown)
            {}
        ~FileInfo(void) {}
        FileInfo(const FileInfo& src) {
            copyMembers(src);
        }
        const FileInfo& operator= (const FileInfo& src) {
            if (this != &src)
                copyMembers(src);
            return *this;
        }

        void copyMembers(const FileInfo& src) {
            mFileName = src.mFileName;
            mFileType = src.mFileType;
            mSourceName = src.mSourceName;
        }

        /* need this for SortedVector; must compare only on file name */
        bool operator< (const FileInfo& rhs) const {
            return mFileName < rhs.mFileName;
        }

        /* used by AssetManager */
        bool operator== (const FileInfo& rhs) const {
            return mFileName == rhs.mFileName;
        }

        void set(const String8& path, FileType type) {
            mFileName = path;
            mFileType = type;
        }

        const String8& getFileName(void) const { return mFileName; }
        void setFileName(const String8& path) { mFileName = path; }

        FileType getFileType(void) const { return mFileType; }
        void setFileType(FileType type) { mFileType = type; }

        const String8& getSourceName(void) const { return mSourceName; }
        void setSourceName(const String8& path) { mSourceName = path; }

        /*
         * Handy utility for finding an entry in a sorted vector of FileInfo.
         * Returns the index of the matching entry, or -1 if none found.
         */
        static int findEntry(const SortedVector<FileInfo>* pVector,
            const String8& fileName);

    private:
        String8    mFileName;      // filename only
        FileType    mFileType;      // regular, directory, etc

        String8    mSourceName;    // currently debug-only
    };

    /* AssetManager uses this to initialize us */
    void setFileList(SortedVector<FileInfo>* list) { mFileInfo = list; }

    SortedVector<FileInfo>* mFileInfo;
};

}; // namespace android

#endif // __LIBS_ASSETDIR_H

include/androidfw/AssetManager.h

deleted100644 → 0
+0 −374

File deleted.

Preview size limit exceeded, changes collapsed.

include/androidfw/BackupHelpers.h

deleted100644 → 0
+0 −169
Original line number Diff line number Diff line
/*
 * 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.
 */

#ifndef _UTILS_BACKUP_HELPERS_H
#define _UTILS_BACKUP_HELPERS_H

#include <utils/Errors.h>
#include <utils/String8.h>
#include <utils/KeyedVector.h>

namespace android {

enum {
    BACKUP_HEADER_ENTITY_V1 = 0x61746144, // Data (little endian)
};

typedef struct {
    int type; // BACKUP_HEADER_ENTITY_V1
    int keyLen; // length of the key name, not including the null terminator
    int dataSize; // size of the data, not including the padding, -1 means delete
} entity_header_v1;

struct SnapshotHeader {
    int magic0;
    int fileCount;
    int magic1;
    int totalSize;
};

struct FileState {
    int modTime_sec;
    int modTime_nsec;
    int mode;
    int size;
    int crc32;
    int nameLen;
};

struct FileRec {
    String8 file;
    bool deleted;
    FileState s;
};


/**
 * Writes the data.
 *
 * If an error occurs, it poisons this object and all write calls will fail
 * with the error that occurred.
 */
class BackupDataWriter
{
public:
    BackupDataWriter(int fd);
    // does not close fd
    ~BackupDataWriter();

    status_t WriteEntityHeader(const String8& key, size_t dataSize);

    /* Note: WriteEntityData will write arbitrary data into the file without
     * validation or a previously-supplied header.  The full backup implementation
     * uses it this way to generate a controlled binary stream that is not
     * entity-structured.  If the implementation here is changed, either this
     * use case must remain valid, or the full backup implementation should be
     * adjusted to use some other appropriate mechanism.
     */
    status_t WriteEntityData(const void* data, size_t size);

    void SetKeyPrefix(const String8& keyPrefix);

private:
    explicit BackupDataWriter();
    status_t write_padding_for(int n);
    
    int m_fd;
    status_t m_status;
    ssize_t m_pos;
    int m_entityCount;
    String8 m_keyPrefix;
};

/**
 * Reads the data.
 *
 * If an error occurs, it poisons this object and all write calls will fail
 * with the error that occurred.
 */
class BackupDataReader
{
public:
    BackupDataReader(int fd);
    // does not close fd
    ~BackupDataReader();

    status_t Status();
    status_t ReadNextHeader(bool* done, int* type);

    bool HasEntities();
    status_t ReadEntityHeader(String8* key, size_t* dataSize);
    status_t SkipEntityData(); // must be called with the pointer at the beginning of the data.
    ssize_t ReadEntityData(void* data, size_t size);

private:
    explicit BackupDataReader();
    status_t skip_padding();
    
    int m_fd;
    bool m_done;
    status_t m_status;
    ssize_t m_pos;
    ssize_t m_dataEndPos;
    int m_entityCount;
    union {
        int type;
        entity_header_v1 entity;
    } m_header;
    String8 m_key;
};

int back_up_files(int oldSnapshotFD, BackupDataWriter* dataStream, int newSnapshotFD,
        char const* const* files, char const* const *keys, int fileCount);

int write_tarfile(const String8& packageName, const String8& domain,
        const String8& rootPath, const String8& filePath, BackupDataWriter* outputStream);

class RestoreHelperBase
{
public:
    RestoreHelperBase();
    ~RestoreHelperBase();

    status_t WriteFile(const String8& filename, BackupDataReader* in);
    status_t WriteSnapshot(int fd);

private:
    void* m_buf;
    bool m_loggedUnknownMetadata;
    KeyedVector<String8,FileRec> m_files;
};

#define TEST_BACKUP_HELPERS 1

#if TEST_BACKUP_HELPERS
int backup_helper_test_empty();
int backup_helper_test_four();
int backup_helper_test_files();
int backup_helper_test_null_base();
int backup_helper_test_missing_file();
int backup_helper_test_data_writer();
int backup_helper_test_data_reader();
#endif

} // namespace android

#endif // _UTILS_BACKUP_HELPERS_H

include/androidfw/CursorWindow.h

deleted100644 → 0
+0 −193
Original line number Diff line number Diff line
/*
 * Copyright (C) 2006 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.
 */

#ifndef _ANDROID__DATABASE_WINDOW_H
#define _ANDROID__DATABASE_WINDOW_H

#include <cutils/log.h>
#include <stddef.h>
#include <stdint.h>

#include <binder/Parcel.h>
#include <utils/String8.h>

#if LOG_NDEBUG

#define IF_LOG_WINDOW() if (false)
#define LOG_WINDOW(...)

#else

#define IF_LOG_WINDOW() IF_ALOG(LOG_DEBUG, "CursorWindow")
#define LOG_WINDOW(...) ALOG(LOG_DEBUG, "CursorWindow", __VA_ARGS__)

#endif

namespace android {

/**
 * This class stores a set of rows from a database in a buffer. The begining of the
 * window has first chunk of RowSlots, which are offsets to the row directory, followed by
 * an offset to the next chunk in a linked-list of additional chunk of RowSlots in case
 * the pre-allocated chunk isn't big enough to refer to all rows. Each row directory has a
 * FieldSlot per column, which has the size, offset, and type of the data for that field.
 * Note that the data types come from sqlite3.h.
 *
 * Strings are stored in UTF-8.
 */
class CursorWindow {
    CursorWindow(const String8& name, int ashmemFd,
            void* data, size_t size, bool readOnly);

public:
    /* Field types. */
    enum {
        FIELD_TYPE_NULL = 0,
        FIELD_TYPE_INTEGER = 1,
        FIELD_TYPE_FLOAT = 2,
        FIELD_TYPE_STRING = 3,
        FIELD_TYPE_BLOB = 4,
    };

    /* Opaque type that describes a field slot. */
    struct FieldSlot {
    private:
        int32_t type;
        union {
            double d;
            int64_t l;
            struct {
                uint32_t offset;
                uint32_t size;
            } buffer;
        } data;

        friend class CursorWindow;
    } __attribute((packed));

    ~CursorWindow();

    static status_t create(const String8& name, size_t size, CursorWindow** outCursorWindow);
    static status_t createFromParcel(Parcel* parcel, CursorWindow** outCursorWindow);

    status_t writeToParcel(Parcel* parcel);

    inline String8 name() { return mName; }
    inline size_t size() { return mSize; }
    inline size_t freeSpace() { return mSize - mHeader->freeOffset; }
    inline uint32_t getNumRows() { return mHeader->numRows; }
    inline uint32_t getNumColumns() { return mHeader->numColumns; }

    status_t clear();
    status_t setNumColumns(uint32_t numColumns);

    /**
     * Allocate a row slot and its directory.
     * The row is initialized will null entries for each field.
     */
    status_t allocRow();
    status_t freeLastRow();

    status_t putBlob(uint32_t row, uint32_t column, const void* value, size_t size);
    status_t putString(uint32_t row, uint32_t column, const char* value, size_t sizeIncludingNull);
    status_t putLong(uint32_t row, uint32_t column, int64_t value);
    status_t putDouble(uint32_t row, uint32_t column, double value);
    status_t putNull(uint32_t row, uint32_t column);

    /**
     * Gets the field slot at the specified row and column.
     * Returns null if the requested row or column is not in the window.
     */
    FieldSlot* getFieldSlot(uint32_t row, uint32_t column);

    inline int32_t getFieldSlotType(FieldSlot* fieldSlot) {
        return fieldSlot->type;
    }

    inline int64_t getFieldSlotValueLong(FieldSlot* fieldSlot) {
        return fieldSlot->data.l;
    }

    inline double getFieldSlotValueDouble(FieldSlot* fieldSlot) {
        return fieldSlot->data.d;
    }

    inline const char* getFieldSlotValueString(FieldSlot* fieldSlot,
            size_t* outSizeIncludingNull) {
        *outSizeIncludingNull = fieldSlot->data.buffer.size;
        return static_cast<char*>(offsetToPtr(fieldSlot->data.buffer.offset));
    }

    inline const void* getFieldSlotValueBlob(FieldSlot* fieldSlot, size_t* outSize) {
        *outSize = fieldSlot->data.buffer.size;
        return offsetToPtr(fieldSlot->data.buffer.offset);
    }

private:
    static const size_t ROW_SLOT_CHUNK_NUM_ROWS = 100;

    struct Header {
        // Offset of the lowest unused byte in the window.
        uint32_t freeOffset;

        // Offset of the first row slot chunk.
        uint32_t firstChunkOffset;

        uint32_t numRows;
        uint32_t numColumns;
    };

    struct RowSlot {
        uint32_t offset;
    };

    struct RowSlotChunk {
        RowSlot slots[ROW_SLOT_CHUNK_NUM_ROWS];
        uint32_t nextChunkOffset;
    };

    String8 mName;
    int mAshmemFd;
    void* mData;
    size_t mSize;
    bool mReadOnly;
    Header* mHeader;

    inline void* offsetToPtr(uint32_t offset) {
        return static_cast<uint8_t*>(mData) + offset;
    }

    inline uint32_t offsetFromPtr(void* ptr) {
        return static_cast<uint8_t*>(ptr) - static_cast<uint8_t*>(mData);
    }

    /**
     * Allocate a portion of the window. Returns the offset
     * of the allocation, or 0 if there isn't enough space.
     * If aligned is true, the allocation gets 4 byte alignment.
     */
    uint32_t alloc(size_t size, bool aligned = false);

    RowSlot* getRowSlot(uint32_t row);
    RowSlot* allocRowSlot();

    status_t putBlobOrString(uint32_t row, uint32_t column,
            const void* value, size_t size, int32_t type);
};

}; // namespace android

#endif
Loading