blob: 1bb04a7123a3e742a8fafbb7f71d0abac17a606d [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);
Christopher Tate4a627c72011-04-01 14:43:32 -070073
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 */
Joe Onorato4535e402009-05-15 09:07:06 -040081 status_t WriteEntityData(const void* data, size_t size);
82
Joe Onorato06290a42009-06-18 20:10:37 -070083 void SetKeyPrefix(const String8& keyPrefix);
84
Joe Onorato4535e402009-05-15 09:07:06 -040085private:
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;
Joe Onorato06290a42009-06-18 20:10:37 -070093 String8 m_keyPrefix;
Joe Onorato4535e402009-05-15 09:07:06 -040094};
95
Joe Onorato2e1da322009-05-15 18:20:19 -040096/**
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 */
102class BackupDataReader
103{
104public:
105 BackupDataReader(int fd);
106 // does not close fd
107 ~BackupDataReader();
108
109 status_t Status();
Joe Onorato5f15d152009-06-16 16:31:35 -0400110 status_t ReadNextHeader(bool* done, int* type);
Joe Onorato2e1da322009-05-15 18:20:19 -0400111
Joe Onorato2e1da322009-05-15 18:20:19 -0400112 bool HasEntities();
113 status_t ReadEntityHeader(String8* key, size_t* dataSize);
Christopher Tate4a627c72011-04-01 14:43:32 -0700114 status_t SkipEntityData(); // must be called with the pointer at the beginning of the data.
Joe Onoratoefd0fab2009-06-17 16:20:55 -0700115 ssize_t ReadEntityData(void* data, size_t size);
Joe Onorato2e1da322009-05-15 18:20:19 -0400116
117private:
118 explicit BackupDataReader();
119 status_t skip_padding();
120
121 int m_fd;
Joe Onorato5f15d152009-06-16 16:31:35 -0400122 bool m_done;
Joe Onorato2e1da322009-05-15 18:20:19 -0400123 status_t m_status;
124 ssize_t m_pos;
Joe Onorato5f15d152009-06-16 16:31:35 -0400125 ssize_t m_dataEndPos;
Joe Onorato2e1da322009-05-15 18:20:19 -0400126 int m_entityCount;
127 union {
128 int type;
Joe Onorato2e1da322009-05-15 18:20:19 -0400129 entity_header_v1 entity;
Joe Onorato2e1da322009-05-15 18:20:19 -0400130 } m_header;
Joe Onorato5d605dc2009-06-18 18:23:43 -0700131 String8 m_key;
Joe Onorato2e1da322009-05-15 18:20:19 -0400132};
133
Joe Onoratod2110db2009-05-19 13:41:21 -0700134int back_up_files(int oldSnapshotFD, BackupDataWriter* dataStream, int newSnapshotFD,
Joe Onorato23ecae32009-06-10 17:07:15 -0700135 char const* const* files, char const* const *keys, int fileCount);
Joe Onoratod2110db2009-05-19 13:41:21 -0700136
Christopher Tate4a627c72011-04-01 14:43:32 -0700137int write_tarfile(const String8& packageName, const String8& domain,
138 const String8& rootPath, const String8& filePath, BackupDataWriter* outputStream);
139
Joe Onoratod2d9ceb2009-06-18 13:11:18 -0700140class RestoreHelperBase
141{
142public:
143 RestoreHelperBase();
144 ~RestoreHelperBase();
145
146 status_t WriteFile(const String8& filename, BackupDataReader* in);
147 status_t WriteSnapshot(int fd);
148
149private:
150 void* m_buf;
Christopher Tate63bcb792009-06-24 13:57:29 -0700151 bool m_loggedUnknownMetadata;
Joe Onoratod2d9ceb2009-06-18 13:11:18 -0700152 KeyedVector<String8,FileRec> m_files;
153};
Joe Onoratod2110db2009-05-19 13:41:21 -0700154
155#define TEST_BACKUP_HELPERS 1
Joe Onorato3ad977b2009-05-05 11:50:51 -0700156
157#if TEST_BACKUP_HELPERS
158int backup_helper_test_empty();
159int backup_helper_test_four();
160int backup_helper_test_files();
Joe Onorato23ecae32009-06-10 17:07:15 -0700161int backup_helper_test_null_base();
Joe Onoratoce88cb12009-06-11 11:27:16 -0700162int backup_helper_test_missing_file();
Joe Onorato4535e402009-05-15 09:07:06 -0400163int backup_helper_test_data_writer();
Joe Onorato2e1da322009-05-15 18:20:19 -0400164int backup_helper_test_data_reader();
Joe Onorato3ad977b2009-05-05 11:50:51 -0700165#endif
166
Joe Onorato4535e402009-05-15 09:07:06 -0400167} // namespace android
168
Joe Onorato3ad977b2009-05-05 11:50:51 -0700169#endif // _UTILS_BACKUP_HELPERS_H