blob: fc701fd3a565fd04fe0c52a511f1207c8de56e8a [file] [log] [blame]
Joe Onorato8d626d62009-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 Onorato0c4863b2009-05-05 11:50:51 -070017#ifndef _UTILS_BACKUP_HELPERS_H
18#define _UTILS_BACKUP_HELPERS_H
19
Joe Onorato8d626d62009-05-15 09:07:06 -040020#include <utils/Errors.h>
21#include <utils/String8.h>
Joe Onoratoda1430b2009-06-18 13:11:18 -070022#include <utils/KeyedVector.h>
Joe Onorato8d626d62009-05-15 09:07:06 -040023
24namespace android {
25
Joe Onorato473b6e22009-05-19 13:41:21 -070026enum {
Joe Onorato473b6e22009-05-19 13:41:21 -070027 BACKUP_HEADER_ENTITY_V1 = 0x61746144, // Data (little endian)
Joe Onorato473b6e22009-05-19 13:41:21 -070028};
Joe Onorato0c4863b2009-05-05 11:50:51 -070029
Joe Onoratod502f052009-05-15 18:20:19 -040030typedef struct {
Joe Onorato473b6e22009-05-19 13:41:21 -070031 int type; // BACKUP_HEADER_ENTITY_V1
Joe Onoratod502f052009-05-15 18:20:19 -040032 int keyLen; // length of the key name, not including the null terminator
Joe Onorato473b6e22009-05-19 13:41:21 -070033 int dataSize; // size of the data, not including the padding, -1 means delete
Joe Onoratod502f052009-05-15 18:20:19 -040034} entity_header_v1;
35
Joe Onoratoda1430b2009-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;
46 int size;
47 int crc32;
48 int nameLen;
49};
50
51struct FileRec {
52 String8 file;
53 bool deleted;
54 FileState s;
55};
56
Joe Onoratod502f052009-05-15 18:20:19 -040057
Joe Onorato8d626d62009-05-15 09:07:06 -040058/**
Joe Onoratod502f052009-05-15 18:20:19 -040059 * Writes the data.
Joe Onorato8d626d62009-05-15 09:07:06 -040060 *
61 * If an error occurs, it poisons this object and all write calls will fail
62 * with the error that occurred.
63 */
64class BackupDataWriter
65{
66public:
67 BackupDataWriter(int fd);
68 // does not close fd
69 ~BackupDataWriter();
70
Joe Onorato8d626d62009-05-15 09:07:06 -040071 status_t WriteEntityHeader(const String8& key, size_t dataSize);
72 status_t WriteEntityData(const void* data, size_t size);
73
Joe Onorato8d626d62009-05-15 09:07:06 -040074private:
75 explicit BackupDataWriter();
76 status_t write_padding_for(int n);
77
78 int m_fd;
79 status_t m_status;
80 ssize_t m_pos;
81 int m_entityCount;
82};
83
Joe Onoratod502f052009-05-15 18:20:19 -040084/**
85 * Reads the data.
86 *
87 * If an error occurs, it poisons this object and all write calls will fail
88 * with the error that occurred.
89 */
90class BackupDataReader
91{
92public:
93 BackupDataReader(int fd);
94 // does not close fd
95 ~BackupDataReader();
96
97 status_t Status();
Joe Onorato03aa8d72009-06-16 16:31:35 -040098 status_t ReadNextHeader(bool* done, int* type);
Joe Onoratod502f052009-05-15 18:20:19 -040099
Joe Onoratod502f052009-05-15 18:20:19 -0400100 bool HasEntities();
101 status_t ReadEntityHeader(String8* key, size_t* dataSize);
Joe Onorato473b6e22009-05-19 13:41:21 -0700102 status_t SkipEntityData(); // must be called with the pointer at the begining of the data.
Joe Onoratof509f662009-06-17 16:20:55 -0700103 ssize_t ReadEntityData(void* data, size_t size);
Joe Onoratod502f052009-05-15 18:20:19 -0400104
105private:
106 explicit BackupDataReader();
107 status_t skip_padding();
108
109 int m_fd;
Joe Onorato03aa8d72009-06-16 16:31:35 -0400110 bool m_done;
Joe Onoratod502f052009-05-15 18:20:19 -0400111 status_t m_status;
112 ssize_t m_pos;
Joe Onorato03aa8d72009-06-16 16:31:35 -0400113 ssize_t m_dataEndPos;
Joe Onoratod502f052009-05-15 18:20:19 -0400114 int m_entityCount;
115 union {
116 int type;
Joe Onoratod502f052009-05-15 18:20:19 -0400117 entity_header_v1 entity;
Joe Onoratod502f052009-05-15 18:20:19 -0400118 } m_header;
119};
120
Joe Onorato473b6e22009-05-19 13:41:21 -0700121int back_up_files(int oldSnapshotFD, BackupDataWriter* dataStream, int newSnapshotFD,
Joe Onorato0ad61202009-06-10 17:07:15 -0700122 char const* const* files, char const* const *keys, int fileCount);
Joe Onorato473b6e22009-05-19 13:41:21 -0700123
Joe Onoratoda1430b2009-06-18 13:11:18 -0700124class RestoreHelperBase
125{
126public:
127 RestoreHelperBase();
128 ~RestoreHelperBase();
129
130 status_t WriteFile(const String8& filename, BackupDataReader* in);
131 status_t WriteSnapshot(int fd);
132
133private:
134 void* m_buf;
135 KeyedVector<String8,FileRec> m_files;
136};
Joe Onorato473b6e22009-05-19 13:41:21 -0700137
138#define TEST_BACKUP_HELPERS 1
Joe Onorato0c4863b2009-05-05 11:50:51 -0700139
140#if TEST_BACKUP_HELPERS
141int backup_helper_test_empty();
142int backup_helper_test_four();
143int backup_helper_test_files();
Joe Onorato0ad61202009-06-10 17:07:15 -0700144int backup_helper_test_null_base();
Joe Onorato1a9e19a2009-06-11 11:27:16 -0700145int backup_helper_test_missing_file();
Joe Onorato8d626d62009-05-15 09:07:06 -0400146int backup_helper_test_data_writer();
Joe Onoratod502f052009-05-15 18:20:19 -0400147int backup_helper_test_data_reader();
Joe Onorato0c4863b2009-05-05 11:50:51 -0700148#endif
149
Joe Onorato8d626d62009-05-15 09:07:06 -0400150} // namespace android
151
Joe Onorato0c4863b2009-05-05 11:50:51 -0700152#endif // _UTILS_BACKUP_HELPERS_H