[projects/pld-ftp-admin] ftptree: compare versions with the epoch when a move removes older ones

arekm arekm at pld-linux.org
Sat Nov 21 10:43:17 CET 2026


commit 14396b4ae220622b060bc9687d1f46ff46aab38f
Author: Arkadiusz Miśkiewicz <arekm at maven.pl>
Date:   Sat Nov 21 01:46:51 2026 +0100

    ftptree: compare versions with the epoch when a move removes older ones
    
    The .info names carry no epoch, so moving pkg-2.0-1 removed pkg-1:1.0-1,
    the newer one. The epoch now comes from the src.rpm headers, read without
    checking signatures, so a key missing from the rpm database is no error;
    one that cannot be read stops the move.

 modules/ftptree.py | 59 ++++++++++++++++++++++++++++++++++++------------------
 1 file changed, 40 insertions(+), 19 deletions(-)
---
diff --git a/modules/ftptree.py b/modules/ftptree.py
index 2e0fd1d..227d22c 100644
--- a/modules/ftptree.py
+++ b/modules/ftptree.py
@@ -18,18 +18,18 @@ errnum = 0
 quietmode = False
 
 # what the move checks take from an rpm header; error is set when the file or its header cannot be read
-RpmInfo = collections.namedtuple('RpmInfo', 'signed name obsoletes error')
+RpmInfo = collections.namedtuple('RpmInfo', 'signed name obsoletes epoch error')
 # files a process of the header pool reads per task
 READ_CHUNK = 16
 # the transaction set of a process of the header pool
 _ts = None
 
-def _start_reader():
+def _start_reader(vsflags):
     global _ts
     # Ctrl-C is for the script: a process stopped by it while holding a lock of the pool queues hangs the rest
     signal.signal(signal.SIGINT, signal.SIG_IGN)
     _ts = rpm.ts()
-    _ts.setVSFlags(rpm.RPMVSF_NODSAHEADER)
+    _ts.setVSFlags(vsflags)
 
 def _read_rpm(path):
     """RpmInfo of the rpm file at path, or None when there is no such file"""
@@ -38,19 +38,21 @@ def _read_rpm(path):
     try:
         fdno = os.open(path, os.O_RDONLY)
     except OSError as e:
-        return RpmInfo(signed_by_key(None), None, [], f"{path}: {e.strerror}")
+        return RpmInfo(signed_by_key(None), None, [], None, f"{path}: {e.strerror}")
     try:
         hdr = _ts.hdrFromFdno(fdno)
     except rpm.error as e:
-        return RpmInfo(signed_by_key(None), None, [], f"{path}: {e}")
+        return RpmInfo(signed_by_key(None), None, [], None, f"{path}: {e}")
     finally:
         os.close(fdno)
-    return RpmInfo(signed_by_key(hdr), hdr[rpm.RPMTAG_NAME], list(hdr[rpm.RPMTAG_OBSOLETES]), None)
+    return RpmInfo(signed_by_key(hdr), hdr[rpm.RPMTAG_NAME], list(hdr[rpm.RPMTAG_OBSOLETES]), hdr[rpm.RPMTAG_EPOCH], None)
 
-def read_rpms(paths):
+def read_rpms(paths, verify = True):
     """RpmInfo of each of paths in turn (None for a missing file), read by a pool of processes
 
-    The pool stops when the last one is taken, or when the generator is closed.
+    verify=False reads headers without checking signatures, so a key missing from the rpm database
+    is no error, and signed means nothing. The pool stops when the last one is taken, or when the
+    generator is closed.
     """
     if not paths:
         return
