Use `errno_t` in all uspace and kernel code.
[helenos.git] / uspace / srv / fs / tmpfs / tmpfs_dump.c
blob6f6df69b0d952ec4c3bd833765b517ef4422aa5e
1 /*
2 * Copyright (c) 2008 Jakub Jermar
3 * Copyright (c) 2008 Martin Decky
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
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.
30 /** @addtogroup fs
31 * @{
32 */
34 /**
35 * @file tmpfs_dump.c
36 * @brief Support for loading TMPFS file system dump.
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>
50 #define TMPFS_COMM_SIZE 1024
52 static uint8_t tmpfs_buf[TMPFS_COMM_SIZE];
54 struct rdentry {
55 uint8_t type;
56 uint32_t len;
57 } __attribute__((packed));
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)
63 struct rdentry entry;
64 libfs_ops_t *ops = &tmpfs_libfs_ops;
65 errno_t rc;
67 do {
68 char *fname;
69 fs_node_t *fn;
70 tmpfs_node_t *nodep;
71 uint32_t size;
73 if (block_seqread(dsid, tmpfs_buf, bufpos, buflen, pos, &entry,
74 sizeof(entry)) != EOK)
75 return false;
77 entry.len = uint32_t_le2host(entry.len);
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;
87 rc = ops->create(&fn, dsid, L_FILE);
88 if (rc != EOK || fn == NULL) {
89 free(fname);
90 return false;
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;
99 fname[entry.len] = 0;
101 rc = ops->link(pfn, fn, fname);
102 if (rc != EOK) {
103 (void) ops->destroy(fn);
104 free(fname);
105 return false;
107 free(fname);
109 if (block_seqread(dsid, tmpfs_buf, bufpos, buflen, pos, &size,
110 sizeof(size)) != EOK)
111 return false;
113 size = uint32_t_le2host(size);
115 nodep = TMPFS_NODE(fn);
116 nodep->data = malloc(size);
117 if (nodep->data == NULL)
118 return false;
120 nodep->size = size;
121 if (block_seqread(dsid, tmpfs_buf, bufpos, buflen, pos, nodep->data,
122 size) != EOK)
123 return false;
125 break;
126 case TMPFS_DIRECTORY:
127 fname = malloc(entry.len + 1);
128 if (fname == NULL)
129 return false;
131 rc = ops->create(&fn, dsid, L_DIRECTORY);
132 if (rc != EOK || fn == NULL) {
133 free(fname);
134 return false;
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;
143 fname[entry.len] = 0;
145 rc = ops->link(pfn, fn, fname);
146 if (rc != EOK) {
147 (void) ops->destroy(fn);
148 free(fname);
149 return false;
151 free(fname);
153 if (!tmpfs_restore_recursion(dsid, bufpos, buflen, pos,
154 fn))
155 return false;
157 break;
158 default:
159 return false;
161 } while (entry.type != TMPFS_NONE);
163 return true;
166 bool tmpfs_restore(service_id_t dsid)
168 libfs_ops_t *ops = &tmpfs_libfs_ops;
169 fs_node_t *fn;
170 errno_t rc;
172 rc = block_init(dsid, TMPFS_COMM_SIZE);
173 if (rc != EOK)
174 return false;
176 size_t bufpos = 0;
177 size_t buflen = 0;
178 aoff64_t pos = 0;
180 char tag[6];
181 if (block_seqread(dsid, tmpfs_buf, &bufpos, &buflen, &pos, tag, 5) != EOK)
182 goto error;
184 tag[5] = 0;
185 if (str_cmp(tag, "TMPFS") != 0)
186 goto error;
188 rc = ops->root_get(&fn, dsid);
189 if (rc != EOK)
190 goto error;
192 if (!tmpfs_restore_recursion(dsid, &bufpos, &buflen, &pos, fn))
193 goto error;
195 block_fini(dsid);
196 return true;
198 error:
199 block_fini(dsid);
200 return false;
204 * @}