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