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))
84 { _info.st_mode = mode; }
86 const char *path() const { return _path; }
87 struct stat64 *info() { return &_info; }
89 void add_ref() throw() { ++_ref_cnt; }
90 int remove_ref() throw() { return --_ref_cnt; }
92 bool is_dir() const { return S_ISDIR(_info.st_mode); }
94 virtual ~Node() { free(_path); }
104 typedef cxx::String Key_type;
105 static Key_type key_of(Node const *n)
106 { return n->path(); }
109 struct Path_avl_tree_compare
111 bool operator () (const char *l, const char *r) const
112 { return strcmp(l, r) < 0; }
113 bool operator () (const cxx::String l, const cxx::String r) const
114 { return strncmp(l.start(), r.start(), cxx::min(l.len(), r.len())) < 0; }
117 class Pers_file : public Node
120 Pers_file(const char *name, mode_t mode)
121 : Node(name, (mode & 0777) | __S_IFREG) {}
122 File_data const &data() const { return _data; }
123 File_data &data() { return _data; }
128 class Pers_dir : public Node
131 typedef cxx::Avl_tree<Node, Node_get_key, Path_avl_tree_compare> Tree;
135 Pers_dir(const char *name, mode_t mode)
136 : Node(name, (mode & 0777) | __S_IFDIR) {}
137 Ref_ptr<Node> find_path(cxx::String);
138 int add_node(Ref_ptr<Node> const &);
140 typedef Tree::Const_iterator Const_iterator;
141 Const_iterator begin() const { return _tree.begin(); }
142 Const_iterator end() const { return _tree.end(); }
145 Ref_ptr<Node> Pers_dir::find_path(cxx::String path)
147 return cxx::ref_ptr(_tree.find_node(path));
150 int Pers_dir::add_node(Ref_ptr<Node> const &n)
153 e = _tree.insert(n.ptr()).second;
159 class Tmpfs_dir : public Be_file
162 explicit Tmpfs_dir(Ref_ptr<Pers_dir> const &d) throw()
163 : _dir(d), _getdents_state(false) {}
164 int get_entry(const char *, int, mode_t, Ref_ptr<File> *) throw();
165 ssize_t getdents(char *, size_t) throw();
166 int fstat64(struct stat64 *buf) const throw();
167 int mkdir(const char *, mode_t) throw();
168 int unlink(const char *) throw();
169 int rename(const char *, const char *) throw();
172 int walk_path(cxx::String const &_s,
173 Ref_ptr<Node> *ret, cxx::String *remaining = 0);
175 Ref_ptr<Pers_dir> _dir;
176 bool _getdents_state;
177 Pers_dir::Const_iterator _getdents_iter;
180 class Tmpfs_file : public Be_file
183 explicit Tmpfs_file(Ref_ptr<Pers_file> const &f) throw()
184 : Be_file(), _file(f), _pos(0) {}
186 ssize_t readv(const struct iovec*, int iovcnt) throw();
187 ssize_t writev(const struct iovec*, int iovcnt) throw();
188 off64_t lseek64(off64_t, int) throw();
189 int fstat64(struct stat64 *buf) const throw();
190 int ioctl(unsigned long, va_list) throw();
193 Ref_ptr<Pers_file> _file;
197 ssize_t Tmpfs_file::readv(const struct iovec *v, int iovcnt) throw()
204 for (int i = 0; i < iovcnt; ++i)
206 sum += _file->data().get(_pos, v[i].iov_len, v[i].iov_base);
207 _pos += v[i].iov_len;
212 ssize_t Tmpfs_file::writev(const struct iovec *v, int iovcnt) throw()
218 for (int i = 0; i < iovcnt; ++i)
220 sum += _file->data().put(_pos, v[i].iov_len, v[i].iov_base);
221 _pos += v[i].iov_len;
226 int Tmpfs_file::fstat64(struct stat64 *buf) const throw()
228 _file->info()->st_size = _file->data().size();
229 memcpy(buf, _file->info(), sizeof(*buf));
233 off64_t Tmpfs_file::lseek64(off64_t offset, int whence) throw()
237 case SEEK_SET: _pos = offset; break;
238 case SEEK_CUR: _pos += offset; break;
239 case SEEK_END: _pos = _file->data().size() + offset; break;
240 default: return -EINVAL;
250 Tmpfs_file::ioctl(unsigned long v, va_list args) throw()
254 case FIONREAD: // return amount of data still available
255 int *available = va_arg(args, int *);
256 *available = _file->data().size() - _pos;
266 Tmpfs_dir::get_entry(const char *name, int flags, mode_t mode,
267 Ref_ptr<File> *file) throw()
276 cxx::String n = name;
278 int e = walk_path(n, &path, &n);
283 if (!(flags & O_CREAT) && e < 0)
286 if ((flags & O_CREAT) && e == -ENOENT)
288 Ref_ptr<Node> node(new Pers_file(n.start(), mode));
289 // when ENOENT is return, path is always a directory
290 int e = cxx::ref_ptr_static_cast<Pers_dir>(path)->add_node(node);
297 *file = new Tmpfs_dir(cxx::ref_ptr_static_cast<Pers_dir>(path));
299 *file = new Tmpfs_file(cxx::ref_ptr_static_cast<Pers_file>(path));
309 Tmpfs_dir::getdents(char *buf, size_t sz) throw()
311 struct dirent64 *d = (struct dirent64 *)buf;
314 if (!_getdents_state)
316 _getdents_iter = _dir->begin();
317 _getdents_state = true;
319 else if (_getdents_iter == _dir->end())
321 _getdents_state = false;
325 for (; _getdents_iter != _dir->end(); ++_getdents_iter)
327 unsigned l = strlen(_getdents_iter->path()) + 1;
328 if (l > sizeof(d->d_name))
329 l = sizeof(d->d_name);
331 unsigned n = offsetof (struct dirent64, d_name) + l;
338 memcpy(d->d_name, _getdents_iter->path(), l);
343 d = (struct dirent64 *)((unsigned long)d + n);
350 Tmpfs_dir::fstat64(struct stat64 *buf) const throw()
352 memcpy(buf, _dir->info(), sizeof(*buf));
357 Tmpfs_dir::walk_path(cxx::String const &_s,
358 Ref_ptr<Node> *ret, cxx::String *remaining)
360 Ref_ptr<Pers_dir> p = _dir;
366 cxx::String::Index sep = s.find("/");
368 n = p->find_path(s.head(sep - s.start()));
374 *remaining = s.head(sep - s.start());
388 s = s.substr(sep + 1);
390 p = cxx::ref_ptr_static_cast<Pers_dir>(n);
399 Tmpfs_dir::mkdir(const char *name, mode_t mode) throw()
401 Ref_ptr<Node> node = _dir;
402 cxx::String p = cxx::String(name);
403 cxx::String path, last = p;
404 cxx::String::Index s = p.rfind("/");
408 last = p.substr(s + 1, p.end() - s);
410 int e = walk_path(path, &node);
418 Ref_ptr<Pers_dir> dnode = cxx::ref_ptr_static_cast<Pers_dir>(node);
420 Ref_ptr<Pers_dir> dir(new Pers_dir(last.start(), mode));
421 return dnode->add_node(dir);
425 Tmpfs_dir::unlink(const char *name) throw()
427 printf("Unimplemented: %s(%s)\n", __func__, name);
432 Tmpfs_dir::rename(const char *old, const char *newn) throw()
434 printf("Unimplemented: %s(%s, %s)\n", __func__, old, newn);
440 class Tmpfs_fs : public Be_file_system
443 Tmpfs_fs() : Be_file_system("tmpfs") {}
444 int mount(char const *source, unsigned long mountflags,
445 void const *data, cxx::Ref_ptr<File> *dir) throw()
450 *dir = cxx::ref_ptr(new Tmpfs_dir(cxx::ref_ptr(new Pers_dir("root", 0777))));
457 static Tmpfs_fs _tmpfs;