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
117 int v = strncmp(l.start(), r.start(), cxx::min(l.len(), r.len()));
118 return v < 0 || (v == 0 && l.len() < r.len());
122 class Pers_file : public Node
125 Pers_file(const char *name, mode_t mode)
126 : Node(name, (mode & 0777) | __S_IFREG) {}
127 File_data const &data() const { return _data; }
128 File_data &data() { return _data; }
133 class Pers_dir : public Node
136 typedef cxx::Avl_tree<Node, Node_get_key, Path_avl_tree_compare> Tree;
140 Pers_dir(const char *name, mode_t mode)
141 : Node(name, (mode & 0777) | __S_IFDIR) {}
142 Ref_ptr<Node> find_path(cxx::String);
143 bool add_node(Ref_ptr<Node> const &);
145 typedef Tree::Const_iterator Const_iterator;
146 Const_iterator begin() const { return _tree.begin(); }
147 Const_iterator end() const { return _tree.end(); }
150 Ref_ptr<Node> Pers_dir::find_path(cxx::String path)
152 return cxx::ref_ptr(_tree.find_node(path));
155 bool Pers_dir::add_node(Ref_ptr<Node> const &n)
157 bool e = _tree.insert(n.ptr()).second;
163 class Tmpfs_dir : public Be_file
166 explicit Tmpfs_dir(Ref_ptr<Pers_dir> const &d) throw()
167 : _dir(d), _getdents_state(false) {}
168 int get_entry(const char *, int, mode_t, Ref_ptr<File> *) throw();
169 ssize_t getdents(char *, size_t) throw();
170 int fstat64(struct stat64 *buf) const throw();
171 int utime(const struct utimbuf *) throw();
172 int fchmod(mode_t) throw();
173 int mkdir(const char *, mode_t) throw();
174 int unlink(const char *) throw();
175 int rename(const char *, const char *) throw();
178 int walk_path(cxx::String const &_s,
179 Ref_ptr<Node> *ret, cxx::String *remaining = 0);
181 Ref_ptr<Pers_dir> _dir;
182 bool _getdents_state;
183 Pers_dir::Const_iterator _getdents_iter;
186 class Tmpfs_file : public Be_file_pos
189 explicit Tmpfs_file(Ref_ptr<Pers_file> const &f) throw()
190 : Be_file_pos(), _file(f) {}
192 off64_t size() const throw();
193 int fstat64(struct stat64 *buf) const throw();
194 int ioctl(unsigned long, va_list) throw();
195 int utime(const struct utimbuf *) throw();
196 int fchmod(mode_t) throw();
199 ssize_t preadv(const struct iovec *v, int iovcnt, off64_t p) throw();
200 ssize_t pwritev(const struct iovec *v, int iovcnt, off64_t p) throw();
201 Ref_ptr<Pers_file> _file;
204 ssize_t Tmpfs_file::preadv(const struct iovec *v, int iovcnt, off64_t p) throw()
210 for (int i = 0; i < iovcnt; ++i)
212 sum += _file->data().get(p, v[i].iov_len, v[i].iov_base);
218 ssize_t Tmpfs_file::pwritev(const struct iovec *v, int iovcnt, off64_t p) throw()
224 for (int i = 0; i < iovcnt; ++i)
226 sum += _file->data().put(p, v[i].iov_len, v[i].iov_base);
232 int Tmpfs_file::fstat64(struct stat64 *buf) const throw()
234 _file->info()->st_size = _file->data().size();
235 memcpy(buf, _file->info(), sizeof(*buf));
239 off64_t Tmpfs_file::size() const throw()
240 { return _file->data().size(); }
243 Tmpfs_file::ioctl(unsigned long v, va_list args) throw()
247 case FIONREAD: // return amount of data still available
248 int *available = va_arg(args, int *);
249 *available = _file->data().size() - pos();
256 Tmpfs_file::utime(const struct utimbuf *times) throw()
258 _file->info()->st_atime = times->actime;
259 _file->info()->st_mtime = times->modtime;
264 Tmpfs_file::fchmod(mode_t m) throw()
266 _file->info()->st_mode = m;
272 Tmpfs_dir::get_entry(const char *name, int flags, mode_t mode,
273 Ref_ptr<File> *file) throw()
282 cxx::String n = name;
284 int e = walk_path(n, &path, &n);
289 if (!(flags & O_CREAT) && e < 0)
292 if ((flags & O_CREAT) && e == -ENOENT)
294 Ref_ptr<Node> node(new Pers_file(n.start(), mode));
295 // when ENOENT is return, path is always a directory
296 bool e = cxx::ref_ptr_static_cast<Pers_dir>(path)->add_node(node);
303 *file = new Tmpfs_dir(cxx::ref_ptr_static_cast<Pers_dir>(path));
305 *file = new Tmpfs_file(cxx::ref_ptr_static_cast<Pers_file>(path));
315 Tmpfs_dir::getdents(char *buf, size_t sz) throw()
317 struct dirent64 *d = (struct dirent64 *)buf;
320 if (!_getdents_state)
322 _getdents_iter = _dir->begin();
323 _getdents_state = true;
325 else if (_getdents_iter == _dir->end())
327 _getdents_state = false;
331 for (; _getdents_iter != _dir->end(); ++_getdents_iter)
333 unsigned l = strlen(_getdents_iter->path()) + 1;
334 if (l > sizeof(d->d_name))
335 l = sizeof(d->d_name);
337 unsigned n = offsetof (struct dirent64, d_name) + l;
338 n = (n + sizeof(long) - 1) & ~(sizeof(long) - 1);
345 memcpy(d->d_name, _getdents_iter->path(), l);
350 d = (struct dirent64 *)((unsigned long)d + n);
357 Tmpfs_dir::fstat64(struct stat64 *buf) const throw()
359 memcpy(buf, _dir->info(), sizeof(*buf));
364 Tmpfs_dir::utime(const struct utimbuf *times) throw()
366 _dir->info()->st_atime = times->actime;
367 _dir->info()->st_mtime = times->modtime;
372 Tmpfs_dir::fchmod(mode_t m) throw()
374 _dir->info()->st_mode = m;
379 Tmpfs_dir::walk_path(cxx::String const &_s,
380 Ref_ptr<Node> *ret, cxx::String *remaining)
382 Ref_ptr<Pers_dir> p = _dir;
394 cxx::String::Index sep = s.find("/");
396 if (sep - s.start() == 1 && *s.start() == '.')
398 s = s.substr(s.start() + 2);
402 n = p->find_path(s.head(sep - s.start()));
408 *remaining = s.head(sep - s.start());
422 s = s.substr(sep + 1);
424 p = cxx::ref_ptr_static_cast<Pers_dir>(n);
433 Tmpfs_dir::mkdir(const char *name, mode_t mode) throw()
435 Ref_ptr<Node> node = _dir;
436 cxx::String p = cxx::String(name);
437 cxx::String path, last = p;
438 cxx::String::Index s = p.rfind("/");
440 // trim /'s at the end
441 while (p.len() && s == p.end() - 1)
447 //printf("MKDIR '%s' p=%p %p\n", name, p.start(), s);
452 last = p.substr(s + 1, p.end() - s);
454 int e = walk_path(path, &node);
462 // due to path walking we can end up with an empty name
463 if (p.len() == 0 || p == cxx::String("."))
466 Ref_ptr<Pers_dir> dnode = cxx::ref_ptr_static_cast<Pers_dir>(node);
468 Ref_ptr<Pers_dir> dir(new Pers_dir(last.start(), mode));
469 return dnode->add_node(dir) ? 0 : -EEXIST;
473 Tmpfs_dir::unlink(const char *name) throw()
475 cxx::Ref_ptr<Node> n;
477 int e = walk_path(name, &n);
481 printf("Unimplemented (if file exists): %s(%s)\n", __func__, name);
486 Tmpfs_dir::rename(const char *old, const char *newn) throw()
488 printf("Unimplemented: %s(%s, %s)\n", __func__, old, newn);
494 class Tmpfs_fs : public Be_file_system
497 Tmpfs_fs() : Be_file_system("tmpfs") {}
498 int mount(char const *source, unsigned long mountflags,
499 void const *data, cxx::Ref_ptr<File> *dir) throw()
504 *dir = cxx::ref_ptr(new Tmpfs_dir(cxx::ref_ptr(new Pers_dir("root", 0777))));
511 static Tmpfs_fs _tmpfs L4RE_VFS_FILE_SYSTEM_ATTRIBUTE;