source: mainline/uspace/srv/fs/tmpfs/tmpfs_dump.c@ 46577995

Last change on this file since 46577995 was 46577995, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Use errno_t in all uspace and kernel code.

Change type of every variable, parameter and return value that holds an
<errno.h> constant to either errno_t (the usual case), or sys_errno_t
(some places in kernel). This is for the purpose of self-documentation,
as well as for type-checking with a bit of type definition hackery.

After this commit, HelenOS is free of code that mixes error codes with non-error
values on the assumption that error codes are negative.

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