libc: add support for 'O_TRUNC' flag in 'open()'

Fixes #320.
This commit is contained in:
Christian Prochaska 2012-08-07 17:32:15 +02:00 committed by Norman Feske
parent 266d5e0be3
commit 94fad13adb
4 changed files with 20 additions and 4 deletions

View File

@ -505,7 +505,10 @@ class Plugin : public Libc::Plugin
case FR_OK: {
Plugin_context *context = new (Genode::env()->heap())
File_plugin_context(pathname, ffat_file);
return Libc::file_descriptor_allocator()->alloc(this, context);
Libc::File_descriptor *fd = Libc::file_descriptor_allocator()->alloc(this, context);
if ((flags & O_TRUNC) && (ftruncate(fd, 0) == -1))
return 0;
return fd;
}
case FR_NO_FILE: {
/*

View File

@ -550,8 +550,10 @@ class Plugin : public Libc::Plugin
Plugin_context *context = new (Genode::env()->heap())
Plugin_context(handle);
return Libc::file_descriptor_allocator()->alloc(this, context);
Libc::File_descriptor *fd = Libc::file_descriptor_allocator()->alloc(this, context);
if ((flags & O_TRUNC) && (ftruncate(fd, 0) == -1))
return 0;
return fd;
}
catch (File_system::Lookup_failed) {
PERR("open(%s) lookup failed", pathname); }

View File

@ -180,6 +180,13 @@ int main(int argc, char *argv[])
(ret == 0) && (stat_buf.st_size == 10),
"file_name=%s", file_name4);
/* test 'O_TRUNC' flag */
CALL_AND_CHECK(fd, open(file_name4, O_WRONLY | O_TRUNC), fd >= 0, "file_name=%s", file_name4);
CALL_AND_CHECK(ret, close(fd), ret == 0, "");
CALL_AND_CHECK(ret, stat(file_name4, &stat_buf),
(ret == 0) && (stat_buf.st_size == 0),
"file_name=%s", file_name4);
if (i < (iterations - 1))
sleep(2);
}

View File

@ -642,7 +642,11 @@ namespace {
}
Libc::Plugin_context *context = noux_context(sysio()->open_out.fd);
return Libc::file_descriptor_allocator()->alloc(this, context, sysio()->open_out.fd);
Libc::File_descriptor *fd =
Libc::file_descriptor_allocator()->alloc(this, context, sysio()->open_out.fd);
if ((flags & O_TRUNC) && (ftruncate(fd, 0) == -1))
return 0;
return fd;
}