Commit Graph

112 Commits

Author SHA1 Message Date
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
Colin Walters
1ac35488f1 xattrs: Dedup fd reading code
By taking both fd and path into one copy of the reader func, exactly like we do
in `read_xattr_name_array`, we can abstract over the difference.

Preparatory cleanup for more work here.
2017-01-29 03:23:43 -05:00
Colin Walters
afd178fb52 xattrs: Handle xattrs changing size concurrently
We should be robust in the face of this and return a snapshot of the current
value we saw, not transiently fail. This is the semantics we expect with ostree
upgrades for `/etc` for example.
2017-01-29 03:23:43 -05:00
William Manley
6bf55255e8 listxattr: Don't assume that first call to listxattr gives correct size
To get the right sized buffer to pass to `flistattr` and `llistattr` we
first call them with a zero byte buffer.  They then return the number of
bytes they'll actually need to operate.  We would `malloc` and then call
again assuming that the size we got originally was correct.

On my computer at least this isn't always the case.  I've seen instances
where the first call returns 23B, but then on the second one returns no
data at all.  Getting these non-existant xattrs would then cause ostree
to fail.

I'm not sure why it's behaving this way on my machine.  I suspect its some
interaction with overlayfs but I haven't proven this.
2017-01-29 03:22:46 -05:00
Colin Walters
597f03b405 dirfd: Use better and faster random algorithm for gen_temp_name()
I was looking at ostree performance, and a surprising amount of
time was spent in `glnx_gen_temp_name()`.  We end up calling it
from the main loop, and the iteration here shows up in my perf
profiles.

The glibc algorithm here that we adopted is *very* dated; let's
switch to use `GRand`, which gives us a better algorithm.

It'd be even better of course to use `getrandom()`, but we should do that in
glib at some point.

While I had the patient open, I extended the charset with lowercase, to better
avoid collisions.
2017-01-26 08:53:38 -05:00
Colin Walters
abd37a4790 dirfd: Set initialized flag for iters
And use it when deinitializing, to avoid calling `closedir(NULL)`.
In practice, this doesn't matter, because `closedir` *does* handle `NULL`
in glibc.

However, I'm playing with the GCC `-fsanitize=undefined`, and it
aborts because `closedir` is tagged as requiring a non-`NULL` pointer.
2016-10-27 13:30:11 -04:00
Colin Walters
7d2f577d76 fdio: Make GLnxFileCopyFlags actually flags
I wanted to add a new one, and realized it was wrong.  Luckily,
I think we were safe until now, since the set of bits for `(0, 1, 2)`
is actually distinct.

Although, hm, callers specifying `GLNX_FILE_COPY_OVERWRITE` may
have not actually been getting that.
2016-10-25 12:25:12 -04:00
Colin Walters
36396b49ad build: Add --enable-wrpseudo-compat
See https://mail.gnome.org/archives/ostree-list/2016-October/msg00003.html

Basically https://github.com/wrpseudo/pseudo doesn't implement newer
APIs like renameat2() and O_TMPFILE, so on the host side (as
potentially opposed to the target system) we want to be able to
disable them.
2016-10-05 10:53:45 -04:00
Dan Nicholson
1e7b96808a Distribute libglnx.m4
This is needed by ostree when creating a tarball with make dist.
2016-08-31 16:40:17 -04:00
Colin Walters
7ce80827bd Remove libcontainer
No longer used by anything; see https://github.com/projectatomic/rpm-ostree/pull/429
2016-08-30 16:14:22 -04:00
Colin Walters
4ae5e3beaa libcontainer: Add a fd-relative API
I'm porting rpm-ostree and need this.  Of course all this libcontainer
stuff will be nuked in favor of bubblewrap when everything comes
together.
2016-08-07 07:29:48 -04:00
Simon McVittie
871617d519 Add missing files to libglnx distribution
Signed-off-by: Simon McVittie <smcv@debian.org>
2016-08-05 14:38:18 +01:00
Colin Walters
5ac0d702d7 fdio: Only invoke fallocate() for sizes > 0
In some cases we want to replace with zero size, and `posix_fallocate()`
is documented to return `EINVAL` in this case.

Making this change since I noticed it elsewhere.
2016-08-04 14:42:59 -04:00
Colin Walters
c2ba4d8799 Add --disable-otmpfile
Some systems have bugs with it, so let's allow downstreams to easily
disable it.

https://bugzilla.gnome.org/show_bug.cgi?id=769453
https://github.com/ostreedev/ostree/issues/421
2016-08-03 11:39:16 -04:00
Colin Walters
80e5af9218 shutil: Use new API to iterate ensuring d_type
This drops a lot of duplicate code.
2016-07-29 13:08:17 -04:00
Jonathan Lebon
c072ef1eba text_percent_internal: compare uints before printing
A wild sordid tale of substractions and unsigned integers leads this
team of variables down a loonng path...

Reported-by: Gatis Paeglis <gatis.paeglis@qt.io>
2016-07-22 09:36:51 -04:00
Colin Walters
d2e588d94f fdio: Add unlinkat() in error paths for tmpfiles
This is kind of an ABI change but it's for the better I think; on
error we consistently clean up the temp file.

