Added ramdisk driver.
[planlOS.git] / system / modules / ramdisk / res.c
blobf3ba2be7def820789176ca0101598de36aee86af
1 /*
2 * Copyright (c) 2008 The LOST Project. All rights reserved.
4 * This code is derived from software contributed to the LOST Project
5 * by Alexander Siol and Janosch Graef.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. 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 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the LOST Project
18 * and its contributors.
19 * 4. Neither the name of the LOST Project nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include "ramdisk_cdi.h"
38 int ramdisk_fs_res_load(struct cdi_fs_stream* stream)
40 struct ramdisk_fs_res* res = (struct ramdisk_fs_res*) stream->res;
42 if (res->res.loaded) {
43 return 0;
46 res->res.loaded = 1;
47 return 1;
50 int ramdisk_fs_res_unload(struct cdi_fs_stream* stream)
52 struct ramdisk_fs_res* res = (struct ramdisk_fs_res*) stream->res;
54 if (!res->res.loaded) {
55 return 0;
58 res->res.loaded = 0;
59 return 1;
62 int64_t ramdisk_fs_res_meta_read(struct cdi_fs_stream* stream, cdi_fs_meta_t meta)
64 struct ramdisk_fs_res* res = (struct ramdisk_fs_res*) stream->res;
66 switch (meta) {
67 case CDI_FS_META_SIZE:
68 return res->size;
70 case CDI_FS_META_USEDBLOCKS:
71 return 1;
73 case CDI_FS_META_BLOCKSZ:
74 return res->size;
76 case CDI_FS_META_BESTBLOCKSZ:
77 return res->size;
79 case CDI_FS_META_CREATETIME:
80 return res->creation_time;
82 case CDI_FS_META_ACCESSTIME:
83 return res->access_time;
85 case CDI_FS_META_CHANGETIME:
86 return res->modification_time;
89 return 0;
92 int ramdisk_fs_res_meta_write(struct cdi_fs_stream* stream, cdi_fs_meta_t meta,
93 int64_t value)
95 struct ramdisk_fs_res* res = (struct ramdisk_fs_res*) stream->res;
97 switch (meta) {
98 case CDI_FS_META_ACCESSTIME:
99 res->access_time = value;
100 return 1;
102 case CDI_FS_META_CHANGETIME:
103 res->modification_time = value;
104 return 1;
106 // RO:
107 case CDI_FS_META_SIZE:
108 case CDI_FS_META_USEDBLOCKS:
109 case CDI_FS_META_BESTBLOCKSZ:
110 case CDI_FS_META_BLOCKSZ:
111 case CDI_FS_META_CREATETIME:
112 return 0;
115 return 0;
118 int ramdisk_fs_res_assign_class(struct cdi_fs_stream* stream,
119 cdi_fs_res_class_t class)
121 struct ramdisk_fs_res* res = (struct ramdisk_fs_res*) stream->res;
123 // In ramdisk koennen die Ressource nur zu maximal einer Klasse gleichzeitig
124 // gehoeren
125 if (res->res.file || res->res.dir || res->res.link || res->res.special)
127 stream->error = CDI_FS_ERROR_ONS;
128 return 0;
131 switch (class) {
132 case CDI_FS_CLASS_FILE:
133 res->res.file = &ramdisk_fs_file;
134 break;
136 case CDI_FS_CLASS_DIR:
137 res->res.dir = &ramdisk_fs_dir;
138 break;
140 case CDI_FS_CLASS_LINK:
141 res->res.link = &ramdisk_fs_link;
142 break;
144 case CDI_FS_CLASS_SPECIAL:
145 res->res.special = &ramdisk_fs_special;
146 break;
150 return 1;
153 int ramdisk_fs_res_remove_class(struct cdi_fs_stream* stream,
154 cdi_fs_res_class_t class)
156 struct ramdisk_fs_res* res = (struct ramdisk_fs_res*) stream->res;
158 switch (class) {
159 case CDI_FS_CLASS_FILE:
160 res->size = 0;
161 res->res.file = NULL;
162 break;
164 case CDI_FS_CLASS_DIR:
165 res->res.dir = NULL;
166 break;
168 case CDI_FS_CLASS_LINK:
169 res->res.link = NULL;
170 break;
172 case CDI_FS_CLASS_SPECIAL:
173 res->res.special = NULL;
174 break;
177 return 1;
180 int ramdisk_fs_res_rename(struct cdi_fs_stream* stream, const char* name)
182 struct ramdisk_fs_res* res = (struct ramdisk_fs_res*) stream->res;
183 free(res->res.name);
184 res->res.name = strdup(name);
185 return 1;
188 int ramdisk_fs_res_remove(struct cdi_fs_stream* stream)
190 struct ramdisk_fs_res* res = (struct ramdisk_fs_res*) stream->res;
192 if (res->res.file!=NULL || res->res.dir!=NULL || res->res.link!=NULL || res->res.special!=NULL) {
193 stream->error = CDI_FS_ERROR_ONS;
194 return 0;
197 // Link aus der Liste der Vater-Resource entfernen
198 size_t i;
199 struct ramdisk_fs_res* child;
200 for (i=0;(child = cdi_list_get(res->res.parent->children,i));i++) {
201 if (child==res) {
202 cdi_list_remove(res->res.parent->children,i);
203 break;
207 return ramdisk_fs_res_destroy(res);
210 int ramdisk_fs_res_destroy(struct ramdisk_fs_res* res)
212 if (cdi_list_size(res->res.children)>0) return 0;
214 free(res->buffer);
215 free(res->res.name);
216 free(res->res.link_path);
217 cdi_list_destroy(res->res.children);
218 free(res);
220 return 1;