2013-11-08 16:44:44 +01:00
|
|
|
/*
|
|
|
|
* \brief File-system directory node
|
|
|
|
* \author Norman Feske
|
|
|
|
* \author Christian Helmuth
|
|
|
|
* \date 2013-11-11
|
|
|
|
*/
|
|
|
|
|
2017-06-20 14:23:22 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013-2017 Genode Labs GmbH
|
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
|
|
|
*/
|
|
|
|
|
2013-11-08 16:44:44 +01:00
|
|
|
#ifndef _DIRECTORY_H_
|
|
|
|
#define _DIRECTORY_H_
|
|
|
|
|
|
|
|
/* Genode include */
|
2015-04-23 21:26:54 +02:00
|
|
|
#include <file_system/util.h>
|
2013-11-08 16:44:44 +01:00
|
|
|
#include <os/path.h>
|
|
|
|
|
|
|
|
/* local includes */
|
|
|
|
#include <node.h>
|
|
|
|
#include <file.h>
|
|
|
|
|
|
|
|
#include <lx_util.h>
|
2019-01-31 15:04:22 +01:00
|
|
|
#include <stdio.h>
|
2013-11-08 16:44:44 +01:00
|
|
|
|
|
|
|
|
2017-06-20 14:23:22 +02:00
|
|
|
namespace Lx_fs {
|
2016-01-07 10:52:10 +01:00
|
|
|
using namespace Genode;
|
2017-06-20 14:23:22 +02:00
|
|
|
using namespace File_system;
|
2013-11-08 16:44:44 +01:00
|
|
|
class Directory;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-20 14:23:22 +02:00
|
|
|
class Lx_fs::Directory : public Node
|
2013-11-08 16:44:44 +01:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
Follow practices suggested by "Effective C++"
The patch adjust the code of the base, base-<kernel>, and os repository.
To adapt existing components to fix violations of the best practices
suggested by "Effective C++" as reported by the -Weffc++ compiler
argument. The changes follow the patterns outlined below:
* A class with virtual functions can no longer publicly inherit base
classed without a vtable. The inherited object may either be moved
to a member variable, or inherited privately. The latter would be
used for classes that inherit 'List::Element' or 'Avl_node'. In order
to enable the 'List' and 'Avl_tree' to access the meta data, the
'List' must become a friend.
* Instead of adding a virtual destructor to abstract base classes,
we inherit the new 'Interface' class, which contains a virtual
destructor. This way, single-line abstract base classes can stay
as compact as they are now. The 'Interface' utility resides in
base/include/util/interface.h.
* With the new warnings enabled, all member variables must be explicitly
initialized. Basic types may be initialized with '='. All other types
are initialized with braces '{ ... }' or as class initializers. If
basic types and non-basic types appear in a row, it is nice to only
use the brace syntax (also for basic types) and align the braces.
* If a class contains pointers as members, it must now also provide a
copy constructor and assignment operator. In the most cases, one
would make them private, effectively disallowing the objects to be
copied. Unfortunately, this warning cannot be fixed be inheriting
our existing 'Noncopyable' class (the compiler fails to detect that
the inheriting class cannot be copied and still gives the error).
For now, we have to manually add declarations for both the copy
constructor and assignment operator as private class members. Those
declarations should be prepended with a comment like this:
/*
* Noncopyable
*/
Thread(Thread const &);
Thread &operator = (Thread const &);
In the future, we should revisit these places and try to replace
the pointers with references. In the presence of at least one
reference member, the compiler would no longer implicitly generate
a copy constructor. So we could remove the manual declaration.
Issue #465
2017-12-21 15:42:15 +01:00
|
|
|
/*
|
|
|
|
* Noncopyable
|
|
|
|
*/
|
|
|
|
Directory(Directory const &);
|
|
|
|
Directory &operator = (Directory const &);
|
|
|
|
|
2013-11-08 16:44:44 +01:00
|
|
|
typedef Genode::Path<MAX_PATH_LEN> Path;
|
|
|
|
|
|
|
|
DIR *_fd;
|
|
|
|
Path _path;
|
|
|
|
Allocator &_alloc;
|
|
|
|
|
|
|
|
unsigned long _inode(char const *path, bool create)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (create) {
|
|
|
|
mode_t ugo = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
|
|
|
|
ret = mkdir(path, ugo);
|
2017-06-12 16:02:11 +02:00
|
|
|
if (ret == -1) {
|
|
|
|
switch (errno) {
|
|
|
|
case ENAMETOOLONG: throw Name_too_long();
|
|
|
|
case EACCES: throw Permission_denied();
|
|
|
|
case ENOENT: throw Lookup_failed();
|
|
|
|
case EEXIST: throw Node_already_exists();
|
|
|
|
case ENOSPC:
|
|
|
|
default: throw No_space();
|
|
|
|
}
|
|
|
|
}
|
2013-11-08 16:44:44 +01:00
|
|
|
}
|
|
|
|
|
2019-09-25 17:13:29 +02:00
|
|
|
struct stat s { };
|
2013-11-08 16:44:44 +01:00
|
|
|
|
|
|
|
ret = lstat(path, &s);
|
|
|
|
if (ret == -1)
|
|
|
|
throw Lookup_failed();
|
|
|
|
|
|
|
|
return s.st_ino;
|
|
|
|
}
|
|
|
|
|
|
|
|
DIR *_open(char const *path)
|
|
|
|
{
|
|
|
|
DIR *fd = opendir(path);
|
|
|
|
if (!fd)
|
|
|
|
throw Lookup_failed();
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2017-06-20 14:23:22 +02:00
|
|
|
size_t _num_entries() const
|
|
|
|
{
|
|
|
|
unsigned num = 0;
|
|
|
|
|
|
|
|
rewinddir(_fd);
|
|
|
|
while (readdir(_fd)) ++num;
|
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2013-11-08 16:44:44 +01:00
|
|
|
public:
|
|
|
|
|
|
|
|
Directory(Allocator &alloc, char const *path, bool create)
|
|
|
|
:
|
|
|
|
Node(_inode(path, create)),
|
|
|
|
_fd(_open(path)),
|
|
|
|
_path(path, "./"),
|
|
|
|
_alloc(alloc)
|
|
|
|
{
|
|
|
|
Node::name(basename(path));
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~Directory()
|
|
|
|
{
|
|
|
|
closedir(_fd);
|
|
|
|
}
|
|
|
|
|
2019-03-20 13:59:56 +01:00
|
|
|
void update_modification_time(Timestamp const time) override
|
|
|
|
{
|
|
|
|
struct timespec ts[2] = {
|
|
|
|
{ .tv_sec = (time_t)0, .tv_nsec = 0 },
|
|
|
|
{ .tv_sec = (time_t)time.value, .tv_nsec = 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
/* silently ignore errors */
|
|
|
|
futimens(dirfd(_fd), (const timespec*)&ts);
|
|
|
|
}
|
|
|
|
|
2019-01-31 15:04:22 +01:00
|
|
|
void rename(Directory &dir_to, char const *name_from, char const *name_to)
|
|
|
|
{
|
|
|
|
int ret = renameat(dirfd(_fd), name_from,
|
|
|
|
dirfd(dir_to._fd), name_to);
|
|
|
|
if (ret != 0)
|
|
|
|
throw Unavailable();
|
|
|
|
}
|
|
|
|
|
2013-11-08 16:44:44 +01:00
|
|
|
/* FIXME returned file node must be locked */
|
2017-06-20 14:23:22 +02:00
|
|
|
File * file(char const *name, Mode mode, bool create) override
|
2013-11-08 16:44:44 +01:00
|
|
|
{
|
|
|
|
File *file = new (&_alloc) File(dirfd(_fd), name, mode, create);
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME returned directory node must be locked */
|
|
|
|
Directory * subdir(char const *path, bool create)
|
|
|
|
{
|
|
|
|
Path dir_path(path, _path.base());
|
|
|
|
|
|
|
|
Directory *dir = new (&_alloc) Directory(_alloc, dir_path.base(), create);
|
|
|
|
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node * node(char const *path)
|
|
|
|
{
|
|
|
|
Path node_path(path, _path.base());
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX Currently, symlinks are transparently dereferenced by the
|
|
|
|
* use of stat(). For symlink detection we would need lstat()
|
|
|
|
* and implement special handling of the root, which may be a
|
|
|
|
* link!
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct stat s;
|
|
|
|
int ret = stat(node_path.base(), &s);
|
|
|
|
if (ret == -1)
|
|
|
|
throw Lookup_failed();
|
|
|
|
|
|
|
|
Node *node = 0;
|
|
|
|
|
|
|
|
if (S_ISDIR(s.st_mode))
|
|
|
|
node = new (&_alloc) Directory(_alloc, node_path.base(), false);
|
|
|
|
else if (S_ISREG(s.st_mode))
|
|
|
|
node = new (&_alloc) File(node_path.base(), STAT_ONLY);
|
|
|
|
else
|
|
|
|
throw Lookup_failed();
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2017-06-20 14:23:22 +02:00
|
|
|
size_t read(char *dst, size_t len, seek_off_t seek_offset) override
|
2013-11-08 16:44:44 +01:00
|
|
|
{
|
|
|
|
if (len < sizeof(Directory_entry)) {
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 19:07:09 +02:00
|
|
|
Genode::error("read buffer too small for directory entry");
|
2013-11-08 16:44:44 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (seek_offset % sizeof(Directory_entry)) {
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 19:07:09 +02:00
|
|
|
Genode::error("seek offset not aligned to sizeof(Directory_entry)");
|
2013-11-08 16:44:44 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
seek_off_t index = seek_offset / sizeof(Directory_entry);
|
|
|
|
|
|
|
|
/* seek to index and read entry */
|
|
|
|
struct dirent *dent;
|
|
|
|
rewinddir(_fd);
|
|
|
|
for (unsigned i = 0; i <= index; ++i) {
|
|
|
|
dent = readdir(_fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dent)
|
|
|
|
return 0;
|
|
|
|
|
2019-09-25 17:13:29 +02:00
|
|
|
Path dent_path(dent->d_name, _path.base());
|
2013-11-08 16:44:44 +01:00
|
|
|
|
2019-09-25 17:13:29 +02:00
|
|
|
struct stat st { };
|
|
|
|
lstat(dent_path.base(), &st);
|
2013-11-08 16:44:44 +01:00
|
|
|
|
2019-09-25 17:13:29 +02:00
|
|
|
auto type = [] (unsigned char type)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case DT_REG: return Node_type::CONTINUOUS_FILE;
|
|
|
|
case DT_DIR: return Node_type::DIRECTORY;
|
|
|
|
case DT_LNK: return Node_type::SYMLINK;
|
|
|
|
default: return Node_type::CONTINUOUS_FILE;
|
|
|
|
}
|
|
|
|
};
|
2019-02-22 19:22:13 +01:00
|
|
|
|
2019-09-25 17:13:29 +02:00
|
|
|
Directory_entry &e = *(Directory_entry *)(dst);
|
|
|
|
e = {
|
|
|
|
.inode = (unsigned long)dent->d_ino,
|
|
|
|
.type = type(dent->d_type),
|
|
|
|
.rwx = { .readable = (st.st_mode & S_IRUSR),
|
|
|
|
.writeable = (st.st_mode & S_IWUSR),
|
|
|
|
.executable = (st.st_mode & S_IXUSR) },
|
|
|
|
.name = { dent->d_name }
|
|
|
|
};
|
2013-11-08 16:44:44 +01:00
|
|
|
|
|
|
|
return sizeof(Directory_entry);
|
|
|
|
}
|
|
|
|
|
Follow practices suggested by "Effective C++"
The patch adjust the code of the base, base-<kernel>, and os repository.
To adapt existing components to fix violations of the best practices
suggested by "Effective C++" as reported by the -Weffc++ compiler
argument. The changes follow the patterns outlined below:
* A class with virtual functions can no longer publicly inherit base
classed without a vtable. The inherited object may either be moved
to a member variable, or inherited privately. The latter would be
used for classes that inherit 'List::Element' or 'Avl_node'. In order
to enable the 'List' and 'Avl_tree' to access the meta data, the
'List' must become a friend.
* Instead of adding a virtual destructor to abstract base classes,
we inherit the new 'Interface' class, which contains a virtual
destructor. This way, single-line abstract base classes can stay
as compact as they are now. The 'Interface' utility resides in
base/include/util/interface.h.
* With the new warnings enabled, all member variables must be explicitly
initialized. Basic types may be initialized with '='. All other types
are initialized with braces '{ ... }' or as class initializers. If
basic types and non-basic types appear in a row, it is nice to only
use the brace syntax (also for basic types) and align the braces.
* If a class contains pointers as members, it must now also provide a
copy constructor and assignment operator. In the most cases, one
would make them private, effectively disallowing the objects to be
copied. Unfortunately, this warning cannot be fixed be inheriting
our existing 'Noncopyable' class (the compiler fails to detect that
the inheriting class cannot be copied and still gives the error).
For now, we have to manually add declarations for both the copy
constructor and assignment operator as private class members. Those
declarations should be prepended with a comment like this:
/*
* Noncopyable
*/
Thread(Thread const &);
Thread &operator = (Thread const &);
In the future, we should revisit these places and try to replace
the pointers with references. In the presence of at least one
reference member, the compiler would no longer implicitly generate
a copy constructor. So we could remove the manual declaration.
Issue #465
2017-12-21 15:42:15 +01:00
|
|
|
size_t write(char const *, size_t, seek_off_t) override
|
2013-11-08 16:44:44 +01:00
|
|
|
{
|
|
|
|
/* writing to directory nodes is not supported */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-20 14:23:22 +02:00
|
|
|
Status status() override
|
2013-11-08 16:44:44 +01:00
|
|
|
{
|
2019-09-25 17:13:29 +02:00
|
|
|
struct stat st { };
|
2019-03-20 13:59:56 +01:00
|
|
|
|
|
|
|
int fd = dirfd(_fd);
|
|
|
|
if (fd == -1 || fstat(fd, &st) < 0)
|
|
|
|
st.st_mtime = 0;
|
|
|
|
|
2019-09-25 17:13:29 +02:00
|
|
|
return {
|
|
|
|
.size = _num_entries() * sizeof(File_system::Directory_entry),
|
|
|
|
.type = Node_type::DIRECTORY,
|
|
|
|
.rwx = { .readable = (st.st_mode & S_IRUSR),
|
|
|
|
.writeable = (st.st_mode & S_IWUSR),
|
|
|
|
.executable = (st.st_mode & S_IXUSR) },
|
|
|
|
.inode = inode(),
|
|
|
|
.modification_time = { st.st_mtime }
|
|
|
|
};
|
2013-11-08 16:44:44 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* _DIRECTORY_H_ */
|