Joe Onorato | 4535e40 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 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 | |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 17 | #define LOG_TAG "backup_data" |
| 18 | |
Mathias Agopian | 8ae2335 | 2009-06-04 13:53:57 -0700 | [diff] [blame] | 19 | #include <utils/BackupHelpers.h> |
Joe Onorato | 4535e40 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 20 | #include <utils/ByteOrder.h> |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <unistd.h> |
| 24 | |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 25 | #include <cutils/log.h> |
| 26 | |
Joe Onorato | 4535e40 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 27 | namespace 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 Onorato | 4535e40 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 42 | const static int ROUND_UP[4] = { 0, 3, 2, 1 }; |
| 43 | |
| 44 | static inline size_t |
| 45 | round_up(size_t n) |
| 46 | { |
| 47 | return n + ROUND_UP[n % 4]; |
| 48 | } |
| 49 | |
| 50 | static inline size_t |
| 51 | padding_extra(size_t n) |
| 52 | { |
| 53 | return ROUND_UP[n % 4]; |
| 54 | } |
| 55 | |
| 56 | BackupDataWriter::BackupDataWriter(int fd) |
| 57 | :m_fd(fd), |
| 58 | m_status(NO_ERROR), |
| 59 | m_pos(0), |
| 60 | m_entityCount(0) |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | BackupDataWriter::~BackupDataWriter() |
| 65 | { |
| 66 | } |
| 67 | |
| 68 | // Pad out anything they've previously written to the next 4 byte boundary. |
| 69 | status_t |
| 70 | BackupDataWriter::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 | |
| 88 | status_t |
Joe Onorato | 4535e40 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 89 | BackupDataWriter::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 Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 102 | String8 k; |
| 103 | if (m_keyPrefix.length() > 0) { |
| 104 | k = m_keyPrefix; |
| 105 | k += ":"; |
| 106 | k += key; |
| 107 | } else { |
| 108 | k = key; |
| 109 | } |
Joe Onorato | 568bc32 | 2009-06-26 17:19:11 -0400 | [diff] [blame] | 110 | if (true) { |
| 111 | LOGD("Writing entity: prefix='%s' key='%s' dataSize=%d", m_keyPrefix.string(), key.string(), |
| 112 | dataSize); |
| 113 | } |
Joe Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 114 | |
Joe Onorato | 4535e40 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 115 | entity_header_v1 header; |
| 116 | ssize_t keyLen; |
| 117 | |
Joe Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 118 | keyLen = k.length(); |
Joe Onorato | 4535e40 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 119 | |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 120 | header.type = tolel(BACKUP_HEADER_ENTITY_V1); |
Joe Onorato | 4535e40 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 121 | 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 Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 131 | amt = write(m_fd, k.string(), keyLen+1); |
Joe Onorato | 4535e40 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 132 | 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 | |
| 145 | status_t |
| 146 | BackupDataWriter::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 Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 164 | void |
| 165 | BackupDataWriter::SetKeyPrefix(const String8& keyPrefix) |
| 166 | { |
| 167 | m_keyPrefix = keyPrefix; |
| 168 | } |
Joe Onorato | 4535e40 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 169 | |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 170 | |
| 171 | BackupDataReader::BackupDataReader(int fd) |
| 172 | :m_fd(fd), |
Joe Onorato | 5f15d15 | 2009-06-16 16:31:35 -0400 | [diff] [blame] | 173 | m_done(false), |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 174 | m_status(NO_ERROR), |
| 175 | m_pos(0), |
| 176 | m_entityCount(0) |
| 177 | { |
| 178 | memset(&m_header, 0, sizeof(m_header)); |
| 179 | } |
| 180 | |
| 181 | BackupDataReader::~BackupDataReader() |
| 182 | { |
| 183 | } |
| 184 | |
| 185 | status_t |
| 186 | BackupDataReader::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; \ |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame^] | 196 | m_done = true; \ |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 197 | } else { \ |
| 198 | m_status = errno; \ |
| 199 | } \ |
Joe Onorato | 8d72569 | 2009-07-28 18:23:05 -0700 | [diff] [blame] | 200 | LOGD("CHECK_SIZE failed with at line %d m_status='%s'", __LINE__, strerror(m_status)); \ |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 201 | return m_status; \ |
| 202 | } \ |
| 203 | } while(0) |
| 204 | #define SKIP_PADDING() \ |
| 205 | do { \ |
| 206 | status_t err = skip_padding(); \ |
| 207 | if (err != NO_ERROR) { \ |
Joe Onorato | 8d72569 | 2009-07-28 18:23:05 -0700 | [diff] [blame] | 208 | LOGD("SKIP_PADDING FAILED at line %d", __LINE__); \ |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 209 | m_status = err; \ |
| 210 | return err; \ |
| 211 | } \ |
| 212 | } while(0) |
| 213 | |
| 214 | status_t |
Joe Onorato | 5f15d15 | 2009-06-16 16:31:35 -0400 | [diff] [blame] | 215 | BackupDataReader::ReadNextHeader(bool* done, int* type) |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 216 | { |
Joe Onorato | 5f15d15 | 2009-06-16 16:31:35 -0400 | [diff] [blame] | 217 | *done = m_done; |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 218 | if (m_status != NO_ERROR) { |
| 219 | return m_status; |
| 220 | } |
| 221 | |
| 222 | int amt; |
| 223 | |
Joe Onorato | 8d72569 | 2009-07-28 18:23:05 -0700 | [diff] [blame] | 224 | amt = skip_padding(); |
| 225 | if (amt == EIO) { |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame^] | 226 | *done = m_done = true; |
Joe Onorato | 8d72569 | 2009-07-28 18:23:05 -0700 | [diff] [blame] | 227 | return NO_ERROR; |
| 228 | } |
| 229 | else if (amt != NO_ERROR) { |
| 230 | return amt; |
| 231 | } |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 232 | amt = read(m_fd, &m_header, sizeof(m_header)); |
Joe Onorato | 5f15d15 | 2009-06-16 16:31:35 -0400 | [diff] [blame] | 233 | *done = m_done = (amt == 0); |
Joe Onorato | 8d72569 | 2009-07-28 18:23:05 -0700 | [diff] [blame] | 234 | if (*done) { |
| 235 | return NO_ERROR; |
| 236 | } |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 237 | CHECK_SIZE(amt, sizeof(m_header)); |
Joe Onorato | 5d605dc | 2009-06-18 18:23:43 -0700 | [diff] [blame] | 238 | m_pos += sizeof(m_header); |
| 239 | if (type) { |
| 240 | *type = m_header.type; |
| 241 | } |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 242 | |
| 243 | // validate and fix up the fields. |
| 244 | m_header.type = fromlel(m_header.type); |
| 245 | switch (m_header.type) |
| 246 | { |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 247 | case BACKUP_HEADER_ENTITY_V1: |
Joe Onorato | 5d605dc | 2009-06-18 18:23:43 -0700 | [diff] [blame] | 248 | { |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 249 | m_header.entity.keyLen = fromlel(m_header.entity.keyLen); |
| 250 | if (m_header.entity.keyLen <= 0) { |
| 251 | LOGD("Entity header at %d has keyLen<=0: 0x%08x\n", (int)m_pos, |
| 252 | (int)m_header.entity.keyLen); |
| 253 | m_status = EINVAL; |
| 254 | } |
| 255 | m_header.entity.dataSize = fromlel(m_header.entity.dataSize); |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 256 | m_entityCount++; |
Joe Onorato | 5d605dc | 2009-06-18 18:23:43 -0700 | [diff] [blame] | 257 | |
| 258 | // read the rest of the header (filename) |
| 259 | size_t size = m_header.entity.keyLen; |
| 260 | char* buf = m_key.lockBuffer(size); |
| 261 | if (buf == NULL) { |
| 262 | m_status = ENOMEM; |
| 263 | return m_status; |
| 264 | } |
| 265 | int amt = read(m_fd, buf, size+1); |
| 266 | CHECK_SIZE(amt, (int)size+1); |
| 267 | m_key.unlockBuffer(size); |
| 268 | m_pos += size+1; |
| 269 | SKIP_PADDING(); |
| 270 | m_dataEndPos = m_pos + m_header.entity.dataSize; |
| 271 | |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 272 | break; |
Joe Onorato | 5d605dc | 2009-06-18 18:23:43 -0700 | [diff] [blame] | 273 | } |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 274 | default: |
| 275 | LOGD("Chunk header at %d has invalid type: 0x%08x", (int)m_pos, (int)m_header.type); |
| 276 | m_status = EINVAL; |
| 277 | } |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 278 | |
| 279 | return m_status; |
| 280 | } |
| 281 | |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 282 | bool |
| 283 | BackupDataReader::HasEntities() |
| 284 | { |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 285 | return m_status == NO_ERROR && m_header.type == BACKUP_HEADER_ENTITY_V1; |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | status_t |
| 289 | BackupDataReader::ReadEntityHeader(String8* key, size_t* dataSize) |
| 290 | { |
| 291 | if (m_status != NO_ERROR) { |
| 292 | return m_status; |
| 293 | } |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 294 | if (m_header.type != BACKUP_HEADER_ENTITY_V1) { |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 295 | return EINVAL; |
| 296 | } |
Joe Onorato | 5d605dc | 2009-06-18 18:23:43 -0700 | [diff] [blame] | 297 | *key = m_key; |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 298 | *dataSize = m_header.entity.dataSize; |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 299 | return NO_ERROR; |
| 300 | } |
| 301 | |
| 302 | status_t |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 303 | BackupDataReader::SkipEntityData() |
| 304 | { |
| 305 | if (m_status != NO_ERROR) { |
| 306 | return m_status; |
| 307 | } |
| 308 | if (m_header.type != BACKUP_HEADER_ENTITY_V1) { |
| 309 | return EINVAL; |
| 310 | } |
| 311 | if (m_header.entity.dataSize > 0) { |
Joe Onorato | 5f15d15 | 2009-06-16 16:31:35 -0400 | [diff] [blame] | 312 | int pos = lseek(m_fd, m_dataEndPos, SEEK_SET); |
Joe Onorato | ee5bbb7 | 2009-07-13 14:44:07 -0700 | [diff] [blame] | 313 | if (pos == -1) { |
| 314 | return errno; |
| 315 | } |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 316 | } |
Joe Onorato | ee5bbb7 | 2009-07-13 14:44:07 -0700 | [diff] [blame] | 317 | SKIP_PADDING(); |
| 318 | return NO_ERROR; |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Joe Onorato | efd0fab | 2009-06-17 16:20:55 -0700 | [diff] [blame] | 321 | ssize_t |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 322 | BackupDataReader::ReadEntityData(void* data, size_t size) |
| 323 | { |
| 324 | if (m_status != NO_ERROR) { |
Joe Onorato | 5d605dc | 2009-06-18 18:23:43 -0700 | [diff] [blame] | 325 | return -1; |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 326 | } |
Joe Onorato | 5f15d15 | 2009-06-16 16:31:35 -0400 | [diff] [blame] | 327 | int remaining = m_dataEndPos - m_pos; |
Joe Onorato | efd0fab | 2009-06-17 16:20:55 -0700 | [diff] [blame] | 328 | //LOGD("ReadEntityData size=%d m_pos=0x%x m_dataEndPos=0x%x remaining=%d\n", |
| 329 | // size, m_pos, m_dataEndPos, remaining); |
Joe Onorato | 5f15d15 | 2009-06-16 16:31:35 -0400 | [diff] [blame] | 330 | if (remaining <= 0) { |
| 331 | return 0; |
| 332 | } |
Joe Onorato | 06290a4 | 2009-06-18 20:10:37 -0700 | [diff] [blame] | 333 | if (((int)size) > remaining) { |
Joe Onorato | 5d605dc | 2009-06-18 18:23:43 -0700 | [diff] [blame] | 334 | size = remaining; |
| 335 | } |
| 336 | //LOGD(" reading %d bytes", size); |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 337 | int amt = read(m_fd, data, size); |
Joe Onorato | 5d605dc | 2009-06-18 18:23:43 -0700 | [diff] [blame] | 338 | if (amt < 0) { |
| 339 | m_status = errno; |
| 340 | return -1; |
| 341 | } |
Joe Onorato | 9bb8fd7 | 2009-07-28 18:24:51 -0700 | [diff] [blame^] | 342 | if (amt == 0) { |
| 343 | m_status = EIO; |
| 344 | m_done = true; |
| 345 | } |
Joe Onorato | 5d605dc | 2009-06-18 18:23:43 -0700 | [diff] [blame] | 346 | m_pos += amt; |
Joe Onorato | efd0fab | 2009-06-17 16:20:55 -0700 | [diff] [blame] | 347 | return amt; |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | status_t |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 351 | BackupDataReader::skip_padding() |
| 352 | { |
| 353 | ssize_t amt; |
| 354 | ssize_t paddingSize; |
| 355 | |
| 356 | paddingSize = padding_extra(m_pos); |
| 357 | if (paddingSize > 0) { |
| 358 | uint32_t padding; |
| 359 | amt = read(m_fd, &padding, paddingSize); |
| 360 | CHECK_SIZE(amt, paddingSize); |
| 361 | m_pos += amt; |
| 362 | } |
| 363 | return NO_ERROR; |
| 364 | } |
| 365 | |
| 366 | |
Joe Onorato | 4535e40 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 367 | } // namespace android |