iso9660: Directories have filesize 0 (in meta read)
[meinos.git] / apps / iso9660 / res.c
blob65d88feb807001b606a2a65603e1ca2c805d9a82
1 /*
2 * iso9660 - An iso9660 CDI driver with Rockridge support
4 * Copyright (C) 2008 Janosch Gräf
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 3 of the License, or (at your option)
9 * any later version.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, see <http://www.gnu.org/licenses/>.
20 #include <string.h>
21 #include <stdlib.h>
23 #include "cdi/lists.h"
24 #include "cdi/fs.h"
25 #include "cdi/cache.h"
27 #include "volume_descriptor.h"
29 #include "iso9660_cdi.h"
31 /**
32 * Creates a new resource
33 * @param name Name of resource
34 * @param parent Parent resource
35 * @param class File class
36 * @param type Special file type
37 * @return ISO9660 class
39 struct iso9660_fs_res *iso9660_fs_res_create(const char *name,struct iso9660_fs_res *parent,cdi_fs_res_class_t class,cdi_fs_res_type_t type) {
40 debug("iso9660_fs_res_create(%s,0x%x,%d)\n",name,parent,class);
41 struct iso9660_fs_res *res = malloc(sizeof(struct iso9660_fs_res));
43 memset(res,0,sizeof(struct iso9660_fs_res));
44 res->res.name = strdup(name);
45 res->res.res = &iso9660_fs_res_res;
46 res->res.parent = (struct cdi_fs_res*)parent;
47 res->class = class;
48 res->res.type = type;
49 if (class==CDI_FS_CLASS_DIR) res->res.dir = &iso9660_fs_res_dir;
50 else res->res.file = &iso9660_fs_res_file;
51 res->res.flags.read = 1;
52 res->res.flags.execute = 1;
53 res->res.flags.browse = 1;
54 res->res.flags.read_link = 1;
56 if (parent!=NULL) {
57 res->voldesc = parent->voldesc;
58 res->cache = parent->cache;
60 return res;
63 /**
64 * Destroys a resource
65 * @param res ISO9660 resource
66 * @return 0=success; -1=failure
68 int iso9660_fs_res_destroy(struct iso9660_fs_res *res) {
69 debug("iso9660_fs_res_destroy(0x%x)\n",res);
70 free(res->res.name);
71 if (res->res.children!=NULL) {
72 size_t i;
73 struct iso9660_fs_res *child;
74 for (i=0;(child = cdi_list_get(res->res.children,i));i++) iso9660_fs_res_destroy(child);
75 cdi_list_destroy(res->res.children);
77 return 0;
80 /**
81 * Loads a resource
82 * @param stream CDI FS stream
83 * @return 0=success; -1=failure
85 int iso9660_fs_res_load(struct cdi_fs_stream *stream) {
86 struct iso9660_fs_res *res = (struct iso9660_fs_res*)stream->res;
88 if (!res->res.loaded) {
89 if (res->class==CDI_FS_CLASS_DIR) res->res.children = iso9660_dir_load(res);
90 else res->res.children = cdi_list_create();
91 res->res.loaded = 1;
93 return 1;
96 /**
97 * Unloads a resource
98 * @param stream CDI FS stream
99 * @return 0=success; -1=failure
101 int iso9660_fs_res_unload(struct cdi_fs_stream *stream) {
102 struct iso9660_fs_res *res = (struct iso9660_fs_res*)stream->res;
104 if (res->res.loaded) {
105 // Destroy children
106 struct iso9660_fs_res *child;
107 while ((child = cdi_list_pop(res->res.children))) iso9660_fs_res_destroy(child);
108 cdi_list_destroy(res->res.children);
110 res->res.loaded = 0;
112 return 1;
116 * Reads meta data from resource
117 * @param stream CDI FS stream
118 * @param meta Type of meta data
119 * @return Meta data
121 int64_t iso9660_fs_res_meta_read(struct cdi_fs_stream *stream,cdi_fs_meta_t meta) {
122 struct iso9660_fs_res *res = (struct iso9660_fs_res*)stream->res;
123 switch (meta) {
124 case CDI_FS_META_SIZE:
125 return res->res.dir!=NULL?0:res->data_size;
127 case CDI_FS_META_USEDBLOCKS:
128 return (res->data_size-1)/res->voldesc->sector_size+1;
130 case CDI_FS_META_BESTBLOCKSZ:
131 case CDI_FS_META_BLOCKSZ:
132 return res->voldesc->sector_size;
134 case CDI_FS_META_CREATETIME:
135 return res->ctime;
137 case CDI_FS_META_ACCESSTIME:
138 return res->atime;
140 case CDI_FS_META_CHANGETIME:
141 return res->mtime;
143 return 0;