Commit Graph

146 Commits

Author SHA1 Message Date
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
Jonathan Lebon
c820571bc4 errors: check for an error before prefixing
Minor tweak to the new `GLNX_AUTO_PREFIX_ERROR`. Since the common case
is that there's no errors, let's bring down the same check that
`g_prefix_error` does to avoid a function call most of the time.
2017-07-21 14:09:24 -07:00
Colin Walters
7d6a31fb93 errors: Mark GLNX_AUTO_PREFIX_ERROR() as used
Since it's intentional we never use it, and `clang` barfs on this (rightly).
2017-07-20 15:12:08 -04:00
Colin Walters
1468b70dbf dirfd: Add missing includes for errno
Thought the previous patch would have been obvious enough not
to compile test but...
2017-07-20 15:12:08 -04:00
Colin Walters
1c0bfd24b1 dirfd: Add glnx_ensure_dir()
Another one where we have a lot of inlines in ostree at least. Not the same as
`glnx_shutil_mkdir_p_at()` since in these cases we don't want automatic
intermediate dirs, and it's cheaper to just call `mkdirat()` and handle `EEXIST`
rather than do a `stat()` first.
2017-07-20 09:59:08 -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
23f7df1500 dirfd: Add filename to glnx_opendirat()
This showed up in https://github.com/projectatomic/rpm-ostree/issues/883

We'll have to audit callers to be sure to avoid double-prefixing.
2017-07-19 09:30:13 -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
61ef326ad8 fdio: Add string prefix for glnx_fstat()
For consistency.
2017-07-17 12:12:14 -04:00
Colin Walters
547bcea280 fdio: Add a fchmod wrapper
There are a number of versions of this in ostree at least, might as well wrap
it.
2017-07-17 12:12:14 -04:00
Colin Walters
8b75c8e341 Remove glnx_stream_fstat()
There are only two users of this in ostree, and one of them is
fairly bogus; we can just use `fstat()`.
2017-07-17 12:12:14 -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
Matthew Leeds
210bcfcb65 README.md: Change xdg-app to flatpak 2017-07-13 15:43:48 -07: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
a37e672739 macros: Add a size check for hashtable iters
If the user provides a less than pointer-sized type, we'll clobber other things
on the stack.

See https://github.com/ostreedev/ostree/pull/990/
2017-06-30 12:17:49 -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
Colin Walters
6c2967c1ad fdio: Remove extra ';' in header
This was confusing `g-ir-scanner`.
2017-06-28 11:23:01 -04:00
Colin Walters
71d875543c macros: Avoid scanning macros
`g-ir-scanner` is confused by some of the syntax extensions in `G_IN_SET()`;
none of this is applicable to bindings, so just skip it.
2017-06-28 11:23:01 -04:00
Colin Walters
e55fd8ee31 fdio: Introduce glnx_open_anonymous_tmpfile()
There was a user of this in the libostree static delta code.
2017-06-28 11:23:01 -04:00
Colin Walters
d4c5c02327 fdio: Be sure to unset tmpfile's initialized state on cleanup
I'm not aware of a problem in practice here, but we should do this on general
principle. Writing this patch now because I hit a fd leak in the ostree static
delta processing that was introduced in the tmpfile prep code, but fixed in the
final port.
2017-06-28 11:23:01 -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
Colin Walters
05abf2143f fdio: Add glnx_try_fallocate()
The glibc `posix_fallocate()` implementation has a bad fallback,
and further we need to handle `EOPNOTSUPP` for musl.
https://github.com/flatpak/flatpak/issues/802
2017-06-13 11:43:29 -04:00
Colin Walters
f5ba01cf65 dirfd: Have dfd iter _take_fd() take a pointer and do a steal
This avoids callers having to use `glnx_steal_fd()` on their own; in general, I
think we should implement move semantics like this at the callee level.

Another reason to do this is there's a subtle problem with doing:

```
somefunction (steal_value (&v), ..., error);
```

in that if `somefunction` throws, it may not have taken ownership of the value.
At least `glnx_dirfd_iterator_init_take_fd()` didn't.
2017-05-31 14:56:04 -04:00
Colin Walters
66d162873c dirfd,xattrs: Port mostly to new code style
Not everything, but a good chunk of the remaining bits.
2017-05-30 10:05:59 -04:00
Alexander Larsson
2f8fdf80ec fdio: Allow using AT_FDCWD with GlnxTmpfile
Add an `initialized` member which means we work by default
in structs allocated with `g_new0` etc. and don't need
a special initializer.  This also fixes a bug where
we need to support `src_dfd == -1` or `AT_FDCWD`.

This fixes flatpak which uses AT_FDCWD.

Modified-by: Colin Walters <walters@verbum.org>
2017-05-19 09:44:22 -04:00
Colin Walters
4fbd48fb88 fdio: Add missing return in tmpfile error case
Just noticed this while reading the code.
2017-05-17 16:57:54 -04:00
Colin Walters
9929adcd99 fdio: Redo tmpfile API with GLnxTmpfile struct
The core problem with the previous tmpfile code
is we don't have an autocleanup that calls `unlinkat`
in the non-`O_TMPFILE` case.  And even if we did, it'd
be awkward still since the `glnx_link_tmpfile_at()` call
*consumes* the tmpfile.

