fs_tool: add <new-file> operation

Adds the <new-file> operation to the fs_tool. When configured, the
<new-file path="...">...</new-file> tag will cause creation or overwriting of
the file given through the 'path' attribute. The file will contain the text
content of the tag.

Ref #4032
This commit is contained in:
Martin Stein 2021-03-13 22:07:01 +01:00 committed by Christian Helmuth
parent 100583e262
commit 70797fe879
3 changed files with 73 additions and 2 deletions

View File

@ -11,6 +11,10 @@
[init -> report_rom] &lt;listing>
[init -> report_rom] &lt;dir path="/fs/items">
[init -> report_rom] &lt;file name="1" writeable="yes">first&lt;/file>
[init -> report_rom] &lt;file name="5" writeable="yes">fifth&lt;/file>
[init -> report_rom] &lt;/dir>
[init -> report_rom] &lt;dir path="/fs/new/file/at/subdir">
[init -> report_rom] &lt;file name="content" writeable="yes">new file&lt;/file>
[init -> report_rom] &lt;/dir>
[init -> report_rom] &lt;/listing>
</log>
@ -81,6 +85,26 @@
</config>
</start>
<start name="new-file" caps="500">
<binary name="fs_tool"/>
<config verbose="yes" exit="yes">
<vfs>
<fs writeable="yes"/>
</vfs>
<new-file path="/items/5">fifth</new-file>
<new-file path="/new/file/at/subdir/content">new file</new-file>
</config>
</start>
<start name="sleep" caps="100">
<resource name="RAM" quantum="4M"/>
<binary name="dummy"/>
<config>
<sleep ms="500"/>
<exit/>
</config>
</start>
<start name="remove" caps="500">
<binary name="fs_tool"/>
<config verbose="yes" exit="yes">
@ -99,6 +123,7 @@
<config>
<vfs> <dir name="fs"> <fs/> </dir> </vfs>
<query path="/fs/items" content="yes"/>
<query path="/fs/new/file/at/subdir" content="yes"/>
</config>
</start>

View File

@ -5,9 +5,17 @@ configured VFS. The file operations are given the configuration as follows:
! <vfs>
! ...
! </vfs>
! <new-file path="/path/to/file">Content to write to the file</new-file>
! <remove-file path="/path/to/file" />
! </config>
The 'exit="yes"' attribute instructs the component to exit after completing
the sequence. Otherwise, the component keeps responding to configuration
changes by executing the operations found in the updated configurations.
The <new-file> operation creates the new file or overwrites the existing file
given through the 'path' attribute with the content that is given inside the
tag.
The <remove-file> tag removes the existing file given through the 'path'
attribute.

View File

@ -17,6 +17,7 @@
#include <base/attached_rom_dataspace.h>
#include <base/heap.h>
#include <os/vfs.h>
#include <base/buffered_output.h>
namespace Fs_tool {
using namespace Genode;
@ -60,6 +61,8 @@ struct Fs_tool::Main
void _remove_file(Xml_node);
void _new_file(Xml_node);
void _handle_config()
{
_config.update();
@ -71,9 +74,12 @@ struct Fs_tool::Main
_root_dir_fs.apply_config(config.sub_node("vfs"));
config.for_each_sub_node([&] (Xml_node operation) {
if (operation.has_type("remove-file")) {
if (operation.has_type("remove-file"))
_remove_file(operation);
}
if (operation.has_type("new-file"))
_new_file(operation);
});
if (config.attribute_value("exit", false)) {
@ -115,5 +121,37 @@ void Fs_tool::Main::_remove_file(Xml_node operation)
}
void Fs_tool::Main::_new_file(Xml_node operation)
{
Path const path { operation.attribute_value("path", Path()) };
bool write_error = false;
bool create_error = false;
try {
New_file new_file(_root_dir, path);
auto write = [&] (char const *str)
{
if (new_file.append(str, strlen(str)) != New_file::Append_result::OK)
write_error = true;
};
Buffered_output<128, decltype(write)> output(write);
operation.with_raw_content([&] (char const *start, size_t size) {
print(output, Cstring(start, size)); });
}
catch (New_file::Create_failed) {
create_error = true; }
if (create_error && _verbose)
warning("operation <new-file path=\"", path, "\"> "
"failed because creating the file failed");
if (write_error && _verbose)
warning("operation <new-file path=\"", path, "\"> "
"failed because writing to the file failed");
}
void Component::construct(Genode::Env &env) { static Fs_tool::Main main(env); }