]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/l4re_vfs/include/impl/ns_fs_impl.h
update
[l4.git] / l4 / pkg / l4re_vfs / include / impl / ns_fs_impl.h
1 /*
2  * (c) 2010 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
3  *          Alexander Warg <warg@os.inf.tu-dresden.de>
4  *     economic rights: Technische Universität Dresden (Germany)
5  *
6  * This file is part of TUD:OS and distributed under the terms of the
7  * GNU General Public License 2.
8  * Please see the COPYING-GPL-2 file for details.
9  *
10  * As a special exception, you may use this file as part of a free software
11  * library without restriction.  Specifically, if other files instantiate
12  * templates or use macros or inline functions from this file, or you compile
13  * this file and link it with other files to produce an executable, this
14  * file does not by itself cause the resulting executable to be covered by
15  * the GNU General Public License.  This exception does not however
16  * invalidate any other reasons why the executable file might be covered by
17  * the GNU General Public License.
18  */
19 #include "ns_fs.h"
20 #include "vfs_api.h"
21 #include "ro_file.h"
22
23 #include <l4/re/dataspace>
24 #include <l4/re/util/env_ns>
25 #include <dirent.h>
26
27 namespace L4Re { namespace Core {
28
29 Simple_store_sz<Env_dir::Size> Ns_base_dir::store __attribute__((init_priority(1000)));
30
31 void *
32 Ns_base_dir::operator new(size_t s) throw()
33 {
34   if (s > Size)
35     return 0;
36
37   return store.alloc();
38 }
39
40 void
41 Ns_base_dir::operator delete(void *b) throw()
42 {
43   store.free(b);
44 }
45
46
47 int
48 Ns_dir::get_ds(const char *path, L4Re::Auto_cap<L4Re::Dataspace>::Cap *ds) throw()
49 {
50   L4Re::Auto_cap<L4Re::Dataspace>::Cap file(cap_alloc()->alloc<L4Re::Dataspace>(), cap_alloc());
51
52   if (!file.is_valid())
53     return -ENOMEM;
54
55   int err = _ns->query(path, file.get());
56
57   if (err < 0)
58     return -ENOENT;
59
60   *ds = file;
61   return err;
62 }
63
64 int
65 Ns_dir::get_entry(const char *path, int flags, mode_t mode,
66                   Ref_ptr<L4Re::Vfs::File> *f) throw()
67 {
68   (void)mode; (void)flags;
69   if (!*path)
70     {
71       *f = this;
72       return 0;
73     }
74
75   L4Re::Auto_cap<Dataspace>::Cap file;
76   int err = get_ds(path, &file);
77
78   if (err < 0)
79     return -ENOENT;
80
81   // FIXME: should check if it is a dataspace, somehow
82   L4Re::Vfs::File *fi = 0;
83
84   L4::Cap<L4Re::Namespace> nsc
85     = L4::cap_dynamic_cast<L4Re::Namespace>(file.get());
86
87   if (!nsc.is_valid())
88     fi = new Ro_file(file.get());
89   else // use mat protocol here!!
90     fi = new Ns_dir(nsc);
91
92   if (!fi)
93     return -ENOMEM;
94
95   file.release();
96   *f = fi;
97   return 0;
98 }
99
100 int
101 Ns_dir::faccessat(const char *path, int mode, int flags) throw()
102 {
103   (void)flags;
104   L4Re::Auto_cap<void>::Cap tmpcap(cap_alloc()->alloc<void>(), cap_alloc());
105
106   if (!tmpcap.is_valid())
107     return -ENOMEM;
108
109   if (_ns->query(path, tmpcap.get()))
110     return -ENOENT;
111
112   if (mode & W_OK)
113     return -EACCES;
114
115   return 0;
116 }
117
118 int
119 Ns_dir::fstat64(struct stat64 *b) const throw()
120 {
121   b->st_dev = 1;
122   b->st_ino = 1;
123   b->st_mode = S_IRWXU | S_IFDIR;
124   b->st_nlink = 0;
125   b->st_uid = 0;
126   b->st_gid = 0;
127   b->st_rdev = 0;
128   b->st_size = 0;
129   b->st_blksize = 0;
130   b->st_blocks = 0;
131   b->st_atime = 0;
132   b->st_mtime = 0;
133   b->st_ctime = 0;
134   return 0;
135 }
136
137 ssize_t
138 Ns_dir::getdents(char *buf, size_t sz) throw()
139 {
140   struct dirent64 *d = (struct dirent64 *)buf;
141   ssize_t ret = 0;
142   l4_addr_t infoaddr;
143   size_t infosz;
144
145   L4Re::Auto_cap<Dataspace>::Cap dirinfofile;
146   int err = get_ds(".dirinfo", &dirinfofile);
147   if (err)
148     return 0;
149
150   infosz = dirinfofile->size();
151   if (infosz <= 0)
152     return 0;
153
154   infoaddr = L4_PAGESIZE;
155   err = L4Re::Env::env()->rm()->attach(&infoaddr, infosz,
156                                        Rm::Search_addr | Rm::Read_only,
157                                        dirinfofile.get(), 0);
158   char *p   = (char *)infoaddr + _current_dir_pos;
159   char *end = (char *)infoaddr + infosz;
160
161   while (d && p < end)
162     {
163       // parse lines of dirinfofile
164       long len;
165       for (len = 0; p < end && *p >= '0' && *p <= '9'; ++p)
166         {
167           len *= 10;
168           len += *p - '0';
169         }
170       if (len)
171         {
172           // skip colon
173           p++;
174           if (p + len >= end)
175             return 0; // error in dirinfofile
176
177           unsigned l = len + 1;
178           if (l > sizeof(d->d_name))
179             l = sizeof(d->d_name);
180
181           unsigned n = offsetof (struct dirent64, d_name) + l;
182
183           if (n > sz)
184             break;
185
186           d->d_ino = 1;
187           d->d_off = 0;
188           memcpy(d->d_name, p, len);
189           d->d_name[l - 1] = 0;
190           d->d_reclen = n;
191           d->d_type   = DT_REG;
192           ret += n;
193           sz  -= n;
194           d    = (struct dirent64 *)((unsigned long)d + n);
195         }
196
197       // next infodirfile line
198       while (p < end && *p && *p != '\n' && *p != '\r')
199         p++;
200       while (p < end && *p && (*p == '\n' || *p == '\r'))
201         p++;
202     }
203
204   _current_dir_pos += p - (char *)infoaddr;
205
206   if (!ret) // hack since we should only reset this at open times
207     _current_dir_pos = 0;
208
209   L4Re::Env::env()->rm()->detach(infoaddr, 0);
210
211   return ret;
212 }
213
214 int
215 Env_dir::get_ds(const char *path, L4Re::Auto_cap<L4Re::Dataspace>::Cap *ds) throw()
216 {
217   Vfs::Path p(path);
218   Vfs::Path first = p.strip_first();
219
220   if (first.empty())
221     return -ENOENT;
222
223   L4::Cap<L4Re::Namespace>
224     c = _env->get_cap<L4Re::Namespace>(first.path(), first.length());
225
226   if (!c.is_valid())
227     return -ENOENT;
228
229   if (p.empty())
230     {
231       *ds = L4Re::Auto_cap<L4Re::Dataspace>::Cap(L4::cap_reinterpret_cast<L4Re::Dataspace>(c));
232       return 0;
233     }
234
235   L4Re::Auto_cap<L4Re::Dataspace>::Cap file(cap_alloc()->alloc<L4Re::Dataspace>(), cap_alloc());
236
237   if (!file.is_valid())
238     return -ENOMEM;
239
240   int err = c->query(p.path(), p.length(), file.get());
241
242   if (err < 0)
243     return -ENOENT;
244
245   *ds = file;
246   return err;
247 }
248
249 int
250 Env_dir::get_entry(const char *path, int flags, mode_t mode,
251                    Ref_ptr<L4Re::Vfs::File> *f) throw()
252 {
253   (void)mode; (void)flags;
254   if (!*path)
255     {
256       *f = this;
257       return 0;
258     }
259
260   L4Re::Auto_cap<Dataspace>::Cap file;
261   int err = get_ds(path, &file);
262
263   if (err < 0)
264     return -ENOENT;
265
266   // FIXME: should check if it is a dataspace, somehow
267   L4Re::Vfs::File *fi = 0;
268
269   L4::Cap<L4Re::Namespace> nsc
270     = L4::cap_dynamic_cast<L4Re::Namespace>(file.get());
271
272   if (!nsc.is_valid())
273     fi = new Ro_file(file.get());
274   else // use mat protocol here!!
275     fi = new Ns_dir(nsc);
276
277   if (!fi)
278     return -ENOMEM;
279
280   file.release();
281   *f = fi;
282   return 0;
283 }
284
285 int
286 Env_dir::faccessat(const char *path, int mode, int flags) throw()
287 {
288   (void)flags;
289   Vfs::Path p(path);
290   Vfs::Path first = p.strip_first();
291
292   if (first.empty())
293     return -ENOENT;
294
295   L4::Cap<L4Re::Namespace>
296     c = _env->get_cap<L4Re::Namespace>(first.path(), first.length());
297
298   if (!c.is_valid())
299     return -ENOENT;
300
301   if (p.empty())
302     {
303       if (mode & W_OK)
304         return -EACCES;
305
306       return 0;
307     }
308
309   L4Re::Auto_cap<void>::Cap tmpcap(cap_alloc()->alloc<void>(), cap_alloc());
310
311   if (!tmpcap.is_valid())
312     return -ENOMEM;
313
314   if (c->query(p.path(), p.length(), tmpcap.get()))
315     return -ENOENT;
316
317   if (mode & W_OK)
318     return -EACCES;
319
320   return 0;
321 }
322
323 bool
324 Env_dir::check_type(Env::Cap_entry const *e, long protocol)
325 {
326   L4::Cap<L4::Meta> m(e->cap);
327   return m->supports(protocol).label();
328 }
329
330 int
331 Env_dir::fstat64(struct stat64 *b) const throw()
332 {
333   b->st_dev = 1;
334   b->st_ino = 1;
335   b->st_mode = S_IRWXU | S_IFDIR;
336   b->st_nlink = 0;
337   b->st_uid = 0;
338   b->st_gid = 0;
339   b->st_rdev = 0;
340   b->st_size = 0;
341   b->st_blksize = 0;
342   b->st_blocks = 0;
343   b->st_atime = 0;
344   b->st_mtime = 0;
345   b->st_ctime = 0;
346   return 0;
347 }
348
349 ssize_t
350 Env_dir::getdents(char *buf, size_t sz) throw()
351 {
352   struct dirent64 *d = (struct dirent64 *)buf;
353   ssize_t ret = 0;
354
355   while (d
356          && _current_cap_entry
357          && _current_cap_entry->flags != ~0UL)
358     {
359       unsigned l = strlen(_current_cap_entry->name) + 1;
360       if (l > sizeof(d->d_name))
361         l = sizeof(d->d_name);
362
363       unsigned n = offsetof (struct dirent64, d_name) + l;
364
365       if (n <= sz)
366         {
367           d->d_ino = 1;
368           d->d_off = 0;
369           memcpy(d->d_name, _current_cap_entry->name, l);
370           d->d_name[l - 1] = 0;
371           d->d_reclen = n;
372           if (check_type(_current_cap_entry, L4Re::Protocol::Namespace))
373             d->d_type = DT_DIR;
374           else if (check_type(_current_cap_entry, L4Re::Protocol::Dataspace))
375             d->d_type = DT_REG;
376           else
377             d->d_type = DT_UNKNOWN;
378           ret += n;
379           sz  -= n;
380           d    = (struct dirent64 *)((unsigned long)d + n);
381           _current_cap_entry++;
382         }
383       else
384         return ret;
385     }
386
387   // bit of a hack because we should only (re)set this when opening the dir
388   if (!ret)
389     _current_cap_entry = _env->initial_caps();
390
391   return ret;
392 }
393
394 }}