File-system interface, ram_fs, libc-fs
This patch introduces the file-system-session interface, provides an
implementation of this interface in the form of an in-memory file
system, and enables the libc to use the new file-system facility.
The new interface resides in 'os/include/file_system_session/'. It
uses synchronous RPC calls for functions referring to directory
and meta-data handling. For transferring payload from/to files, the
packet-stream interface is used. I envision that the asynchronous design
of the packet-stream interface fits well will the block-session
interface. Compared to Unix-like file-system APIs, Genode's file-system
session interface is much simpler. In particular, it does not support
per-file permissions. On Genode, we facilitate binding policy (such as
write-permission) is sessions rather than individual file objects.
As a reference implementation of the new interface, there is the
new 'ram_fs' service at 'os/src/server/ram_fs'. It stores sparse
files in memory. At the startup, 'ram_fs' is able to populate the
file-system content with directories and ROM modules as specified
in its configuration.
To enable libc-using programs to access the new file-system interface,
there is the new libc plugin at 'libports/src/lib/libc-fs'. Using this
plugin, files stored on a native Genode file system can be accessed
using the traditional POSIX file API.
To see how the three parts described above fit together, the test
case at 'libports/run/libc_fs' can be taken as reference. It reuses
the original 'libc_ffat' test to exercise several file operations
on a RAM file-system using the libc API.
:Known limitations:
The current state should be regarded as work in progress. In particular
the error handling is not complete yet. Not all of the session functions
return the proper exceptions in the event of an error. I plan to
successively refine the interface while advancing the file-system
implementations. Also the support for truncating files and symlink
handling are not yet implemented.
Furthermore, there is much room for optimization, in particular for the
handling of directory entries. Currently, we communicate only one dir
entry at a time, which is bad when traversing large trees. However, I
decided to focus on functionality first and defer optimizations (such as
batching dir entries) to a later stage.
The current implementation does not handle file modification times at
all, which may be a severe limitation for tools that depend on this
information such as GNU make. Support for time will be added after we
have revisited Genode's timer-session interface (issue #1).
Fixes #54
Fixes #171
2012-04-11 13:46:33 +00:00
|
|
|
/*
|
|
|
|
* \brief File node
|
|
|
|
* \author Norman Feske
|
|
|
|
* \date 2012-04-11
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _FILE_H_
|
|
|
|
#define _FILE_H_
|
|
|
|
|
|
|
|
/* Genode includes */
|
|
|
|
#include <base/allocator.h>
|
|
|
|
|
|
|
|
/* local includes */
|
|
|
|
#include <node.h>
|
|
|
|
#include <chunk.h>
|
|
|
|
|
|
|
|
namespace File_system {
|
|
|
|
|
|
|
|
class File : public Node
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
|
|
|
typedef Chunk<4096> Chunk_level_3;
|
|
|
|
typedef Chunk_index<128, Chunk_level_3> Chunk_level_2;
|
|
|
|
typedef Chunk_index<64, Chunk_level_2> Chunk_level_1;
|
|
|
|
typedef Chunk_index<64, Chunk_level_1> Chunk_level_0;
|
|
|
|
|
|
|
|
Chunk_level_0 _chunk;
|
|
|
|
|
|
|
|
file_size_t _length;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
File(Allocator &alloc, char const *name)
|
|
|
|
: _chunk(alloc, 0), _length(0) { Node::name(name); }
|
|
|
|
|
|
|
|
size_t read(char *dst, size_t len, seek_off_t seek_offset)
|
|
|
|
{
|
|
|
|
file_size_t const chunk_used_size = _chunk.used_size();
|
|
|
|
|
|
|
|
if (seek_offset >= _length)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Constrain read transaction to available chunk data
|
|
|
|
*
|
|
|
|
* Note that 'chunk_used_size' may be lower than '_length'
|
|
|
|
* because 'Chunk' may have truncated tailing zeros.
|
|
|
|
*/
|
|
|
|
if (seek_offset + len >= _length)
|
|
|
|
len = _length - seek_offset;
|
|
|
|
|
|
|
|
file_size_t read_len = len;
|
|
|
|
|
|
|
|
if (seek_offset + read_len > chunk_used_size) {
|
|
|
|
if (chunk_used_size >= seek_offset)
|
|
|
|
read_len = chunk_used_size - seek_offset;
|
|
|
|
else
|
|
|
|
read_len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
_chunk.read(dst, read_len, seek_offset);
|
|
|
|
|
|
|
|
/* add zero padding if needed */
|
|
|
|
if (read_len < len)
|
|
|
|
memset(dst + read_len, 0, len - read_len);
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t write(char const *src, size_t len, seek_off_t seek_offset)
|
|
|
|
{
|
|
|
|
if (seek_offset == (seek_off_t)(~0))
|
|
|
|
seek_offset = _chunk.used_size();
|
|
|
|
|
|
|
|
if (seek_offset + len >= Chunk_level_0::SIZE)
|
|
|
|
throw Size_limit_reached();
|
|
|
|
|
|
|
|
_chunk.write(src, len, (size_t)seek_offset);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Keep track of file length. We cannot use 'chunk.used_size()'
|
|
|
|
* as file length because trailing zeros may by represented
|
|
|
|
* by zero chunks, which do not contribute to 'used_size()'.
|
|
|
|
*/
|
|
|
|
_length = max(_length, seek_offset + len);
|
2013-01-12 20:17:20 +00:00
|
|
|
|
|
|
|
mark_as_updated();
|
File-system interface, ram_fs, libc-fs
This patch introduces the file-system-session interface, provides an
implementation of this interface in the form of an in-memory file
system, and enables the libc to use the new file-system facility.
The new interface resides in 'os/include/file_system_session/'. It
uses synchronous RPC calls for functions referring to directory
and meta-data handling. For transferring payload from/to files, the
packet-stream interface is used. I envision that the asynchronous design
of the packet-stream interface fits well will the block-session
interface. Compared to Unix-like file-system APIs, Genode's file-system
session interface is much simpler. In particular, it does not support
per-file permissions. On Genode, we facilitate binding policy (such as
write-permission) is sessions rather than individual file objects.
As a reference implementation of the new interface, there is the
new 'ram_fs' service at 'os/src/server/ram_fs'. It stores sparse
files in memory. At the startup, 'ram_fs' is able to populate the
file-system content with directories and ROM modules as specified
in its configuration.
To enable libc-using programs to access the new file-system interface,
there is the new libc plugin at 'libports/src/lib/libc-fs'. Using this
plugin, files stored on a native Genode file system can be accessed
using the traditional POSIX file API.
To see how the three parts described above fit together, the test
case at 'libports/run/libc_fs' can be taken as reference. It reuses
the original 'libc_ffat' test to exercise several file operations
on a RAM file-system using the libc API.
:Known limitations:
The current state should be regarded as work in progress. In particular
the error handling is not complete yet. Not all of the session functions
return the proper exceptions in the event of an error. I plan to
successively refine the interface while advancing the file-system
implementations. Also the support for truncating files and symlink
handling are not yet implemented.
Furthermore, there is much room for optimization, in particular for the
handling of directory entries. Currently, we communicate only one dir
entry at a time, which is bad when traversing large trees. However, I
decided to focus on functionality first and defer optimizations (such as
batching dir entries) to a later stage.
The current implementation does not handle file modification times at
all, which may be a severe limitation for tools that depend on this
information such as GNU make. Support for time will be added after we
have revisited Genode's timer-session interface (issue #1).
Fixes #54
Fixes #171
2012-04-11 13:46:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
file_size_t length() const { return _length; }
|
2012-08-02 13:17:24 +00:00
|
|
|
|
|
|
|
void truncate(file_size_t size)
|
|
|
|
{
|
|
|
|
if (size < _chunk.used_size())
|
|
|
|
_chunk.truncate(size);
|
2012-10-02 16:47:20 +00:00
|
|
|
|
|
|
|
_length = size;
|
2013-01-12 20:17:20 +00:00
|
|
|
|
|
|
|
mark_as_updated();
|
2012-08-02 13:17:24 +00:00
|
|
|
}
|
File-system interface, ram_fs, libc-fs
This patch introduces the file-system-session interface, provides an
implementation of this interface in the form of an in-memory file
system, and enables the libc to use the new file-system facility.
The new interface resides in 'os/include/file_system_session/'. It
uses synchronous RPC calls for functions referring to directory
and meta-data handling. For transferring payload from/to files, the
packet-stream interface is used. I envision that the asynchronous design
of the packet-stream interface fits well will the block-session
interface. Compared to Unix-like file-system APIs, Genode's file-system
session interface is much simpler. In particular, it does not support
per-file permissions. On Genode, we facilitate binding policy (such as
write-permission) is sessions rather than individual file objects.
As a reference implementation of the new interface, there is the
new 'ram_fs' service at 'os/src/server/ram_fs'. It stores sparse
files in memory. At the startup, 'ram_fs' is able to populate the
file-system content with directories and ROM modules as specified
in its configuration.
To enable libc-using programs to access the new file-system interface,
there is the new libc plugin at 'libports/src/lib/libc-fs'. Using this
plugin, files stored on a native Genode file system can be accessed
using the traditional POSIX file API.
To see how the three parts described above fit together, the test
case at 'libports/run/libc_fs' can be taken as reference. It reuses
the original 'libc_ffat' test to exercise several file operations
on a RAM file-system using the libc API.
:Known limitations:
The current state should be regarded as work in progress. In particular
the error handling is not complete yet. Not all of the session functions
return the proper exceptions in the event of an error. I plan to
successively refine the interface while advancing the file-system
implementations. Also the support for truncating files and symlink
handling are not yet implemented.
Furthermore, there is much room for optimization, in particular for the
handling of directory entries. Currently, we communicate only one dir
entry at a time, which is bad when traversing large trees. However, I
decided to focus on functionality first and defer optimizations (such as
batching dir entries) to a later stage.
The current implementation does not handle file modification times at
all, which may be a severe limitation for tools that depend on this
information such as GNU make. Support for time will be added after we
have revisited Genode's timer-session interface (issue #1).
Fixes #54
Fixes #171
2012-04-11 13:46:33 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* _FILE_H_ */
|