Subversion Repositories HelenOS

Rev

Rev 4581 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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