| 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 <str.h>
|
|---|
| 44 | #include <sys/types.h>
|
|---|
| 45 | #include <as.h>
|
|---|
| 46 | #include <block.h>
|
|---|
| 47 | #include <byteorder.h>
|
|---|
| 48 |
|
|---|
| 49 | #define TMPFS_COMM_SIZE 1024
|
|---|
| 50 |
|
|---|
| 51 | static uint8_t tmpfs_buf[TMPFS_COMM_SIZE];
|
|---|
| 52 |
|
|---|
| 53 | struct rdentry {
|
|---|
| 54 | uint8_t type;
|
|---|
| 55 | uint32_t len;
|
|---|
| 56 | } __attribute__((packed));
|
|---|
| 57 |
|
|---|
| 58 | static bool
|
|---|
| 59 | tmpfs_restore_recursion(service_id_t dsid, size_t *bufpos, size_t *buflen,
|
|---|
| 60 | aoff64_t *pos, fs_node_t *pfn)
|
|---|
| 61 | {
|
|---|
| 62 | struct rdentry entry;
|
|---|
| 63 | libfs_ops_t *ops = &tmpfs_libfs_ops;
|
|---|
| 64 | int rc;
|
|---|
| 65 |
|
|---|
| 66 | do {
|
|---|
| 67 | char *fname;
|
|---|
| 68 | fs_node_t *fn;
|
|---|
| 69 | tmpfs_node_t *nodep;
|
|---|
| 70 | uint32_t size;
|
|---|
| 71 |
|
|---|
| 72 | if (block_seqread(dsid, tmpfs_buf, bufpos, buflen, pos, &entry,
|
|---|
| 73 | sizeof(entry)) != EOK)
|
|---|
| 74 | return false;
|
|---|
| 75 |
|
|---|
| 76 | entry.len = uint32_t_le2host(entry.len);
|
|---|
| 77 |
|
|---|
| 78 | switch (entry.type) {
|
|---|
| 79 | case TMPFS_NONE:
|
|---|
| 80 | break;
|
|---|
| 81 | case TMPFS_FILE:
|
|---|
| 82 | fname = malloc(entry.len + 1);
|
|---|
| 83 | if (fname == NULL)
|
|---|
| 84 | return false;
|
|---|
| 85 |
|
|---|
| 86 | rc = ops->create(&fn, dsid, L_FILE);
|
|---|
| 87 | if (rc != EOK || fn == NULL) {
|
|---|
| 88 | free(fname);
|
|---|
| 89 | return false;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | if (block_seqread(dsid, tmpfs_buf, bufpos, buflen, pos, fname,
|
|---|
| 93 | entry.len) != EOK) {
|
|---|
| 94 | (void) ops->destroy(fn);
|
|---|
| 95 | free(fname);
|
|---|
| 96 | return false;
|
|---|
| 97 | }
|
|---|
| 98 | fname[entry.len] = 0;
|
|---|
| 99 |
|
|---|
| 100 | rc = ops->link(pfn, fn, fname);
|
|---|
| 101 | if (rc != EOK) {
|
|---|
| 102 | (void) ops->destroy(fn);
|
|---|
| 103 | free(fname);
|
|---|
| 104 | return false;
|
|---|
| 105 | }
|
|---|
| 106 | free(fname);
|
|---|
| 107 |
|
|---|
| 108 | if (block_seqread(dsid, tmpfs_buf, bufpos, buflen, pos, &size,
|
|---|
| 109 | sizeof(size)) != EOK)
|
|---|
| 110 | return false;
|
|---|
| 111 |
|
|---|
| 112 | size = uint32_t_le2host(size);
|
|---|
| 113 |
|
|---|
| 114 | nodep = TMPFS_NODE(fn);
|
|---|
| 115 | nodep->data = malloc(size);
|
|---|
| 116 | if (nodep->data == NULL)
|
|---|
| 117 | return false;
|
|---|
| 118 |
|
|---|
| 119 | nodep->size = size;
|
|---|
| 120 | if (block_seqread(dsid, tmpfs_buf, bufpos, buflen, pos, nodep->data,
|
|---|
| 121 | size) != EOK)
|
|---|
| 122 | return false;
|
|---|
| 123 |
|
|---|
| 124 | break;
|
|---|
| 125 | case TMPFS_DIRECTORY:
|
|---|
| 126 | fname = malloc(entry.len + 1);
|
|---|
| 127 | if (fname == NULL)
|
|---|
| 128 | return false;
|
|---|
| 129 |
|
|---|
| 130 | rc = ops->create(&fn, dsid, L_DIRECTORY);
|
|---|
| 131 | if (rc != EOK || fn == NULL) {
|
|---|
| 132 | free(fname);
|
|---|
| 133 | return false;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | if (block_seqread(dsid, tmpfs_buf, bufpos, buflen, pos, fname,
|
|---|
| 137 | entry.len) != EOK) {
|
|---|
| 138 | (void) ops->destroy(fn);
|
|---|
| 139 | free(fname);
|
|---|
| 140 | return false;
|
|---|
| 141 | }
|
|---|
| 142 | fname[entry.len] = 0;
|
|---|
| 143 |
|
|---|
| 144 | rc = ops->link(pfn, fn, fname);
|
|---|
| 145 | if (rc != EOK) {
|
|---|
| 146 | (void) ops->destroy(fn);
|
|---|
| 147 | free(fname);
|
|---|
| 148 | return false;
|
|---|
| 149 | }
|
|---|
| 150 | free(fname);
|
|---|
| 151 |
|
|---|
| 152 | if (!tmpfs_restore_recursion(dsid, bufpos, buflen, pos,
|
|---|
| 153 | fn))
|
|---|
| 154 | return false;
|
|---|
| 155 |
|
|---|
| 156 | break;
|
|---|
| 157 | default:
|
|---|
| 158 | return false;
|
|---|
| 159 | }
|
|---|
| 160 | } while (entry.type != TMPFS_NONE);
|
|---|
| 161 |
|
|---|
| 162 | return true;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | bool tmpfs_restore(service_id_t dsid)
|
|---|
| 166 | {
|
|---|
| 167 | libfs_ops_t *ops = &tmpfs_libfs_ops;
|
|---|
| 168 | fs_node_t *fn;
|
|---|
| 169 | int rc;
|
|---|
| 170 |
|
|---|
| 171 | rc = block_init(EXCHANGE_SERIALIZE, dsid, TMPFS_COMM_SIZE);
|
|---|
| 172 | if (rc != EOK)
|
|---|
| 173 | return false;
|
|---|
| 174 |
|
|---|
| 175 | size_t bufpos = 0;
|
|---|
| 176 | size_t buflen = 0;
|
|---|
| 177 | aoff64_t pos = 0;
|
|---|
| 178 |
|
|---|
| 179 | char tag[6];
|
|---|
| 180 | if (block_seqread(dsid, tmpfs_buf, &bufpos, &buflen, &pos, tag, 5) != EOK)
|
|---|
| 181 | goto error;
|
|---|
| 182 |
|
|---|
| 183 | tag[5] = 0;
|
|---|
| 184 | if (str_cmp(tag, "TMPFS") != 0)
|
|---|
| 185 | goto error;
|
|---|
| 186 |
|
|---|
| 187 | rc = ops->root_get(&fn, dsid);
|
|---|
| 188 | if (rc != EOK)
|
|---|
| 189 | goto error;
|
|---|
| 190 |
|
|---|
| 191 | if (!tmpfs_restore_recursion(dsid, &bufpos, &buflen, &pos, fn))
|
|---|
| 192 | goto error;
|
|---|
| 193 |
|
|---|
| 194 | block_fini(dsid);
|
|---|
| 195 | return true;
|
|---|
| 196 |
|
|---|
| 197 | error:
|
|---|
| 198 | block_fini(dsid);
|
|---|
| 199 | return false;
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | /**
|
|---|
| 203 | * @}
|
|---|
| 204 | */
|
|---|