blob: b1f504512f51d95fd89ca1f6e4c5f27e6c148410 [file] [log] [blame]
Joe Onorato4535e402009-05-15 09:07:06 -04001/*
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
Joe Onorato3ad977b2009-05-05 11:50:51 -070017#ifndef _UTILS_BACKUP_HELPERS_H
18#define _UTILS_BACKUP_HELPERS_H
19
Joe Onorato4535e402009-05-15 09:07:06 -040020#include <utils/Errors.h>
21#include <utils/String8.h>
Joe Onoratod2d9ceb2009-06-18 13:11:18 -070022#include <utils/KeyedVector.h>
Joe Onorato4535e402009-05-15 09:07:06 -040023
24namespace android {
25
Joe Onoratod2110db2009-05-19 13:41:21 -070026enum {
Joe Onoratod2110db2009-05-19 13:41:21 -070027 BACKUP_HEADER_ENTITY_V1 = 0x61746144, // Data (little endian)
Joe Onoratod2110db2009-05-19 13:41:21 -070028};
Joe Onorato3ad977b2009-05-05 11:50:51 -070029
Joe Onorato2e1da322009-05-15 18:20:19 -040030typedef struct {
Joe Onoratod2110db2009-05-19 13:41:21 -070031 int type; // BACKUP_HEADER_ENTITY_V1
Joe Onorato2e1da322009-05-15 18:20:19 -040032 int keyLen; // length of the key name, not including the null terminator
Joe Onoratod2110db2009-05-19 13:41:21 -070033 int dataSize; // size of the data, not including the padding, -1 means delete
Joe Onorato2e1da322009-05-15 18:20:19 -040034} entity_header_v1;
35
Joe Onoratod2d9ceb2009-06-18 13:11:18 -070036struct 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;
Christopher Tate11b15772009-06-23 13:03:00 -070046 int mode;
Joe Onoratod2d9ceb2009-06-18 13:11:18 -070047 int size;
48 int crc32;
49 int nameLen;
50};
51
52struct FileRec {
53 String8 file;
54 bool deleted;
55 FileState s;
56};
57
Joe Onorato2e1da322009-05-15 18:20:19 -040058
Joe Onorato4535e402009-05-15 09:07:06 -040059/**
Joe Onorato2e1da322009-05-15 18:20:19 -040060 * Writes the data.
Joe Onorato4535e402009-05-15 09:07:06 -040061 *
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
Joe Onorato4535e402009-05-15 09:07:06 -040072 status_t WriteEntityHeader(const String8& key, size_t dataSize);
73 status_t WriteEntityData(const void* data, size_t size);
74
Joe Onorato06290a42009-06-18 20:10:37 -070075 void SetKeyPrefix(const String8& keyPrefix);
76
Joe Onorato4535e402009-05-15 09:07:06 -040077private:
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;
Joe Onorato06290a42009-06-18 20:10:37 -070085 String8 m_keyPrefix;
Joe Onorato4535e402009-05-15 09:07:06 -040086};
87
Joe Onorato2e1da322009-05-15 18:20:19 -040088/**
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();
Joe Onorato5f15d152009-06-16 16:31:35 -0400102 status_t ReadNextHeader(bool* done, int* type);
Joe Onorato2e1da322009-05-15 18:20:19 -0400103
Joe Onorato2e1da322009-05-15 18:20:19 -0400104 bool HasEntities();
105 status_t ReadEntityHeader(String8* key, size_t* dataSize);
Joe Onoratod2110db2009-05-19 13:41:21 -0700106 status_t SkipEntityData(); // must be called with the pointer at the begining of the data.
Joe Onoratoefd0fab2009-06-17 16:20:55 -0700107 ssize_t ReadEntityData(void* data, size_t size);
Joe Onorato2e1da322009-05-15 18:20:19 -0400108
109private:
110 explicit BackupDataReader();
111 status_t skip_padding();
112
113 int m_fd;
Joe Onorato5f15d152009-06-16 16:31:35 -0400114 bool m_done;
Joe Onorato2e1da322009-05-15 18:20:19 -0400115 status_t m_status;
116 ssize_t m_pos;
Joe Onorato5f15d152009-06-16 16:31:35 -0400117 ssize_t m_dataEndPos;
Joe Onorato2e1da322009-05-15 18:20:19 -0400118 int m_entityCount;
119 union {
120 int type;
Joe Onorato2e1da322009-05-15 18:20:19 -0400121 entity_header_v1 entity;
Joe Onorato2e1da322009-05-15 18:20:19 -0400122 } m_header;
Joe Onorato5d605dc2009-06-18 18:23:43 -0700123 String8 m_key;
Joe Onorato2e1da322009-05-15 18:20:19 -0400124};
125
Joe Onoratod2110db2009-05-19 13:41:21 -0700126int back_up_files(int oldSnapshotFD, BackupDataWriter* dataStream, int newSnapshotFD,
Joe Onorato23ecae32009-06-10 17:07:15 -0700127 char const* const* files, char const* const *keys, int fileCount);
Joe Onoratod2110db2009-05-19 13:41:21 -0700128
Joe Onoratod2d9ceb2009-06-18 13:11:18 -0700129class 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;
Christopher Tate63bcb792009-06-24 13:57:29 -0700140 bool m_loggedUnknownMetadata;
Joe Onoratod2d9ceb2009-06-18 13:11:18 -0700141 KeyedVector<String8,FileRec> m_files;
142};
Joe Onoratod2110db2009-05-19 13:41:21 -0700143
144#define TEST_BACKUP_HELPERS 1
Joe Onorato3ad977b2009-05-05 11:50:51 -0700145
146#if TEST_BACKUP_HELPERS
147int backup_helper_test_empty();
148int backup_helper_test_four();
149int backup_helper_test_files();
Joe Onorato23ecae32009-06-10 17:07:15 -0700150int backup_helper_test_null_base();
Joe Onoratoce88cb12009-06-11 11:27:16 -0700151int backup_helper_test_missing_file();
Joe Onorato4535e402009-05-15 09:07:06 -0400152int backup_helper_test_data_writer();
Joe Onorato2e1da322009-05-15 18:20:19 -0400153int backup_helper_test_data_reader();
Joe Onorato3ad977b2009-05-05 11:50:51 -0700154#endif
155
Joe Onorato4535e402009-05-15 09:07:06 -0400156} // namespace android
157
Joe Onorato3ad977b2009-05-05 11:50:51 -0700158#endif // _UTILS_BACKUP_HELPERS_H