]> git.saurik.com Git - apt.git/blobdiff - debian/apt.cron.daily
* run check_size_constraints independent of AUTOCLEAN (thanks Matt)
[apt.git] / debian / apt.cron.daily
index bb6c0151e71413ab7ddfae1c983781db45e9a9ff..466028ee5ba1d1a9df38b98b035c95aa990b8d3a 100644 (file)
@@ -20,7 +20,6 @@ check_stamp()
     stamp=$(date --date=$(date -r $stamp --iso-8601) +%s)
     now=$(date --date=$(date --iso-8601) +%s)
     delta=$(($now-$stamp))
-    echo "stamp=$stamp, now=$now, delta=$delta"
 
     if [ $delta -ge $interval ]; then
         return 0
@@ -36,10 +35,60 @@ update_stamp()
     touch $stamp
 }
 
+
+
+# we check here if autoclean was enough sizewise
+check_size_constraints()
+{
+    # min-age in days
+    MaxAge=0
+    MaxSize=0
+    CacheDir="var/cache/apt"
+    CacheArchive="archives/"
+    eval $(apt-config shell MaxAge APT::Archives::MaxAge)
+    eval $(apt-config shell MaxSize APT::Archives::MaxSize)
+    eval $(apt-config shell Dir Dir)
+    eval $(apt-config shell CacheDir Dir::Cache)
+    eval $(apt-config shell CacheArchive Dir::Cache::archives)
+
+    # sanity check
+    if [ -z "$CacheDir" -o -z "$CacheArchive" ]; then
+       echo "empty Dir::Cache or Dir::Cache::archives, exiting"
+       exit
+    fi
+    
+    Cache="${Dir%/}/${CacheDir%/}/${CacheArchive%/}/"
+
+    # check age
+    if [ ! $MaxAge -eq 0 ]; then
+       find $Cache -name "*.deb"  -mtime +$MaxAge -print0 | xargs -r -0 rm -f
+    fi
+    
+    # check size
+    if [ ! $MaxSize -eq 0 ]; then
+       # reverse-sort by mtime
+       for file in $(ls -rt $Cache/*.deb); do 
+           du=$(du -s $Cache)
+           size=${du%%/*}
+           # check if the cache is small enough
+           if [ $size -lt $MaxSize ]; then
+               break
+           fi
+           # delete oldest file
+           rm -f $file
+       done
+    fi
+}
+
+
+# check cache size first
+check_size_constraints
+
 UpdateInterval=0
 DownloadUpgradeableInterval=0
-RES=`apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages`
-eval $RES
+eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages)
+AutocleanInterval=$DownloadUpgradeableInterval
+eval $(apt-config shell AutocleanInterval APT::Periodic::Autoclean)
 
 # laptop check, on_ac_power returns:
 #       0 (true)    System is on mains power
@@ -68,3 +117,9 @@ if check_stamp $DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then
     apt-get -qq -d dist-upgrade 2>/dev/null
     update_stamp $DOWNLOAD_UPGRADEABLE_STAMP
 fi
+
+AUTOCLEAN_STAMP=/var/lib/apt/periodic/autoclean-stamp
+if check_stamp $AUTOCLEAN_STAMP $AutocleanInterval; then
+    apt-get -qq autoclean
+    update_stamp $AUTOCLEAN_STAMP
+fi