Commit Graph

812 Commits

Author SHA1 Message Date
Alexander Larsson
08dc7cb2aa dir: Remove unused non-_decomposed() helpers
This removes flatpak_dir_list_remote_refs() and
flatpak_dir_list_all_remote_refs() which have no more users, as everything
migrated to the _decomposed() versions.
2020-11-10 14:32:13 +01:00
Alexander Larsson
db11607016 installation: Use the FlatpakDecomposed APIs to create FlatpakRef:s
This means we do a bit less string operations, but it also allows us
to pick up the collection-id when listing refs from a file: uri, fixing
the issues described in https://github.com/flatpak/flatpak/pull/3909

This slightly changes the behaviour of collection ids and remotes.
When listing regular remotes we only ever return the refs with the
collection id specified by the configuration of that remote. However,
when specifying a file: uri for the remote name we return all the refs
in the repo, with their corresponding collection id.

This means the test suite has to change a bit as before we returned
all the collrefs from the remote, not just the one matching the configured
collection id.
2020-11-05 10:43:10 +01:00
Alexander Larsson
247fed25ff remote-state: For local repos, set collection id for main ref table too
If you're listing a file: uri we (as before) add the collection-ref
tables, but we now also set the proper collection-id for the main ref
table if the ostree.summary.collection-id key is set.
2020-11-05 10:43:10 +01:00
Alexander Larsson
bd5029e72a dir: Add flatpak_dir_list_refs_decomposed() 2020-11-05 10:43:10 +01:00
Alexander Larsson
3b63c51ce6 decomposed: Add support for FlatpakRefKind 2020-11-05 10:43:10 +01:00
Alexander Larsson
7757b1dea7 RemoteState: For a local repos, add refs with all collection ids.
If you are listing a remote named (e.g.) file:///some/path/to/repo then we
have no idea what the actual collection id is for that remote, so we can't
pick the right part. In practice, this is typically done when listing
a side-load repo, such as described in
https://blogs.gnome.org/mclasen/2018/08/26/about-flatpak-installations/
or by the endless software installer that lists available offline apps.

This kinda abuses the FlatpakDecomposed object to store the collection
id, even if that doesn't normally carry this. This is not optimal, but it
is kind of a weird case and there isn't really any other place for it that
makes sense either, and having it there is efficient and keeps the APIs.
2020-11-05 10:43:10 +01:00
Alexander Larsson
089a5bebd5 Add flatpak_dir_list_remote_refs_decomposed() 2020-11-05 10:43:10 +01:00
Alexander Larsson
7465ba0f18 Various FlatpakDecomposed API updates
getters now have three forms:
  const char *foo_peek() returns pointer into data and length
  const char* foo_get() returns zero terminated pointer into data
  char *foo_dup() returns allocated zero terminated pointer

Not everything has all of these (as that is not always possible), but
it is clear what each one does.

We also now support storing a collection id for a
FlatpakDecomposed. This is not typically done (as we use the source
remote to define the collecion id and don't expose this concept to the
user). However, for the case when directly listing a local repo (such as
a side-load repo) we need this.

We now validate the arch string. The valid chars are a-z, A-Z, 0-9 and
_. This was't previously verified, but no arches in existance have other
chars.

There is a new flatpak_decomposed_new_from_decomposed() which lets you
modify part of another ref. Say replace the arch or the branch and
keep the rest the same.
2020-11-05 10:43:10 +01:00
Alexander Larsson
97740ab3f6 remote state: Try loading non-index summary from memory cache before fetching
We used to go:
 * indexed summary memory cache
 * indexed summary fetch
 * compat summary memory cache
 * compat summary fetch

But now we go:
 * indexed summary memory cache
 * compat summary memory cache
 * indexed summary fetch
 * compat summary fetch

