tags 1085265 + patch
thanks

I've taken these patches, added a few more, and created a merge request at:

    https://salsa.debian.org/debian/local-apt-repository/-/merge_requests/2

I've also attached the same series of patches to this message.

--
Darsey Litzenberger <dl...@dlitz.net>
>From 77f2569ca6259a22b86c1d058f979743bb78ee48 Mon Sep 17 00:00:00 2001
From: mike <okgomdjgbm...@gmail.com>
Date: Sun, 20 Oct 2024 12:21:52 +0200
Subject: [PATCH 1/7] use caching (Closes: #1085265)

Committer's note (dlitz):

Patch imported into git by applying the contents of the "rebuild" file
submitted in the bug report to the "rebuild" file from version 0.9 of
this package, then rebasing onto the latest master.  Commit attributed
to its original author.

Link: https://bugs.debian.org/1085265
Link: https://bugs.debian.org/cgi-bin/bugreport.cgi?att=1;bug=1085265;filename=rebuild;msg=15
Commit-message-by: Darsey Litzenberger <dl...@dlitz.net>
---
 rebuild | 20 +++-----------------
 1 file changed, 3 insertions(+), 17 deletions(-)

diff --git a/rebuild b/rebuild
index 7f55e84..9afa60c 100755
--- a/rebuild
+++ b/rebuild
@@ -2,12 +2,6 @@
 
 set -e
 
-force=no
-if [ "$1" = "-f" ]
-then
-  force=yes
-fi
-
 DEBS=/srv/local-apt-repository
 REPO=/var/lib/local-apt-repository
 
@@ -24,10 +18,6 @@ else
   # and restart. But lets bound this to 10 runs.
   for n in $(seq 0 10)
   do
-    if [ "$force" = "yes" ] || ! test -e  $REPO/stamp || find $DEBS/ -newer $REPO/stamp -print -quit | fgrep -q ''
-    then
-      # we need to rebuild
-
       # This is the second round alreay, lets wait a while
       if [ "$n" != "0" ]
       then
@@ -35,18 +25,14 @@ else
         sleep 10
       fi
 
-      touch $REPO/stamp
-
       # Relative paths work better than absolute
       (cd $REPO
-      apt-ftparchive packages ../../../$DEBS > $REPO/Packages
-      apt-ftparchive sources ../../../$DEBS > $REPO/Sources
-      ) || true
+      apt-ftparchive packages --db cache.db ../../../$DEBS > $REPO/Packages
+      apt-ftparchive sources --db cache.db ../../../$DEBS > $REPO/Sources
+      ) && break || true
       # ^ this can fail during a partial write to the directory (which
       #   would be detected by the loop), so ignore errors here
 
-      force=no
-    fi
   done
 
 fi
-- 
2.50.0

>From 61cacc7eb9e687ad6a69996edde468ced5a54b36 Mon Sep 17 00:00:00 2001
From: Darsey Litzenberger <dl...@dlitz.net>
Date: Mon, 21 Jul 2025 10:41:23 -0600
Subject: [PATCH 2/7] Generate Contents files for each architecture configured
 in dpkg

This also removes stale Contents files for architectures that have been
removed using `dpkg --remove-architecture`.
---
 rebuild | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/rebuild b/rebuild
index 9afa60c..f648753 100755
--- a/rebuild
+++ b/rebuild
@@ -10,6 +10,7 @@ then
   # We still need ot create the files lest apt will complain
   > $REPO/Packages
   > $REPO/Sources
+  rm -f "$REPO"/Contents-*
 
 else
 
@@ -29,6 +30,12 @@ else
       (cd $REPO
       apt-ftparchive packages --db cache.db ../../../$DEBS > $REPO/Packages
       apt-ftparchive sources --db cache.db ../../../$DEBS > $REPO/Sources
+      rm -f "$REPO"/Contents-*
+      for arch in $(dpkg --print-architecture) $(dpkg --print-foreign-architectures)
+      do
+        apt-ftparchive contents --db cache.db --arch "$arch" \
+          ../../../"$DEBS" > "$REPO/Contents-$arch"
+      done
       ) && break || true
       # ^ this can fail during a partial write to the directory (which
       #   would be detected by the loop), so ignore errors here
-- 
2.50.0

>From fcb9f8715abe17c0f4549542c3d9864e36994764 Mon Sep 17 00:00:00 2001
From: Darsey Litzenberger <dl...@dlitz.net>
Date: Mon, 21 Jul 2025 10:41:10 -0600
Subject: [PATCH 3/7] Put cache into /var/cache to comply with FHS

Note that we only remove the cache when the package is purged (not just
removed or deconfigured), since regenerating the cache can take a long
time with a large local repository.
---
 debian/local-apt-repository.dirs   |  3 +++
 debian/local-apt-repository.postrm | 22 ++++++++++++++++++++++
 rebuild                            |  7 ++++---
 3 files changed, 29 insertions(+), 3 deletions(-)
 create mode 100644 debian/local-apt-repository.postrm

