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>
25 using namespace L4Re::Vfs;
31 File_data() : _buf(0), _size(0) {}
33 unsigned long put(unsigned long offset,
34 unsigned long bufsize, void *srcbuf);
35 unsigned long get(unsigned long offset,
36 unsigned long bufsize, void *dstbuf);
38 unsigned long size() const { return _size; }
40 ~File_data() throw() { free(_buf); }
48 File_data::put(unsigned long offset, unsigned long bufsize, void *srcbuf)
50 if (offset + bufsize > _size)
52 _size = offset + bufsize;
53 _buf = realloc(_buf, _size);
59 memcpy((char *)_buf + offset, srcbuf, bufsize);
64 File_data::get(unsigned long offset, unsigned long bufsize, void *dstbuf)
66 unsigned long s = bufsize;
71 if (offset + bufsize > _size)
74 memcpy(dstbuf, (char *)_buf + offset, s);
79 class Node : public cxx::Avl_tree_node
82 Node(const char *path, mode_t mode)
83 : _ref_cnt(0), _path(strdup(path))
85 memset(&_info, 0, sizeof(_info));
89 const char *path() const { return _path; }
90 struct stat64 *info() { return &_info; }
92 void add_ref() throw() { ++_ref_cnt; }
93 int remove_ref() throw() { return --_ref_cnt; }
95 bool is_dir() const { return S_ISDIR(_info.st_mode); }
97 virtual ~Node() { free(_path); }
107 typedef cxx::String Key_type;
108 static Key_type key_of(Node const *n)
109 { return n->path(); }
112 struct Path_avl_tree_compare
114 bool operator () (const char *l, const char *r) const
115 { return strcmp(l, r) < 0; }
116 bool operator () (const cxx::String l, const cxx::String r) const
117 { return strncmp(l.start(), r.start(), cxx::min(l.len(), r.len())) < 0; }
120 class Pers_file : public Node
123 Pers_file(const char *name, mode_t mode)
124 : Node(name, (mode & 0777) | __S_IFREG) {}
125 File_data const &data() const { return _data; }
126 File_data &data() { return _data; }
131 class Pers_dir : public Node
134 typedef cxx::Avl_tree<Node, Node_get_key, Path_avl_tree_compare> Tree;
138 Pers_dir(const char *name, mode_t mode)
139 : Node(name, (mode & 0777) | __S_IFDIR) {}
140 Ref_ptr<Node> find_path(cxx::String);
141 int add_node(Ref_ptr<Node> const &);
143 typedef Tree::Const_iterator Const_iterator;
144 Const_iterator begin() const { return _tree.begin(); }
145 Const_iterator end() const { return _tree.end(); }
148 Ref_ptr<Node> Pers_dir::find_path(cxx::String path)
150 return cxx::ref_ptr(_tree.find_node(path));
153 int Pers_dir::add_node(Ref_ptr<Node> const &n)
155 int e = _tree.insert(n.ptr()).second;
161 class Tmpfs_dir : public Be_file
164 explicit Tmpfs_dir(Ref_ptr<Pers_dir> const &d) throw()
165 : _dir(d), _getdents_state(false) {}
166 int get_entry(const char *, int, mode_t, Ref_ptr<File> *) throw();
167 ssize_t getdents(char *, size_t) throw();
168 int fstat64(struct stat64 *buf) const throw();
169 int utime(const struct utimbuf *) throw();
170 int fchmod(mode_t) throw();
171 int mkdir(const char *, mode_t) throw();
172 int unlink(const char *) throw();
173 int rename(const char *, const char *) throw();
176 int walk_path(cxx::String const &_s,
177 Ref_ptr<Node> *ret, cxx::String *remaining = 0);
179 Ref_ptr<Pers_dir> _dir;
180 bool _getdents_state;
181 Pers_dir::Const_iterator _getdents_iter;
184 class Tmpfs_file : public Be_file
187 explicit Tmpfs_file(Ref_ptr<Pers_file> const &f) throw()
188 : Be_file(), _file(f), _pos(0) {}
190 ssize_t readv(const struct iovec*, int iovcnt) throw();
191 ssize_t writev(const struct iovec*, int iovcnt) throw();
192 off64_t lseek64(off64_t, int) 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 Ref_ptr<Pers_file> _file;
203 ssize_t Tmpfs_file::readv(const struct iovec *v, int iovcnt) throw()
210 for (int i = 0; i < iovcnt; ++i)
212 sum += _file->data().get(_pos, v[i].iov_len, v[i].iov_base);
213 _pos += v[i].iov_len;
218 ssize_t Tmpfs_file::writev(const struct iovec *v, int iovcnt) throw()
224 for (int i = 0; i < iovcnt; ++i)
226 sum += _file->data().put(_pos, v[i].iov_len, v[i].iov_base);
227 _pos += v[i].iov_len;
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::lseek64(off64_t offset, int whence) throw()
243 case SEEK_SET: _pos = offset; break;
244 case SEEK_CUR: _pos += offset; break;
245 case SEEK_END: _pos = _file->data().size() + offset; break;
246 default: return -EINVAL;
256 Tmpfs_file::ioctl(unsigned long v, va_list args) throw()
260 case FIONREAD: // return amount of data still available
261 int *available = va_arg(args, int *);
262 *available = _file->data().size() - _pos;
269 Tmpfs_file::utime(const struct utimbuf *times) throw()
271 _file->info()->st_atime = times->actime;
272 _file->info()->st_mtime = times->modtime;
277 Tmpfs_file::fchmod(mode_t m) throw()
279 _file->info()->st_mode = m;
285 Tmpfs_dir::get_entry(const char *name, int flags, mode_t mode,
286 Ref_ptr<File> *file) throw()
295 cxx::String n = name;
297 int e = walk_path(n, &path, &n);
302 if (!(flags & O_CREAT) && e < 0)
305 if ((flags & O_CREAT) && e == -ENOENT)
307 Ref_ptr<Node> node(new Pers_file(n.start(), mode));
308 // when ENOENT is return, path is always a directory
309 int e = cxx::ref_ptr_static_cast<Pers_dir>(path)->add_node(node);
316 *file = new Tmpfs_dir(cxx::ref_ptr_static_cast<Pers_dir>(path));
318 *file = new Tmpfs_file(cxx::ref_ptr_static_cast<Pers_file>(path));
328 Tmpfs_dir::getdents(char *buf, size_t sz) throw()
330 struct dirent64 *d = (struct dirent64 *)buf;
333 if (!_getdents_state)
335 _getdents_iter = _dir->begin();
336 _getdents_state = true;
338 else if (_getdents_iter == _dir->end())
340 _getdents_state = false;
344 for (; _getdents_iter != _dir->end(); ++_getdents_iter)
346 unsigned l = strlen(_getdents_iter->path()) + 1;
347 if (l > sizeof(d->d_name))
348 l = sizeof(d->d_name);
350 unsigned n = offsetof (struct dirent64, d_name) + l;
357 memcpy(d->d_name, _getdents_iter->path(), l);
362 d = (struct dirent64 *)((unsigned long)d + n);
369 Tmpfs_dir::fstat64(struct stat64 *buf) const throw()
371 memcpy(buf, _dir->info(), sizeof(*buf));
376 Tmpfs_dir::utime(const struct utimbuf *times) throw()
378 _dir->info()->st_atime = times->actime;
379 _dir->info()->st_mtime = times->modtime;
384 Tmpfs_dir::fchmod(mode_t m) throw()
386 _dir->info()->st_mode = m;
391 Tmpfs_dir::walk_path(cxx::String const &_s,
392 Ref_ptr<Node> *ret, cxx::String *remaining)
394 Ref_ptr<Pers_dir> p = _dir;
400 cxx::String::Index sep = s.find("/");
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("/");
442 last = p.substr(s + 1, p.end() - s);
444 int e = walk_path(path, &node);
452 Ref_ptr<Pers_dir> dnode = cxx::ref_ptr_static_cast<Pers_dir>(node);
454 Ref_ptr<Pers_dir> dir(new Pers_dir(last.start(), mode));
455 return dnode->add_node(dir);
459 Tmpfs_dir::unlink(const char *name) throw()
461 cxx::Ref_ptr<Node> n;
463 int e = walk_path(name, &n);
467 printf("Unimplemented (if file exists): %s(%s)\n", __func__, name);
472 Tmpfs_dir::rename(const char *old, const char *newn) throw()
474 printf("Unimplemented: %s(%s, %s)\n", __func__, old, newn);
480 class Tmpfs_fs : public Be_file_system
483 Tmpfs_fs() : Be_file_system("tmpfs") {}
484 int mount(char const *source, unsigned long mountflags,
485 void const *data, cxx::Ref_ptr<File> *dir) throw()
490 *dir = cxx::ref_ptr(new Tmpfs_dir(cxx::ref_ptr(new Pers_dir("root", 0777))));
497 static Tmpfs_fs _tmpfs;