This means in the compat case we don't constantly have to fail the
summary fetch before using the memory cache.
2020-11-03 15:14:16 +01:00
Alexander Larsson
dddeaca8c3 transaction: Reuse FlatpakRemoteState when finding dependencies
This moves a bunch of code from FlatpakDir to FlatpakTransaction which was
only used to find dependencies. This allows us to reuse the existing
cache of FlatpakRemoteState objects in the transaction.
2020-11-03 14:19:58 +01:00
Alexander Larsson
f647a5ea79 remote-modify: Clear cached summaries when the remote is modified.
This fixed some issues in the test where changing the url cause the
cache:ing to fail to update due to the mtimes being to new in combination
with the HTTP If-Modified-Since use.
2020-11-03 12:57:37 +01:00
Alexander Larsson
a3eaa36ce4 dir: Clear summary cache when updating metadata
For example, if the url changes we need to re-read the summary. This
was causing issues in a repo test where we re-wrote the url, and then
the new summary was not downloaded because of a st_mtime based
If-Modified-After check in the python httpd.
2020-11-03 12:57:37 +01:00
Alexander Larsson
a8fe3247e7 summary cache: Rename CachedSummary.remote to CachedSummary.name
This doesn't change the results but makes it more clear now that we don't
only use the remote name as the cache key.
2020-11-03 12:57:37 +01:00
Alexander Larsson
c04df80ee1 Drop some spew from flatpak_dir_list_unused_refs()
This was showing up in every flatpak update, making it hard
to see the important stuff.
2020-10-29 16:08:12 +01:00
Alexander Larsson
e6d7a1148e summaries: Cache subsummaries per-arch
We store the cached subsummaries as `$remote-$arch-digest.sub`, this
way we can prune everything but the most recent (i.e. highest mtime)
rather than looking at the index. This way we don't prune old
subsummaries for other arches when we update one arch, which would not
let us use deltas for those.

Some details:
 * If mtimes are identical (mtime precision issue) for several
   subsummaries we keep all.
 * If we just saved a subsummary in the cache we don't prune that
   in case there is something wonky with mtimes.

The subsets for a remote are pruned every time we write a new cached
subsummary.
2020-10-29 15:30:35 +01:00
Alexander Larsson
2c8fe5b94c Use the new FlatpakDecomposed when mathing refs
This avoids lots of re-splitting and re-validation as well as strdups.
2020-10-29 15:30:35 +01:00
Alexander Larsson
7483a98769 utils: Allow passing sizes to more utils
These now take (optional) string length args:
  flatpak_id_has_subref_suffix()
  flatpak_levenshtein_distance()
  flatpak_is_valid_name()
  flatpak_is_valid_branch()
2020-10-29 15:30:35 +01:00
Alexander Larsson
98e20c18f9 summaries: Support using summary deltas on client
Also prune old unreferenced subsummaries on each transaction.
2020-10-29 15:30:35 +01:00
Alexander Larsson
2c6fec556f summaries: Generate summary deltas when updating repo 2020-10-29 15:30:35 +01:00
Alexander Larsson
0221f5a1fa summaries: Ensure we can support non-default arches W/ indexed summaries
We need to load the ensure the right arch whenever we need it.

Also this restructures the RemoteState handling a bit in general so that
we avoid loading the same remote state multiple times when converting
partial refs to full refs.
2020-10-29 15:30:35 +01:00
Alexander Larsson
dce6f748bb summaries: Rework the debug spew for summary/cache loading
This way its easier to tell exactly what happens.
2020-10-29 15:30:35 +01:00
Alexander Larsson
57954e2efc summary: Enable use of indexed summaries in the client
We first try to download the index, then fall back to the old format.
2020-10-29 15:30:35 +01:00
Alexander Larsson
096daf91f2 summary: Implement a new, more efficient, summary format
This drops the deltas from the summary and uses the per-commit
metadata field to add the cache data to avoid the need for the separate
xa.cache and xa.sparse-cache indexes. This way we avoid repeating the
refs in multiple places.

