blob: ba4a4ff4fe635bd419c1ee3850c1074265c1eda9 [file] [log] [blame]
Adam Lesinski16c4d152014-01-24 13:27:13 -08001/*
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#define LOG_TAG "backup_data"
18
19#include <androidfw/BackupHelpers.h>
20#include <utils/ByteOrder.h>
21
22#include <stdio.h>
23#include <string.h>
24#include <unistd.h>
25
26#include <cutils/log.h>
27
28namespace android {
29
Andreas Gampe2204f0b2014-10-21 23:04:54 -070030static const bool kIsDebug = false;
Adam Lesinski16c4d152014-01-24 13:27:13 -080031
32/*
33 * File Format (v1):
34 *
35 * All ints are stored little-endian.
36 *
37 * - An app_header_v1 struct.
38 * - The name of the package, utf-8, null terminated, padded to 4-byte boundary.
39 * - A sequence of zero or more key/value paires (entities), each with
40 * - A entity_header_v1 struct
41 * - The key, utf-8, null terminated, padded to 4-byte boundary.
42 * - The value, padded to 4 byte boundary
43 */
44
45const static int ROUND_UP[4] = { 0, 3, 2, 1 };
46
47static inline size_t
Adam Lesinski16c4d152014-01-24 13:27:13 -080048padding_extra(size_t n)
49{
50 return ROUND_UP[n % 4];
51}
52
53BackupDataWriter::BackupDataWriter(int fd)
54 :m_fd(fd),
55 m_status(NO_ERROR),
Adam Lesinski16c4d152014-01-24 13:27:13 -080056 m_entityCount(0)
57{
Christopher Tateb048c332014-02-21 12:50:21 -080058 m_pos = (ssize_t) lseek(fd, 0, SEEK_CUR);
Andreas Gampe2204f0b2014-10-21 23:04:54 -070059 if (kIsDebug) ALOGI("BackupDataWriter(%d) @ %ld", fd, (long)m_pos);
Adam Lesinski16c4d152014-01-24 13:27:13 -080060}
61
62BackupDataWriter::~BackupDataWriter()
63{
64}
65
66// Pad out anything they've previously written to the next 4 byte boundary.
67status_t
68BackupDataWriter::write_padding_for(int n)
69{
70 ssize_t amt;
71 ssize_t paddingSize;
72
73 paddingSize = padding_extra(n);
74 if (paddingSize > 0) {
75 uint32_t padding = 0xbcbcbcbc;
Andreas Gampe2204f0b2014-10-21 23:04:54 -070076 if (kIsDebug) ALOGI("writing %zd padding bytes for %d", paddingSize, n);
Adam Lesinski16c4d152014-01-24 13:27:13 -080077 amt = write(m_fd, &padding, paddingSize);
78 if (amt != paddingSize) {
79 m_status = errno;
80 return m_status;
81 }
82 m_pos += amt;
83 }
84 return NO_ERROR;
85}
86
87status_t
88BackupDataWriter::WriteEntityHeader(const String8& key, size_t dataSize)
89{
90 if (m_status != NO_ERROR) {
91 return m_status;
92 }
93
94 ssize_t amt;
95
96 amt = write_padding_for(m_pos);
97 if (amt != 0) {
98 return amt;
99 }
100
101 String8 k;
102 if (m_keyPrefix.length() > 0) {
103 k = m_keyPrefix;
104 k += ":";
105 k += key;
106 } else {
107 k = key;
108 }
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700109 if (kIsDebug) {
Ashok Bhatf5df7002014-03-25 20:51:35 +0000110 ALOGD("Writing header: prefix='%s' key='%s' dataSize=%zu", m_keyPrefix.string(),
Adam Lesinski16c4d152014-01-24 13:27:13 -0800111 key.string(), dataSize);
112 }
113
114 entity_header_v1 header;
115 ssize_t keyLen;
116
117 keyLen = k.length();
118
119 header.type = tolel(BACKUP_HEADER_ENTITY_V1);
120 header.keyLen = tolel(keyLen);
121 header.dataSize = tolel(dataSize);
122
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700123 if (kIsDebug) ALOGI("writing entity header, %zu bytes", sizeof(entity_header_v1));
Adam Lesinski16c4d152014-01-24 13:27:13 -0800124 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
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700131 if (kIsDebug) ALOGI("writing entity header key, %zd bytes", keyLen+1);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800132 amt = write(m_fd, k.string(), keyLen+1);
133 if (amt != keyLen+1) {
134 m_status = errno;
135 return m_status;
136 }
137 m_pos += amt;
138
139 amt = write_padding_for(keyLen+1);
140
141 m_entityCount++;
142
143 return amt;
144}
145
146status_t
147BackupDataWriter::WriteEntityData(const void* data, size_t size)
148{
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700149 if (kIsDebug) ALOGD("Writing data: size=%lu", (unsigned long) size);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800150
151 if (m_status != NO_ERROR) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700152 if (kIsDebug) {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800153 ALOGD("Not writing data - stream in error state %d (%s)", m_status, strerror(m_status));
154 }
155 return m_status;
156 }
157
158 // We don't write padding here, because they're allowed to call this several
159 // times with smaller buffers. We write it at the end of WriteEntityHeader
160 // instead.
161 ssize_t amt = write(m_fd, data, size);
162 if (amt != (ssize_t)size) {
163 m_status = errno;
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700164 if (kIsDebug) ALOGD("write returned error %d (%s)", m_status, strerror(m_status));
Adam Lesinski16c4d152014-01-24 13:27:13 -0800165 return m_status;
166 }
167 m_pos += amt;
168 return NO_ERROR;
169}
170
171void
172BackupDataWriter::SetKeyPrefix(const String8& keyPrefix)
173{
174 m_keyPrefix = keyPrefix;
175}
176
177
178BackupDataReader::BackupDataReader(int fd)
179 :m_fd(fd),
180 m_done(false),
181 m_status(NO_ERROR),
Adam Lesinski16c4d152014-01-24 13:27:13 -0800182 m_entityCount(0)
183{
184 memset(&m_header, 0, sizeof(m_header));
Christopher Tateb048c332014-02-21 12:50:21 -0800185 m_pos = (ssize_t) lseek(fd, 0, SEEK_CUR);
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700186 if (kIsDebug) ALOGI("BackupDataReader(%d) @ %ld", fd, (long)m_pos);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800187}
188
189BackupDataReader::~BackupDataReader()
190{
191}
192
193status_t
194BackupDataReader::Status()
195{
196 return m_status;
197}
198
199#define CHECK_SIZE(actual, expected) \
200 do { \
201 if ((actual) != (expected)) { \
202 if ((actual) == 0) { \
203 m_status = EIO; \
204 m_done = true; \
205 } else { \
206 m_status = errno; \
207 ALOGD("CHECK_SIZE(a=%ld e=%ld) failed at line %d m_status='%s'", \
208 long(actual), long(expected), __LINE__, strerror(m_status)); \
209 } \
210 return m_status; \
211 } \
212 } while(0)
213#define SKIP_PADDING() \
214 do { \
215 status_t err = skip_padding(); \
216 if (err != NO_ERROR) { \
217 ALOGD("SKIP_PADDING FAILED at line %d", __LINE__); \
218 m_status = err; \
219 return err; \
220 } \
221 } while(0)
222
223status_t
224BackupDataReader::ReadNextHeader(bool* done, int* type)
225{
226 *done = m_done;
227 if (m_status != NO_ERROR) {
228 return m_status;
229 }
230
231 int amt;
232
233 amt = skip_padding();
234 if (amt == EIO) {
235 *done = m_done = true;
236 return NO_ERROR;
237 }
238 else if (amt != NO_ERROR) {
239 return amt;
240 }
241 amt = read(m_fd, &m_header, sizeof(m_header));
242 *done = m_done = (amt == 0);
243 if (*done) {
244 return NO_ERROR;
245 }
246 CHECK_SIZE(amt, sizeof(m_header));
247 m_pos += sizeof(m_header);
248 if (type) {
249 *type = m_header.type;
250 }
251
252 // validate and fix up the fields.
253 m_header.type = fromlel(m_header.type);
254 switch (m_header.type)
255 {
256 case BACKUP_HEADER_ENTITY_V1:
257 {
258 m_header.entity.keyLen = fromlel(m_header.entity.keyLen);
259 if (m_header.entity.keyLen <= 0) {
260 ALOGD("Entity header at %d has keyLen<=0: 0x%08x\n", (int)m_pos,
261 (int)m_header.entity.keyLen);
262 m_status = EINVAL;
263 }
264 m_header.entity.dataSize = fromlel(m_header.entity.dataSize);
265 m_entityCount++;
266
267 // read the rest of the header (filename)
268 size_t size = m_header.entity.keyLen;
269 char* buf = m_key.lockBuffer(size);
270 if (buf == NULL) {
271 m_status = ENOMEM;
272 return m_status;
273 }
274 int amt = read(m_fd, buf, size+1);
275 CHECK_SIZE(amt, (int)size+1);
276 m_key.unlockBuffer(size);
277 m_pos += size+1;
278 SKIP_PADDING();
279 m_dataEndPos = m_pos + m_header.entity.dataSize;
280
281 break;
282 }
283 default:
284 ALOGD("Chunk header at %d has invalid type: 0x%08x",
285 (int)(m_pos - sizeof(m_header)), (int)m_header.type);
286 m_status = EINVAL;
287 }
Mark Salyzyn00adb862014-03-19 11:00:06 -0700288
Adam Lesinski16c4d152014-01-24 13:27:13 -0800289 return m_status;
290}
291
292bool
293BackupDataReader::HasEntities()
294{
295 return m_status == NO_ERROR && m_header.type == BACKUP_HEADER_ENTITY_V1;
296}
297
298status_t
299BackupDataReader::ReadEntityHeader(String8* key, size_t* dataSize)
300{
301 if (m_status != NO_ERROR) {
302 return m_status;
303 }
304 if (m_header.type != BACKUP_HEADER_ENTITY_V1) {
305 return EINVAL;
306 }
307 *key = m_key;
308 *dataSize = m_header.entity.dataSize;
309 return NO_ERROR;
310}
311
312status_t
313BackupDataReader::SkipEntityData()
314{
315 if (m_status != NO_ERROR) {
316 return m_status;
317 }
318 if (m_header.type != BACKUP_HEADER_ENTITY_V1) {
319 return EINVAL;
320 }
321 if (m_header.entity.dataSize > 0) {
322 int pos = lseek(m_fd, m_dataEndPos, SEEK_SET);
323 if (pos == -1) {
324 return errno;
325 }
326 m_pos = pos;
327 }
328 SKIP_PADDING();
329 return NO_ERROR;
330}
331
332ssize_t
333BackupDataReader::ReadEntityData(void* data, size_t size)
334{
335 if (m_status != NO_ERROR) {
336 return -1;
337 }
338 int remaining = m_dataEndPos - m_pos;
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700339 if (kIsDebug) {
Andreas Gampe25df5fb2014-11-07 22:24:57 -0800340 ALOGD("ReadEntityData size=%zu m_pos=0x%zx m_dataEndPos=0x%zx remaining=%d\n",
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700341 size, m_pos, m_dataEndPos, remaining);
342 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800343 if (remaining <= 0) {
344 return 0;
345 }
346 if (((int)size) > remaining) {
347 size = remaining;
348 }
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700349 if (kIsDebug) {
Andreas Gampe25df5fb2014-11-07 22:24:57 -0800350 ALOGD(" reading %zu bytes", size);
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700351 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800352 int amt = read(m_fd, data, size);
353 if (amt < 0) {
354 m_status = errno;
355 return -1;
356 }
357 if (amt == 0) {
358 m_status = EIO;
359 m_done = true;
360 }
361 m_pos += amt;
362 return amt;
363}
364
365status_t
366BackupDataReader::skip_padding()
367{
368 ssize_t amt;
369 ssize_t paddingSize;
370
371 paddingSize = padding_extra(m_pos);
372 if (paddingSize > 0) {
373 uint32_t padding;
374 amt = read(m_fd, &padding, paddingSize);
375 CHECK_SIZE(amt, paddingSize);
376 m_pos += amt;
377 }
378 return NO_ERROR;
379}
380
381
382} // namespace android