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.)
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.
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.
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.
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.
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
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