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