Blame SOURCES/0001-reposync-Fix-delete-with-multiple-repos-RhBug1774103.patch

22d84c
From 8a9d9b7c09fb126baac22eda8ebb940412f4464c Mon Sep 17 00:00:00 2001
22d84c
From: Marek Blaha <mblaha@redhat.com>
22d84c
Date: Wed, 27 Nov 2019 13:43:58 +0100
22d84c
Subject: [PATCH] [reposync] Fix --delete with multiple repos (RhBug:1774103)
22d84c
22d84c
When reposync was used with --delete option and multiple repositories
22d84c
to sync, only packages from the latest repository were kept and all
22d84c
downloaded content from former repositories was imediately deleted.
22d84c
22d84c
Additionaly it fixes the problem with multiple packages having the same
22d84c
filename (in different subdirectories) in one repository. In this case
22d84c
when --delete option was used, only one of those files was kept and the
22d84c
others were deleted.
22d84c
22d84c
https://bugzilla.redhat.com/show_bug.cgi?id=1774103
22d84c
---
22d84c
 plugins/reposync.py | 35 +++++++++++++++--------------------
22d84c
 1 file changed, 15 insertions(+), 20 deletions(-)
22d84c
22d84c
diff --git a/plugins/reposync.py b/plugins/reposync.py
22d84c
index 10e9b0b5..fc612a7e 100644
22d84c
--- a/plugins/reposync.py
22d84c
+++ b/plugins/reposync.py
22d84c
@@ -145,7 +145,7 @@ def run(self):
22d84c
             else:
22d84c
                 self.download_packages(pkglist)
22d84c
             if self.opts.delete:
22d84c
-                self.delete_old_local_packages(pkglist)
22d84c
+                self.delete_old_local_packages(repo, pkglist)
22d84c
 
22d84c
     def repo_target(self, repo):
22d84c
         return _pkgdir(self.opts.destdir or self.opts.download_path, repo.id)
22d84c
@@ -169,25 +169,20 @@ def pkg_download_path(self, pkg):
22d84c
                     pkg_download_path, repo_target))
22d84c
         return pkg_download_path
22d84c
 
22d84c
-    def delete_old_local_packages(self, packages_to_download):
22d84c
-        download_map = dict()
22d84c
-        for pkg in packages_to_download:
22d84c
-            download_map[(pkg.repo.id, os.path.basename(pkg.location))] = pkg.location
22d84c
-        # delete any *.rpm file, that is not going to be downloaded from repository
22d84c
-        for repo in self.base.repos.iter_enabled():
22d84c
-            repo_target = self.repo_target(repo)
22d84c
-            for dirpath, dirnames, filenames in os.walk(repo_target):
22d84c
-                for filename in filenames:
22d84c
-                    path = os.path.join(dirpath, filename)
22d84c
-                    if filename.endswith('.rpm') and os.path.isfile(path):
22d84c
-                        location = download_map.get((repo.id, filename))
22d84c
-                        if location is None or os.path.join(repo_target, location) != path:
22d84c
-                            # Delete disappeared or relocated file
22d84c
-                            try:
22d84c
-                                os.unlink(path)
22d84c
-                                logger.info(_("[DELETED] %s"), path)
22d84c
-                            except OSError:
22d84c
-                                logger.error(_("failed to delete file %s"), path)
22d84c
+    def delete_old_local_packages(self, repo, pkglist):
22d84c
+        # delete any *.rpm file under target path, that was not downloaded from repository
22d84c
+        downloaded_files = set(self.pkg_download_path(pkg) for pkg in pkglist)
22d84c
+        for dirpath, dirnames, filenames in os.walk(self.repo_target(repo)):
22d84c
+            for filename in filenames:
22d84c
+                path = os.path.join(dirpath, filename)
22d84c
+                if filename.endswith('.rpm') and os.path.isfile(path):
22d84c
+                    if path not in downloaded_files:
22d84c
+                        # Delete disappeared or relocated file
22d84c
+                        try:
22d84c
+                            os.unlink(path)
22d84c
+                            logger.info(_("[DELETED] %s"), path)
22d84c
+                        except OSError:
22d84c
+                            logger.error(_("failed to delete file %s"), path)
22d84c
 
22d84c
     def getcomps(self, repo):
22d84c
         comps_fn = repo._repo.getCompsFn()