From fd442080e478e262217d2995297fffc4562efd68 Mon Sep 17 00:00:00 2001 From: Marek Blaha Date: Wed, 6 Oct 2021 16:43:10 +0200 Subject: [PATCH] reposync: Implement --safe-write-path option (RhBug:1898089) = changelog = msg: With --safe-write-path option reposync can download repositories with relative package locations (like ../package-store/f/foo.rpm) type: enhancement resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1898089 --- doc/reposync.rst | 3 +++ plugins/reposync.py | 27 ++++++++++++++++++++------- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/doc/reposync.rst b/doc/reposync.rst index 0df00b92..613751ea 100644 --- a/doc/reposync.rst +++ b/doc/reposync.rst @@ -67,6 +67,9 @@ All general DNF options are accepted. Namely, the ``--repoid`` option can be use ``-p , --download-path=`` Root path under which the downloaded repositories are stored, relative to the current working directory. Defaults to the current working directory. Every downloaded repository has a subdirectory named after its ID under this path. +``--safe-write-path`` + Root path that is considered safe for writing. If not specified it defaults to download path of the repository. Useful for repositories that use relative locations of packages out of repository directory (e.g. "../packages_store/foo.rpm"). Use with care, any file under the ``safe-write-path`` can be overwritten. Can be only used when syncing a single repository. + ``--remote-time`` Try to set the timestamps of the downloaded files to those on the remote side. diff --git a/plugins/reposync.py b/plugins/reposync.py index 66c76a77..f2d88911 100644 --- a/plugins/reposync.py +++ b/plugins/reposync.py @@ -88,6 +88,8 @@ def set_argparser(parser): parser.add_argument('-u', '--urls', default=False, action='store_true', help=_("Just list urls of what would be downloaded, " "don't download")) + parser.add_argument('--safe-write-path', default=None, + help=_("Filesystem path that is considered safe for writing. Defaults to download path.")) def configure(self): demands = self.cli.demands @@ -108,9 +110,16 @@ def configure(self): if self.opts.source: repos.enable_source_repos() - if len(list(repos.iter_enabled())) > 1 and self.opts.norepopath: - raise dnf.cli.CliError( - _("Can't use --norepopath with multiple repositories")) + if self.opts.safe_write_path is not None: + self.opts.safe_write_path = os.path.realpath(self.opts.safe_write_path) + + if len(list(repos.iter_enabled())) > 1: + if self.opts.norepopath: + raise dnf.cli.CliError( + _("Can't use --norepopath with multiple repositories")) + elif self.opts.safe_write_path is not None: + raise dnf.cli.CliError( + _("Can't use --safe-write-path with multiple repositories")) for repo in repos.iter_enabled(): repo._repo.expire() @@ -188,13 +197,17 @@ def pkg_download_path(self, pkg): repo_target = self.repo_target(pkg.repo) pkg_download_path = os.path.realpath( os.path.join(repo_target, pkg.location)) - # join() ensures repo_target ends with a path separator (otherwise the + + # join() ensures safe_write_path ends with a path separator (otherwise the # check would pass if pkg_download_path was a "sibling" path component # of repo_target that has the same prefix). - if not pkg_download_path.startswith(os.path.join(repo_target, '')): + safe_write_path = os.path.join(self.opts.safe_write_path or repo_target, '') + + if not pkg_download_path.startswith(safe_write_path): raise dnf.exceptions.Error( - _("Download target '{}' is outside of download path '{}'.").format( - pkg_download_path, repo_target)) + _("Download target '{0}' for location '{1}' of '{2}' package " + "is outside of safe write path '{3}'.").format( + pkg_download_path, pkg.location, pkg.name, safe_write_path)) return pkg_download_path def delete_old_local_packages(self, repo, pkglist):