Fix this by introducing a struct with a cleanup macro. This simplifies a number
of the callers in libostree - a notable case is where we had two arrays, one of
fds, one of paths. It makes other places in libostree a bit more complex, but
that's because some of the commit code paths want to deal with temporary
*symlinks* too.

Most callers are better though - in libglnx itself, `glnx_file_copy_at()` now
correctly unlinks on failure for example.
2017-05-15 17:15:12 -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
3a4d0f4684 fdio: Expose glnx_regfile_copy_bytes(), rewrite: GNU style, POSIX errno
NOTE: This changes the error handling API of `glnx_loop_write()` to be
"old school POSIX" instead of "systemd".

In ostree in a few places we use `g_output_stream_splice()`.  I
thought this would use `splice()`, but actually it doesn't today.

They also, if a cancellable is provided, end up dropping into `poll()` for every
read and write. (In addition to copying data to/from userspace).

My opinion on this is - for *local files* that's dumb. In the big picture, you
really only need cancellation when copying gigabytes. Down the line, we could
perhaps add a `glnx_copy_bytes_cancellable()` that only did that check e.g.
every gigabyte of copied data. And when we do that we should use
`g_cancellable_set_error_if_cancelled()` rather than a `poll()` with the regular
file FD, since regular files are *always* readable and writable.

For my use case with rpm-ostree though, we don't have gigabyte sized files, and
seeing all of the `poll()` calls in strace is annoying. So let's have the
non-cancellable file copying API that's modern and uses both reflink and
`sendfile()` if available, in that order.

My plan at some point once this is tested more is to migrate this code
into GLib.

Note that in order to keep our APIs consistent, I switched the systemd-imported
code to "old school POSIX" error conventions. Otherwise we'd have *3* (POSIX,
systemd, and GError) and particularly given the first two are easily confused,
it'd be a recipe for bugs.
2017-04-28 09:57:36 -04:00
Colin Walters
dc1956b289 fdio: Mostly port to new code style
There's one function that did `unlinkat()` in the cleanup section,
not doing that yet.

Note I uncovered a few bugs in a few places where we didn't preserve errno
before doing an `unlinkat()` in error paths in a few cases.

I also tried to prefix a few more error cases with the system call name.
2017-04-25 10:59:05 -04:00
Colin Walters
47fafa97e9 Port most code (except fdio) to new style
There's a lot more fdio code, starting with some of the easier ones.
2017-04-25 10:30:05 -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
Philip Withnall
2b82858169 glnx-fdio: Add wrappers around fstat() and fstatat() to handle errors
Add two inline wrappers around fstat() and fstatat() which handle
retrying on EINTR and return other errors using GError, to be consistent
with other glnx functions.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2017-04-21 14:06:19 +01:00
Philip Withnall
6746e6f54d glnx-dirfd: Add variants of glnx_mkdtempat() which open the directory
At the moment, it’s not possible for them to do this race-free (since
openat(O_DIRECTORY | O_CREAT | O_EXCL) doesn’t work), but in future this
could be possible. In any case, it’s a useful thing to want to do.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2017-04-21 14:06:19 +01:00
Philip Withnall
9307f51893 glnx-shutil: Add glnx_shutil_mkdir_p_at_open()
This is a variant of glnx_shutil_mkdir_p_at() which opens the given
directory and returns a dirfd to it. Currently, the implementation
cannot be race-free (due to a kernel bug), but it could eventually be
made race-free.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2017-04-21 14:06:19 +01:00
Philip Withnall
2576a07e6e glnx-local-alloc: Make check for invalid FDs more general
In general, all FDs < 0 are invalid (and should not have close() called
on them), so check that. This could have caused problems if a function
returned an error value < -1.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2017-04-21 10:22:27 +01: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
074236b88d errors: Add new glnx_throw_errno{,_prefix}() APIs
We have a *lot* of code of the form:

```
if (unlinkat (fd, pathname) < 0)
  {
     glnx_set_error_from_errno (error);
     goto out;
  }
```

After conversion to `return FALSE style` which is in progress, it's way shorter,
and clearer like this:

```
if (unlinkat (fd, pathname) < 0)
  return glnx_throw_errno (error);
```
2017-03-22 11:03:32 -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
2a71cb6c5b COPYING: Bump to LGPL 2.1 due to systemd import
We originally inherited LGPL 2.0 from glib I think.  But
I didn't notice when importing systemd code it's LGPL 2.1.

While individual file licenses still apply; I'm not going
to bother bumping all of them to 2.1, the complete module
should be viewed as under 2.1.

Bump the master COPYING file accordingly.
2017-02-11 08:59:54 -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
Colin Walters
7be21dee4d xattrs: Handle ERANGE
This is symmetric with an earlier commit which handled a transition from
`size != 0` -> `size = 0`. Now if xattrs are added we retry.
2017-01-29 03:23:43 -05:00