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 | |
| 102 | entity_header_v1 header; |
| 103 | ssize_t keyLen; |
| 104 | |
| 105 | keyLen = key.length(); |
| 106 | |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 107 | header.type = tolel(BACKUP_HEADER_ENTITY_V1); |
Joe Onorato | 4535e40 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 108 | header.keyLen = tolel(keyLen); |
| 109 | header.dataSize = tolel(dataSize); |
| 110 | |
| 111 | amt = write(m_fd, &header, sizeof(entity_header_v1)); |
| 112 | if (amt != sizeof(entity_header_v1)) { |
| 113 | m_status = errno; |
| 114 | return m_status; |
| 115 | } |
| 116 | m_pos += amt; |
| 117 | |
| 118 | amt = write(m_fd, key.string(), keyLen+1); |
| 119 | if (amt != keyLen+1) { |
| 120 | m_status = errno; |
| 121 | return m_status; |
| 122 | } |
| 123 | m_pos += amt; |
| 124 | |
| 125 | amt = write_padding_for(keyLen+1); |
| 126 | |
| 127 | m_entityCount++; |
| 128 | |
| 129 | return amt; |
| 130 | } |
| 131 | |
| 132 | status_t |
| 133 | BackupDataWriter::WriteEntityData(const void* data, size_t size) |
| 134 | { |
| 135 | if (m_status != NO_ERROR) { |
| 136 | return m_status; |
| 137 | } |
| 138 | |
| 139 | // We don't write padding here, because they're allowed to call this several |
| 140 | // times with smaller buffers. We write it at the end of WriteEntityHeader |
| 141 | // instead. |
| 142 | ssize_t amt = write(m_fd, data, size); |
| 143 | if (amt != (ssize_t)size) { |
| 144 | m_status = errno; |
| 145 | return m_status; |
| 146 | } |
| 147 | m_pos += amt; |
| 148 | return NO_ERROR; |
| 149 | } |
| 150 | |
Joe Onorato | 4535e40 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 151 | |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 152 | |
| 153 | BackupDataReader::BackupDataReader(int fd) |
| 154 | :m_fd(fd), |
Joe Onorato | 5f15d15 | 2009-06-16 16:31:35 -0400 | [diff] [blame] | 155 | m_done(false), |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 156 | m_status(NO_ERROR), |
| 157 | m_pos(0), |
| 158 | m_entityCount(0) |
| 159 | { |
| 160 | memset(&m_header, 0, sizeof(m_header)); |
| 161 | } |
| 162 | |
| 163 | BackupDataReader::~BackupDataReader() |
| 164 | { |
| 165 | } |
| 166 | |
| 167 | status_t |
| 168 | BackupDataReader::Status() |
| 169 | { |
| 170 | return m_status; |
| 171 | } |
| 172 | |
| 173 | #define CHECK_SIZE(actual, expected) \ |
| 174 | do { \ |
| 175 | if ((actual) != (expected)) { \ |
| 176 | if ((actual) == 0) { \ |
| 177 | m_status = EIO; \ |
| 178 | } else { \ |
| 179 | m_status = errno; \ |
| 180 | } \ |
| 181 | return m_status; \ |
| 182 | } \ |
| 183 | } while(0) |
| 184 | #define SKIP_PADDING() \ |
| 185 | do { \ |
| 186 | status_t err = skip_padding(); \ |
| 187 | if (err != NO_ERROR) { \ |
| 188 | m_status = err; \ |
| 189 | return err; \ |
| 190 | } \ |
| 191 | } while(0) |
| 192 | |
| 193 | status_t |
Joe Onorato | 5f15d15 | 2009-06-16 16:31:35 -0400 | [diff] [blame] | 194 | BackupDataReader::ReadNextHeader(bool* done, int* type) |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 195 | { |
Joe Onorato | 5f15d15 | 2009-06-16 16:31:35 -0400 | [diff] [blame] | 196 | *done = m_done; |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 197 | if (m_status != NO_ERROR) { |
| 198 | return m_status; |
| 199 | } |
| 200 | |
| 201 | int amt; |
| 202 | |
Joe Onorato | 5f15d15 | 2009-06-16 16:31:35 -0400 | [diff] [blame] | 203 | // No error checking here, in case we're at the end of the stream. Just let read() fail. |
| 204 | skip_padding(); |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 205 | amt = read(m_fd, &m_header, sizeof(m_header)); |
Joe Onorato | 5f15d15 | 2009-06-16 16:31:35 -0400 | [diff] [blame] | 206 | *done = m_done = (amt == 0); |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 207 | CHECK_SIZE(amt, sizeof(m_header)); |
Joe Onorato | 5d605dc | 2009-06-18 18:23:43 -0700 | [diff] [blame^] | 208 | m_pos += sizeof(m_header); |
| 209 | if (type) { |
| 210 | *type = m_header.type; |
| 211 | } |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 212 | |
| 213 | // validate and fix up the fields. |
| 214 | m_header.type = fromlel(m_header.type); |
| 215 | switch (m_header.type) |
| 216 | { |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 217 | case BACKUP_HEADER_ENTITY_V1: |
Joe Onorato | 5d605dc | 2009-06-18 18:23:43 -0700 | [diff] [blame^] | 218 | { |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 219 | m_header.entity.keyLen = fromlel(m_header.entity.keyLen); |
| 220 | if (m_header.entity.keyLen <= 0) { |
| 221 | LOGD("Entity header at %d has keyLen<=0: 0x%08x\n", (int)m_pos, |
| 222 | (int)m_header.entity.keyLen); |
| 223 | m_status = EINVAL; |
| 224 | } |
| 225 | m_header.entity.dataSize = fromlel(m_header.entity.dataSize); |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 226 | m_entityCount++; |
Joe Onorato | 5d605dc | 2009-06-18 18:23:43 -0700 | [diff] [blame^] | 227 | |
| 228 | // read the rest of the header (filename) |
| 229 | size_t size = m_header.entity.keyLen; |
| 230 | char* buf = m_key.lockBuffer(size); |
| 231 | if (buf == NULL) { |
| 232 | m_status = ENOMEM; |
| 233 | return m_status; |
| 234 | } |
| 235 | int amt = read(m_fd, buf, size+1); |
| 236 | CHECK_SIZE(amt, (int)size+1); |
| 237 | m_key.unlockBuffer(size); |
| 238 | m_pos += size+1; |
| 239 | SKIP_PADDING(); |
| 240 | m_dataEndPos = m_pos + m_header.entity.dataSize; |
| 241 | |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 242 | break; |
Joe Onorato | 5d605dc | 2009-06-18 18:23:43 -0700 | [diff] [blame^] | 243 | } |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 244 | default: |
| 245 | LOGD("Chunk header at %d has invalid type: 0x%08x", (int)m_pos, (int)m_header.type); |
| 246 | m_status = EINVAL; |
| 247 | } |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 248 | |
| 249 | return m_status; |
| 250 | } |
| 251 | |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 252 | bool |
| 253 | BackupDataReader::HasEntities() |
| 254 | { |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 255 | return m_status == NO_ERROR && m_header.type == BACKUP_HEADER_ENTITY_V1; |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | status_t |
| 259 | BackupDataReader::ReadEntityHeader(String8* key, size_t* dataSize) |
| 260 | { |
| 261 | if (m_status != NO_ERROR) { |
| 262 | return m_status; |
| 263 | } |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 264 | if (m_header.type != BACKUP_HEADER_ENTITY_V1) { |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 265 | return EINVAL; |
| 266 | } |
Joe Onorato | 5d605dc | 2009-06-18 18:23:43 -0700 | [diff] [blame^] | 267 | *key = m_key; |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 268 | *dataSize = m_header.entity.dataSize; |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 269 | return NO_ERROR; |
| 270 | } |
| 271 | |
| 272 | status_t |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 273 | BackupDataReader::SkipEntityData() |
| 274 | { |
| 275 | if (m_status != NO_ERROR) { |
| 276 | return m_status; |
| 277 | } |
| 278 | if (m_header.type != BACKUP_HEADER_ENTITY_V1) { |
| 279 | return EINVAL; |
| 280 | } |
| 281 | if (m_header.entity.dataSize > 0) { |
Joe Onorato | 5f15d15 | 2009-06-16 16:31:35 -0400 | [diff] [blame] | 282 | int pos = lseek(m_fd, m_dataEndPos, SEEK_SET); |
Joe Onorato | d2110db | 2009-05-19 13:41:21 -0700 | [diff] [blame] | 283 | return pos == -1 ? (int)errno : (int)NO_ERROR; |
| 284 | } else { |
| 285 | return NO_ERROR; |
| 286 | } |
| 287 | } |
| 288 | |
Joe Onorato | efd0fab | 2009-06-17 16:20:55 -0700 | [diff] [blame] | 289 | ssize_t |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 290 | BackupDataReader::ReadEntityData(void* data, size_t size) |
| 291 | { |
| 292 | if (m_status != NO_ERROR) { |
Joe Onorato | 5d605dc | 2009-06-18 18:23:43 -0700 | [diff] [blame^] | 293 | return -1; |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 294 | } |
Joe Onorato | 5f15d15 | 2009-06-16 16:31:35 -0400 | [diff] [blame] | 295 | int remaining = m_dataEndPos - m_pos; |
Joe Onorato | efd0fab | 2009-06-17 16:20:55 -0700 | [diff] [blame] | 296 | //LOGD("ReadEntityData size=%d m_pos=0x%x m_dataEndPos=0x%x remaining=%d\n", |
| 297 | // size, m_pos, m_dataEndPos, remaining); |
Joe Onorato | 5f15d15 | 2009-06-16 16:31:35 -0400 | [diff] [blame] | 298 | if (remaining <= 0) { |
| 299 | return 0; |
| 300 | } |
Joe Onorato | 5d605dc | 2009-06-18 18:23:43 -0700 | [diff] [blame^] | 301 | if (size > remaining) { |
| 302 | size = remaining; |
| 303 | } |
| 304 | //LOGD(" reading %d bytes", size); |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 305 | int amt = read(m_fd, data, size); |
Joe Onorato | 5d605dc | 2009-06-18 18:23:43 -0700 | [diff] [blame^] | 306 | if (amt < 0) { |
| 307 | m_status = errno; |
| 308 | return -1; |
| 309 | } |
| 310 | m_pos += amt; |
Joe Onorato | efd0fab | 2009-06-17 16:20:55 -0700 | [diff] [blame] | 311 | return amt; |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | status_t |
Joe Onorato | 2e1da32 | 2009-05-15 18:20:19 -0400 | [diff] [blame] | 315 | BackupDataReader::skip_padding() |
| 316 | { |
| 317 | ssize_t amt; |
| 318 | ssize_t paddingSize; |
| 319 | |
| 320 | paddingSize = padding_extra(m_pos); |
| 321 | if (paddingSize > 0) { |
| 322 | uint32_t padding; |
| 323 | amt = read(m_fd, &padding, paddingSize); |
| 324 | CHECK_SIZE(amt, paddingSize); |
| 325 | m_pos += amt; |
| 326 | } |
| 327 | return NO_ERROR; |
| 328 | } |
| 329 | |
| 330 | |
Joe Onorato | 4535e40 | 2009-05-15 09:07:06 -0400 | [diff] [blame] | 331 | } // namespace android |