BackupHelpers.h revision efd0fab04b96d7ab0c1d8bf3b79397c8621e31c5
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_ENTITY_V1 = 0x61746144, // Data (little endian)
27};
28
29typedef struct {
30    int type; // BACKUP_HEADER_ENTITY_V1
31    int keyLen; // length of the key name, not including the null terminator
32    int dataSize; // size of the data, not including the padding, -1 means delete
33} entity_header_v1;
34
35
36/**
37 * Writes the data.
38 *
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
49    status_t WriteEntityHeader(const String8& key, size_t dataSize);
50    status_t WriteEntityData(const void* data, size_t size);
51
52private:
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
62/**
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();
76    status_t ReadNextHeader(bool* done, int* type);
77
78    bool HasEntities();
79    status_t ReadEntityHeader(String8* key, size_t* dataSize);
80    status_t SkipEntityData(); // must be called with the pointer at the begining of the data.
81    ssize_t ReadEntityData(void* data, size_t size);
82
83private:
84    explicit BackupDataReader();
85    status_t skip_padding();
86
87    int m_fd;
88    bool m_done;
89    status_t m_status;
90    ssize_t m_pos;
91    ssize_t m_dataEndPos;
92    int m_entityCount;
93    union {
94        int type;
95        entity_header_v1 entity;
96    } m_header;
97};
98
99int back_up_files(int oldSnapshotFD, BackupDataWriter* dataStream, int newSnapshotFD,
100        char const* const* files, char const* const *keys, int fileCount);
101
102
103#define TEST_BACKUP_HELPERS 1
104
105#if TEST_BACKUP_HELPERS
106int backup_helper_test_empty();
107int backup_helper_test_four();
108int backup_helper_test_files();
109int backup_helper_test_null_base();
110int backup_helper_test_missing_file();
111int backup_helper_test_data_writer();
112int backup_helper_test_data_reader();
113#endif
114
115} // namespace android
116
117#endif // _UTILS_BACKUP_HELPERS_H
118