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