Commit Graph

11 Commits

Author SHA1 Message Date
a1346054
d6e93b7ee0 bash: fix issues identified through shellcheck
Signed-off-by: a1346054 <36859588+a1346054@users.noreply.github.com>
2021-08-23 12:19:03 +02:00
Will Thompson
c59a5db5a9 sideload-repo-systemd: Don't require media dir to exist
When launched when the session starts, /run/media/%u may not exist if
the user has never mounted any removable storage devices. This causes
the unit to fail on login.

Relax the check to just validate that the argument has been provided.
Since we use shopt -s nullglob, the "$media_dir"/* glob will expand to
the empty list if $media_dir does not exist.

(The other option is to add a condition check to the .service, but the
script might conceivably want to clean up some stale symlinks.)
2020-09-23 10:02:31 +01:00
Phaedrus Leeds
192da15f60 sideload-repos-systemd: Update a comment 2020-09-21 20:49:01 -07:00
Phaedrus Leeds
093b22f5a3 sideload-repo-systemd: Only remove owned sym links
Check file ownership to ensure flatpak-create-sideload-symlinks.sh
only cleans up links it created. This could be relevant on multi-user
systems with fast user switching.
2020-09-21 20:48:51 -07:00
Phaedrus Leeds
49096521fd sideload-repos-systemd: Make sym link more human friendly
Use escaping rather than checksumming to generate a unique name, as
discussed here:
https://github.com/flatpak/flatpak/pull/3866#issuecomment-694784359
2020-09-21 18:16:26 -07:00
Phaedrus Leeds
9b82fbfabb sideload-repos-systemd: Adjust whitespace 2020-09-21 18:14:38 -07:00
Will Thompson
c43e8e1331 sideload-repos-systemd: Don't launch service in a tight loop
man systemd.path has the following to say:

> When a service unit triggered by a path unit terminates (regardless
> whether it exited successfully or failed), monitored paths are checked
> immediately again, and the service accordingly restarted instantly.

On my system, I observe that once /run/media/wjt is created,
flatpak-sideload-usb-repo.service is invoked in a tight loop. I think
what's happening is that PathExists=/run/media/wjt continues to be true,
so the service keeps getting restarted.

What we instead want is to activate the .service:

- On login
- When the media directory *changes* (because mount points beneath it
  appear or disappear)

Remove PathExists from the .path, and instead mark the .service as
WantedBy default.target so it is launched on login.
2020-09-18 11:57:38 +01:00
Will Thompson
8c61324b6b sideload-repo-systemd: Set 'nullglob' option
Previously, if there were no existing symlinks, this script would fail:

    wjt@camille:~$ LANG=en_GB.utf8 bash -x /usr/libexec/flatpak-create-sideload-symlinks.sh /run/media/wjt
    + test 1 -eq 1
    + test -d /run/media/wjt
    + for f in "$1"/*
    + test -d '/run/media/wjt/*'
    + continue
    + for f in /run/flatpak/sideload-repos/automount-*
    + test -e '/run/flatpak/sideload-repos/automount-*'
    + rm '/run/flatpak/sideload-repos/automount-*'
    rm: cannot remove '/run/flatpak/sideload-repos/automount-*': No such file or directory

This is due to the surprising POSIX shell behaviour that a glob that
matches no files expands to itself, rather than to the empty list.

http://mywiki.wooledge.org/BashFAQ/004

The POSIX solution is to add 'test -L $f' inside the loop to check if
the variable actually exists.  The first loop in this script uses this
technique: it has a 'test -d "$f"', seen in the trace above.

Bash implements a 'nullglob' feature which gives globs the behaviour you
might hope for. Set it in this script.
2020-09-18 11:09:35 +01:00
Phaedrus Leeds
d73d6c16f3 sideload-repos-systemd: Use systemd-tmpfiles
Instead of using a systemd service to create the directory we need for
sideloading (/run/flatpak/sideload-repos), use a systemd-tmpfiles conf
file. This is more elegant, and the service also had a bug which meant
it would create a "/run/flatpak;" directory instead of the one intended.

Since systemd-tmpfiles-setup.service runs before sysinit.target and path
units are run after sysinit.target, the tmp directory should be in place
when flatpak-sideload-usb-repo.path runs in the systemd user instance.

Endless OS already has a file at /lib/tmpfiles.d/flatpak.conf, so we
can't use that filename.
2020-09-14 10:57:46 +02:00
Phaedrus Leeds
f1383c10be sideload-repos-systemd: Dist service unconditionally
Add flatpak-sideload-usb-repo.service.in to EXTRA_DIST regardless of if
the --enable-auto-sideloading configure option was passed. This allows
building a tarball without that option and then building from the
tarball with the option.

This matches what is done in system-helper/Makefile.am.inc with
flatpak-system-helper.service.in.

Picked from https://github.com/endlessm/flatpak/pull/228
2020-08-12 10:07:58 +02:00
Matthew Leeds
9caf664fab Add systemd units to sideload from hotplugged USBs
Currently with the sideload implementation of offline updates you have
to manually create a symlink to your USB drive to sideload from it,
which is a regression compared to the previous implementation which
scanned all mounted filesystems in OstreeRepoFinderMount in libostree.
So this commit adds a few systemd units and a bash script so that any
time a USB drive is plugged in and automatically mounted by udisks, a
symlink to it is created in /run/flatpak/sideload-repos. When the drive
is unplugged the symlink is removed.

However this solution still has a lot of moving parts, so we may want to
instead have libflatpak use GVolumeMonitor and find the mounted
filesystems itself; see https://github.com/flatpak/flatpak/issues/3705

Fixes https://github.com/flatpak/flatpak/issues/3490
2020-06-23 09:37:10 +02:00