[40257f5] | 1 | /*
|
---|
| 2 | * Copyright (c) 2008 Jakub Jermar
|
---|
| 3 | * Copyright (c) 2008 Martin Decky
|
---|
| 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | /** @addtogroup fs
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 |
|
---|
| 34 | /**
|
---|
| 35 | * @file tmpfs_dump.c
|
---|
| 36 | * @brief Support for loading TMPFS file system dump.
|
---|
| 37 | */
|
---|
| 38 |
|
---|
| 39 | #include "tmpfs.h"
|
---|
| 40 | #include "../../vfs/vfs.h"
|
---|
| 41 | #include <errno.h>
|
---|
| 42 | #include <stdlib.h>
|
---|
| 43 | #include <string.h>
|
---|
| 44 | #include <sys/types.h>
|
---|
| 45 | #include <as.h>
|
---|
[fc840d9] | 46 | #include <libblock.h>
|
---|
[40257f5] | 47 | #include <byteorder.h>
|
---|
| 48 |
|
---|
[d2c1fd5] | 49 | #define TMPFS_BLOCK_SIZE 1024
|
---|
[40257f5] | 50 |
|
---|
| 51 | struct rdentry {
|
---|
| 52 | uint8_t type;
|
---|
| 53 | uint32_t len;
|
---|
| 54 | } __attribute__((packed));
|
---|
| 55 |
|
---|
| 56 | static bool
|
---|
[7858bc5f] | 57 | tmpfs_restore_recursion(int dev, off_t *bufpos, size_t *buflen, off_t *pos,
|
---|
| 58 | tmpfs_dentry_t *parent)
|
---|
[40257f5] | 59 | {
|
---|
| 60 | struct rdentry entry;
|
---|
| 61 | libfs_ops_t *ops = &tmpfs_libfs_ops;
|
---|
[0013b9ce] | 62 | int rc;
|
---|
[40257f5] | 63 |
|
---|
| 64 | do {
|
---|
| 65 | char *fname;
|
---|
| 66 | tmpfs_dentry_t *node;
|
---|
| 67 | uint32_t size;
|
---|
| 68 |
|
---|
[0c243b4] | 69 | if (block_read(dev, bufpos, buflen, pos, &entry, sizeof(entry),
|
---|
| 70 | TMPFS_BLOCK_SIZE) != EOK)
|
---|
[40257f5] | 71 | return false;
|
---|
| 72 |
|
---|
| 73 | entry.len = uint32_t_le2host(entry.len);
|
---|
| 74 |
|
---|
| 75 | switch (entry.type) {
|
---|
[ade06b4] | 76 | case TMPFS_NONE:
|
---|
[40257f5] | 77 | break;
|
---|
[ade06b4] | 78 | case TMPFS_FILE:
|
---|
[40257f5] | 79 | fname = malloc(entry.len + 1);
|
---|
| 80 | if (fname == NULL)
|
---|
| 81 | return false;
|
---|
| 82 |
|
---|
[adb5fe3] | 83 | node = (tmpfs_dentry_t *) ops->create(dev, L_FILE);
|
---|
[40257f5] | 84 | if (node == NULL) {
|
---|
| 85 | free(fname);
|
---|
| 86 | return false;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[0c243b4] | 89 | if (block_read(dev, bufpos, buflen, pos, fname,
|
---|
| 90 | entry.len, TMPFS_BLOCK_SIZE) != EOK) {
|
---|
[40257f5] | 91 | ops->destroy((void *) node);
|
---|
| 92 | free(fname);
|
---|
| 93 | return false;
|
---|
| 94 | }
|
---|
| 95 | fname[entry.len] = 0;
|
---|
| 96 |
|
---|
[0013b9ce] | 97 | rc = ops->link((void *) parent, (void *) node, fname);
|
---|
| 98 | if (rc != EOK) {
|
---|
[40257f5] | 99 | ops->destroy((void *) node);
|
---|
| 100 | free(fname);
|
---|
| 101 | return false;
|
---|
| 102 | }
|
---|
| 103 | free(fname);
|
---|
| 104 |
|
---|
[0c243b4] | 105 | if (block_read(dev, bufpos, buflen, pos, &size,
|
---|
| 106 | sizeof(size), TMPFS_BLOCK_SIZE) != EOK)
|
---|
[40257f5] | 107 | return false;
|
---|
| 108 |
|
---|
| 109 | size = uint32_t_le2host(size);
|
---|
| 110 |
|
---|
| 111 | node->data = malloc(size);
|
---|
| 112 | if (node->data == NULL)
|
---|
| 113 | return false;
|
---|
| 114 |
|
---|
| 115 | node->size = size;
|
---|
[0c243b4] | 116 | if (block_read(dev, bufpos, buflen, pos, node->data,
|
---|
| 117 | size, TMPFS_BLOCK_SIZE) != EOK)
|
---|
[40257f5] | 118 | return false;
|
---|
| 119 |
|
---|
| 120 | break;
|
---|
[ade06b4] | 121 | case TMPFS_DIRECTORY:
|
---|
[40257f5] | 122 | fname = malloc(entry.len + 1);
|
---|
| 123 | if (fname == NULL)
|
---|
| 124 | return false;
|
---|
| 125 |
|
---|
[adb5fe3] | 126 | node = (tmpfs_dentry_t *) ops->create(dev, L_DIRECTORY);
|
---|
[40257f5] | 127 | if (node == NULL) {
|
---|
| 128 | free(fname);
|
---|
| 129 | return false;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[0c243b4] | 132 | if (block_read(dev, bufpos, buflen, pos, fname,
|
---|
| 133 | entry.len, TMPFS_BLOCK_SIZE) != EOK) {
|
---|
[40257f5] | 134 | ops->destroy((void *) node);
|
---|
| 135 | free(fname);
|
---|
| 136 | return false;
|
---|
| 137 | }
|
---|
| 138 | fname[entry.len] = 0;
|
---|
[0013b9ce] | 139 |
|
---|
| 140 | rc = ops->link((void *) parent, (void *) node, fname);
|
---|
| 141 | if (rc != EOK) {
|
---|
[40257f5] | 142 | ops->destroy((void *) node);
|
---|
| 143 | free(fname);
|
---|
| 144 | return false;
|
---|
| 145 | }
|
---|
| 146 | free(fname);
|
---|
| 147 |
|
---|
[7858bc5f] | 148 | if (!tmpfs_restore_recursion(dev, bufpos, buflen, pos,
|
---|
| 149 | node))
|
---|
[40257f5] | 150 | return false;
|
---|
| 151 |
|
---|
| 152 | break;
|
---|
| 153 | default:
|
---|
| 154 | return false;
|
---|
| 155 | }
|
---|
[ade06b4] | 156 | } while (entry.type != TMPFS_NONE);
|
---|
[40257f5] | 157 |
|
---|
| 158 | return true;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | bool tmpfs_restore(dev_handle_t dev)
|
---|
| 162 | {
|
---|
| 163 | libfs_ops_t *ops = &tmpfs_libfs_ops;
|
---|
[7858bc5f] | 164 | int rc;
|
---|
[40257f5] | 165 |
|
---|
[6284978] | 166 | rc = block_init(dev, TMPFS_BLOCK_SIZE);
|
---|
[7858bc5f] | 167 | if (rc != EOK)
|
---|
| 168 | return false;
|
---|
[40257f5] | 169 |
|
---|
[61bc901] | 170 | off_t bufpos = 0;
|
---|
[40257f5] | 171 | size_t buflen = 0;
|
---|
[61bc901] | 172 | off_t pos = 0;
|
---|
[40257f5] | 173 |
|
---|
| 174 | char tag[6];
|
---|
[0c243b4] | 175 | if (block_read(dev, &bufpos, &buflen, &pos, tag, 5,
|
---|
| 176 | TMPFS_BLOCK_SIZE) != EOK)
|
---|
[40257f5] | 177 | goto error;
|
---|
| 178 |
|
---|
| 179 | tag[5] = 0;
|
---|
| 180 | if (strcmp(tag, "TMPFS") != 0)
|
---|
| 181 | goto error;
|
---|
| 182 |
|
---|
[7858bc5f] | 183 | if (!tmpfs_restore_recursion(dev, &bufpos, &buflen, &pos,
|
---|
[40257f5] | 184 | ops->root_get(dev)))
|
---|
| 185 | goto error;
|
---|
| 186 |
|
---|
[7858bc5f] | 187 | block_fini(dev);
|
---|
[40257f5] | 188 | return true;
|
---|
| 189 |
|
---|
| 190 | error:
|
---|
[7858bc5f] | 191 | block_fini(dev);
|
---|
[40257f5] | 192 | return false;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | /**
|
---|
| 196 | * @}
|
---|
| 197 | */
|
---|