blob: 0c59fec59ca7f2172363c673e07a65f840c3715d [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 Onoratoc7bbc692009-05-13 18:57:29 -040025int back_up_files(int oldSnapshotFD, int oldDataStream, int newSnapshotFD,
Joe Onorato0c4863b2009-05-05 11:50:51 -070026 char const* fileBase, char const* const* files, int fileCount);
27
Joe Onoratod502f052009-05-15 18:20:19 -040028// the sizes of all of these match.
29typedef struct {
30 int type; // == APP_MAGIC_V1
31 int packageLen; // length of the name of the package that follows, not including the null.
32 int cookie;
33} app_header_v1;
34
35typedef struct {
36 int type; // ENTITY_MAGIC_V1
37 int keyLen; // length of the key name, not including the null terminator
38 int dataSize; // size of the data, not including the padding
39} entity_header_v1;
40
41typedef struct {
42 int type; // FOOTER_MAGIC_V1
43 int entityCount; // the number of entities that were written
44 int cookie;
45} app_footer_v1;
46
47
Joe Onorato8d626d62009-05-15 09:07:06 -040048/**
Joe Onoratod502f052009-05-15 18:20:19 -040049 * Writes the data.
Joe Onorato8d626d62009-05-15 09:07:06 -040050 *
51 * If an error occurs, it poisons this object and all write calls will fail
52 * with the error that occurred.
53 */
54class BackupDataWriter
55{
56public:
57 BackupDataWriter(int fd);
58 // does not close fd
59 ~BackupDataWriter();
60
Joe Onoratod502f052009-05-15 18:20:19 -040061 status_t WriteAppHeader(const String8& packageName, int cookie);
Joe Onorato8d626d62009-05-15 09:07:06 -040062
63 status_t WriteEntityHeader(const String8& key, size_t dataSize);
64 status_t WriteEntityData(const void* data, size_t size);
65
Joe Onoratod502f052009-05-15 18:20:19 -040066 status_t WriteAppFooter(int cookie);
Joe Onorato8d626d62009-05-15 09:07:06 -040067
68private:
69 explicit BackupDataWriter();
70 status_t write_padding_for(int n);
71
72 int m_fd;
73 status_t m_status;
74 ssize_t m_pos;
75 int m_entityCount;
76};
77
Joe Onoratod502f052009-05-15 18:20:19 -040078/**
79 * Reads the data.
80 *
81 * If an error occurs, it poisons this object and all write calls will fail
82 * with the error that occurred.
83 */
84class BackupDataReader
85{
86public:
87 BackupDataReader(int fd);
88 // does not close fd
89 ~BackupDataReader();
90
91 status_t Status();
92 status_t ReadNextHeader();
93
94 status_t ReadAppHeader(String8* packageName, int* cookie);
95 bool HasEntities();
96 status_t ReadEntityHeader(String8* key, size_t* dataSize);
97 status_t ReadEntityData(void* data, size_t size);
98 status_t ReadAppFooter(int* cookie);
99
100private:
101 explicit BackupDataReader();
102 status_t skip_padding();
103
104 int m_fd;
105 status_t m_status;
106 ssize_t m_pos;
107 int m_entityCount;
108 union {
109 int type;
110 app_header_v1 app;
111 entity_header_v1 entity;
112 app_footer_v1 footer;
113 } m_header;
114};
115
Joe Onorato2a98fb92009-05-06 12:55:46 -0400116#define TEST_BACKUP_HELPERS 0
Joe Onorato0c4863b2009-05-05 11:50:51 -0700117
118#if TEST_BACKUP_HELPERS
119int backup_helper_test_empty();
120int backup_helper_test_four();
121int backup_helper_test_files();
Joe Onorato8d626d62009-05-15 09:07:06 -0400122int backup_helper_test_data_writer();
Joe Onoratod502f052009-05-15 18:20:19 -0400123int backup_helper_test_data_reader();
Joe Onorato0c4863b2009-05-05 11:50:51 -0700124#endif
125
Joe Onorato8d626d62009-05-15 09:07:06 -0400126} // namespace android
127
Joe Onorato0c4863b2009-05-05 11:50:51 -0700128#endif // _UTILS_BACKUP_HELPERS_H