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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 15f3c3f was 15f3c3f, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Rename devmap to loc, devfs to locfs.

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