This is obviously necessary without `O_TMPFILE`.  With it, we still
need an error cleanup in the case where we're trying to replace an
existing file.  I noticed this in ostree's `tests/test-refs.sh` which
intentionally tries to rename a file over a directory path.
2016-07-08 13:10:40 -04:00
Colin Walters
78ae787757 fdio: Use correct dfd with O_TMPFILE in rename case
While auditing this code to figure out why ostree's
`tests/test-refs.sh` was failing, while the bug turned out to be
different, I noticed that in the case where `dfd != target_dfd`, we
failed to do the right `renameat()`.  (No code I'm aware of does this
now).
2016-07-08 13:09:19 -04:00
Colin Walters
113c770dc1 fdio: Add open_tmpfile_linkable() and link_tmpfile_at()
We had a bug previously where we failed to clean up a temporary file
in an error path.  This is a classic case where the new `O_TMPFILE`
API in Linux is nicer.

To implement this, as usual we start with some original bits from
systemd.  But in this case I ended up having to heavily modify it
because systemd doesn't support "link into place and overwrite".  They
don't actually use their tempfile code much at all in fact - as far as
I can tell, just in the coredump code.

Whereas in many apps, ostree included, a very common use case is
atomically updating an existing file, which is
`glnx_file_replace_contents_at()`, including subtleties like doing an
`fdatasync()` if the file already existed.

Implementing this then is slightly weird since we need to link() the
file into place, then rename() after.

It's still better though because if we e.g. hit `ENOSPC` halfway
through, we'll clean up the file automatically.

We still do keep the mode where we error out if the file exists.
Finally, the ostree core though does have a more unusual case where we
want to ignore EEXIST (allow concurrent object writers), so add
support for that now.

Note: One really confusing bug I had here was that `O_TMPFILE` ignores
the provided mode, and this caused ostree to write refs that weren't
world readable.

Rework things so we always call `fchmod()`, but as a consequence we're
no longer honoring umask in the default case.  I doubt anyone will
care, and if they do we should probably fix ostree to consistently use
a mode inherited from the repo or something.
2016-07-01 15:03:01 -04:00
Alexander Larsson
4f83b70f69 glnx_release_lock_file - Don't close fd -1 (i.e. if we never locked)
This happens a lot if you use autocleanup for lock files, and the
function returns early without the lock being taken.
2016-06-28 11:23:47 +02:00
Yu Qi Zhang
a6d08657aa fdio: Delete .tmp file on failure
We noticed the temp files being left over in ostree when (mistakenly)
trying to replace the contents of a subpath that wasn't a directory.

In the future we should look at the systemd code using `O_TMPFILE`
here.
2016-06-16 13:58:55 -04:00
Colin Walters
afe3c3a861 dirfd: Fix inverted precondition in previous tmpname commit
I swear I tested it...
2016-05-31 09:29:08 -04:00
Colin Walters
4919f6ee68 Introduce glnx_gen_temp_name()
We have multiple copies growing again of this code.  glibc has this
API internally and uses it in multiple places, let's do the same.

Closes: #14
2016-05-30 11:22:23 -04:00
Jonathan Lebon
40ef5f7400 text_percent_internal: only pad right in the text case
Padding in the percentage case was useless (and actually didn't work
properly) since all the real estate is taken up by the text and the bar.
We only need padding in the text case, in case the new string is
shorter.
2016-05-10 14:01:40 -04:00
Colin Walters
3d162e772d fdio: Add glnx_stream_fstat
Migrated from libgsystem's `gs_stream_fstat()`.  It's a small function
but I end up using it in OSTree a fair bit.
2016-05-03 17:23:17 -04:00
Colin Walters
85c9dd5c07 libcontainer: Always set PATH when running in new root
For rpm-ostree's use we always run in a new root, so we don't want to
inherit the host system's PATH.  For example, NixOS uses PATH for its
software namespacing, but one could be using rpm-ostree to build
CentOS commits.
2016-05-02 10:38:16 -04:00
Colin Walters
47ddbfa563 console: Fix glnx_console_text
Not sure if it ever worked.  We need to not print the bars, etc.
2016-05-01 14:22:25 -04:00
Alexander Larsson
69d8a597f7 Don't touch errno in glnx_fd_close
We're ignoring the result from the close, but it can still affect
errno, which is bad if you use this in functions that sets
errno, because errno can unexpectedly change after you've set it.
2016-03-10 23:12:07 +01:00
Colin Walters
08ae6639e5 console: Add an API to just emit text
We had this internally, just need to expose it.
2016-03-10 13:51:13 -05:00
Colin Walters
8a7943fef6 console: Fix bar progress length
The previous fix added the last character of text, but failed to
account for the space we're adding.
2016-02-08 14:09:13 +01:00
Colin Walters
769522753c console: Don't delete last character of output text
Not sure why we were doing this...I guess people were working around
it by adding their own spaces?
2016-01-26 11:16:56 -05:00
Colin Walters
34a96c03dd console: Fix g_auto() and unlock cleanup
This way, one can unlock the console while still using the cleanup
macro.  Otherwise we miss a lot of the ergonomics of cleanup macros.
2016-01-25 11:25:19 -05:00
Colin Walters
aac5a6cef7 console: g_auto() macro no-ops if console is not locked
Otherwise we miss a lot of the ergonomics of cleanup macros.
2016-01-25 10:23:34 -05:00
Colin Walters
2ca280f012 Introduce glnx-alloca.h with glnx_strjoina()
This is taken from systemd, and is really useful when one has a few
known-to-be-small strings one wants to concatenate without resorting
to malloc.
2016-01-24 12:10:26 -05:00
Alexander Larsson
194eb7a09c Add autoptr support for GZlib* 2016-01-22 15:27:11 +01:00