BackupHelpers.h revision 63bcb79dd437e70593b63cc5a87baab3251c2183
14535e40544aeb957d44fad75fbe5676effe03689Joe Onorato/*
24535e40544aeb957d44fad75fbe5676effe03689Joe Onorato * Copyright (C) 2009 The Android Open Source Project
34535e40544aeb957d44fad75fbe5676effe03689Joe Onorato *
44535e40544aeb957d44fad75fbe5676effe03689Joe Onorato * Licensed under the Apache License, Version 2.0 (the "License");
54535e40544aeb957d44fad75fbe5676effe03689Joe Onorato * you may not use this file except in compliance with the License.
64535e40544aeb957d44fad75fbe5676effe03689Joe Onorato * You may obtain a copy of the License at
74535e40544aeb957d44fad75fbe5676effe03689Joe Onorato *
84535e40544aeb957d44fad75fbe5676effe03689Joe Onorato *      http://www.apache.org/licenses/LICENSE-2.0
94535e40544aeb957d44fad75fbe5676effe03689Joe Onorato *
104535e40544aeb957d44fad75fbe5676effe03689Joe Onorato * Unless required by applicable law or agreed to in writing, software
114535e40544aeb957d44fad75fbe5676effe03689Joe Onorato * distributed under the License is distributed on an "AS IS" BASIS,
124535e40544aeb957d44fad75fbe5676effe03689Joe Onorato * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134535e40544aeb957d44fad75fbe5676effe03689Joe Onorato * See the License for the specific language governing permissions and
144535e40544aeb957d44fad75fbe5676effe03689Joe Onorato * limitations under the License.
154535e40544aeb957d44fad75fbe5676effe03689Joe Onorato */
164535e40544aeb957d44fad75fbe5676effe03689Joe Onorato
173ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato#ifndef _UTILS_BACKUP_HELPERS_H
183ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato#define _UTILS_BACKUP_HELPERS_H
193ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
204535e40544aeb957d44fad75fbe5676effe03689Joe Onorato#include <utils/Errors.h>
214535e40544aeb957d44fad75fbe5676effe03689Joe Onorato#include <utils/String8.h>
22d2d9ceb7305d593c1b767bbb05de0082a9af4109Joe Onorato#include <utils/KeyedVector.h>
234535e40544aeb957d44fad75fbe5676effe03689Joe Onorato
244535e40544aeb957d44fad75fbe5676effe03689Joe Onoratonamespace android {
254535e40544aeb957d44fad75fbe5676effe03689Joe Onorato
26d2110dbce071a236b6176de344ca797b737542ebJoe Onoratoenum {
27d2110dbce071a236b6176de344ca797b737542ebJoe Onorato    BACKUP_HEADER_ENTITY_V1 = 0x61746144, // Data (little endian)
28d2110dbce071a236b6176de344ca797b737542ebJoe Onorato};
293ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
302e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onoratotypedef struct {
31d2110dbce071a236b6176de344ca797b737542ebJoe Onorato    int type; // BACKUP_HEADER_ENTITY_V1
322e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onorato    int keyLen; // length of the key name, not including the null terminator
33d2110dbce071a236b6176de344ca797b737542ebJoe Onorato    int dataSize; // size of the data, not including the padding, -1 means delete
342e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onorato} entity_header_v1;
352e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onorato
36d2d9ceb7305d593c1b767bbb05de0082a9af4109Joe Onoratostruct SnapshotHeader {
37d2d9ceb7305d593c1b767bbb05de0082a9af4109Joe Onorato    int magic0;
38d2d9ceb7305d593c1b767bbb05de0082a9af4109Joe Onorato    int fileCount;
39d2d9ceb7305d593c1b767bbb05de0082a9af4109Joe Onorato    int magic1;
40d2d9ceb7305d593c1b767bbb05de0082a9af4109Joe Onorato    int totalSize;
41d2d9ceb7305d593c1b767bbb05de0082a9af4109Joe Onorato};
42d2d9ceb7305d593c1b767bbb05de0082a9af4109Joe Onorato
43d2d9ceb7305d593c1b767bbb05de0082a9af4109Joe Onoratostruct FileState {
44d2d9ceb7305d593c1b767bbb05de0082a9af4109Joe Onorato    int modTime_sec;
45d2d9ceb7305d593c1b767bbb05de0082a9af4109Joe Onorato    int modTime_nsec;
4611b157790234d3d2f116ce4c7ed1d3d00fb78bc3Christopher Tate    int mode;
47d2d9ceb7305d593c1b767bbb05de0082a9af4109Joe Onorato    int size;
48d2d9ceb7305d593c1b767bbb05de0082a9af4109Joe Onorato    int crc32;
49d2d9ceb7305d593c1b767bbb05de0082a9af4109Joe Onorato    int nameLen;
50d2d9ceb7305d593c1b767bbb05de0082a9af4109Joe Onorato};
51d2d9ceb7305d593c1b767bbb05de0082a9af4109Joe Onorato
52d2d9ceb7305d593c1b767bbb05de0082a9af4109Joe Onoratostruct FileRec {
53d2d9ceb7305d593c1b767bbb05de0082a9af4109Joe Onorato    String8 file;
54d2d9ceb7305d593c1b767bbb05de0082a9af4109Joe Onorato    bool deleted;
55d2d9ceb7305d593c1b767bbb05de0082a9af4109Joe Onorato    FileState s;
56d2d9ceb7305d593c1b767bbb05de0082a9af4109Joe Onorato};
57d2d9ceb7305d593c1b767bbb05de0082a9af4109Joe Onorato
582e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onorato
594535e40544aeb957d44fad75fbe5676effe03689Joe Onorato/**
602e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onorato * Writes the data.
614535e40544aeb957d44fad75fbe5676effe03689Joe Onorato *
624535e40544aeb957d44fad75fbe5676effe03689Joe Onorato * If an error occurs, it poisons this object and all write calls will fail
634535e40544aeb957d44fad75fbe5676effe03689Joe Onorato * with the error that occurred.
644535e40544aeb957d44fad75fbe5676effe03689Joe Onorato */
654535e40544aeb957d44fad75fbe5676effe03689Joe Onoratoclass BackupDataWriter
664535e40544aeb957d44fad75fbe5676effe03689Joe Onorato{
674535e40544aeb957d44fad75fbe5676effe03689Joe Onoratopublic:
684535e40544aeb957d44fad75fbe5676effe03689Joe Onorato    BackupDataWriter(int fd);
694535e40544aeb957d44fad75fbe5676effe03689Joe Onorato    // does not close fd
704535e40544aeb957d44fad75fbe5676effe03689Joe Onorato    ~BackupDataWriter();
714535e40544aeb957d44fad75fbe5676effe03689Joe Onorato
724535e40544aeb957d44fad75fbe5676effe03689Joe Onorato    status_t WriteEntityHeader(const String8& key, size_t dataSize);
734535e40544aeb957d44fad75fbe5676effe03689Joe Onorato    status_t WriteEntityData(const void* data, size_t size);
744535e40544aeb957d44fad75fbe5676effe03689Joe Onorato
7506290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato    void SetKeyPrefix(const String8& keyPrefix);
7606290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato
774535e40544aeb957d44fad75fbe5676effe03689Joe Onoratoprivate:
784535e40544aeb957d44fad75fbe5676effe03689Joe Onorato    explicit BackupDataWriter();
794535e40544aeb957d44fad75fbe5676effe03689Joe Onorato    status_t write_padding_for(int n);
804535e40544aeb957d44fad75fbe5676effe03689Joe Onorato
814535e40544aeb957d44fad75fbe5676effe03689Joe Onorato    int m_fd;
824535e40544aeb957d44fad75fbe5676effe03689Joe Onorato    status_t m_status;
834535e40544aeb957d44fad75fbe5676effe03689Joe Onorato    ssize_t m_pos;
844535e40544aeb957d44fad75fbe5676effe03689Joe Onorato    int m_entityCount;
8506290a4bb9b280fa14a2bbeb2d3ceb09396a78c3Joe Onorato    String8 m_keyPrefix;
864535e40544aeb957d44fad75fbe5676effe03689Joe Onorato};
874535e40544aeb957d44fad75fbe5676effe03689Joe Onorato
882e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onorato/**
892e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onorato * Reads the data.
902e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onorato *
912e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onorato * If an error occurs, it poisons this object and all write calls will fail
922e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onorato * with the error that occurred.
932e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onorato */
942e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onoratoclass BackupDataReader
952e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onorato{
962e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onoratopublic:
972e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onorato    BackupDataReader(int fd);
982e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onorato    // does not close fd
992e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onorato    ~BackupDataReader();
1002e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onorato
1012e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onorato    status_t Status();
1025f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato    status_t ReadNextHeader(bool* done, int* type);
1032e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onorato
1042e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onorato    bool HasEntities();
1052e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onorato    status_t ReadEntityHeader(String8* key, size_t* dataSize);
106d2110dbce071a236b6176de344ca797b737542ebJoe Onorato    status_t SkipEntityData(); // must be called with the pointer at the begining of the data.
107efd0fab04b96d7ab0c1d8bf3b79397c8621e31c5Joe Onorato    ssize_t ReadEntityData(void* data, size_t size);
1082e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onorato
1092e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onoratoprivate:
1102e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onorato    explicit BackupDataReader();
1112e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onorato    status_t skip_padding();
1122e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onorato
1132e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onorato    int m_fd;
1145f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato    bool m_done;
1152e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onorato    status_t m_status;
1162e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onorato    ssize_t m_pos;
1175f15d151b5101fadfe6cba1e8f4aa6367e8c603eJoe Onorato    ssize_t m_dataEndPos;
1182e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onorato    int m_entityCount;
1192e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onorato    union {
1202e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onorato        int type;
1212e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onorato        entity_header_v1 entity;
1222e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onorato    } m_header;
1235d605dc56b036232e885f6ec36b888b729673060Joe Onorato    String8 m_key;
1242e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onorato};
1252e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onorato
126d2110dbce071a236b6176de344ca797b737542ebJoe Onoratoint back_up_files(int oldSnapshotFD, BackupDataWriter* dataStream, int newSnapshotFD,
12723ecae3bbb60c5af940f3a22170d75eb6ac05b69Joe Onorato        char const* const* files, char const* const *keys, int fileCount);
128d2110dbce071a236b6176de344ca797b737542ebJoe Onorato
129d2d9ceb7305d593c1b767bbb05de0082a9af4109Joe Onoratoclass RestoreHelperBase
130d2d9ceb7305d593c1b767bbb05de0082a9af4109Joe Onorato{
131d2d9ceb7305d593c1b767bbb05de0082a9af4109Joe Onoratopublic:
132d2d9ceb7305d593c1b767bbb05de0082a9af4109Joe Onorato    RestoreHelperBase();
133d2d9ceb7305d593c1b767bbb05de0082a9af4109Joe Onorato    ~RestoreHelperBase();
134d2d9ceb7305d593c1b767bbb05de0082a9af4109Joe Onorato
135d2d9ceb7305d593c1b767bbb05de0082a9af4109Joe Onorato    status_t WriteFile(const String8& filename, BackupDataReader* in);
136d2d9ceb7305d593c1b767bbb05de0082a9af4109Joe Onorato    status_t WriteSnapshot(int fd);
137d2d9ceb7305d593c1b767bbb05de0082a9af4109Joe Onorato
138d2d9ceb7305d593c1b767bbb05de0082a9af4109Joe Onoratoprivate:
139d2d9ceb7305d593c1b767bbb05de0082a9af4109Joe Onorato    void* m_buf;
14063bcb79dd437e70593b63cc5a87baab3251c2183Christopher Tate    bool m_loggedUnknownMetadata;
141d2d9ceb7305d593c1b767bbb05de0082a9af4109Joe Onorato    KeyedVector<String8,FileRec> m_files;
142d2d9ceb7305d593c1b767bbb05de0082a9af4109Joe Onorato};
143d2110dbce071a236b6176de344ca797b737542ebJoe Onorato
144d2110dbce071a236b6176de344ca797b737542ebJoe Onorato#define TEST_BACKUP_HELPERS 1
1453ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
1463ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato#if TEST_BACKUP_HELPERS
1473ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onoratoint backup_helper_test_empty();
1483ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onoratoint backup_helper_test_four();
1493ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onoratoint backup_helper_test_files();
15023ecae3bbb60c5af940f3a22170d75eb6ac05b69Joe Onoratoint backup_helper_test_null_base();
151ce88cb15b52998e16c3ba548a4ec49117a835e21Joe Onoratoint backup_helper_test_missing_file();
1524535e40544aeb957d44fad75fbe5676effe03689Joe Onoratoint backup_helper_test_data_writer();
1532e1da32203b7f6df76023f25a7382a31fad6b19dJoe Onoratoint backup_helper_test_data_reader();
1543ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato#endif
1553ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato
1564535e40544aeb957d44fad75fbe5676effe03689Joe Onorato} // namespace android
1574535e40544aeb957d44fad75fbe5676effe03689Joe Onorato
1583ad977b41c6e4ef30c2f4f316b909b742ffc04aaJoe Onorato#endif // _UTILS_BACKUP_HELPERS_H
159