* run check_size_constraints independent of AUTOCLEAN (thanks Matt)
[ntk/apt.git] / debian / apt.cron.daily
CommitLineData
0c132682
MZ
1#!/bin/sh
2#
0c132682
MZ
3
4#set -e
5
05f6a46a 6check_stamp()
0c132682 7{
05f6a46a
MZ
8 stamp="$1"
9 interval="$2"
10
10946ddc
MZ
11 if [ $interval -eq 0 ]; then
12 return 1
13 fi
14
05f6a46a
MZ
15 if [ ! -f $stamp ]; then
16 return 0
0c132682 17 fi
05f6a46a
MZ
18
19 # compare midnight today to midnight the day the stamp was updated
20 stamp=$(date --date=$(date -r $stamp --iso-8601) +%s)
21 now=$(date --date=$(date --iso-8601) +%s)
22 delta=$(($now-$stamp))
05f6a46a
MZ
23
24 if [ $delta -ge $interval ]; then
25 return 0
26 fi
27
28 return 1
29}
30
31update_stamp()
32{
33 stamp="$1"
34
35 touch $stamp
0c132682
MZ
36}
37
6cce801a
MV
38
39
40# we check here if autoclean was enough sizewise
2e8a92e5 41check_size_constraints()
6cce801a
MV
42{
43 # min-age in days
44 MaxAge=0
45 MaxSize=0
46 CacheDir="var/cache/apt"
47 CacheArchive="archives/"
48 eval $(apt-config shell MaxAge APT::Archives::MaxAge)
49 eval $(apt-config shell MaxSize APT::Archives::MaxSize)
01717245 50 eval $(apt-config shell Dir Dir)
6cce801a
MV
51 eval $(apt-config shell CacheDir Dir::Cache)
52 eval $(apt-config shell CacheArchive Dir::Cache::archives)
53
54 # sanity check
55 if [ -z "$CacheDir" -o -z "$CacheArchive" ]; then
56 echo "empty Dir::Cache or Dir::Cache::archives, exiting"
57 exit
58 fi
01717245
MV
59
60 Cache="${Dir%/}/${CacheDir%/}/${CacheArchive%/}/"
6cce801a
MV
61
62 # check age
63 if [ ! $MaxAge -eq 0 ]; then
60ea46b2 64 find $Cache -name "*.deb" -mtime +$MaxAge -print0 | xargs -r -0 rm -f
6cce801a
MV
65 fi
66
67 # check size
68 if [ ! $MaxSize -eq 0 ]; then
69 # reverse-sort by mtime
70 for file in $(ls -rt $Cache/*.deb); do
71 du=$(du -s $Cache)
72 size=${du%%/*}
73 # check if the cache is small enough
74 if [ $size -lt $MaxSize ]; then
75 break
76 fi
77 # delete oldest file
78 rm -f $file
79 done
80 fi
81}
82
87ddfb96
MV
83
84# check cache size first
85check_size_constraints
86
0c132682 87UpdateInterval=0
05f6a46a 88DownloadUpgradeableInterval=0
9bd1cf87
MZ
89eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages)
90AutocleanInterval=$DownloadUpgradeableInterval
91eval $(apt-config shell AutocleanInterval APT::Periodic::Autoclean)
0c132682 92
0c132682
MZ
93# laptop check, on_ac_power returns:
94# 0 (true) System is on mains power
95# 1 (false) System is not on mains power
96# 255 (false) Power status could not be determined
97# Desktop systems always return 255 it seems
2288cf77 98if which on_ac_power >/dev/null; then
05f6a46a 99 on_ac_power
0c132682
MZ
100 if [ $? -eq 1 ]; then
101 exit 0
102 fi
103fi
104
05f6a46a
MZ
105UPDATE_STAMP=/var/lib/apt/periodic/update-stamp
106if check_stamp $UPDATE_STAMP $UpdateInterval; then
107 if apt-get -qq update 2>/dev/null; then
108 if which dbus-send >/dev/null; then
109 dbus-send --system / app.apt.dbus.updated boolean:true
110 fi
111 update_stamp $UPDATE_STAMP
112 fi
0c132682
MZ
113fi
114
05f6a46a
MZ
115DOWNLOAD_UPGRADEABLE_STAMP=/var/lib/apt/periodic/download-upgradeable-stamp
116if check_stamp $DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then
117 apt-get -qq -d dist-upgrade 2>/dev/null
118 update_stamp $DOWNLOAD_UPGRADEABLE_STAMP
0c132682 119fi
9bd1cf87
MZ
120
121AUTOCLEAN_STAMP=/var/lib/apt/periodic/autoclean-stamp
122if check_stamp $AUTOCLEAN_STAMP $AutocleanInterval; then
123 apt-get -qq autoclean
124 update_stamp $AUTOCLEAN_STAMP
125fi