Nothing uses this format yet, but we still pass make check if we enable
it.
2020-10-29 15:30:35 +01:00
Alexander Larsson
387bb8c64e Add flatpak_dir_get_remote_subset()
Reads the subset config for the remote, if any.
2020-10-29 15:30:35 +01:00
Simon McVittie
1a58a86676 Skip parental controls checks on ServiceUnknown or NameHasNoOwner
If accountsservice isn't available on the system bus, then we can't
ask it for the user's parental controls settings, and we also can't
ask it whether it even has the malcontent extension. Since this is
not a real security boundary, fail open.

This can be dropped if we depend on a version of libmalcontent that maps
these errors to MCT_APP_FILTER_ERROR_DISABLED.

Resolves: https://github.com/flatpak/flatpak/issues/3902
Bug-Debian: https://bugs.debian.org/972138
Signed-off-by: Simon McVittie <smcv@collabora.com>
2020-10-15 09:24:14 +01:00
Alexander Larsson
a5d86b8acb transaction: inject eol status to flatpak_dir_list_unused_refs()
This way we can get the proper eol status for the new to-be-installed
refs, rather than whatever was previously installed. This allows us to
detect when a runtime is updated and the new one is eol, and nothing
uses it, so it can be auto-uninstalled.
2020-10-08 09:14:51 +02:00
Alexander Larsson
05a7bda140 transaction: Simplify add_uninstall_unused_ops()
Rather than trying to figure out which runtimes are affected byt
the current setup of ops we run flatpak_dir_list_unused_refs() twice,
once with and once without the changes the transaction will cause.

Any unused refs after the transaction that were not unused before are
caused by the transaction and we start uninstall ops for those.

Also rename flatpak_dir_list_unused_refs_with_options() to
flatpak_dir_list_unused_refs() as it need not be so long.
2020-10-08 09:14:51 +02:00
Alexander Larsson
6bdc364e95 find-unused: Don't care about origin when finding used deps
If we're auto-installing dependencies we want to limit them to those
from the same remote. However you can still (manually) install such
dependencies, so when we're looking for things to possibly uninstall
we need to check dependencies from all remotes.
2020-10-08 09:14:51 +02:00
Alexander Larsson
478bcc0b12 Rewrite flatpak_dir_list_unused_refs_with_options()
This simplifies the implementation and makes it more effective in
avoiding to re-scan the same things several times.
2020-10-08 09:14:51 +02:00
Phaedrus Leeds
44aa0d6830 Add internal API for list_unused_refs() and add _with_options()
In a few places we are using
flatpak_installation_list_unused_refs() and then only
using the ref strings not the FlatpakInstalledRef objects, so the
resources used to construct those objects are wasted. Add a flatpak_dir_
function to be used internally instead. One day we will figure out how
to make flatpak-dir.c less of a wilderness.

This also adds the flatpak_installation_list_unused_refs_with_options()
verion that has extended features.
2020-10-08 09:14:51 +02:00
Phaedrus Leeds
ce2e36d8ea dir: Remove unused variable
This is no longer needed since commit 124b0be92
2020-10-02 15:30:58 +02:00
Phaedrus Leeds
92742d8387 dir: Remove superfluous flatpak_dir_ensure_repo()
This is no longer needed since a FlatpakRemoteState is passed in, which
ensures the repo exists. The similar call was already removed from
flatpak_dir_install().
2020-09-15 08:58:49 +02:00
Phaedrus Leeds
08f692962e Avoid shadowing local variables
Avoid shadowing variables that are already declared in a previous scope,
and make such occurrences compile-time errors. These are not functional
changes.

In a few places do related code cleanup.

A similar ostree PR is here:
https://github.com/ostreedev/ostree/pull/2195
2020-09-15 08:58:49 +02:00
Phaedrus Leeds
238317d99b Avoid critical error in remote-info command
Without this patch, the remote-info command will sometimes emit a
critical error "g_utf8_strlen: assertion 'p != NULL || max == 0' failed"
and print (null) for the "Commit:" field, since the commit doesn't get
initialized properly.
2020-09-14 09:32:27 +02:00
Phaedrus Leeds
7cd1990196 Automatically pin explicitly installed runtimes
If a runtime is installed explicitly rather than as a dependency, pin it
so it doesn't get automatically removed when unused runtimes are being
removed. We do this because the runtime might be installed for
development or other uses.

