2013-12-09 15:30:07 +00:00
|
|
|
/*
|
|
|
|
* \brief Facility for managing the session-local node-handle namespace
|
|
|
|
* \author Norman Feske
|
|
|
|
* \date 2012-04-11
|
|
|
|
*/
|
|
|
|
|
2015-04-23 19:26:54 +00:00
|
|
|
#ifndef _FILE_SYSTEM__NODE_HANDLE_REGISTRY_H_
|
|
|
|
#define _FILE_SYSTEM__NODE_HANDLE_REGISTRY_H_
|
|
|
|
|
|
|
|
#include <file_system/node.h>
|
2013-12-09 15:30:07 +00:00
|
|
|
|
|
|
|
namespace File_system {
|
|
|
|
|
|
|
|
class Node;
|
|
|
|
class Directory;
|
|
|
|
class File;
|
|
|
|
class Symlink;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Type trait for determining the node type for a given handle type
|
|
|
|
*/
|
|
|
|
template<typename T> struct Node_type;
|
|
|
|
template<> struct Node_type<Node_handle> { typedef Node Type; };
|
|
|
|
template<> struct Node_type<Dir_handle> { typedef Directory Type; };
|
|
|
|
template<> struct Node_type<File_handle> { typedef File Type; };
|
|
|
|
template<> struct Node_type<Symlink_handle> { typedef Symlink Type; };
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Type trait for determining the handle type for a given node type
|
|
|
|
*/
|
|
|
|
template<typename T> struct Handle_type;
|
|
|
|
template<> struct Handle_type<Node> { typedef Node_handle Type; };
|
|
|
|
template<> struct Handle_type<Directory> { typedef Dir_handle Type; };
|
|
|
|
template<> struct Handle_type<File> { typedef File_handle Type; };
|
|
|
|
template<> struct Handle_type<Symlink> { typedef Symlink_handle Type; };
|
|
|
|
|
|
|
|
|
|
|
|
class Node_handle_registry
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
|
|
|
/* maximum number of open nodes per session */
|
|
|
|
enum { MAX_NODE_HANDLES = 128U };
|
|
|
|
|
2016-01-07 09:52:10 +00:00
|
|
|
Genode::Lock mutable _lock;
|
2013-12-09 15:30:07 +00:00
|
|
|
|
2015-04-23 19:26:54 +00:00
|
|
|
Node_base *_nodes[MAX_NODE_HANDLES];
|
2013-12-09 15:30:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Each open node handle can act as a listener to be informed about
|
|
|
|
* node changes.
|
|
|
|
*/
|
|
|
|
Listener _listeners[MAX_NODE_HANDLES];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocate node handle
|
|
|
|
*
|
2016-03-17 14:09:07 +00:00
|
|
|
* \throw Out_of_metadata
|
2013-12-09 15:30:07 +00:00
|
|
|
*/
|
2015-04-23 19:26:54 +00:00
|
|
|
int _alloc(Node_base *node)
|
2013-12-09 15:30:07 +00:00
|
|
|
{
|
2016-01-07 09:52:10 +00:00
|
|
|
Genode::Lock::Guard guard(_lock);
|
2013-12-09 15:30:07 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < MAX_NODE_HANDLES; i++)
|
|
|
|
if (!_nodes[i]) {
|
|
|
|
_nodes[i] = node;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2016-03-17 14:09:07 +00:00
|
|
|
throw Out_of_metadata();
|
2013-12-09 15:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool _in_range(int handle) const
|
|
|
|
{
|
|
|
|
return ((handle >= 0) && (handle < MAX_NODE_HANDLES));
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
Node_handle_registry()
|
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < MAX_NODE_HANDLES; i++)
|
|
|
|
_nodes[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename NODE_TYPE>
|
|
|
|
typename Handle_type<NODE_TYPE>::Type alloc(NODE_TYPE *node)
|
|
|
|
{
|
|
|
|
typedef typename Handle_type<NODE_TYPE>::Type Handle;
|
|
|
|
return Handle(_alloc(node));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Release node handle
|
|
|
|
*/
|
|
|
|
void free(Node_handle handle)
|
|
|
|
{
|
2016-01-07 09:52:10 +00:00
|
|
|
Genode::Lock::Guard guard(_lock);
|
2013-12-09 15:30:07 +00:00
|
|
|
|
|
|
|
if (!_in_range(handle.value))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Notify listeners about the changed file.
|
|
|
|
*/
|
2015-04-23 19:26:54 +00:00
|
|
|
Node_base *node = dynamic_cast<Node_base *>(_nodes[handle.value]);
|
2013-12-09 15:30:07 +00:00
|
|
|
if (!node) { return; }
|
|
|
|
|
|
|
|
node->lock();
|
|
|
|
node->notify_listeners();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* De-allocate handle
|
|
|
|
*/
|
|
|
|
Listener &listener = _listeners[handle.value];
|
|
|
|
|
|
|
|
if (listener.valid())
|
|
|
|
node->remove_listener(&listener);
|
|
|
|
|
|
|
|
_nodes[handle.value] = 0;
|
|
|
|
listener = Listener();
|
|
|
|
|
|
|
|
node->unlock();
|
|
|
|
}
|
|
|
|
|
2015-04-23 19:26:54 +00:00
|
|
|
/**
|
|
|
|
* Lookup node using its handle as key
|
|
|
|
*
|
|
|
|
* \throw Invalid_handle
|
|
|
|
*/
|
|
|
|
template <typename HANDLE_TYPE>
|
|
|
|
typename Node_type<HANDLE_TYPE>::Type *lookup(HANDLE_TYPE handle)
|
|
|
|
{
|
2016-01-07 09:52:10 +00:00
|
|
|
Genode::Lock::Guard guard(_lock);
|
2015-04-23 19:26:54 +00:00
|
|
|
|
|
|
|
if (!_in_range(handle.value))
|
|
|
|
throw Invalid_handle();
|
|
|
|
|
|
|
|
typedef typename Node_type<HANDLE_TYPE>::Type Node;
|
|
|
|
Node *node = dynamic_cast<Node *>(_nodes[handle.value]);
|
|
|
|
if (!node)
|
|
|
|
throw Invalid_handle();
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2013-12-09 15:30:07 +00:00
|
|
|
/**
|
|
|
|
* Lookup node using its handle as key
|
|
|
|
*
|
|
|
|
* The node returned by this function is in a locked state.
|
|
|
|
*
|
|
|
|
* \throw Invalid_handle
|
|
|
|
*/
|
|
|
|
template <typename HANDLE_TYPE>
|
|
|
|
typename Node_type<HANDLE_TYPE>::Type *lookup_and_lock(HANDLE_TYPE handle)
|
|
|
|
{
|
2016-01-07 09:52:10 +00:00
|
|
|
Genode::Lock::Guard guard(_lock);
|
2013-12-09 15:30:07 +00:00
|
|
|
|
|
|
|
if (!_in_range(handle.value))
|
|
|
|
throw Invalid_handle();
|
|
|
|
|
2015-04-23 19:26:54 +00:00
|
|
|
typedef typename Node_type<HANDLE_TYPE>::Type Node_base;
|
|
|
|
Node_base *node = dynamic_cast<Node_base *>(_nodes[handle.value]);
|
2013-12-09 15:30:07 +00:00
|
|
|
if (!node)
|
|
|
|
throw Invalid_handle();
|
|
|
|
|
|
|
|
node->lock();
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool refer_to_same_node(Node_handle h1, Node_handle h2) const
|
|
|
|
{
|
2016-01-07 09:52:10 +00:00
|
|
|
Genode::Lock::Guard guard(_lock);
|
2013-12-09 15:30:07 +00:00
|
|
|
|
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 17:07:09 +00:00
|
|
|
if (!_in_range(h1.value) || !_in_range(h2.value))
|
2013-12-09 15:30:07 +00:00
|
|
|
throw Invalid_handle();
|
|
|
|
|
|
|
|
return _nodes[h1.value] == _nodes[h2.value];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register signal handler to be notified of node changes
|
|
|
|
*/
|
2016-01-07 09:52:10 +00:00
|
|
|
void sigh(Node_handle handle, Genode::Signal_context_capability sigh)
|
2013-12-09 15:30:07 +00:00
|
|
|
{
|
2016-01-07 09:52:10 +00:00
|
|
|
Genode::Lock::Guard guard(_lock);
|
2013-12-09 15:30:07 +00:00
|
|
|
|
|
|
|
if (!_in_range(handle.value))
|
|
|
|
throw Invalid_handle();
|
|
|
|
|
2015-04-23 19:26:54 +00:00
|
|
|
Node_base *node = dynamic_cast<Node_base *>(_nodes[handle.value]);
|
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 17:07:09 +00:00
|
|
|
if (!node)
|
2013-12-09 15:30:07 +00:00
|
|
|
throw Invalid_handle();
|
|
|
|
|
|
|
|
node->lock();
|
2015-04-23 19:26:54 +00:00
|
|
|
Node_lock_guard node_lock_guard(node);
|
2013-12-09 15:30:07 +00:00
|
|
|
|
|
|
|
Listener &listener = _listeners[handle.value];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If there was already a handler registered for the node,
|
|
|
|
* remove the old handler.
|
|
|
|
*/
|
|
|
|
if (listener.valid())
|
|
|
|
node->remove_listener(&listener);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Register new handler
|
|
|
|
*/
|
|
|
|
listener = Listener(sigh);
|
|
|
|
node->add_listener(&listener);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-04-23 19:26:54 +00:00
|
|
|
#endif /* _FILE_SYSTEM__NODE_HANDLE_REGISTRY_H_ */
|