* check cache size after AUTOCLEAN was run (but independent)
[ntk/apt.git] / debian / apt.cron.daily
1 #!/bin/sh
2 #
3
4 #set -e
5
6 check_stamp()
7 {
8 stamp="$1"
9 interval="$2"
10
11 if [ $interval -eq 0 ]; then
12 return 1
13 fi
14
15 if [ ! -f $stamp ]; then
16 return 0
17 fi
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))
23
24 if [ $delta -ge $interval ]; then
25 return 0
26 fi
27
28 return 1
29 }
30
31 update_stamp()
32 {
33 stamp="$1"
34
35 touch $stamp
36 }
37
38
39
40 # we check here if autoclean was enough sizewise
41 check_size_constraints()
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)
50 eval $(apt-config shell Dir Dir)
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
59
60 Cache="${Dir%/}/${CacheDir%/}/${CacheArchive%/}/"
61
62 # check age
63 if [ ! $MaxAge -eq 0 ]; then
64 find $Cache -name "*.deb" -mtime +$MaxAge -print0 | xargs -r -0 rm -f
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
83
84 UpdateInterval=0
85 DownloadUpgradeableInterval=0
86 eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages)
87 AutocleanInterval=$DownloadUpgradeableInterval
88 eval $(apt-config shell AutocleanInterval APT::Periodic::Autoclean)
89
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
95 if which on_ac_power >/dev/null; then
96 on_ac_power
97 if [ $? -eq 1 ]; then
98 exit 0
99 fi
100 fi
101
102 UPDATE_STAMP=/var/lib/apt/periodic/update-stamp
103 if 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
110 fi
111
112 DOWNLOAD_UPGRADEABLE_STAMP=/var/lib/apt/periodic/download-upgradeable-stamp
113 if check_stamp $DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then
114 apt-get -qq -d dist-upgrade 2>/dev/null
115 update_stamp $DOWNLOAD_UPGRADEABLE_STAMP
116 fi
117
118 AUTOCLEAN_STAMP=/var/lib/apt/periodic/autoclean-stamp
119 if check_stamp $AUTOCLEAN_STAMP $AutocleanInterval; then
120 apt-get -qq autoclean
121 update_stamp $AUTOCLEAN_STAMP
122 fi
123
124 # check cache size
125 check_size_constraints