BackupHelpers.h revision 63bcb79dd437e70593b63cc5a87baab3251c2183
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
24namespace android {
25
26enum {
27    BACKUP_HEADER_ENTITY_V1 = 0x61746144, // Data (little endian)
28};
29
30typedef 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
36struct SnapshotHeader {
37    int magic0;
38    int fileCount;
39    int magic1;
40    int totalSize;
41};
42
43struct FileState {
44    int modTime_sec;
45    int modTime_nsec;
46    int mode;
47    int size;
48    int crc32;
49    int nameLen;
50};
51
52struct 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 */
65class BackupDataWriter
66{
67public:
68    BackupDataWriter(int fd);
69    // does not close fd
70    ~BackupDataWriter();
71
72    status_t WriteEntityHeader(const String8& key, size_t dataSize);
73    status_t WriteEntityData(const void* data, size_t size);
74
75    void SetKeyPrefix(const String8& keyPrefix);
76
77private:
78    explicit BackupDataWriter();
79    status_t write_padding_for(int n);
80
81    int m_fd;
82    status_t m_status;
83    ssize_t m_pos;
84    int m_entityCount;
85    String8 m_keyPrefix;
86};
87
88/**
89 * Reads the data.
90 *
91 * If an error occurs, it poisons this object and all write calls will fail
92 * with the error that occurred.
93 */
94class BackupDataReader
95{
96public:
97    BackupDataReader(int fd);
98    // does not close fd
99    ~BackupDataReader();
100
101    status_t Status();
102    status_t ReadNextHeader(bool* done, int* type);
103
104    bool HasEntities();
105    status_t ReadEntityHeader(String8* key, size_t* dataSize);
106    status_t SkipEntityData(); // must be called with the pointer at the begining of the data.
107    ssize_t ReadEntityData(void* data, size_t size);
108
109private:
110    explicit BackupDataReader();
111    status_t skip_padding();
112
113    int m_fd;
114    bool m_done;
115    status_t m_status;
116    ssize_t m_pos;
117    ssize_t m_dataEndPos;
118    int m_entityCount;
119    union {
120        int type;
121        entity_header_v1 entity;
122    } m_header;
123    String8 m_key;
124};
125
126int back_up_files(int oldSnapshotFD, BackupDataWriter* dataStream, int newSnapshotFD,
127        char const* const* files, char const* const *keys, int fileCount);
128
129class RestoreHelperBase
130{
131public:
132    RestoreHelperBase();
133    ~RestoreHelperBase();
134
135    status_t WriteFile(const String8& filename, BackupDataReader* in);
136    status_t WriteSnapshot(int fd);
137
138private:
139    void* m_buf;
140    bool m_loggedUnknownMetadata;
141    KeyedVector<String8,FileRec> m_files;
142};
143
144#define TEST_BACKUP_HELPERS 1
145
146#if TEST_BACKUP_HELPERS
147int backup_helper_test_empty();
148int backup_helper_test_four();
149int backup_helper_test_files();
150int backup_helper_test_null_base();
151int backup_helper_test_missing_file();
152int backup_helper_test_data_writer();
153int backup_helper_test_data_reader();
154#endif
155
156} // namespace android
157
158#endif // _UTILS_BACKUP_HELPERS_H
159