blob: c51d989b4c50928d1a9251b7d61e1bd2c9cbbb51 [file] [log] [blame]
Joe Onorato4535e402009-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 Onorato2e1da322009-05-15 18:20:19 -040017#define LOG_TAG "backup_data"
18
Mathias Agopian8ae23352009-06-04 13:53:57 -070019#include <utils/BackupHelpers.h>
Joe Onorato4535e402009-05-15 09:07:06 -040020#include <utils/ByteOrder.h>
21
22#include <stdio.h>
23#include <unistd.h>
24
Joe Onorato2e1da322009-05-15 18:20:19 -040025#include <cutils/log.h>
26
Joe Onorato4535e402009-05-15 09:07:06 -040027namespace android {
28
29/*
30 * File Format (v1):
31 *
32 * All ints are stored little-endian.
33 *
34 * - An app_header_v1 struct.
35 * - The name of the package, utf-8, null terminated, padded to 4-byte boundary.
36 * - A sequence of zero or more key/value paires (entities), each with
37 * - A entity_header_v1 struct
38 * - The key, utf-8, null terminated, padded to 4-byte boundary.
39 * - The value, padded to 4 byte boundary
40 */
41
Joe Onorato4535e402009-05-15 09:07:06 -040042const static int ROUND_UP[4] = { 0, 3, 2, 1 };
43
44static inline size_t
45round_up(size_t n)
46{
47 return n + ROUND_UP[n % 4];
48}
49
50static inline size_t
51padding_extra(size_t n)
52{
53 return ROUND_UP[n % 4];
54}
55
56BackupDataWriter::BackupDataWriter(int fd)
57 :m_fd(fd),
58 m_status(NO_ERROR),
59 m_pos(0),
60 m_entityCount(0)
61{
62}
63
64BackupDataWriter::~BackupDataWriter()
65{
66}
67
68// Pad out anything they've previously written to the next 4 byte boundary.
69status_t
70BackupDataWriter::write_padding_for(int n)
71{
72 ssize_t amt;
73 ssize_t paddingSize;
74
75 paddingSize = padding_extra(n);
76 if (paddingSize > 0) {
77 uint32_t padding = 0xbcbcbcbc;
78 amt = write(m_fd, &padding, paddingSize);
79 if (amt != paddingSize) {
80 m_status = errno;
81 return m_status;
82 }
83 m_pos += amt;
84 }
85 return NO_ERROR;
86}
87
88status_t
Joe Onorato4535e402009-05-15 09:07:06 -040089BackupDataWriter::WriteEntityHeader(const String8& key, size_t dataSize)
90{
91 if (m_status != NO_ERROR) {
92 return m_status;
93 }
94
95 ssize_t amt;
96
97 amt = write_padding_for(m_pos);
98 if (amt != 0) {
99 return amt;
100 }
101
Joe Onorato06290a42009-06-18 20:10:37 -0700102 String8 k;
103 if (m_keyPrefix.length() > 0) {
104 k = m_keyPrefix;
105 k += ":";
106 k += key;
107 } else {
108 k = key;
109 }
Joe Onorato568bc322009-06-26 17:19:11 -0400110 if (true) {
111 LOGD("Writing entity: prefix='%s' key='%s' dataSize=%d", m_keyPrefix.string(), key.string(),
112 dataSize);
113 }
Joe Onorato06290a42009-06-18 20:10:37 -0700114
Joe Onorato4535e402009-05-15 09:07:06 -0400115 entity_header_v1 header;
116 ssize_t keyLen;
117
Joe Onorato06290a42009-06-18 20:10:37 -0700118 keyLen = k.length();
Joe Onorato4535e402009-05-15 09:07:06 -0400119
Joe Onoratod2110db2009-05-19 13:41:21 -0700120 header.type = tolel(BACKUP_HEADER_ENTITY_V1);
Joe Onorato4535e402009-05-15 09:07:06 -0400121 header.keyLen = tolel(keyLen);
122 header.dataSize = tolel(dataSize);
123
124 amt = write(m_fd, &header, sizeof(entity_header_v1));
125 if (amt != sizeof(entity_header_v1)) {
126 m_status = errno;
127 return m_status;
128 }
129 m_pos += amt;
130
Joe Onorato06290a42009-06-18 20:10:37 -0700131 amt = write(m_fd, k.string(), keyLen+1);
Joe Onorato4535e402009-05-15 09:07:06 -0400132 if (amt != keyLen+1) {
133 m_status = errno;
134 return m_status;
135 }
136 m_pos += amt;
137
138 amt = write_padding_for(keyLen+1);
139
140 m_entityCount++;
141
142 return amt;
143}
144
145status_t
146BackupDataWriter::WriteEntityData(const void* data, size_t size)
147{
148 if (m_status != NO_ERROR) {
149 return m_status;
150 }
151
152 // We don't write padding here, because they're allowed to call this several
153 // times with smaller buffers. We write it at the end of WriteEntityHeader
154 // instead.
155 ssize_t amt = write(m_fd, data, size);
156 if (amt != (ssize_t)size) {
157 m_status = errno;
158 return m_status;
159 }
160 m_pos += amt;
161 return NO_ERROR;
162}
163
Joe Onorato06290a42009-06-18 20:10:37 -0700164void
165BackupDataWriter::SetKeyPrefix(const String8& keyPrefix)
166{
167 m_keyPrefix = keyPrefix;
168}
Joe Onorato4535e402009-05-15 09:07:06 -0400169
Joe Onorato2e1da322009-05-15 18:20:19 -0400170
171BackupDataReader::BackupDataReader(int fd)
172 :m_fd(fd),
Joe Onorato5f15d152009-06-16 16:31:35 -0400173 m_done(false),
Joe Onorato2e1da322009-05-15 18:20:19 -0400174 m_status(NO_ERROR),
175 m_pos(0),
176 m_entityCount(0)
177{
178 memset(&m_header, 0, sizeof(m_header));
179}
180
181BackupDataReader::~BackupDataReader()
182{
183}
184
185status_t
186BackupDataReader::Status()
187{
188 return m_status;
189}
190
191#define CHECK_SIZE(actual, expected) \
192 do { \
193 if ((actual) != (expected)) { \
194 if ((actual) == 0) { \
195 m_status = EIO; \
196 } else { \
197 m_status = errno; \
198 } \
Joe Onorato8d725692009-07-28 18:23:05 -0700199 LOGD("CHECK_SIZE failed with at line %d m_status='%s'", __LINE__, strerror(m_status)); \
Joe Onorato2e1da322009-05-15 18:20:19 -0400200 return m_status; \
201 } \
202 } while(0)
203#define SKIP_PADDING() \
204 do { \
205 status_t err = skip_padding(); \
206 if (err != NO_ERROR) { \
Joe Onorato8d725692009-07-28 18:23:05 -0700207 LOGD("SKIP_PADDING FAILED at line %d", __LINE__); \
Joe Onorato2e1da322009-05-15 18:20:19 -0400208 m_status = err; \
209 return err; \
210 } \
211 } while(0)
212
213status_t
Joe Onorato5f15d152009-06-16 16:31:35 -0400214BackupDataReader::ReadNextHeader(bool* done, int* type)
Joe Onorato2e1da322009-05-15 18:20:19 -0400215{
Joe Onorato5f15d152009-06-16 16:31:35 -0400216 *done = m_done;
Joe Onorato2e1da322009-05-15 18:20:19 -0400217 if (m_status != NO_ERROR) {
218 return m_status;
219 }
220
221 int amt;
222
Joe Onorato8d725692009-07-28 18:23:05 -0700223 amt = skip_padding();
224 if (amt == EIO) {
225 *done = true;
226 return NO_ERROR;
227 }
228 else if (amt != NO_ERROR) {
229 return amt;
230 }
Joe Onorato2e1da322009-05-15 18:20:19 -0400231 amt = read(m_fd, &m_header, sizeof(m_header));
Joe Onorato5f15d152009-06-16 16:31:35 -0400232 *done = m_done = (amt == 0);
Joe Onorato8d725692009-07-28 18:23:05 -0700233 if (*done) {
234 return NO_ERROR;
235 }
Joe Onorato2e1da322009-05-15 18:20:19 -0400236 CHECK_SIZE(amt, sizeof(m_header));
Joe Onorato5d605dc2009-06-18 18:23:43 -0700237 m_pos += sizeof(m_header);
238 if (type) {
239 *type = m_header.type;
240 }
Joe Onorato2e1da322009-05-15 18:20:19 -0400241
242 // validate and fix up the fields.
243 m_header.type = fromlel(m_header.type);
244 switch (m_header.type)
245 {
Joe Onoratod2110db2009-05-19 13:41:21 -0700246 case BACKUP_HEADER_ENTITY_V1:
Joe Onorato5d605dc2009-06-18 18:23:43 -0700247 {
Joe Onorato2e1da322009-05-15 18:20:19 -0400248 m_header.entity.keyLen = fromlel(m_header.entity.keyLen);
249 if (m_header.entity.keyLen <= 0) {
250 LOGD("Entity header at %d has keyLen<=0: 0x%08x\n", (int)m_pos,
251 (int)m_header.entity.keyLen);
252 m_status = EINVAL;
253 }
254 m_header.entity.dataSize = fromlel(m_header.entity.dataSize);
Joe Onorato2e1da322009-05-15 18:20:19 -0400255 m_entityCount++;
Joe Onorato5d605dc2009-06-18 18:23:43 -0700256
257 // read the rest of the header (filename)
258 size_t size = m_header.entity.keyLen;
259 char* buf = m_key.lockBuffer(size);
260 if (buf == NULL) {
261 m_status = ENOMEM;
262 return m_status;
263 }
264 int amt = read(m_fd, buf, size+1);
265 CHECK_SIZE(amt, (int)size+1);
266 m_key.unlockBuffer(size);
267 m_pos += size+1;
268 SKIP_PADDING();
269 m_dataEndPos = m_pos + m_header.entity.dataSize;
270
Joe Onorato2e1da322009-05-15 18:20:19 -0400271 break;
Joe Onorato5d605dc2009-06-18 18:23:43 -0700272 }
Joe Onorato2e1da322009-05-15 18:20:19 -0400273 default:
274 LOGD("Chunk header at %d has invalid type: 0x%08x", (int)m_pos, (int)m_header.type);
275 m_status = EINVAL;
276 }
Joe Onorato2e1da322009-05-15 18:20:19 -0400277
278 return m_status;
279}
280
Joe Onorato2e1da322009-05-15 18:20:19 -0400281bool
282BackupDataReader::HasEntities()
283{
Joe Onoratod2110db2009-05-19 13:41:21 -0700284 return m_status == NO_ERROR && m_header.type == BACKUP_HEADER_ENTITY_V1;
Joe Onorato2e1da322009-05-15 18:20:19 -0400285}
286
287status_t
288BackupDataReader::ReadEntityHeader(String8* key, size_t* dataSize)
289{
290 if (m_status != NO_ERROR) {
291 return m_status;
292 }
Joe Onoratod2110db2009-05-19 13:41:21 -0700293 if (m_header.type != BACKUP_HEADER_ENTITY_V1) {
Joe Onorato2e1da322009-05-15 18:20:19 -0400294 return EINVAL;
295 }
Joe Onorato5d605dc2009-06-18 18:23:43 -0700296 *key = m_key;
Joe Onorato2e1da322009-05-15 18:20:19 -0400297 *dataSize = m_header.entity.dataSize;
Joe Onorato2e1da322009-05-15 18:20:19 -0400298 return NO_ERROR;
299}
300
301status_t
Joe Onoratod2110db2009-05-19 13:41:21 -0700302BackupDataReader::SkipEntityData()
303{
304 if (m_status != NO_ERROR) {
305 return m_status;
306 }
307 if (m_header.type != BACKUP_HEADER_ENTITY_V1) {
308 return EINVAL;
309 }
310 if (m_header.entity.dataSize > 0) {
Joe Onorato5f15d152009-06-16 16:31:35 -0400311 int pos = lseek(m_fd, m_dataEndPos, SEEK_SET);
Joe Onoratoee5bbb72009-07-13 14:44:07 -0700312 if (pos == -1) {
313 return errno;
314 }
Joe Onoratod2110db2009-05-19 13:41:21 -0700315 }
Joe Onoratoee5bbb72009-07-13 14:44:07 -0700316 SKIP_PADDING();
317 return NO_ERROR;
Joe Onoratod2110db2009-05-19 13:41:21 -0700318}
319
Joe Onoratoefd0fab2009-06-17 16:20:55 -0700320ssize_t
Joe Onorato2e1da322009-05-15 18:20:19 -0400321BackupDataReader::ReadEntityData(void* data, size_t size)
322{
323 if (m_status != NO_ERROR) {
Joe Onorato5d605dc2009-06-18 18:23:43 -0700324 return -1;
Joe Onorato2e1da322009-05-15 18:20:19 -0400325 }
Joe Onorato5f15d152009-06-16 16:31:35 -0400326 int remaining = m_dataEndPos - m_pos;
Joe Onoratoefd0fab2009-06-17 16:20:55 -0700327 //LOGD("ReadEntityData size=%d m_pos=0x%x m_dataEndPos=0x%x remaining=%d\n",
328 // size, m_pos, m_dataEndPos, remaining);
Joe Onorato5f15d152009-06-16 16:31:35 -0400329 if (remaining <= 0) {
330 return 0;
331 }
Joe Onorato06290a42009-06-18 20:10:37 -0700332 if (((int)size) > remaining) {
Joe Onorato5d605dc2009-06-18 18:23:43 -0700333 size = remaining;
334 }
335 //LOGD(" reading %d bytes", size);
Joe Onorato2e1da322009-05-15 18:20:19 -0400336 int amt = read(m_fd, data, size);
Joe Onorato5d605dc2009-06-18 18:23:43 -0700337 if (amt < 0) {
338 m_status = errno;
339 return -1;
340 }
341 m_pos += amt;
Joe Onoratoefd0fab2009-06-17 16:20:55 -0700342 return amt;
Joe Onorato2e1da322009-05-15 18:20:19 -0400343}
344
345status_t
Joe Onorato2e1da322009-05-15 18:20:19 -0400346BackupDataReader::skip_padding()
347{
348 ssize_t amt;
349 ssize_t paddingSize;
350
351 paddingSize = padding_extra(m_pos);
352 if (paddingSize > 0) {
353 uint32_t padding;
354 amt = read(m_fd, &padding, paddingSize);
355 CHECK_SIZE(amt, paddingSize);
356 m_pos += amt;
357 }
358 return NO_ERROR;
359}
360
361
Joe Onorato4535e402009-05-15 09:07:06 -0400362} // namespace android