Home | History | Annotate | Download | only in androidfw
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef _UTILS_BACKUP_HELPERS_H
     18 #define _UTILS_BACKUP_HELPERS_H
     19 
     20 #include <utils/Errors.h>
     21 #include <utils/String8.h>
     22 #include <utils/KeyedVector.h>
     23 
     24 namespace android {
     25 
     26 enum {
     27     BACKUP_HEADER_ENTITY_V1 = 0x61746144, // Data (little endian)
     28 };
     29 
     30 typedef struct {
     31     int type; // BACKUP_HEADER_ENTITY_V1
     32     int keyLen; // length of the key name, not including the null terminator
     33     int dataSize; // size of the data, not including the padding, -1 means delete
     34 } entity_header_v1;
     35 
     36 struct SnapshotHeader {
     37     int magic0;
     38     int fileCount;
     39     int magic1;
     40     int totalSize;
     41 };
     42 
     43 struct FileState {
     44     int modTime_sec;
     45     int modTime_nsec;
     46     int mode;
     47     int size;
     48     int crc32;
     49     int nameLen;
     50 };
     51 
     52 struct FileRec {
     53     String8 file;
     54     bool deleted;
     55     FileState s;
     56 };
     57 
     58 
     59 /**
     60  * Writes the data.
     61  *
     62  * If an error occurs, it poisons this object and all write calls will fail
     63  * with the error that occurred.
     64  */
     65 class BackupDataWriter
     66 {
     67 public:
     68     BackupDataWriter(int fd);
     69     // does not close fd
     70     ~BackupDataWriter();
     71 
     72     status_t WriteEntityHeader(const String8& key, size_t dataSize);
     73 
     74     /* Note: WriteEntityData will write arbitrary data into the file without
     75      * validation or a previously-supplied header.  The full backup implementation
     76      * uses it this way to generate a controlled binary stream that is not
     77      * entity-structured.  If the implementation here is changed, either this
     78      * use case must remain valid, or the full backup implementation should be
     79      * adjusted to use some other appropriate mechanism.
     80      */
     81     status_t WriteEntityData(const void* data, size_t size);
     82 
     83     void SetKeyPrefix(const String8& keyPrefix);
     84 
     85 private:
     86     explicit BackupDataWriter();
     87     status_t write_padding_for(int n);
     88 
     89     int m_fd;
     90     status_t m_status;
     91     ssize_t m_pos;
     92     int m_entityCount;
     93     String8 m_keyPrefix;
     94 };
     95 
     96 /**
     97  * Reads the data.
     98  *
     99  * If an error occurs, it poisons this object and all write calls will fail
    100  * with the error that occurred.
    101  */
    102 class BackupDataReader
    103 {
    104 public:
    105     BackupDataReader(int fd);
    106     // does not close fd
    107     ~BackupDataReader();
    108 
    109     status_t Status();
    110     status_t ReadNextHeader(bool* done, int* type);
    111 
    112     bool HasEntities();
    113     status_t ReadEntityHeader(String8* key, size_t* dataSize);
    114     status_t SkipEntityData(); // must be called with the pointer at the beginning of the data.
    115     ssize_t ReadEntityData(void* data, size_t size);
    116 
    117 private:
    118     explicit BackupDataReader();
    119     status_t skip_padding();
    120 
    121     int m_fd;
    122     bool m_done;
    123     status_t m_status;
    124     ssize_t m_pos;
    125     ssize_t m_dataEndPos;
    126     int m_entityCount;
    127     union {
    128         int type;
    129         entity_header_v1 entity;
    130     } m_header;
    131     String8 m_key;
    132 };
    133 
    134 int back_up_files(int oldSnapshotFD, BackupDataWriter* dataStream, int newSnapshotFD,
    135         char const* const* files, char const* const *keys, int fileCount);
    136 
    137 int write_tarfile(const String8& packageName, const String8& domain,
    138         const String8& rootPath, const String8& filePath, BackupDataWriter* outputStream);
    139 
    140 class RestoreHelperBase
    141 {
    142 public:
    143     RestoreHelperBase();
    144     ~RestoreHelperBase();
    145 
    146     status_t WriteFile(const String8& filename, BackupDataReader* in);
    147     status_t WriteSnapshot(int fd);
    148 
    149 private:
    150     void* m_buf;
    151     bool m_loggedUnknownMetadata;
    152     KeyedVector<String8,FileRec> m_files;
    153 };
    154 
    155 #define TEST_BACKUP_HELPERS 1
    156 
    157 #if TEST_BACKUP_HELPERS
    158 int backup_helper_test_empty();
    159 int backup_helper_test_four();
    160 int backup_helper_test_files();
    161 int backup_helper_test_null_base();
    162 int backup_helper_test_missing_file();
    163 int backup_helper_test_data_writer();
    164 int backup_helper_test_data_reader();
    165 #endif
    166 
    167 } // namespace android
    168 
    169 #endif // _UTILS_BACKUP_HELPERS_H
    170