blob: 3ca8ad2d5c6ba8dab15613e3328d838c7959675a [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>
22
23namespace android {
24
Joe Onorato473b6e22009-05-19 13:41:21 -070025enum {
Joe Onorato473b6e22009-05-19 13:41:21 -070026 BACKUP_HEADER_ENTITY_V1 = 0x61746144, // Data (little endian)
Joe Onorato473b6e22009-05-19 13:41:21 -070027};
Joe Onorato0c4863b2009-05-05 11:50:51 -070028
Joe Onoratod502f052009-05-15 18:20:19 -040029typedef struct {
Joe Onorato473b6e22009-05-19 13:41:21 -070030 int type; // BACKUP_HEADER_ENTITY_V1
Joe Onoratod502f052009-05-15 18:20:19 -040031 int keyLen; // length of the key name, not including the null terminator
Joe Onorato473b6e22009-05-19 13:41:21 -070032 int dataSize; // size of the data, not including the padding, -1 means delete
Joe Onoratod502f052009-05-15 18:20:19 -040033} entity_header_v1;
34
Joe Onoratod502f052009-05-15 18:20:19 -040035
Joe Onorato8d626d62009-05-15 09:07:06 -040036/**
Joe Onoratod502f052009-05-15 18:20:19 -040037 * Writes the data.
Joe Onorato8d626d62009-05-15 09:07:06 -040038 *
39 * If an error occurs, it poisons this object and all write calls will fail
40 * with the error that occurred.
41 */
42class BackupDataWriter
43{
44public:
45 BackupDataWriter(int fd);
46 // does not close fd
47 ~BackupDataWriter();
48
Joe Onorato8d626d62009-05-15 09:07:06 -040049 status_t WriteEntityHeader(const String8& key, size_t dataSize);
50 status_t WriteEntityData(const void* data, size_t size);
51
Joe Onorato8d626d62009-05-15 09:07:06 -040052private:
53 explicit BackupDataWriter();
54 status_t write_padding_for(int n);
55
56 int m_fd;
57 status_t m_status;
58 ssize_t m_pos;
59 int m_entityCount;
60};
61
Joe Onoratod502f052009-05-15 18:20:19 -040062/**
63 * Reads the data.
64 *
65 * If an error occurs, it poisons this object and all write calls will fail
66 * with the error that occurred.
67 */
68class BackupDataReader
69{
70public:
71 BackupDataReader(int fd);
72 // does not close fd
73 ~BackupDataReader();
74
75 status_t Status();
Joe Onorato03aa8d72009-06-16 16:31:35 -040076 status_t ReadNextHeader(bool* done, int* type);
Joe Onoratod502f052009-05-15 18:20:19 -040077
Joe Onoratod502f052009-05-15 18:20:19 -040078 bool HasEntities();
79 status_t ReadEntityHeader(String8* key, size_t* dataSize);
Joe Onorato473b6e22009-05-19 13:41:21 -070080 status_t SkipEntityData(); // must be called with the pointer at the begining of the data.
Joe Onoratod502f052009-05-15 18:20:19 -040081 status_t ReadEntityData(void* data, size_t size);
Joe Onoratod502f052009-05-15 18:20:19 -040082
83private:
84 explicit BackupDataReader();
85 status_t skip_padding();
86
87 int m_fd;
Joe Onorato03aa8d72009-06-16 16:31:35 -040088 bool m_done;
Joe Onoratod502f052009-05-15 18:20:19 -040089 status_t m_status;
90 ssize_t m_pos;
Joe Onorato03aa8d72009-06-16 16:31:35 -040091 ssize_t m_dataEndPos;
Joe Onoratod502f052009-05-15 18:20:19 -040092 int m_entityCount;
93 union {
94 int type;
Joe Onoratod502f052009-05-15 18:20:19 -040095 entity_header_v1 entity;
Joe Onoratod502f052009-05-15 18:20:19 -040096 } m_header;
97};
98
Joe Onorato473b6e22009-05-19 13:41:21 -070099int back_up_files(int oldSnapshotFD, BackupDataWriter* dataStream, int newSnapshotFD,
Joe Onorato0ad61202009-06-10 17:07:15 -0700100 char const* const* files, char const* const *keys, int fileCount);
Joe Onorato473b6e22009-05-19 13:41:21 -0700101
102
103#define TEST_BACKUP_HELPERS 1
Joe Onorato0c4863b2009-05-05 11:50:51 -0700104
105#if TEST_BACKUP_HELPERS
106int backup_helper_test_empty();
107int backup_helper_test_four();
108int backup_helper_test_files();
Joe Onorato0ad61202009-06-10 17:07:15 -0700109int backup_helper_test_null_base();
Joe Onorato1a9e19a2009-06-11 11:27:16 -0700110int backup_helper_test_missing_file();
Joe Onorato8d626d62009-05-15 09:07:06 -0400111int backup_helper_test_data_writer();
Joe Onoratod502f052009-05-15 18:20:19 -0400112int backup_helper_test_data_reader();
Joe Onorato0c4863b2009-05-05 11:50:51 -0700113#endif
114
Joe Onorato8d626d62009-05-15 09:07:06 -0400115} // namespace android
116
Joe Onorato0c4863b2009-05-05 11:50:51 -0700117#endif // _UTILS_BACKUP_HELPERS_H