feat: (iso9660) directory read support
[lunaix-os.git] / lunaix-os / kernel / fs / iso9660 / directory.c
1 #include <lunaix/dirent.h>
2 #include <lunaix/fs.h>
3 #include <lunaix/fs/iso9660.h>
4 #include <lunaix/mm/cake.h>
5 #include <lunaix/mm/valloc.h>
6 #include <lunaix/spike.h>
7
8 #include <klibc/string.h>
9
10 extern struct cake_pile* drec_cache_pile;
11
12 void
13 iso9660_fill_drecache(struct iso_drecache* cache, struct iso_drecord* drec)
14 {
15     *cache = (struct iso_drecache){ .data_size = drec->data_size.le,
16                                     .extent_addr = drec->extent_addr.le,
17                                     .flags = drec->flags,
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--] != ';')
23         ;
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);
29 }
30
31 int
32 iso9660_setup_dnode(struct v_dnode* dnode, struct v_inode* inode)
33 {
34     if (!(inode->itype & VFS_IFDIR)) {
35         return;
36     }
37
38     int errno = 0;
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;
45
46     llist_init_head(lead);
47
48     // As per 6.8.1, Directory structure shall NOT recorded in interleave mode.
49     do {
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);
53             if (errno < 0) {
54                 errno = EIO;
55                 goto done;
56             }
57             blk_offset = 0;
58         }
59
60         struct iso_drecord* drec;
61         struct iso_var_mdu* mdu = (struct iso_var_mdu*)(records + blk_offset);
62
63         if (!(drec = iso9660_get_drecord(mdu))) {
64             break;
65         }
66
67         // ignore the '.', '..' as we have built-in support
68         if (drec->name.len == 1) {
69             goto cont;
70         }
71
72         struct iso_drecache* cache = cake_grab(drec_cache_pile);
73
74         iso9660_fill_drecache(cache, drec);
75         llist_append(lead, &cache->caches);
76     cont:
77         blk_offset += mdu->len;
78     } while (current_pos + blk_offset < max_pos);
79
80     dnode->data = lead;
81     isoino->drecaches = lead;
82
83     vfs_assign_inode(dnode, inode);
84
85     errno = 0;
86
87 done:
88     vfree(records);
89     return errno;
90 }
91
92 int
93 iso9660_dir_lookup(struct v_inode* this, struct v_dnode* dnode)
94 {
95     struct iso_inode* isoino = this->data;
96     struct llist_header* lead = isoino->drecaches;
97     struct iso_drecache *pos, *n;
98
99     llist_for_each(pos, n, lead, caches)
100     {
101         if (HSTR_EQ(&dnode->name, &pos->name)) {
102             goto found;
103         }
104     }
105
106     return ENOENT;
107 found:
108     struct v_inode* inode = vfs_i_find(dnode->super_block, pos->extent_addr);
109
110     if (!inode) {
111         inode = vfs_i_alloc(dnode->super_block);
112         iso9660_fill_inode(inode, pos, pos->extent_addr);
113         vfs_i_addhash(inode);
114     }
115
116     iso9660_setup_dnode(dnode, inode);
117
118     return 0;
119 }
120
121 static int
122 __get_dtype(struct iso_drecache* pos)
123 {
124     if ((pos->flags & ISO_FDIR)) {
125         return DT_DIR;
126     } else {
127         return DT_FILE;
128     }
129 }
130
131 int
132 iso9660_readdir(struct v_file* file, struct dir_context* dctx)
133 {
134     struct llist_header* lead = file->dnode->data;
135     struct iso_drecache *pos, *n;
136     u32_t counter = dctx->index - 1;
137
138     llist_for_each(pos, n, lead, caches)
139     {
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));
143             return 1;
144         }
145         counter--;
146     }
147     return 0;
148 }