Subversion Repositories HelenOS

Rev

Rev 4435 | Details | Compare with Previous | Last modification | View Log | RSS feed

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