diff --git a/debian/local-apt-repository.dirs b/debian/local-apt-repository.dirs
index 80e3bb0..604ce98 100644
--- a/debian/local-apt-repository.dirs
+++ b/debian/local-apt-repository.dirs
@@ -8,3 +8,6 @@
 
 # This is for the actual generated repository
 /var/lib/local-apt-repository
+
+# This is for the cache database
+/var/cache/local-apt-repository
diff --git a/debian/local-apt-repository.postrm b/debian/local-apt-repository.postrm
new file mode 100644
index 0000000..0405840
--- /dev/null
+++ b/debian/local-apt-repository.postrm
@@ -0,0 +1,22 @@
+#!/bin/sh
+
+set -e
+
+
+case "$1" in
+  purge)
+    rm -rf /var/cache/local-apt-repository
+    ;;
+
+  remove|upgrade|failed-upgrade|abort-install|abort-upgrade|disappear)
+    ;;
+
+  *)
+    echo "postrm called with unknown argument '$1'" >&2
+    exit 0
+    ;;
+esac
+
+#DEBHELPER#
+
+exit 0
diff --git a/rebuild b/rebuild
index f648753..694491b 100755
--- a/rebuild
+++ b/rebuild
@@ -4,6 +4,7 @@ set -e
 
 DEBS=/srv/local-apt-repository
 REPO=/var/lib/local-apt-repository
+CACHE=/var/cache/local-apt-repository
 
 if ! test -d $DEBS
 then
@@ -28,12 +29,12 @@ else
 
       # Relative paths work better than absolute
       (cd $REPO
-      apt-ftparchive packages --db cache.db ../../../$DEBS > $REPO/Packages
-      apt-ftparchive sources --db cache.db ../../../$DEBS > $REPO/Sources
+      apt-ftparchive packages --db "$CACHE/cache.db" ../../../"$DEBS" > "$REPO/Packages"
+      apt-ftparchive sources --db "$CACHE/cache.db" ../../../"$DEBS" > "$REPO/Sources"
       rm -f "$REPO"/Contents-*
       for arch in $(dpkg --print-architecture) $(dpkg --print-foreign-architectures)
       do
-        apt-ftparchive contents --db cache.db --arch "$arch" \
+        apt-ftparchive contents --db "$CACHE/cache.db" --arch "$arch" \
           ../../../"$DEBS" > "$REPO/Contents-$arch"
       done
       ) && break || true
-- 
2.50.0

>From f7422f53b6006252d2dc9c546bee9bf5d39333d7 Mon Sep 17 00:00:00 2001
From: Darsey Litzenberger <dl...@dlitz.net>
Date: Mon, 21 Jul 2025 10:51:04 -0600
Subject: [PATCH 4/7] Update debian/copyright

---
 debian/copyright | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/debian/copyright b/debian/copyright
index 2eb9710..b37f29e 100644
--- a/debian/copyright
+++ b/debian/copyright
@@ -4,8 +4,10 @@ Files: *
 Copyright: 2015-2017 Joachim Breitner <m...@joachim-breitner.de>
            2015 Felipe Sateler <fsate...@debian.org>
            2024 Petter Reinholdtsen <p...@hungry.com>
+           2024 mike <okgomdjgbm...@gmail.com>
            2025 NoisyCoil <noisyc...@tutanota.com>
            2025 Jakob Haufe <su...@debian.org>
+           2025 Darsey Litzenberger <dl...@dlitz.net>
 License: MIT
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
-- 
2.50.0

>From f9b9d988de60932088ee1bcfd4c0bfa32d06b045 Mon Sep 17 00:00:00 2001
From: Darsey Litzenberger <dl...@dlitz.net>
Date: Mon, 21 Jul 2025 14:39:34 -0600
Subject: [PATCH 5/7] Add a message during upgrade that this might take longer
 than usual.

---
 debian/local-apt-repository.postinst | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/debian/local-apt-repository.postinst b/debian/local-apt-repository.postinst
index e458d31..329880c 100644
--- a/debian/local-apt-repository.postinst
+++ b/debian/local-apt-repository.postinst
@@ -8,6 +8,9 @@ case "$1" in
     if dpkg --compare-versions -- "$2" le-nl "0.10~"; then
       rm -f /etc/apt/sources.list.d/local-apt-repository.list
     fi
+    if dpkg --compare-versions -- "$2" le-nl "0.11~"; then
+      echo "Indexing package contents for the first time.  This may take a while."
+    fi
     /usr/lib/local-apt-repository/rebuild -f
     ln -fs /usr/lib/local-apt-repository/local-apt-repository.sources /etc/apt/sources.list.d/local-apt-repository.sources
   ;;
-- 
2.50.0

>From 553f61e5f918727a423e12558a03e5778cdfa01a Mon Sep 17 00:00:00 2001
From: Darsey Litzenberger <dl...@dlitz.net>
Date: Mon, 21 Jul 2025 13:48:25 -0600
Subject: [PATCH 6/7] Fix several error cases; improve comments

