blob: 1d0daa741e4c688c7b6babbdaa489c0c340fb21a [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 {
26 BACKUP_HEADER_APP_V1 = 0x31707041, // App1 (little endian)
27 BACKUP_HEADER_ENTITY_V1 = 0x61746144, // Data (little endian)
28 BACKUP_FOOTER_APP_V1 = 0x746f6f46, // Foot (little endian)
29};
Joe Onorato0c4863b2009-05-05 11:50:51 -070030
Joe Onoratod502f052009-05-15 18:20:19 -040031// the sizes of all of these match.
32typedef struct {
Joe Onorato473b6e22009-05-19 13:41:21 -070033 int type; // == BACKUP_HEADER_APP_V1
Joe Onoratod502f052009-05-15 18:20:19 -040034 int packageLen; // length of the name of the package that follows, not including the null.
35 int cookie;
36} app_header_v1;
37
38typedef struct {
Joe Onorato473b6e22009-05-19 13:41:21 -070039 int type; // BACKUP_HEADER_ENTITY_V1
Joe Onoratod502f052009-05-15 18:20:19 -040040 int keyLen; // length of the key name, not including the null terminator
Joe Onorato473b6e22009-05-19 13:41:21 -070041 int dataSize; // size of the data, not including the padding, -1 means delete
Joe Onoratod502f052009-05-15 18:20:19 -040042} entity_header_v1;
43
44typedef struct {
Joe Onorato473b6e22009-05-19 13:41:21 -070045 int type; // BACKUP_FOOTER_APP_V1
Joe Onoratod502f052009-05-15 18:20:19 -040046 int entityCount; // the number of entities that were written
47 int cookie;
48} app_footer_v1;
49
50
Joe Onorato8d626d62009-05-15 09:07:06 -040051/**
Joe Onoratod502f052009-05-15 18:20:19 -040052 * Writes the data.
Joe Onorato8d626d62009-05-15 09:07:06 -040053 *
54 * If an error occurs, it poisons this object and all write calls will fail
55 * with the error that occurred.
56 */
57class BackupDataWriter
58{
59public:
60 BackupDataWriter(int fd);
61 // does not close fd
62 ~BackupDataWriter();
63
Joe Onoratod502f052009-05-15 18:20:19 -040064 status_t WriteAppHeader(const String8& packageName, int cookie);
Joe Onorato8d626d62009-05-15 09:07:06 -040065
66 status_t WriteEntityHeader(const String8& key, size_t dataSize);
67 status_t WriteEntityData(const void* data, size_t size);
68
Joe Onoratod502f052009-05-15 18:20:19 -040069 status_t WriteAppFooter(int cookie);
Joe Onorato8d626d62009-05-15 09:07:06 -040070
71private:
72 explicit BackupDataWriter();
73 status_t write_padding_for(int n);
74
75 int m_fd;
76 status_t m_status;
77 ssize_t m_pos;
78 int m_entityCount;
79};
80
Joe Onoratod502f052009-05-15 18:20:19 -040081/**
82 * Reads the data.
83 *
84 * If an error occurs, it poisons this object and all write calls will fail
85 * with the error that occurred.
86 */
87class BackupDataReader
88{
89public:
90 BackupDataReader(int fd);
91 // does not close fd
92 ~BackupDataReader();
93
94 status_t Status();
Joe Onorato473b6e22009-05-19 13:41:21 -070095 status_t ReadNextHeader(int* type = NULL);
Joe Onoratod502f052009-05-15 18:20:19 -040096
97 status_t ReadAppHeader(String8* packageName, int* cookie);
98 bool HasEntities();
99 status_t ReadEntityHeader(String8* key, size_t* dataSize);
Joe Onorato473b6e22009-05-19 13:41:21 -0700100 status_t SkipEntityData(); // must be called with the pointer at the begining of the data.
Joe Onoratod502f052009-05-15 18:20:19 -0400101 status_t ReadEntityData(void* data, size_t size);
102 status_t ReadAppFooter(int* cookie);
103
104private:
105 explicit BackupDataReader();
106 status_t skip_padding();
107
108 int m_fd;
109 status_t m_status;
110 ssize_t m_pos;
111 int m_entityCount;
112 union {
113 int type;
114 app_header_v1 app;
115 entity_header_v1 entity;
116 app_footer_v1 footer;
117 } m_header;
118};
119
Joe Onorato473b6e22009-05-19 13:41:21 -0700120int back_up_files(int oldSnapshotFD, BackupDataWriter* dataStream, int newSnapshotFD,
Joe Onorato0ad61202009-06-10 17:07:15 -0700121 char const* const* files, char const* const *keys, int fileCount);
Joe Onorato473b6e22009-05-19 13:41:21 -0700122
123
124#define TEST_BACKUP_HELPERS 1
Joe Onorato0c4863b2009-05-05 11:50:51 -0700125
126#if TEST_BACKUP_HELPERS
127int backup_helper_test_empty();
128int backup_helper_test_four();
129int backup_helper_test_files();
Joe Onorato0ad61202009-06-10 17:07:15 -0700130int backup_helper_test_null_base();
Joe Onorato8d626d62009-05-15 09:07:06 -0400131int backup_helper_test_data_writer();
Joe Onoratod502f052009-05-15 18:20:19 -0400132int backup_helper_test_data_reader();
Joe Onorato0c4863b2009-05-05 11:50:51 -0700133#endif
134
Joe Onorato8d626d62009-05-15 09:07:06 -0400135} // namespace android
136
Joe Onorato0c4863b2009-05-05 11:50:51 -0700137#endif // _UTILS_BACKUP_HELPERS_H