Merge branch 'master' into prog-loader
[lunaix-os.git] / lunaix-os / kernel / fs / iso9660 / directory.c
1 #include <lunaix/fs.h>
2 #include <lunaix/fs/iso9660.h>
3 #include <lunaix/mm/cake.h>
4 #include <lunaix/mm/valloc.h>
5 #include <lunaix/spike.h>
6
7 #include <klibc/string.h>
8
9 #include <sys/dirent_defs.h>
10
11 extern struct cake_pile* drec_cache_pile;
12
13 void
14 iso9660_fill_drecache(struct iso_drecache* cache,
15                       struct iso_drecord* drec,
16                       u32_t drec_len)
17 {
18     *cache = (struct iso_drecache){ .data_size = drec->data_size.le,
19                                     .extent_addr = drec->extent_addr.le,
20                                     .flags = drec->flags,
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;
27
28     while (i < su_len) {
29         struct isosu_base* su_entry =
30           (struct isosu_base*)((void*)drec + su_offset + i);
31         switch (su_entry->signature) {
32             case ISORR_NM:
33                 i += isorr_parse_nm(cache, (void*)su_entry);
34                 break;
35             case ISORR_PX:
36                 i += isorr_parse_px(cache, (void*)su_entry);
37                 break;
38             case ISORR_TF:
39                 i += isorr_parse_tf(cache, (void*)su_entry);
40                 break;
41             case ISOSU_ST:
42                 goto done;
43             default:
44                 i += su_entry->length;
45                 break;
46         }
47     }
48
49 done:
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--] != ';')
54             ;
55         l = (l + 1) ? l : drec->name.len;
56         l = MIN(l, ISO9660_IDLEN - 1);
57
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);
61     }
62 }
63
64 int
65 iso9660_setup_dnode(struct v_dnode* dnode, struct v_inode* inode)
66 {
67     if (!(inode->itype & VFS_IFDIR)) {
68         vfs_assign_inode(dnode, inode);
69         return 0;
70     }
71
72     struct iso_inode* isoino = inode->data;
73     if (!llist_empty(&isoino->drecaches)) {
74         dnode->data = &isoino->drecaches;
75         vfs_assign_inode(dnode, inode);
76         return 0;
77     }
78
79     int errno = 0;
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;
84
85     // As per 6.8.1, Directory structure shall NOT recorded in interleave mode.
86     do {
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);
90             if (errno < 0) {
91                 errno = EIO;
92                 goto done;
93             }
94             blk_offset = 0;
95         }
96
97         struct iso_drecord* drec;
98         struct iso_var_mdu* mdu = (struct iso_var_mdu*)(records + blk_offset);
99
100         if (!(drec = iso9660_get_drecord(mdu))) {
101             break;
102         }
103
104         // ignore the '.', '..' as we have built-in support
105         if (drec->name.len == 1) {
106             goto cont;
107         }
108
109         struct iso_drecache* cache = cake_grab(drec_cache_pile);
110
111         iso9660_fill_drecache(cache, drec, mdu->len);
112         llist_append(&isoino->drecaches, &cache->caches);
113     cont:
114         blk_offset += mdu->len;
115     } while (current_pos + blk_offset < max_pos);
116
117     dnode->data = &isoino->drecaches;
118
119     vfs_assign_inode(dnode, inode);
120
121     errno = 0;
122
123 done:
124     vfree(records);
125     return errno;
126 }
127
128 int
129 iso9660_dir_lookup(struct v_inode* this, struct v_dnode* dnode)
130 {
131     struct iso_inode* isoino = this->data;
132     struct iso_drecache *pos, *n;
133
134     llist_for_each(pos, n, &isoino->drecaches, caches)
135     {
136         if (HSTR_EQ(&dnode->name, &pos->name)) {
137             goto found;
138         }
139     }
140
141     return ENOENT;
142 found:
143     struct v_inode* inode = vfs_i_find(dnode->super_block, pos->extent_addr);
144
145     if (!inode) {
146         inode = vfs_i_alloc(dnode->super_block);
147         iso9660_fill_inode(inode, pos, pos->extent_addr);
148         vfs_i_addhash(inode);
149     }
150
151     return iso9660_setup_dnode(dnode, inode);
152 }
153
154 static int
155 __get_dtype(struct iso_drecache* pos)
156 {
157     if ((pos->flags & ISO_FDIR)) {
158         return DT_DIR;
159     } else {
160         return DT_FILE;
161     }
162 }
163
164 int
165 iso9660_readdir(struct v_file* file, struct dir_context* dctx)
166 {
167     struct llist_header* lead = file->dnode->data;
168     struct iso_drecache *pos, *n;
169     u32_t counter = dctx->index - 1;
170
171     llist_for_each(pos, n, lead, caches)
172     {
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));
176             return 1;
177         }
178         counter--;
179     }
180     return 0;
181 }