Commit Graph

31 Commits

Author SHA1 Message Date
Colin Walters
771d7a01d1 tree-wide: Use glnx_autofd and glnx_close_fd()
Port to `glnx_autofd` tree wide, and add one missed `glnx_close_fd()` use in the
tmpfile code.
2017-10-16 18:06:53 -04:00
Jonathan Lebon
b923a950af tests: drop unused variable 2017-10-11 20:03:12 +00:00
Jonathan Lebon
5362f6bc3f fdio: allow NULL for fstatat_allow_noent stbuf
Often, the caller doesn't actually care about the details of the stat
struct itself, but just whether the entry exists or not. It does work
to just pass `NULL` directly to glibc in a quick test, but given that
the argument is tagged as `__nonnull` and that the documentation does
not explicitly specify this is supported, let's do this safely.
2017-10-06 21:45:55 +00:00
Philip Withnall
e30154431d shutil: Fix assertion failure in glnx_shutil_mkdir_p_at()
If the directory for @dfd is deleted after being opened,
glnx_shutil_mkdir_p_at() would fail with an assertion failure. Fix that,
and make it return an ENOENT error instead.

Add a unit test.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
Reviewed-by: Colin Walters <walters@verbum.org>
Reviewed-by: Jonathan Lebon <jlebon@redhat.com>

https://github.com/ostreedev/ostree/issues/1215
2017-09-26 15:08:04 +01:00
Colin Walters
5ee2f1be7a fdio: Open target dirname for glnx_file_copy_at()
Particularly if `AT_FDCWD` is used, we need to open
in the target dir, otherwise we can get `EXDEV` when trying
to do the final link.

(Theoretically we can cross a mountpoint even with fd-relative
 though this is a lot less likely)
2017-09-25 11:53:40 -04:00
Colin Walters
c2bcca04ba tests: Add macro for auto-error checking
Having our tests forced into a `goto out` style is seriously annoying
since we can't write tests like we write production code.  Add
a macro that checks for the error being NULL.

This doesn't fully solve the problem since the test functions are
still forced into `void` returns; at some point I may extend
GLib to have `g_test_add_err_func()`.
2017-09-13 10:57:30 -04:00
Colin Walters
673f48f6ca fdio: Use O_TMPFILE + rename-overwrite for regfile copies
I was working on rpm-ostree unified core, and hit the fact that
`glnx_file_copy_at()` had the same bug with `fsetxattr()` and files whose mode
is <= `0400` (e.g. `000` in the case of `/etc/shadow`) that libostree did a
while ago.  Basically, Linux currently allows `write()` on non-writable open files
but not `fsetxattr()`.  This situation is masked for privileged (i.e.
`CAP_DAC_OVERRIDE`) code.

Looking at this, I think it's cleaner to convert to `O_TMPFILE` here,
since that code already handles setting the tmpfile to mode `0600`.  Now,
this *is* a behavior change in the corner case of existing files which
are symbolic links.  Previously we'd do an `open(O_TRUNC)` which would follow
the link.

But in the big picture, I think the use cases for `open(O_TRUNC)` are really
rare - I audited all callers of this in ostree/rpm-ostree/flatpak, and all of
them will be fine with this behavior change. For example, the ostree `/etc`
merge code already explicitly unlinks the target beforehand. Other cases like
supporting `repo/pubring.gpg` in an ostree repo being a symlink...eh, just no.

Making this change allows us to convert to new style, and brings all of the
general benefits of using `O_TMPFILE` too.
2017-09-12 11:05:59 -04:00
Colin Walters
9d995a3620 fdio: Support taking ownership of tmpfile fd
While reading a strace I noticed a double close in the tests; this was because
we were missing an assignment to `-1` in the tests. However, let's make
supporting this clearer by explicitly supporting the fd being `-1` while still
setting the `initialized` variable to `FALSE`. We also add the `EBADF` assertion
checking.
2017-09-12 09:43:05 -04:00
Colin Walters
627d4e2f15 fdio: Add glnx_fstatat_allow_noent()
This is a very common pattern in both ostree/rpm-ostree. Make a better API for
this. I thought a lot about simply zeroing out `struct stat` but that feels
dangerous; none of the values have seem obviously `cannot be zero`.
2017-09-07 16:05:26 -04:00
Jonathan Lebon
47d8163293 test-libglnx-xattrs.c: appease -Wunused-variable 2017-08-25 11:02:37 -04:00
Colin Walters
7100ebbc68 dirfd: New tmpdir API
Basically all of the ostree/rpm-ostree callers want to both create and open, so
let's merge `glnx_mkdtempat()` and `glnx_mkdtempat_open()`.

Second, all of them want to do `glnx_shutil_rm_rf_at()` on cleanup, so we do the
same thing we did with `GLnxTmpfile` and create `GLnxTmpDir` that has a cleanup
attribute.

The cleanup this results in for rpm-ostree is pretty substantial.
2017-08-18 16:01:38 -04:00
Colin Walters
ea6df95f22 tests: Fix a -Wmaybe-uninitialized warning
It'd be really nice if gtest had a variant which had the funcs take `GError`.
May work on that.
2017-07-24 12:01:25 -04:00
Colin Walters
268ae48816 fdio: Introduce glnx_openat_read()
This is kind of long overdue. Reasons are the same as the other wrappers. I
debated adding `O_NOFOLLOW` support but the use cases for that are pretty
obscure, callers who want that can just use the syscall directly for now.
2017-07-19 11:35:09 -04:00
Colin Walters
607f1775bb errors: Add GLNX_AUTO_PREFIX_ERROR
In a lot of places in ostree, we end up prefixing errors in the *caller*.
Often we only have 1-2 callers, and doing the error prefixing isn't
too duplicative.  But there are definitely cases where it's cleaner
to do the prefixing in the callee.  We have functions that aren't
ported to new style for this reason (they still do the prefixing
in `out:`).