@@ -58,7 +60,9 @@ def read_rpms(paths):
     workers = min(len(os.sched_getaffinity(0)), -(-len(paths) // READ_CHUNK))
     # not spawn or forkserver: the scripts have no __main__ guard, so they would run again
     pool = concurrent.futures.ProcessPoolExecutor(workers, mp_context=multiprocessing.get_context('fork'),
-                                                  initializer=_start_reader)
+                                                  initializer=_start_reader, initargs=(
+                                                      rpm.RPMVSF_NODSAHEADER if verify else
+                                                      rpm.RPMVSF_MASK_NOSIGNATURES | rpm.RPMVSF_MASK_NODIGESTS,))
     try:
         yield from pool.map(_read_rpm, paths, chunksize=READ_CHUNK)
     except BaseException:
@@ -365,6 +369,7 @@ class FtpTree(BaseFtpTree):
         bailoutonerror()
 
         older, other = self.__toremove(dsttree)
+        bailoutonerror()
         self.__rmolderfromsrc(older)
         self.__rmotherfromdst(other, archivetree = archivetree)
 
@@ -539,10 +544,19 @@ class FtpTree(BaseFtpTree):
     def __toremove(self, dsttree):
         """
         The older versions of marked pkgs here and their other versions in dsttree, which a move
-        removes or archives, all read first: an unreadable .info stops the move before it changes anything
+        removes or archives, all read first: an unreadable .info stops the move before it changes anything,
+        and so does a version that cannot be compared, which is an error on its pkg
         """
-        older = [] if self.treename.count("archive") else [
-            self[nvr] for pkg in self.marked4moving for nvr in self.__find_older_pkgs(pkg)]
+        older = []
+        if not self.treename.count("archive"):
+            others = {pkg: nvrs for pkg in self.marked4moving if (nvrs := self.__find_other_pkgs(pkg, self))}
+            evr, unreadable = self.evrs([nvr for pkg, nvrs in others.items() for nvr in [pkg.nvr, *nvrs]])
+            for pkg, nvrs in others.items():
+                bad = [unreadable[nvr] for nvr in [pkg.nvr, *nvrs] if nvr in unreadable]
+                for msg in bad:
+                    pkg.error(f'cannot tell which versions are older: {msg}')
+                if not bad:
+                    older += [self[nvr] for nvr in nvrs if rpm.labelCompare(evr[pkg.nvr], evr[nvr]) > 0]
         other = [] if dsttree.treename.count("archive") else [
             dsttree[nvr] for pkg in self.marked4moving for nvr in self.__find_other_pkgs(pkg, dsttree)]
         return older, other
@@ -566,13 +580,20 @@ class FtpTree(BaseFtpTree):
             return ziewre.match(x) is not None and x != pkg.nvr
         return list(filter(filter_other_pkgs, tree.pkgnames))
 
-    def __find_older_pkgs(self, pkg):
-        def filter_older_pkgs(x):
-            c = x.split('-')
-            rc = rpm.labelCompare(('0', pkg.version, pkg.release),
-                                                        ('0', c[-2], c[-1]))
-            return rc == 1 # pkg > x
-        return list(filter(filter_older_pkgs, self.__find_other_pkgs(pkg, self)))
+    def evrs(self, nvrs):
+        """
+        (epoch, version, release) of each of nvrs here, for rpm.labelCompare, with the epoch from the
+        src.rpm header, as .info files lack it; and the reason for each whose src.rpm cannot be read
+        """
+        paths = {nvr: f'{self.basedir}/SRPMS/RPMS/{nvr}.src.rpm' for nvr in nvrs}
+        info = dict(zip(paths.values(), read_rpms(list(paths.values()), verify = False), strict=True))
+        evr, unreadable = {}, {}
+        for nvr, path in paths.items():
+            if info[path] is None or info[path].error:
+                unreadable[nvr] = info[path].error if info[path] else f'{path}: no such file'
+            else:
+                evr[nvr] = (str(info[path].epoch or 0), *nvr.split('-')[-2:])
+        return evr, unreadable
 
     def __rpminfo(self, pkgs):
         """RpmInfo of every rpm file of pkgs (None for a missing one), read in parallel"""
================================================================

---- gitweb:

http://git.pld-linux.org/gitweb.cgi/projects/pld-ftp-admin.git/commitdiff/a049a512859eaede288d71649a2320e16c959476



More information about the pld-cvs-commit mailing list