diff options
author | Colin Walters <walters@verbum.org> | 2017-07-10 12:40:33 -0400 |
---|---|---|
committer | Colin Walters <walters@verbum.org> | 2017-07-17 12:06:26 -0400 |
commit | e30a773f2ca45ed7c0654a7c0b2a6449d3de9af0 (patch) | |
tree | f196af13a4f3c2823de68afb69fe3ad00c3dcb2c /tests | |
parent | 210bcfcb65eb50ffef9cb541424dbd3b9b84997e (diff) | |
download | libglnx-e30a773f2ca45ed7c0654a7c0b2a6449d3de9af0.tar.gz |
fdio: Add cleanup+flush API for FILE*
Mostly in ostree/rpm-ostree, we work in either raw `int fd`, or
`G{Input,Output}Stream`. One exception is the rpm-ostree `/etc/passwd`
handling, which uses `FILE*` since that's what glibc exposes.
And in general, there are use cases for `FILE*`; the raw `GUnixOutputStream` for
example isn't buffered, and doing so via e.g. `GBufferedOutputStream` means
allocating *two* GObjects and even worse going through multiple vfuncs for every
write.
`FILE*` is used heavily in systemd, and provides buffering. It is a bit cheaper
than gobjects, but has its own trap; by default every operation locks a mutex.
For more information on that, see `unlocked_stdio(3)`. However, callers can
avoid that by using e.g. `fwrite_unlocked`, which I plan to do for most users of
`FILE*` that aren't writing to one of the standard streams like `stdout` etc.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test-libglnx-fdio.c | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/tests/test-libglnx-fdio.c b/tests/test-libglnx-fdio.c index cee7cb6..d4e6272 100644 --- a/tests/test-libglnx-fdio.c +++ b/tests/test-libglnx-fdio.c @@ -160,6 +160,28 @@ test_tmpfile (void) g_assert_no_error (local_error); } +static void +test_stdio_file (void) +{ + g_autoptr(GError) local_error = NULL; + GError **error = &local_error; + g_auto(GLnxTmpfile) tmpf = { 0, }; + if (!glnx_open_anonymous_tmpfile (O_RDWR|O_CLOEXEC, &tmpf, error)) + goto out; + + g_autoptr(FILE) f = fdopen (tmpf.fd, "w"); + if (fwrite ("hello", 1, strlen ("hello"), f) != strlen ("hello")) + { + (void)glnx_throw_errno_prefix (error, "fwrite"); + goto out; + } + if (!glnx_stdio_file_flush (f, error)) + goto out; + + out: + g_assert_no_error (local_error); +} + int main (int argc, char **argv) { int ret; @@ -167,6 +189,7 @@ int main (int argc, char **argv) g_test_init (&argc, &argv, NULL); g_test_add_func ("/tmpfile", test_tmpfile); + g_test_add_func ("/stdio-file", test_stdio_file); g_test_add_func ("/renameat2-noreplace", test_renameat2_noreplace); g_test_add_func ("/renameat2-exchange", test_renameat2_exchange); |