Most of the bugs fixed here are related to bash's inconsistent errexit
behavior.  In bash, "set -e" (errexit) has no effect when a conditional
is being evaluated on a compound statement.  For example:

    # The following code prints "after false" and succeeds.
    set -e
    ( false
      echo after false
    ) || false

So, errors went uncaught if apt-ftparchive failed while generating
$REPO/Packages but succeeded while generating $REPO/Sources, or if dpkg
failed to print the architecture list for some reason.

The script might also have broken if apt-ftparchive generated partial
output and the 10-retry limit was exceeded.
---
 rebuild | 68 ++++++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 45 insertions(+), 23 deletions(-)

diff --git a/rebuild b/rebuild
index 694491b..053cc9d 100755
--- a/rebuild
+++ b/rebuild
@@ -1,52 +1,74 @@
 #!/bin/bash
 
-set -e
+set -eu
 
 DEBS=/srv/local-apt-repository
 REPO=/var/lib/local-apt-repository
 CACHE=/var/cache/local-apt-repository
+ARCHS="$(dpkg --print-architecture && dpkg --print-foreign-architectures)"
 
-if ! test -d $DEBS
-then
-  # We still need ot create the files lest apt will complain
-  > $REPO/Packages
-  > $REPO/Sources
-  rm -f "$REPO"/Contents-*
+generate_index_files() {
+  # We need "|| return" after every command in this function, because "set -e"
+  # has no effect inside a conditional context.
+  # See https://mywiki.wooledge.org/BashPitfalls#errexit
+
+  # This function assumes that we are already inside the REPO directory.
+
+  apt-ftparchive packages --db "$CACHE/cache.db" ../../../"$DEBS" > Packages || return
+  apt-ftparchive sources --db "$CACHE/cache.db" ../../../"$DEBS" > Sources || return
+
+  rm -f Contents-* || return
+
+  for arch in $ARCHS
+  do
+    apt-ftparchive contents --db "$CACHE/cache.db" \
+      --arch "$arch" ../../../"$DEBS" > "Contents-$arch" || return
+  done
+}
+
+# We want apt-ftparchive to generate files with relative paths, and we want
+# this command to fail if the REPO directory does not exist, so change
+# directories before doing anything else.
+cd "$REPO"
 
-else
+success=
 
+if test -d "$DEBS"
+then
   # We want to cater for the possibility that something is added to $DEBS as we
   # run, or that a file is slowly written. In this case, we want to wait a bit
   # and restart. But lets bound this to 10 runs.
   for n in $(seq 0 10)
   do
-      # This is the second round alreay, lets wait a while
+      # This is the second round already; let's wait a while.
       if [ "$n" != "0" ]
       then
         echo "Further changes are coming in, waiting..."
         sleep 10
       fi
 
-      # Relative paths work better than absolute
-      (cd $REPO
-      apt-ftparchive packages --db "$CACHE/cache.db" ../../../"$DEBS" > "$REPO/Packages"
-      apt-ftparchive sources --db "$CACHE/cache.db" ../../../"$DEBS" > "$REPO/Sources"
-      rm -f "$REPO"/Contents-*
-      for arch in $(dpkg --print-architecture) $(dpkg --print-foreign-architectures)
-      do
-        apt-ftparchive contents --db "$CACHE/cache.db" --arch "$arch" \
-          ../../../"$DEBS" > "$REPO/Contents-$arch"
-      done
-      ) && break || true
-      # ^ this can fail during a partial write to the directory (which
-      #   would be detected by the loop), so ignore errors here
+      # apt-ftparchive can fail during a partial write to the directory, so
+      # repeat the loop on any errors.
+      generate_index_files || continue
 
+      success=1
+      break
   done
+fi
 
+if ! [ "$success" ]
+then
+  # We still need to create the files lest apt will complain
+  > Packages
+  > Sources
+  rm -f Contents-*
 fi
 
 apt-ftparchive \
 	-o "APT::FTPArchive::Release::Origin=local-apt-repository" \
 	-o "APT::FTPArchive::Release::Description=Local repository created by local-apt-repository" \
-	release $REPO > $REPO/Release
+	release . > Release
 
+if ! [ "$success" ]; then
+  exit 1
+fi
-- 
2.50.0

>From f524bbc59adc8b15bf73b26b580c8316e229e342 Mon Sep 17 00:00:00 2001
From: Darsey Litzenberger <dl...@dlitz.net>
Date: Mon, 21 Jul 2025 09:36:55 -0600
Subject: [PATCH 7/7] Add debhelper tempfiles to .gitignore

Gbp-Dch: ignore
---
 .gitignore | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/.gitignore b/.gitignore
index 752af79..b1eda3a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,6 @@
+debian/*.debhelper
+debian/.debhelper/generated/
+debian/debhelper-build-stamp
 debian/files
 debian/local-apt-repository.debhelper.log
 debian/local-apt-repository.postinst.debhelper
-- 
2.50.0

Reply via email to