]> git.saurik.com Git - apt.git/blob - debian/apt.cron.daily
24ad6855a6e1f246bb2ffed6f08a64dab597a30f
[apt.git] / debian / apt.cron.daily
1 #!/bin/sh
2 #
3
4 #set -e
5 #
6 # This file understands the following apt configuration variables:
7 #
8 # "APT::Periodic::Update-Package-Lists=1"
9 # - Do "apt-get update" automatically every n-days (0=disable)
10 #
11 # "APT::Periodic::Download-Upgradeable-Packages=0",
12 # - Do "apt-get upgrade --download-only" every n-days (0=disable)
13 #
14 # "APT::Periodic::AutocleanInterval"
15 # - Do "apt-get autoclean" every n-days (0=disable)
16 #
17 # "APT::Archives::MaxAge",
18 # - Set maximum allowed age of a cache package file. If a cache
19 # package file is older it is deleted (0=disable)
20 #
21 # "APT::Archives::MaxSize",
22 # - Set maximum size of the cache in MB (0=disable). If the cache
23 # is bigger, cached package files are deleted until the size
24 # requirement is met (the biggest packages will be deleted
25 # first).
26 #
27 # "APT::Archives::MinAge"
28 # - Set minimum age of a package file. If a file is younger it
29 # will not be deleted (0=disable). Usefull to prevent races
30 # and to keep backups of the packages for emergency.
31 #
32
33 check_stamp()
34 {
35 stamp="$1"
36 interval="$2"
37
38 if [ $interval -eq 0 ]; then
39 return 1
40 fi
41
42 if [ ! -f $stamp ]; then
43 return 0
44 fi
45
46 # compare midnight today to midnight the day the stamp was updated
47 stamp=$(date --date=$(date -r $stamp --iso-8601) +%s)
48 now=$(date --date=$(date --iso-8601) +%s)
49 delta=$(($now-$stamp))
50
51 # intervall is in days,
52 interval=$(($interval*60*60*24))
53 #echo "stampfile: $1"
54 #echo "interval=$interval, now=$now, stamp=$stamp, delta=$delta"
55
56 if [ $delta -ge $interval ]; then
57 return 0
58 fi
59
60 return 1
61 }
62
63 update_stamp()
64 {
65 stamp="$1"
66
67 touch $stamp
68 }
69
70
71
72 # we check here if autoclean was enough sizewise
73 check_size_constraints()
74 {
75 # min-age in days
76 MaxAge=0
77 MaxSize=0
78 CacheDir="var/cache/apt"
79 CacheArchive="archives/"
80 eval $(apt-config shell MaxAge APT::Archives::MaxAge)
81 eval $(apt-config shell MaxSize APT::Archives::MaxSize)
82 eval $(apt-config shell CacheDir Dir::Cache)
83 eval $(apt-config shell CacheArchive Dir::Cache::archives)
84
85 # sanity check
86 if [ -z "$CacheDir" -o -z "$CacheArchive" ]; then
87 echo "empty Dir::Cache or Dir::Cache::archives, exiting"
88 exit
89 fi
90 Cache="/"$CacheDir$CacheArchive
91
92 # check age
93 if [ ! $MaxAge -eq 0 ]; then
94 find $Cache -name "*.deb" -mtime +$MaxAge -print0 | xargs -r -0 rm -f
95 fi
96
97 # check size
98 if [ ! $MaxSize -eq 0 ]; then
99 # reverse-sort by mtime
100 for file in $(ls -rt $Cache/*.deb); do
101 du=$(du -s $Cache)
102 size=${du%%/*}
103 # check if the cache is small enough
104 if [ $size -lt $MaxSize ]; then
105 break
106 fi
107 # delete oldest file
108 rm -f $file
109 done
110 fi
111 }
112
113 check_size_constraints
114
115
116
117 # we check here if autoclean was enough sizewise
118 check_size_constraints()
119 {
120 # min-age in days
121 MaxAge=0
122 MinAge=2
123 MaxSize=0
124 CacheDir="var/cache/apt"
125 CacheArchive="archives/"
126 eval $(apt-config shell MaxAge APT::Archives::MaxAge)
127 eval $(apt-config shell MinAge APT::Archives::MinAge)
128 eval $(apt-config shell MaxSize APT::Archives::MaxSize)
129 eval $(apt-config shell Dir Dir)
130 eval $(apt-config shell CacheDir Dir::Cache)
131 eval $(apt-config shell CacheArchive Dir::Cache::archives)
132
133 # sanity check
134 if [ -z "$CacheDir" -o -z "$CacheArchive" ]; then
135 echo "empty Dir::Cache or Dir::Cache::archives, exiting"
136 exit
137 fi
138
139 Cache="${Dir%/}/${CacheDir%/}/${CacheArchive%/}/"
140
141 # check age
142 if [ ! $MaxAge -eq 0 ] && [ ! $MinAge -eq 0 ]; then
143 find $Cache -name "*.deb" -mtime +$MaxAge -and -not -mtime -$MinAge -print0 | xargs -r -0 rm -f
144 elif [ ! $MaxAge -eq 0 ]; then
145 find $Cache -name "*.deb" -mtime +$MaxAge -print0 | xargs -r -0 rm -f
146 fi
147
148 # check size
149 if [ ! $MaxSize -eq 0 ]; then
150 # maxSize is in MB
151 MaxSize=$(($MaxSize*1024))
152
153 #get current time
154 now=$(date --date=$(date --iso-8601) +%s)
155 MinAge=$(($MinAge*24*60*60))
156
157 # reverse-sort by mtime
158 for file in $(ls -rt $Cache/*.deb 2>/dev/null); do
159 du=$(du -s $Cache)
160 size=${du%%/*}
161 # check if the cache is small enough
162 if [ $size -lt $MaxSize ]; then
163 break
164 fi
165
166 # check for MinAge of the file
167 if [ ! $MinAge -eq 0 ]; then
168 mtime=$(date --date=$(date -r $file --iso-8601) +%s)
169 delta=$(($now-$mtime))
170 #echo "$file ($delta), $MinAge"
171 if [ $delta -le $MinAge ]; then
172 #echo "Skiping $file (delta=$delta)"
173 break
174 fi
175 fi
176
177 # delete oldest file
178 rm -f $file
179 done
180 fi
181 }
182
183
184 UpdateInterval=0
185 DownloadUpgradeableInterval=0
186 eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages)
187 AutocleanInterval=$DownloadUpgradeableInterval
188 eval $(apt-config shell AutocleanInterval APT::Periodic::Autoclean)
189
190 # laptop check, on_ac_power returns:
191 # 0 (true) System is on mains power
192 # 1 (false) System is not on mains power
193 # 255 (false) Power status could not be determined
194 # Desktop systems always return 255 it seems
195 if which on_ac_power >/dev/null; then
196 on_ac_power
197 if [ $? -eq 1 ]; then
198 exit 0
199 fi
200 fi
201
202 UPDATE_STAMP=/var/lib/apt/periodic/update-stamp
203 if check_stamp $UPDATE_STAMP $UpdateInterval; then
204 if apt-get -qq update 2>/dev/null; then
205 if which dbus-send >/dev/null; then
206 dbus-send --system / app.apt.dbus.updated boolean:true
207 fi
208 update_stamp $UPDATE_STAMP
209 fi
210 fi
211
212 DOWNLOAD_UPGRADEABLE_STAMP=/var/lib/apt/periodic/download-upgradeable-stamp
213 if check_stamp $DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then
214 apt-get -qq -d dist-upgrade 2>/dev/null
215 update_stamp $DOWNLOAD_UPGRADEABLE_STAMP
216 fi
217
218 AUTOCLEAN_STAMP=/var/lib/apt/periodic/autoclean-stamp
219 if check_stamp $AUTOCLEAN_STAMP $AutocleanInterval; then
220 apt-get -qq autoclean
221 check_size_contrains
222 update_stamp $AUTOCLEAN_STAMP
223 fi
224
225 # check cache size
226 check_size_constraints