* check cache size after AUTOCLEAN was run (but independent)
[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 83
0c132682 84UpdateInterval=0
05f6a46a 85DownloadUpgradeableInterval=0
9bd1cf87
MZ
86eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages)
87AutocleanInterval=$DownloadUpgradeableInterval
88eval $(apt-config shell AutocleanInterval APT::Periodic::Autoclean)
0c132682 89
0c132682
MZ
90# laptop check, on_ac_power returns:
91# 0 (true) System is on mains power
92# 1 (false) System is not on mains power
93# 255 (false) Power status could not be determined
94# Desktop systems always return 255 it seems
2288cf77 95if which on_ac_power >/dev/null; then
05f6a46a 96 on_ac_power
0c132682
MZ
97 if [ $? -eq 1 ]; then
98 exit 0
99 fi
100fi
101
05f6a46a
MZ
102UPDATE_STAMP=/var/lib/apt/periodic/update-stamp
103if check_stamp $UPDATE_STAMP $UpdateInterval; then
104 if apt-get -qq update 2>/dev/null; then
105 if which dbus-send >/dev/null; then
106 dbus-send --system / app.apt.dbus.updated boolean:true
107 fi
108 update_stamp $UPDATE_STAMP
109 fi
0c132682
MZ
110fi
111
05f6a46a
MZ
112DOWNLOAD_UPGRADEABLE_STAMP=/var/lib/apt/periodic/download-upgradeable-stamp
113if check_stamp $DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then
114 apt-get -qq -d dist-upgrade 2>/dev/null
115 update_stamp $DOWNLOAD_UPGRADEABLE_STAMP
0c132682 116fi
9bd1cf87
MZ
117
118AUTOCLEAN_STAMP=/var/lib/apt/periodic/autoclean-stamp
119if check_stamp $AUTOCLEAN_STAMP $AutocleanInterval; then
120 apt-get -qq autoclean
121 update_stamp $AUTOCLEAN_STAMP
122fi
47536509
MV
123
124# check cache size
125check_size_constraints