4 * (c) 2010 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
5 * Alexander Warg <warg@os.inf.tu-dresden.de>
6 * economic rights: Technische Universität Dresden (Germany)
7 * This file is part of TUD:OS and distributed under the terms of the
8 * GNU Lesser General Public License 2.1.
9 * Please see the COPYING-LGPL-2.1 file for details.
12 #include <l4/l4re_vfs/backend>
13 #include <l4/cxx/string>
14 #include <l4/cxx/avl_tree>
17 #include <sys/ioctl.h>
24 using namespace L4Re::Vfs;
30 File_data() : _buf(0), _size(0) {}
32 unsigned long put(unsigned long offset,
33 unsigned long bufsize, void *srcbuf);
34 unsigned long get(unsigned long offset,
35 unsigned long bufsize, void *dstbuf);
37 unsigned long size() const { return _size; }
39 ~File_data() throw() { free(_buf); }
47 File_data::put(unsigned long offset, unsigned long bufsize, void *srcbuf)
49 if (offset + bufsize > _size)
51 _size = offset + bufsize;
52 _buf = realloc(_buf, _size);
58 memcpy((char *)_buf + offset, srcbuf, bufsize);
63 File_data::get(unsigned long offset, unsigned long bufsize, void *dstbuf)
65 unsigned long s = bufsize;
70 if (offset + bufsize > _size)
73 memcpy(dstbuf, (char *)_buf + offset, s);
78 class Node : public cxx::Avl_tree_node
81 Node(const char *path, mode_t mode)
82 : _ref_cnt(0), _path(strdup(path))
84 memset(&_info, 0, sizeof(_info));
88 const char *path() const { return _path; }
89 struct stat64 *info() { return &_info; }
91 void add_ref() throw() { ++_ref_cnt; }
92 int remove_ref() throw() { return --_ref_cnt; }
94 bool is_dir() const { return S_ISDIR(_info.st_mode); }
96 virtual ~Node() { free(_path); }
106 typedef cxx::String Key_type;
107 static Key_type key_of(Node const *n)
108 { return n->path(); }
111 struct Path_avl_tree_compare
113 bool operator () (const char *l, const char *r) const
114 { return strcmp(l, r) < 0; }
115 bool operator () (const cxx::String l, const cxx::String r) const
116 { return strncmp(l.start(), r.start(), cxx::min(l.len(), r.len())) < 0; }
119 class Pers_file : public Node
122 Pers_file(const char *name, mode_t mode)
123 : Node(name, (mode & 0777) | __S_IFREG) {}
124 File_data const &data() const { return _data; }
125 File_data &data() { return _data; }
130 class Pers_dir : public Node
133 typedef cxx::Avl_tree<Node, Node_get_key, Path_avl_tree_compare> Tree;
137 Pers_dir(const char *name, mode_t mode)
138 : Node(name, (mode & 0777) | __S_IFDIR) {}
139 Ref_ptr<Node> find_path(cxx::String);
140 bool add_node(Ref_ptr<Node> const &);
142 typedef Tree::Const_iterator Const_iterator;
143 Const_iterator begin() const { return _tree.begin(); }
144 Const_iterator end() const { return _tree.end(); }
147 Ref_ptr<Node> Pers_dir::find_path(cxx::String path)
149 return cxx::ref_ptr(_tree.find_node(path));
152 bool Pers_dir::add_node(Ref_ptr<Node> const &n)
154 bool e = _tree.insert(n.ptr()).second;
160 class Tmpfs_dir : public Be_file
163 explicit Tmpfs_dir(Ref_ptr<Pers_dir> const &d) throw()
164 : _dir(d), _getdents_state(false) {}
165 int get_entry(const char *, int, mode_t, Ref_ptr<File> *) throw();
166 ssize_t getdents(char *, size_t) throw();
167 int fstat64(struct stat64 *buf) const throw();
168 int utime(const struct utimbuf *) throw();
169 int fchmod(mode_t) throw();
170 int mkdir(const char *, mode_t) throw();
171 int unlink(const char *) throw();
172 int rename(const char *, const char *) throw();
175 int walk_path(cxx::String const &_s,
176 Ref_ptr<Node> *ret, cxx::String *remaining = 0);
178 Ref_ptr<Pers_dir> _dir;
179 bool _getdents_state;
180 Pers_dir::Const_iterator _getdents_iter;
183 class Tmpfs_file : public Be_file_pos
186 explicit Tmpfs_file(Ref_ptr<Pers_file> const &f) throw()
187 : Be_file_pos(), _file(f) {}
189 off64_t size() const throw();
190 int fstat64(struct stat64 *buf) const throw();
191 int ioctl(unsigned long, va_list) throw();
192 int utime(const struct utimbuf *) throw();
193 int fchmod(mode_t) throw();
196 ssize_t preadv(const struct iovec *v, int iovcnt, off64_t p) throw();
197 ssize_t pwritev(const struct iovec *v, int iovcnt, off64_t p) throw();
198 Ref_ptr<Pers_file> _file;
201 ssize_t Tmpfs_file::preadv(const struct iovec *v, int iovcnt, off64_t p) throw()
207 for (int i = 0; i < iovcnt; ++i)
209 sum += _file->data().get(p, v[i].iov_len, v[i].iov_base);
215 ssize_t Tmpfs_file::pwritev(const struct iovec *v, int iovcnt, off64_t p) throw()
221 for (int i = 0; i < iovcnt; ++i)
223 sum += _file->data().put(p, v[i].iov_len, v[i].iov_base);
229 int Tmpfs_file::fstat64(struct stat64 *buf) const throw()
231 _file->info()->st_size = _file->data().size();
232 memcpy(buf, _file->info(), sizeof(*buf));
236 off64_t Tmpfs_file::size() const throw()
237 { return _file->data().size(); }
240 Tmpfs_file::ioctl(unsigned long v, va_list args) throw()
244 case FIONREAD: // return amount of data still available
245 int *available = va_arg(args, int *);
246 *available = _file->data().size() - pos();
253 Tmpfs_file::utime(const struct utimbuf *times) throw()
255 _file->info()->st_atime = times->actime;
256 _file->info()->st_mtime = times->modtime;
261 Tmpfs_file::fchmod(mode_t m) throw()
263 _file->info()->st_mode = m;
269 Tmpfs_dir::get_entry(const char *name, int flags, mode_t mode,
270 Ref_ptr<File> *file) throw()
279 cxx::String n = name;
281 int e = walk_path(n, &path, &n);
286 if (!(flags & O_CREAT) && e < 0)
289 if ((flags & O_CREAT) && e == -ENOENT)
291 Ref_ptr<Node> node(new Pers_file(n.start(), mode));
292 // when ENOENT is return, path is always a directory
293 bool e = cxx::ref_ptr_static_cast<Pers_dir>(path)->add_node(node);
300 *file = new Tmpfs_dir(cxx::ref_ptr_static_cast<Pers_dir>(path));
302 *file = new Tmpfs_file(cxx::ref_ptr_static_cast<Pers_file>(path));
312 Tmpfs_dir::getdents(char *buf, size_t sz) throw()
314 struct dirent64 *d = (struct dirent64 *)buf;
317 if (!_getdents_state)
319 _getdents_iter = _dir->begin();
320 _getdents_state = true;
322 else if (_getdents_iter == _dir->end())
324 _getdents_state = false;
328 for (; _getdents_iter != _dir->end(); ++_getdents_iter)
330 unsigned l = strlen(_getdents_iter->path()) + 1;
331 if (l > sizeof(d->d_name))
332 l = sizeof(d->d_name);
334 unsigned n = offsetof (struct dirent64, d_name) + l;
335 n = (n + sizeof(long) - 1) & ~(sizeof(long) - 1);
342 memcpy(d->d_name, _getdents_iter->path(), l);
347 d = (struct dirent64 *)((unsigned long)d + n);
354 Tmpfs_dir::fstat64(struct stat64 *buf) const throw()
356 memcpy(buf, _dir->info(), sizeof(*buf));
361 Tmpfs_dir::utime(const struct utimbuf *times) throw()
363 _dir->info()->st_atime = times->actime;
364 _dir->info()->st_mtime = times->modtime;
369 Tmpfs_dir::fchmod(mode_t m) throw()
371 _dir->info()->st_mode = m;
376 Tmpfs_dir::walk_path(cxx::String const &_s,
377 Ref_ptr<Node> *ret, cxx::String *remaining)
379 Ref_ptr<Pers_dir> p = _dir;
391 cxx::String::Index sep = s.find("/");
393 if (sep - s.start() == 1 && *s.start() == '.')
395 s = s.substr(s.start() + 2);
399 n = p->find_path(s.head(sep - s.start()));
405 *remaining = s.head(sep - s.start());
419 s = s.substr(sep + 1);
421 p = cxx::ref_ptr_static_cast<Pers_dir>(n);
430 Tmpfs_dir::mkdir(const char *name, mode_t mode) throw()
432 Ref_ptr<Node> node = _dir;
433 cxx::String p = cxx::String(name);
434 cxx::String path, last = p;
435 cxx::String::Index s = p.rfind("/");
437 // trim /'s at the end
438 while (p.len() && s == p.end() - 1)
444 //printf("MKDIR '%s' p=%p %p\n", name, p.start(), s);
449 last = p.substr(s + 1, p.end() - s);
451 int e = walk_path(path, &node);
459 // due to path walking we can end up with an empty name
460 if (p.len() == 0 || p == cxx::String("."))
463 Ref_ptr<Pers_dir> dnode = cxx::ref_ptr_static_cast<Pers_dir>(node);
465 Ref_ptr<Pers_dir> dir(new Pers_dir(last.start(), mode));
466 return dnode->add_node(dir) ? 0 : -EEXIST;
470 Tmpfs_dir::unlink(const char *name) throw()
472 cxx::Ref_ptr<Node> n;
474 int e = walk_path(name, &n);
478 printf("Unimplemented (if file exists): %s(%s)\n", __func__, name);
483 Tmpfs_dir::rename(const char *old, const char *newn) throw()
485 printf("Unimplemented: %s(%s, %s)\n", __func__, old, newn);
491 class Tmpfs_fs : public Be_file_system
494 Tmpfs_fs() : Be_file_system("tmpfs") {}
495 int mount(char const *source, unsigned long mountflags,
496 void const *data, cxx::Ref_ptr<File> *dir) throw()
501 *dir = cxx::ref_ptr(new Tmpfs_dir(cxx::ref_ptr(new Pers_dir("root", 0777))));
508 static Tmpfs_fs _tmpfs L4RE_VFS_FILE_SYSTEM_ATTRIBUTE;