Introduce a cleanup-oriented version of error prefixing so we can port those
functions too.
2017-07-17 12:23:31 -04:00
Colin Walters
e30a773f2c 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.
2017-07-17 12:06:26 -04:00
Colin Walters
452c371ff3 fdio: Ensure O_TMPFILE is mode 0600
Work around an older glibc bug.
2017-07-10 12:12:29 -04:00
Colin Walters
01e934c18e tests: Fix compilation of fdio test
Not sure how I missed this before.
2017-06-28 11:23:02 -04:00
Jonathan Lebon
5ab15ac175 macros: add GLNX_HASH_TABLE_FOREACH_V
Looking at converting the ostree codebase, iterating over only the
values of a hash table (while ignoring the key) is actually a more
common pattern than I thought. So let's give it its own macro as well so
users don't have to resort to the _KV variant.
2017-06-28 08:04:11 -07:00
Colin Walters
4d34066a2f fdio: Add wrappers for renameat(), unlinkat()
Besides doing `TEMP_FAILURE_RETRY` and `GError` conversion,
these also prefix the error with arguments.
2017-06-26 13:37:05 -04:00
Jonathan Lebon
caa51ac24f glnx-macros.h: add GLNX_HASH_TABLE_FOREACH macros
These macros make it much easier to iterate over a GHashTable. It takes
care of initializing an iterator and casting keys and values to their
proper types.

See the example usage in the docstring for more info.
2017-06-17 16:26:05 -04:00
Jonathan Lebon
e8b7d8f60c test-libglnx-macros.c: fix missing semicolon 2017-06-17 14:52:41 -04:00
Colin Walters
9a1b77ef96 Add G_IN_SET, patch our internal users via spatch
I originally tried to get this into GLib:
https://bugzilla.gnome.org/show_bug.cgi?id=783751

But that looks like it's going to fail due to MSVC. Let's add it here at least
so I can start using it tomorrow and not wait for the MSVC team to catch up.

I renamed `glnx-alloca.h` to `glnx-macros.h` as a more natural collective
home for things from systemd's `macro.h`.

Finally, I used a Coccinelle spatch similar to the one referenced
in the above BZ to patch our uses.
2017-06-14 12:48:20 -04:00
Jonathan Lebon
32231fdb52 glnx-errors.h: add a glnx_throw_prefix() variant
For completeness. It just looks much cleaner than doing the `, FALSE`
trick. It also takes care of appending the ': ' for you like its errno
version.
2017-05-11 12:34:02 -04:00
Colin Walters
74383ba405 tests/xattrs: Skip on filesystems with no user xattr support
Like tmpfs.

See: https://github.com/flatpak/flatpak/issues/686
2017-04-21 10:17:02 -04:00
Colin Walters
4040f55ac5 errors: Fix legacy set_prefix_error_from_errno()
We were missing the previous automatic `: ` addition; noticed in
a failing ostree test.

Fix this by just calling the new API as the non-prefix case does too.
2017-03-24 15:26:57 -04:00
Jonathan Lebon
0c52d85e69 glnx-errors.h: add glnx_null_throw[_*] variants
These are equivalent to the non-null throw, except that the returned
value is a NULL pointer. They can be used in functions where one wants
to return a pointer. E.g.:

	GKeyFile *foo(GError **error) {
		return glnx_null_throw (error, "foobar");
	}

The function call redirections are wrapped around a compound statement
expression[1] so that they represent a single top-level expression. This
allows us to avoid -Wunused-value warnings vs using a comma operator if
the return value isn't used.

I made the 'args...' absorb the fmt argument as well so that callers can
still use it without always having to specify at least one additional
variadic argument. I had to check to be sure that the expansion is all
done by the preprocessor, so we don't need to worry about stack
intricacies.

[1] https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
2017-03-23 10:57:48 -04:00
Colin Walters
602fdd93cb errors: Add glnx_throw() and tests
Following up to the previous commit, also shorten our use of
`g_set_error (..., G_IO_ERROR_FAILED, ...)`. There's a lot of
this in libostree at least.

See also https://bugzilla.gnome.org/show_bug.cgi?id=774061
2017-03-22 11:08:13 -04:00
Colin Walters
c83ec7f213 fdio: Expose wrappers for renameat2() EXCHANGE and NOREPLACE
I want the `RENAME_EXCHANGE` version for rpm-ostree, to atomically
swap `/usr/share/rpm` (a directory) with a new verison.  While
we're here we might as well expose `RENAME_NOREPLACE` in case
something else wants it.

These both have fallbacks to the non-atomic version.

Closes: https://github.com/GNOME/libglnx/pull/36
2017-03-02 15:43:42 -05:00
Jonathan Lebon
5309e363aa fix bug found by -Wmaybe-uninitialized 2017-03-02 13:57:03 -05:00
Colin Walters
0c1603deba tests/xattrs: Fix possible NULL allocation
This showed up in the ostree runs with `-fsanitize=undefined` - if we happened
to get `0` then `g_malloc` would return `NULL`. However, what's interesting is
it seemed to happen *consistently*. I think what's going on is GCC proved that
the value *could* be zero, and hence it *could* return NULL, and hence it was
undefined behavior. Hooray for `-fsanitize=undefined`.
2017-02-21 09:32:30 -05:00
Colin Walters
7a703638d1 xattrs: Add a test case for previous commits
This is actually the first test case in libglnx 🙌; hopefully the
consumers are prepared for us injecting into `TESTS`.
2017-01-29 03:23:43 -05:00