This commit also rearranges some code in the mask and pin commands, and
adds a unit test.
2020-08-31 16:29:03 +02:00
Matthew Leeds
d2d5397cc1 Add pin command to keep unused runtimes
As discussed here [1], we want a way to mark runtimes to be kept even
when they are unused by any apps and we are removing such runtimes.
Currently this is a command that can be run manually; a subsequent
commit will pin runtimes automatically if they are installed
independently of any app.

A unit test is included.

[1] https://github.com/flatpak/flatpak/issues/2639#issuecomment-662311756
2020-08-31 16:29:03 +02:00
Phaedrus Leeds
ebcad3a402 dir: Fix a typo of .changed 2020-08-31 16:26:12 +02:00
Philip Withnall
78a2e45610 flatpak-dir: Pass cached summary{,.sig} to OSTree
This allows OSTree to avoid re-checking the `summary.sig` for freshness
once per pull in a transaction, since flatpak has already done that.
This avoids latency in a transaction (especially on high latency or
lossy network connections) and avoids the potential race condition of
the `summary` file changing on the server part-way through a
transaction.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-08-24 08:58:08 +02:00
Philip Withnall
95bef8e4a5 flatpak-dir: Cache the summary GBytes in FlatpakRemoteState
`FlatpakRemoteState` already caches the parsed `GVariant` form of the
summary, but it will be useful (in the following commits) to also have a
pointer to the `GBytes` which backs that variant. The `GBytes` will be
passed into OSTree in the following commits, to allow it to avoid
unnecessarily re-downloading the `summary` file.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-08-24 08:58:08 +02:00
Philip Withnall
254a885879 dir: Clarify a comment about caching
`flatpak-dir.c` supports two caches for `summary{,.sig}` files: one in
memory (per `FlatpakDir` instance) and one on disk (shared between all
users of the repository). Clarify in this comment which one is being
referred to.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-08-24 08:58:08 +02:00
Philip Withnall
98e372e85e dir: Tidy up definition of cache timeout
Adding brackets makes it a little more obvious that it’s 5 minutes, and
less likely that operator precedence where the macro is used will result
in the wrong value being calculated.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-08-24 08:58:08 +02:00
Philip Withnall
63b4f3a5c1 dir: Fix caching of summary files
Cache hits didn’t result in the cache entry’s timeout being extended,
and timeouts didn’t result in the entry being removed from the cache to
free up resources (even though it would never be returned as a cache hit
again).

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-08-24 08:58:08 +02:00
Philip Withnall
9bec865995 dir: Fix potential overflow when comparing cache timeouts
Also use a constant to clarify the code a little, although it’s
functionally equivalent.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-08-24 08:58:08 +02:00
Phaedrus Leeds
332f75494b Properly validate configured collection IDs
This commit is a follow-up for the commit "dir: Validate locally
configured collection IDs". Whereas in that commit we validate the
collection ID in flatpak_remote_state_fetch_commit_object(), here we do
it in _flatpak_dir_get_remote_state(), since fetch_commit_object() is
not called on the code path normally taken for a transaction (only if
try_resolve_op_from_metadata() fails and in other niche scenarios).

This should ensure that if someone erroneously sets a collection ID on a
remote, the error will be caught quickly. It also helps the eos-updater
unit tests pass.
2020-08-21 09:35:49 +02:00
Alexander Larsson
83f11317e6 Allow multiarch in the apply_extra for extra-data
Some things could need to run e.g. i386 code in apply_extra (for
example #3742).  In this very limited context (almost everything is
read-only) this seems pretty secure.

