BackupHelpers.h revision ce88cb15b52998e16c3ba548a4ec49117a835e21
1/*
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
17#ifndef _UTILS_BACKUP_HELPERS_H
18#define _UTILS_BACKUP_HELPERS_H
19
20#include <utils/Errors.h>
21#include <utils/String8.h>
22
23namespace android {
24
25enum {
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};
30
31// the sizes of all of these match.
32typedef struct {
33    int type; // == BACKUP_HEADER_APP_V1
34    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 {
39    int type; // BACKUP_HEADER_ENTITY_V1
40    int keyLen; // length of the key name, not including the null terminator
41    int dataSize; // size of the data, not including the padding, -1 means delete
42} entity_header_v1;
43
44typedef struct {
45    int type; // BACKUP_FOOTER_APP_V1
46    int entityCount; // the number of entities that were written
47    int cookie;
48} app_footer_v1;
49
50
51/**
52 * Writes the data.
53 *
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
64    status_t WriteAppHeader(const String8& packageName, int cookie);
65
66    status_t WriteEntityHeader(const String8& key, size_t dataSize);
67    status_t WriteEntityData(const void* data, size_t size);
68
69    status_t WriteAppFooter(int cookie);
70
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
81/**
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();
95    status_t ReadNextHeader(int* type = NULL);
96
97    status_t ReadAppHeader(String8* packageName, int* cookie);
98    bool HasEntities();
99    status_t ReadEntityHeader(String8* key, size_t* dataSize);
100    status_t SkipEntityData(); // must be called with the pointer at the begining of the data.
101    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
120int back_up_files(int oldSnapshotFD, BackupDataWriter* dataStream, int newSnapshotFD,
121        char const* const* files, char const* const *keys, int fileCount);
122
123
124#define TEST_BACKUP_HELPERS 1
125
126#if TEST_BACKUP_HELPERS
127int backup_helper_test_empty();
128int backup_helper_test_four();
129int backup_helper_test_files();
130int backup_helper_test_null_base();
131int backup_helper_test_missing_file();
132int backup_helper_test_data_writer();
133int backup_helper_test_data_reader();
134#endif
135
136} // namespace android
137
138#endif // _UTILS_BACKUP_HELPERS_H
139