summaryrefslogtreecommitdiff
Commit message (Collapse)AuthorAgeFilesLines
* tests: Add macro for auto-error checkingv2017.2Colin Walters2017-09-132-95/+74
| | | | | | | | | | 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()`.
* tree-wide: Use our own syscall wrappers or error prefixingColin Walters2017-09-133-20/+18
| | | | Followup to similar commits in the ostree stack recently.
* dirfd: Extend tmpdir API to support optional cleaningColin Walters2017-09-132-14/+57
| | | | | | | | | | | | | We have a use case in libostree's staging dirs where we try to reuse them across multiple ostree txns, but we want the fd-relative bits here. Extend the tmpdir API to make deletion optional. While here, also extend the API to support checking for errors when deleting for projects like libostree that want to do so consistently. Also while here, add a change to set the fd to `-1` after clearing to be extra defensive.
* fdio: Add gtk-doc for stbuf parameter of glnx_file_copy_at()Colin Walters2017-09-121-0/+1
| | | | Spotted in https://github.com/GNOME/libglnx/pull/80/commits/ba5e1cf9f58770ba879e9fb6ac337ccec9d0a10c
* fdio: Use O_TMPFILE + rename-overwrite for regfile copiesColin Walters2017-09-122-76/+118
| | | | | | | | | | | | | | | | | | | | | | | | 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.
* fdio: Support taking ownership of tmpfile fdColin Walters2017-09-122-3/+6
| | | | | | | | 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.
* fdio: Use O_EXCL for anonymous tmpfilesColin Walters2017-09-121-18/+30
| | | | | | | | | | | | | I noticed while reading the manpage for `linkat()` that `O_TMPFILE` supports `O_EXCL` to mean exactly what we're doing with the anonymous tmpfile API. Change the code to start using it; this required refactoring the internals since we had a check to be sure the caller wasn't passing `O_EXCL` for the non-anonymous path which we want to keep. Presumably the storage system could do smarter things if it knows a file will always be anonymous, e.g. it doesn't need to journal its data.
* fdio: Add glnx_fstatat_allow_noent()Colin Walters2017-09-072-0/+60
| | | | | | 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`.
* test-libglnx-xattrs.c: appease -Wunused-variableJonathan Lebon2017-08-251-1/+0
|
* dirfd: New tmpdir APIColin Walters2017-08-183-86/+87
| | | | | | | | | | | 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.
* console: trim useless checkJonathan Lebon2017-08-151-1/+1
| | | | | | The `percentage` var is a guint and so is always >= 0. Coverity CID: 163703
* dirfd: Fix typo in commentMatthew Leeds2017-08-151-1/+1
|
* glnx-console: Add missing NULL check before writing out textPhilip Withnall2017-08-151-1/+2
| | | | | | | | It’s possible that text is NULL on this path. Coverity CID: 1376570 Signed-off-by: Philip Withnall <withnall@endlessm.com>
* fdio: Merge systemd code to use copy_file_range(), use FICLONEColin Walters2017-08-083-38/+141
| | | | | | | | | | | | | | | | | | | | | | FICLONE is the new alias for the formerly btrfs-specific ioctl; XFS has experimental patches to support it. Further, we should use copy_file_range() for the case where we're only doing a limited copy. Both NFS and XFS (with reflink enabled) understand it. Part of the reason I'm doing this is so that ostree's `/etc` merge will start using XFS reflinks. But another major reason is to take the next step after and copy this code into GLib as well, so that all of the general GLib users will benefit; e.g. Nautilus will transparently do server copy offloads with NFS home directories. See also this coreutils thread about `copy_file_range()`: <https://debbugs.gnu.org/cgi/bugreport.cgi?bug=24399>. I don't care about file holes for our use cases, so it's fine. Other changes while I'm here: - Tweak the sendfile() case to match the newly inlined logic for cfr - Add a TEMP_FAILURE_RETRY() around the read()
* console: Fix Coverity NULL deref warningColin Walters2017-08-021-1/+2
| | | | | | | We need to handle our "empty to NULL canonicalization" before doing the length. Coverity CID: 1376570
* local-alloc: Remove almost all macros like glnx_free, glnx_unref_variantColin Walters2017-07-312-161/+10
| | | | | | | | | | We should be able to rely upstream on everything *except* `glnx_unref_object` which requires the library itself to depend on a newer glib, which isn't true for e.g. RHEL7 libsoup. libostree was almost ready for this; just a few patches to push it to completion in https://github.com/ostreedev/ostree/pull/1042
* localalloc: Abort on EBADF from close() by defaultColin Walters2017-07-261-1/+2
| | | | | | systemd does this by default. I think we should treat this as a fatal error since it can cause really painful-to-debug problems if we don't just get EBADF but actually close something else's fd due to a race.
* tests: Fix a -Wmaybe-uninitialized warningColin Walters2017-07-241-1/+8
| | | | | It'd be really nice if gtest had a variant which had the funcs take `GError`. May work on that.
* errors: check for an error before prefixingJonathan Lebon2017-07-211-2/+3
| | | | | | 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.
* errors: Mark GLNX_AUTO_PREFIX_ERROR() as usedColin Walters2017-07-201-1/+2
| | | | Since it's intentional we never use it, and `clang` barfs on this (rightly).
* dirfd: Add missing includes for errnoColin Walters2017-07-201-0/+2
| | | | | Thought the previous patch would have been obvious enough not to compile test but...
* dirfd: Add glnx_ensure_dir()Colin Walters2017-07-202-0/+30
| | | | | | | 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.
* fdio: Introduce glnx_openat_read()Colin Walters2017-07-193-27/+49
| | | | | | 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.
* dirfd: Add filename to glnx_opendirat()Colin Walters2017-07-191-3/+1
| | | | | | 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.
* errors: Add GLNX_AUTO_PREFIX_ERRORColin Walters2017-07-172-0/+73
| | | | | | | | | | | | 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.
* fdio: Add string prefix for glnx_fstat()Colin Walters2017-07-171-2/+1
| | | | For consistency.
* fdio: Add a fchmod wrapperColin Walters2017-07-171-0/+22
| | | | | There are a number of versions of this in ostree at least, might as well wrap it.
* Remove glnx_stream_fstat()Colin Walters2017-07-172-30/+0
| | | | | There are only two users of this in ostree, and one of them is fairly bogus; we can just use `fstat()`.
* fdio: Add cleanup+flush API for FILE*Colin Walters2017-07-173-0/+49
| | | | | | | | | | | | | | | | | 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.
* README.md: Change xdg-app to flatpakMatthew Leeds2017-07-131-1/+1
|
* fdio: Ensure O_TMPFILE is mode 0600Colin Walters2017-07-102-6/+35
| | | | Work around an older glibc bug.
* macros: Add a size check for hashtable itersColin Walters2017-06-301-0/+2
| | | | | | | 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/
* tests: Fix compilation of fdio testColin Walters2017-06-281-1/+1
| | | | Not sure how I missed this before.
* fdio: Remove extra ';' in headerColin Walters2017-06-281-1/+1
| | | | This was confusing `g-ir-scanner`.
* macros: Avoid scanning macrosColin Walters2017-06-281-0/+5
| | | | | `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.
* fdio: Introduce glnx_open_anonymous_tmpfile()Colin Walters2017-06-282-0/+29
| | | | There was a user of this in the libostree static delta code.
* fdio: Be sure to unset tmpfile's initialized state on cleanupColin Walters2017-06-281-1/+8
| | | | | | | 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.
* macros: add GLNX_HASH_TABLE_FOREACH_VJonathan Lebon2017-06-282-1/+18
| | | | | | | 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.
* fdio: Add wrappers for renameat(), unlinkat()Colin Walters2017-06-263-10/+40
| | | | | Besides doing `TEMP_FAILURE_RETRY` and `GError` conversion, these also prefix the error with arguments.
* glnx-macros.h: add GLNX_HASH_TABLE_FOREACH macrosJonathan Lebon2017-06-172-0/+111
| | | | | | | | 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.
* test-libglnx-macros.c: fix missing semicolonJonathan Lebon2017-06-171-1/+1
|
* Add G_IN_SET, patch our internal users via spatchColin Walters2017-06-148-57/+178
| | | | | | | | | | | | | | 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.
* fdio: Add glnx_try_fallocate()Colin Walters2017-06-132-11/+34
| | | | | | 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
* dirfd: Have dfd iter _take_fd() take a pointer and do a stealColin Walters2017-05-313-8/+7
| | | | | | | | | | | | | | 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.
* dirfd,xattrs: Port mostly to new code styleColin Walters2017-05-302-126/+42
| | | | Not everything, but a good chunk of the remaining bits.
* fdio: Allow using AT_FDCWD with GlnxTmpfileAlexander Larsson2017-05-192-8/+12
| | | | | | | | | | | 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>
* fdio: Add missing return in tmpfile error caseColin Walters2017-05-171-0/+1
| | | | Just noticed this while reading the code.
* fdio: Redo tmpfile API with GLnxTmpfile structColin Walters2017-05-152-44/+68
| | | | | | | | | | | | | | | | | 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.
* glnx-errors.h: add a glnx_throw_prefix() variantJonathan Lebon2017-05-113-9/+59
| | | | | | 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.
* fdio: Expose glnx_regfile_copy_bytes(), rewrite: GNU style, POSIX errnov2017.1Colin Walters2017-04-282-91/+114
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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.