We could require the app to specify a multiarch pemission to allow
this, but such permissions only really make sense for an app, and
extra data is often used for other things like runtimes and
extensions, that seems a bit weird.  Lets just enable it always.
2020-08-20 16:44:39 +02:00
Alexander Larsson
87a44588d6 dir: Cache responses from localed and account service
These are system dbus calls that could potentially be somewhat slow
and its unlikely that the local config changes during runtime of the
session (because the desktop really needs a restart to pick up a new
locale). Also, if there are any issues with these (such as #3755) each
call will be the 25sec dbus timeout, which is not great.

Fixes https://github.com/flatpak/flatpak/issues/3792
2020-08-17 14:27:02 +02:00
Matthew Leeds
845a877ce1 Prioritize an app's origin for its runtime
Currently when searching for a remote to provide the runtime for an app,
we search remotes in priority order. This commit makes it so we search
the remote providing the app before others with the same priority, and
otherwise still search in priority order.  This means for the common
case where every remote has the default priority of 1, the app's origin
will have the first chance to provide the runtime. This behavior seems
logical, but the impetus for this change was also to keep a unit test
passing in eos-updater[1] after a port to FlatpakTransaction.

Originally the eos-updater unit test was written to prioritize the
origin remote regardless of the priorities on any other remote, but
during code review it was decided to let higher priority remotes stay
above the app's origin.

In practice it's usually true that only one remote provides a runtime
and priorities aren't set at all, so this is an edge case that probably
doesn't come up much.

A unit test and documentation updates are included.

[1] eede0a8b9c/tests/test-update-install-flatpaks.c (L1919)
2020-08-17 09:21:30 +02:00
Matthew Leeds
1bf5f2ed9e dir: Validate locally configured collection IDs
Currently if the user configures a collection ID on a remote which is
incorrect, either because it doesn't match the one configured server
side or because the server doesn't have one configured at all, Flatpak
will not notice the issue and happily still pull from the remote. This
is new since 1.7.1; before that such a problem would be caught because
the server wouldn't provide the ostree-metadata ref for the configured
collection ID.

This commit catches such errors by checking commit metadata, as we
already do for the ref binding metadata. Otherwise such a mismatch would
prevent successful offline distribution of the apps/runtimes provided by
the remote.

The impetus for this is to keep an eos-updater unit test passing with
Flatpak 1.8.x:
"/updater/install-flatpaks-pull-to-repo-error-if-collection-id-invalid"
2020-07-23 11:29:13 +02:00
Matthew Leeds
1841803ef3 Fix typos in a comment 2020-07-21 09:02:18 +02:00
Matthew Leeds
2558b086d7 dir: Fix wrong extension refspecs
Currently Flatpak only supports extensions which come from the same
remote as the thing being extended; for discussion on this see
https://github.com/flatpak/flatpak/issues/861

However in general it isn't clear from the metadata what remote provides
an extension. For example com.endlessm.apps.Platform//5 defines the
extension org.freedesktop.Platform.VAAPI.Intel (inherited from
org.freedesktop.Platform) which can be found on flathub not eos-sdk. So
we don't want to add an extension refspec to the transaction if the
current remote is not the one that actually provides it. In
flatpak_dir_find_remote_related_for_metadata() this invariant is
maintained because we check if the ref exists in the remote before
adding it with add_related(). However in
flatpak_dir_find_local_related_for_metadata() we check for existing
deploy data but omit checking that the deploy origin matches the origin
passed in, and in that case can accidentally add an incorrect refspec to
a transaction. So this commit adds the missing origin check.

One way to reproduce this issue is with this command, having both the
Endless platform and the VAAPI extension already installed:
$ flatpak update --no-pull com.endlessm.apps.Platform//5
Looking for updates…
error: Refspec 'eos-sdk:runtime/org.freedesktop.Platform.VAAPI.Intel/x86_64/1.6' not found

This issue also affects GNOME Software which does a no-deploy
transaction followed by a no-pull transaction (this issue occurs in the
latter) and is treated as fatal causing updates not to be applied.
However it only happens in some niche circumstances, because
flatpak_transaction_add_op() will only use the first remote passed to it
if called more than once for the same ref from different remotes. This
is normally fine; refs generally only come from one remote. But it does
mean this issue only occurs if the extension in question was not already
added to the transaction with a correct origin.
2020-06-30 09:07:19 +02:00