Subversion Repositories HelenOS

Rev

Rev 3343 | Go to most recent revision | 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>
3593 rimsky 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
3593 rimsky 57
tmpfs_restore_recursion(int dev, off_t *bufpos, size_t *buflen, off_t *pos,
58
    tmpfs_dentry_t *parent)
3103 jermar 59
{
60
    struct rdentry entry;
61
    libfs_ops_t *ops = &tmpfs_libfs_ops;
62
 
63
    do {
64
        char *fname;
65
        tmpfs_dentry_t *node;
66
        uint32_t size;
67
 
3593 rimsky 68
        if (block_read(dev, bufpos, buflen, pos, &entry, sizeof(entry),
69
            TMPFS_BLOCK_SIZE) != EOK)
3103 jermar 70
            return false;
71
 
72
        entry.len = uint32_t_le2host(entry.len);
73
 
74
        switch (entry.type) {
3247 jermar 75
        case TMPFS_NONE:
3103 jermar 76
            break;
3247 jermar 77
        case TMPFS_FILE:
3103 jermar 78
            fname = malloc(entry.len + 1);
79
            if (fname == NULL)
80
                return false;
81
 
3593 rimsky 82
            node = (tmpfs_dentry_t *) ops->create(dev, L_FILE);
3103 jermar 83
            if (node == NULL) {
84
                free(fname);
85
                return false;
86
            }
87
 
3593 rimsky 88
            if (block_read(dev, bufpos, buflen, pos, fname,
89
                entry.len, TMPFS_BLOCK_SIZE) != EOK) {
3103 jermar 90
                ops->destroy((void *) node);
91
                free(fname);
92
                return false;
93
            }
94
            fname[entry.len] = 0;
95
 
96
            if (!ops->link((void *) parent, (void *) node, fname)) {
97
                ops->destroy((void *) node);
98
                free(fname);
99
                return false;
100
            }
101
            free(fname);
102
 
3593 rimsky 103
            if (block_read(dev, bufpos, buflen, pos, &size,
104
                sizeof(size), TMPFS_BLOCK_SIZE) != EOK)
3103 jermar 105
                return false;
106
 
107
            size = uint32_t_le2host(size);
108
 
109
            node->data = malloc(size);
110
            if (node->data == NULL)
111
                return false;
112
 
113
            node->size = size;
3593 rimsky 114
            if (block_read(dev, bufpos, buflen, pos, node->data,
115
                size, TMPFS_BLOCK_SIZE) != EOK)
3103 jermar 116
                return false;
117
 
118
            break;
3247 jermar 119
        case TMPFS_DIRECTORY:
3103 jermar 120
            fname = malloc(entry.len + 1);
121
            if (fname == NULL)
122
                return false;
123
 
3593 rimsky 124
            node = (tmpfs_dentry_t *) ops->create(dev, L_DIRECTORY);
3103 jermar 125
            if (node == NULL) {
126
                free(fname);
127
                return false;
128
            }
129
 
3593 rimsky 130
            if (block_read(dev, bufpos, buflen, pos, fname,
131
                entry.len, TMPFS_BLOCK_SIZE) != EOK) {
3103 jermar 132
                ops->destroy((void *) node);
133
                free(fname);
134
                return false;
135
            }
136
            fname[entry.len] = 0;
137
 
138
            if (!ops->link((void *) parent, (void *) node, fname)) {
139
                ops->destroy((void *) node);
140
                free(fname);
141
                return false;
142
            }
143
            free(fname);
144
 
3593 rimsky 145
            if (!tmpfs_restore_recursion(dev, bufpos, buflen, pos,
146
                node))
3103 jermar 147
                return false;
148
 
149
            break;
150
        default:
151
            return false;
152
        }
3247 jermar 153
    } while (entry.type != TMPFS_NONE);
3103 jermar 154
 
155
    return true;
156
}
157
 
158
bool tmpfs_restore(dev_handle_t dev)
159
{
160
    libfs_ops_t *ops = &tmpfs_libfs_ops;
3593 rimsky 161
    int rc;
3103 jermar 162
 
3593 rimsky 163
    rc = block_init(dev, TMPFS_BLOCK_SIZE);
164
    if (rc != EOK)
165
        return false;
3103 jermar 166
 
3252 jermar 167
    off_t bufpos = 0;
3103 jermar 168
    size_t buflen = 0;
3252 jermar 169
    off_t pos = 0;
3103 jermar 170
 
171
    char tag[6];
3593 rimsky 172
    if (block_read(dev, &bufpos, &buflen, &pos, tag, 5,
173
        TMPFS_BLOCK_SIZE) != EOK)
3103 jermar 174
        goto error;
175
 
176
    tag[5] = 0;
177
    if (strcmp(tag, "TMPFS") != 0)
178
        goto error;
179
 
3593 rimsky 180
    if (!tmpfs_restore_recursion(dev, &bufpos, &buflen, &pos,
3103 jermar 181
        ops->root_get(dev)))
182
        goto error;
183
 
3593 rimsky 184
    block_fini(dev);
3103 jermar 185
    return true;
186
 
187
error:
3593 rimsky 188
    block_fini(dev);
3103 jermar 189
    return false;
190
}
191
 
192
/**
193
 * @}
194
 */