4 * (c) 2010 Technische Universität Dresden
5 * This file is part of TUD:OS and distributed under the terms of the
6 * GNU Lesser General Public License 2.1.
7 * Please see the COPYING-LGPL-2.1 file for details.
10 #include <l4/l4re_vfs/backend>
11 #include <l4/cxx/string>
12 #include <l4/cxx/avl_tree>
15 #include <sys/ioctl.h>
23 using namespace L4Re::Vfs;
29 File_data() : _buf(0), _size(0) {}
31 unsigned long put(unsigned long offset,
32 unsigned long bufsize, void *srcbuf);
33 unsigned long get(unsigned long offset,
34 unsigned long bufsize, void *dstbuf);
36 unsigned long size() const { return _size; }
38 ~File_data() throw() { free(_buf); }
46 File_data::put(unsigned long offset, unsigned long bufsize, void *srcbuf)
48 if (offset + bufsize > _size)
50 _size = offset + bufsize;
51 _buf = realloc(_buf, _size);
57 memcpy((char *)_buf + offset, srcbuf, bufsize);
62 File_data::get(unsigned long offset, unsigned long bufsize, void *dstbuf)
64 unsigned long s = bufsize;
69 if (offset + bufsize > _size)
72 memcpy(dstbuf, (char *)_buf + offset, s);
77 class Node : public cxx::Avl_tree_node
80 Node(const char *path, mode_t mode)
81 : _ref_cnt(0), _path(strdup(path))
82 { _info.st_mode = mode; }
84 const char *path() const { return _path; }
85 struct stat64 *info() { return &_info; }
87 void add_ref() throw() { ++_ref_cnt; }
88 int remove_ref() throw() { return --_ref_cnt; }
90 bool is_dir() const { return S_ISDIR(_info.st_mode); }
92 virtual ~Node() { free(_path); }
102 typedef cxx::String Key_type;
103 static Key_type key_of(Node const *n)
104 { return n->path(); }
107 struct Path_avl_tree_compare
109 bool operator () (const char *l, const char *r) const
110 { return strcmp(l, r) < 0; }
111 bool operator () (const cxx::String l, const cxx::String r) const
112 { return strncmp(l.start(), r.start(), cxx::min(l.len(), r.len())) < 0; }
115 class Pers_file : public Node
118 Pers_file(const char *name, mode_t mode)
119 : Node(name, (mode & 0777) | __S_IFREG) {}
120 File_data const &data() const { return _data; }
121 File_data &data() { return _data; }
126 class Pers_dir : public Node
129 Pers_dir(const char *name, mode_t mode)
130 : Node(name, (mode & 0777) | __S_IFDIR) {}
131 Ref_ptr<Node> find_path(cxx::String);
132 int add_node(Ref_ptr<Node> const &);
135 typedef cxx::Avl_tree<Node, Node_get_key, Path_avl_tree_compare> Tree;
139 Ref_ptr<Node> Pers_dir::find_path(cxx::String path)
141 return cxx::ref_ptr(_tree.find_node(path));
144 int Pers_dir::add_node(Ref_ptr<Node> const &n)
147 e = _tree.insert(n.ptr()).second;
153 class Tmpfs_dir : public Be_file
156 explicit Tmpfs_dir(Ref_ptr<Pers_dir> const &d) throw()
158 int get_entry(const char *, int, mode_t, Ref_ptr<File> *) throw();
159 int fstat64(struct stat64 *buf) const throw();
160 int mkdir(const char *, mode_t) throw();
161 int unlink(const char *) throw();
162 int rename(const char *, const char *) throw();
165 int walk_path(cxx::String const &_s,
166 Ref_ptr<Node> *ret, cxx::String *remaining = 0);
168 Ref_ptr<Pers_dir> _dir;
171 class Tmpfs_file : public Be_file
174 explicit Tmpfs_file(Ref_ptr<Pers_file> const &f) throw()
175 : Be_file(), _file(f), _pos(0) {}
177 ssize_t readv(const struct iovec*, int iovcnt) throw();
178 ssize_t writev(const struct iovec*, int iovcnt) throw();
179 off64_t lseek64(off64_t, int) throw();
180 int fstat64(struct stat64 *buf) const throw();
181 int ioctl(unsigned long, va_list) throw();
184 Ref_ptr<Pers_file> _file;
188 ssize_t Tmpfs_file::readv(const struct iovec *v, int iovcnt) throw()
195 for (int i = 0; i < iovcnt; ++i)
197 sum += _file->data().get(_pos, v[i].iov_len, v[i].iov_base);
198 _pos += v[i].iov_len;
203 ssize_t Tmpfs_file::writev(const struct iovec *v, int iovcnt) throw()
209 for (int i = 0; i < iovcnt; ++i)
211 sum += _file->data().put(_pos, v[i].iov_len, v[i].iov_base);
212 _pos += v[i].iov_len;
217 int Tmpfs_file::fstat64(struct stat64 *buf) const throw()
219 memcpy(buf, _file->info(), sizeof(*buf));
223 off64_t Tmpfs_file::lseek64(off64_t offset, int whence) throw()
227 case SEEK_SET: _pos = offset; break;
228 case SEEK_CUR: _pos += offset; break;
229 case SEEK_END: _pos = _file->data().size() + offset; break;
230 default: return -EINVAL;
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;
256 Tmpfs_dir::get_entry(const char *name, int flags, mode_t mode,
257 Ref_ptr<File> *file) throw()
266 cxx::String n = name;
268 int e = walk_path(n, &path, &n);
273 if (!(flags & O_CREAT) && e < 0)
276 if ((flags & O_CREAT) && e == -ENOENT)
278 Ref_ptr<Node> node(new Pers_file(n.start(), mode));
279 // when ENOENT is return, path is always a directory
280 int e = cxx::ref_ptr_static_cast<Pers_dir>(path)->add_node(node);
287 *file = new Tmpfs_dir(cxx::ref_ptr_static_cast<Pers_dir>(path));
289 *file = new Tmpfs_file(cxx::ref_ptr_static_cast<Pers_file>(path));
299 Tmpfs_dir::fstat64(struct stat64 *buf) const throw()
301 memcpy(buf, _dir->info(), sizeof(*buf));
306 Tmpfs_dir::walk_path(cxx::String const &_s,
307 Ref_ptr<Node> *ret, cxx::String *remaining)
309 Ref_ptr<Pers_dir> p = _dir;
315 cxx::String::Index sep = s.find("/");
317 n = p->find_path(s.head(sep - s.start()));
323 *remaining = s.head(sep - s.start());
337 s = s.substr(sep + 1);
339 p = cxx::ref_ptr_static_cast<Pers_dir>(n);
348 Tmpfs_dir::mkdir(const char *name, mode_t mode) throw()
350 Ref_ptr<Node> node = _dir;
351 cxx::String p = cxx::String(name);
352 cxx::String path, last = p;
353 cxx::String::Index s = p.rfind("/");
357 last = p.substr(s + 1, p.end() - s);
359 int e = walk_path(path, &node);
367 Ref_ptr<Pers_dir> dnode = cxx::ref_ptr_static_cast<Pers_dir>(node);
369 Ref_ptr<Pers_dir> dir(new Pers_dir(last.start(), mode));
370 return dnode->add_node(dir);
374 Tmpfs_dir::unlink(const char *name) throw()
376 printf("Unimplemented: %s(%s)\n", __func__, name);
381 Tmpfs_dir::rename(const char *old, const char *newn) throw()
383 printf("Unimplemented: %s(%s, %s)\n", __func__, old, newn);
389 class Tmpfs_fs : public Be_file_system
392 Tmpfs_fs() : Be_file_system("tmpfs") {}
393 int mount(char const *source, unsigned long mountflags,
394 void const *data, cxx::Ref_ptr<File> *dir) throw()
399 *dir = cxx::ref_ptr(new Tmpfs_dir(cxx::ref_ptr(new Pers_dir("root", 0777))));
406 static Tmpfs_fs _tmpfs;