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 /glnx-fdio.h | |
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 'glnx-fdio.h')
-rw-r--r-- | glnx-fdio.h | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/glnx-fdio.h b/glnx-fdio.h index f459e93..150b22e 100644 --- a/glnx-fdio.h +++ b/glnx-fdio.h @@ -50,6 +50,23 @@ const char *glnx_basename (const char *path) return (basename) (path); } +/* Utilities for standard FILE* */ +static inline void +glnx_stdio_file_cleanup (void *filep) +{ + FILE *f = filep; + if (f) + fclose (f); +} +G_DEFINE_AUTOPTR_CLEANUP_FUNC(FILE, glnx_stdio_file_cleanup) + +/** + * glnx_stdio_file_flush: + * Call fflush() and check ferror(). + */ +gboolean +glnx_stdio_file_flush (FILE *f, GError **error); + typedef struct { gboolean initialized; gboolean anonymous; |