From 94fad13adb5cf5d09c6dc8aedaf079a9ce84ec6f Mon Sep 17 00:00:00 2001 From: Christian Prochaska Date: Tue, 7 Aug 2012 17:32:15 +0200 Subject: [PATCH] libc: add support for 'O_TRUNC' flag in 'open()' Fixes #320. --- libports/src/lib/libc_ffat/plugin.cc | 5 ++++- libports/src/lib/libc_fs/plugin.cc | 6 ++++-- libports/src/test/libc_ffat/main.cc | 7 +++++++ ports/src/lib/libc_noux/plugin.cc | 6 +++++- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/libports/src/lib/libc_ffat/plugin.cc b/libports/src/lib/libc_ffat/plugin.cc index 01e07c430..280f15851 100644 --- a/libports/src/lib/libc_ffat/plugin.cc +++ b/libports/src/lib/libc_ffat/plugin.cc @@ -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: { /* diff --git a/libports/src/lib/libc_fs/plugin.cc b/libports/src/lib/libc_fs/plugin.cc index 98bfea661..bae5e6492 100644 --- a/libports/src/lib/libc_fs/plugin.cc +++ b/libports/src/lib/libc_fs/plugin.cc @@ -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); } diff --git a/libports/src/test/libc_ffat/main.cc b/libports/src/test/libc_ffat/main.cc index f81834bd6..424ddd9cf 100644 --- a/libports/src/test/libc_ffat/main.cc +++ b/libports/src/test/libc_ffat/main.cc @@ -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); } diff --git a/ports/src/lib/libc_noux/plugin.cc b/ports/src/lib/libc_noux/plugin.cc index e3dff63d6..ad6aa16be 100644 --- a/ports/src/lib/libc_noux/plugin.cc +++ b/ports/src/lib/libc_noux/plugin.cc @@ -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; }