2 #include <lunaix/fs/iso9660.h>
3 #include <lunaix/mm/cake.h>
4 #include <lunaix/mm/valloc.h>
5 #include <lunaix/spike.h>
7 #include <klibc/string.h>
9 #include <sys/dirent_defs.h>
11 extern struct cake_pile* drec_cache_pile;
14 iso9660_fill_drecache(struct iso_drecache* cache,
15 struct iso_drecord* drec,
18 *cache = (struct iso_drecache){ .data_size = drec->data_size.le,
19 .extent_addr = drec->extent_addr.le,
21 .fu_size = drec->fu_sz ? drec->fu_sz : 1,
22 .gap_size = drec->gap_sz,
23 .xattr_len = drec->xattr_len };
24 u32_t padding = ((drec->name.len + sizeof(drec->name)) % 2) != 0;
25 u32_t su_offset = drec->name.len + sizeof(struct iso_drecord) + padding;
26 int su_len = drec_len - su_offset - 2, i = 0;
29 struct isosu_base* su_entry =
30 (struct isosu_base*)((void*)drec + su_offset + i);
31 switch (su_entry->signature) {
33 i += isorr_parse_nm(cache, (void*)su_entry);
36 i += isorr_parse_px(cache, (void*)su_entry);
39 i += isorr_parse_tf(cache, (void*)su_entry);
44 i += su_entry->length;
50 if (!cache->name.len) {
51 // Load ISO9660 file id if no NM found.
52 u32_t l = drec->name.len;
53 while (l < (u32_t)-1 && drec->name.content[l--] != ';')
55 l = (l + 1) ? l : drec->name.len;
56 l = MIN(l, ISO9660_IDLEN - 1);
58 strncpy(cache->name_val, drec->name.content, l);
59 cache->name = HSTR(cache->name_val, l);
60 hstr_rehash(&cache->name, HSTR_FULL_HASH);
65 iso9660_setup_dnode(struct v_dnode* dnode, struct v_inode* inode)
67 if (!(inode->itype & VFS_IFDIR)) {
68 vfs_assign_inode(dnode, inode);
72 struct iso_inode* isoino = inode->data;
73 if (!llist_empty(&isoino->drecaches)) {
74 dnode->data = &isoino->drecaches;
75 vfs_assign_inode(dnode, inode);
80 struct device* dev = dnode->super_block->dev;
81 void* records = valloc(ISO9660_BLKSZ);
82 u32_t current_pos = -ISO9660_BLKSZ, max_pos = inode->fsize,
83 blk = inode->lb_addr * ISO9660_BLKSZ, blk_offset = (u32_t)-1;
85 // As per 6.8.1, Directory structure shall NOT recorded in interleave mode.
87 if (blk_offset >= ISO9660_BLKSZ - sizeof(struct iso_drecord)) {
88 current_pos += ISO9660_BLKSZ;
89 errno = dev->read(dev, records, blk + current_pos, ISO9660_BLKSZ);
97 struct iso_drecord* drec;
98 struct iso_var_mdu* mdu = (struct iso_var_mdu*)(records + blk_offset);
100 if (!(drec = iso9660_get_drecord(mdu))) {
104 // ignore the '.', '..' as we have built-in support
105 if (drec->name.len == 1) {
109 struct iso_drecache* cache = cake_grab(drec_cache_pile);
111 iso9660_fill_drecache(cache, drec, mdu->len);
112 llist_append(&isoino->drecaches, &cache->caches);
114 blk_offset += mdu->len;
115 } while (current_pos + blk_offset < max_pos);
117 dnode->data = &isoino->drecaches;
119 vfs_assign_inode(dnode, inode);
129 iso9660_dir_lookup(struct v_inode* this, struct v_dnode* dnode)
131 struct iso_inode* isoino = this->data;
132 struct iso_drecache *pos, *n;
134 llist_for_each(pos, n, &isoino->drecaches, caches)
136 if (HSTR_EQ(&dnode->name, &pos->name)) {
143 struct v_inode* inode = vfs_i_find(dnode->super_block, pos->extent_addr);
146 inode = vfs_i_alloc(dnode->super_block);
147 iso9660_fill_inode(inode, pos, pos->extent_addr);
148 vfs_i_addhash(inode);
151 return iso9660_setup_dnode(dnode, inode);
155 __get_dtype(struct iso_drecache* pos)
157 if ((pos->flags & ISO_FDIR)) {
165 iso9660_readdir(struct v_file* file, struct dir_context* dctx)
167 struct llist_header* lead = file->dnode->data;
168 struct iso_drecache *pos, *n;
169 u32_t counter = dctx->index - 1;
171 llist_for_each(pos, n, lead, caches)
173 if (counter == (u32_t)-1 && !(pos->flags & ISO_FHIDDEN)) {
174 dctx->read_complete_callback(
175 dctx, pos->name_val, pos->name.len, __get_dtype(pos));