1 #include <lunaix/dirent.h>
3 #include <lunaix/fs/iso9660.h>
4 #include <lunaix/mm/cake.h>
5 #include <lunaix/mm/valloc.h>
6 #include <lunaix/spike.h>
8 #include <klibc/string.h>
10 extern struct cake_pile* drec_cache_pile;
13 iso9660_fill_drecache(struct iso_drecache* cache, struct iso_drecord* drec)
15 *cache = (struct iso_drecache){ .data_size = drec->data_size.le,
16 .extent_addr = drec->extent_addr.le,
18 .fu_size = drec->fu_sz ? drec->fu_sz : 1,
19 .gap_size = drec->gap_sz,
20 .xattr_len = drec->xattr_len };
21 u32_t l = drec->name.len;
22 while (l < (u32_t)-1 && drec->name.content[l--] != ';')
24 l = (l + 1) ? l : drec->name.len;
25 l = MIN(l, ISO9660_IDLEN);
26 strncpy(cache->name_val, drec->name.content, l);
27 cache->name = HSTR(cache->name_val, l);
28 hstr_rehash(&cache->name, HSTR_FULL_HASH);
32 iso9660_setup_dnode(struct v_dnode* dnode, struct v_inode* inode)
34 if (!(inode->itype & VFS_IFDIR)) {
39 struct device* dev = dnode->super_block->dev;
40 struct iso_inode* isoino = inode->data;
41 struct llist_header* lead = valloc(sizeof(*lead));
42 void* records = valloc(ISO9660_BLKSZ);
43 u32_t current_pos = -ISO9660_BLKSZ, max_pos = inode->fsize,
44 blk = inode->lb_addr * ISO9660_BLKSZ, blk_offset = (u32_t)-1;
46 llist_init_head(lead);
48 // As per 6.8.1, Directory structure shall NOT recorded in interleave mode.
50 if (blk_offset >= ISO9660_BLKSZ - sizeof(struct iso_drecord)) {
51 current_pos += ISO9660_BLKSZ;
52 errno = dev->read(dev, records, blk + current_pos, ISO9660_BLKSZ);
60 struct iso_drecord* drec;
61 struct iso_var_mdu* mdu = (struct iso_var_mdu*)(records + blk_offset);
63 if (!(drec = iso9660_get_drecord(mdu))) {
67 // ignore the '.', '..' as we have built-in support
68 if (drec->name.len == 1) {
72 struct iso_drecache* cache = cake_grab(drec_cache_pile);
74 iso9660_fill_drecache(cache, drec);
75 llist_append(lead, &cache->caches);
77 blk_offset += mdu->len;
78 } while (current_pos + blk_offset < max_pos);
81 isoino->drecaches = lead;
83 vfs_assign_inode(dnode, inode);
93 iso9660_dir_lookup(struct v_inode* this, struct v_dnode* dnode)
95 struct iso_inode* isoino = this->data;
96 struct llist_header* lead = isoino->drecaches;
97 struct iso_drecache *pos, *n;
99 llist_for_each(pos, n, lead, caches)
101 if (HSTR_EQ(&dnode->name, &pos->name)) {
108 struct v_inode* inode = vfs_i_find(dnode->super_block, pos->extent_addr);
111 inode = vfs_i_alloc(dnode->super_block);
112 iso9660_fill_inode(inode, pos, pos->extent_addr);
113 vfs_i_addhash(inode);
116 iso9660_setup_dnode(dnode, inode);
122 __get_dtype(struct iso_drecache* pos)
124 if ((pos->flags & ISO_FDIR)) {
132 iso9660_readdir(struct v_file* file, struct dir_context* dctx)
134 struct llist_header* lead = file->dnode->data;
135 struct iso_drecache *pos, *n;
136 u32_t counter = dctx->index - 1;
138 llist_for_each(pos, n, lead, caches)
140 if (counter == (u32_t)-1 && !(pos->flags & ISO_FHIDDEN)) {
141 dctx->read_complete_callback(
142 dctx, pos->name_val, pos->name.len, __get_dtype(pos));