]>
Commit | Line | Data |
---|---|---|
0c132682 MZ |
1 | #!/bin/sh |
2 | # | |
0c132682 MZ |
3 | |
4 | #set -e | |
8a139d4d MV |
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 | # | |
0c132682 | 32 | |
05f6a46a | 33 | check_stamp() |
0c132682 | 34 | { |
05f6a46a MZ |
35 | stamp="$1" |
36 | interval="$2" | |
37 | ||
10946ddc MZ |
38 | if [ $interval -eq 0 ]; then |
39 | return 1 | |
40 | fi | |
41 | ||
05f6a46a MZ |
42 | if [ ! -f $stamp ]; then |
43 | return 0 | |
0c132682 | 44 | fi |
05f6a46a MZ |
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)) | |
05f6a46a | 50 | |
8a139d4d MV |
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 | ||
05f6a46a MZ |
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 | |
0c132682 MZ |
68 | } |
69 | ||
6cce801a MV |
70 | |
71 | ||
72 | # we check here if autoclean was enough sizewise | |
2e8a92e5 | 73 | check_size_constraints() |
6cce801a MV |
74 | { |
75 | # min-age in days | |
76 | MaxAge=0 | |
89eaeb44 | 77 | MinAge=2 |
6cce801a MV |
78 | MaxSize=0 |
79 | CacheDir="var/cache/apt" | |
80 | CacheArchive="archives/" | |
81 | eval $(apt-config shell MaxAge APT::Archives::MaxAge) | |
8a139d4d | 82 | eval $(apt-config shell MinAge APT::Archives::MinAge) |
6cce801a | 83 | eval $(apt-config shell MaxSize APT::Archives::MaxSize) |
01717245 | 84 | eval $(apt-config shell Dir Dir) |
6cce801a MV |
85 | eval $(apt-config shell CacheDir Dir::Cache) |
86 | eval $(apt-config shell CacheArchive Dir::Cache::archives) | |
87 | ||
88 | # sanity check | |
89 | if [ -z "$CacheDir" -o -z "$CacheArchive" ]; then | |
90 | echo "empty Dir::Cache or Dir::Cache::archives, exiting" | |
91 | exit | |
92 | fi | |
01717245 MV |
93 | |
94 | Cache="${Dir%/}/${CacheDir%/}/${CacheArchive%/}/" | |
6cce801a MV |
95 | |
96 | # check age | |
8a139d4d | 97 | if [ ! $MaxAge -eq 0 ] && [ ! $MinAge -eq 0 ]; then |
8e29e348 | 98 | find $Cache -name "*.deb" \( -mtime +$MaxAge -and -ctime +$MaxAge \) -and -not \( -mtime -$MinAge -or -ctime -$MinAge \) -print0 | xargs -r -0 rm -f |
8a139d4d | 99 | elif [ ! $MaxAge -eq 0 ]; then |
8e29e348 | 100 | find $Cache -name "*.deb" -ctime +$MaxAge -and -mtime +$MaxAge -print0 | xargs -r -0 rm -f |
6cce801a MV |
101 | fi |
102 | ||
103 | # check size | |
104 | if [ ! $MaxSize -eq 0 ]; then | |
8a139d4d MV |
105 | # maxSize is in MB |
106 | MaxSize=$(($MaxSize*1024)) | |
107 | ||
108 | #get current time | |
109 | now=$(date --date=$(date --iso-8601) +%s) | |
110 | MinAge=$(($MinAge*24*60*60)) | |
111 | ||
6cce801a | 112 | # reverse-sort by mtime |
3408b58c | 113 | for file in $(ls -rt $Cache/*.deb 2>/dev/null); do |
6cce801a MV |
114 | du=$(du -s $Cache) |
115 | size=${du%%/*} | |
116 | # check if the cache is small enough | |
117 | if [ $size -lt $MaxSize ]; then | |
118 | break | |
119 | fi | |
8a139d4d MV |
120 | |
121 | # check for MinAge of the file | |
122 | if [ ! $MinAge -eq 0 ]; then | |
8e29e348 | 123 | # check both ctime and mtime |
43835916 | 124 | mtime=$(stat -c %Y $file) |
8e29e348 MV |
125 | ctime=$(stat -c %Z $file) |
126 | if [ $mtime -gt $ctime ]; then | |
127 | delta=$(($now-$mtime)) | |
128 | else | |
129 | delta=$(($now-$ctime)) | |
130 | fi | |
8a139d4d MV |
131 | #echo "$file ($delta), $MinAge" |
132 | if [ $delta -le $MinAge ]; then | |
133 | #echo "Skiping $file (delta=$delta)" | |
3408b58c | 134 | break |
8a139d4d MV |
135 | fi |
136 | fi | |
137 | ||
6cce801a MV |
138 | # delete oldest file |
139 | rm -f $file | |
140 | done | |
141 | fi | |
142 | } | |
143 | ||
87ddfb96 | 144 | |
0c132682 | 145 | UpdateInterval=0 |
05f6a46a | 146 | DownloadUpgradeableInterval=0 |
9bd1cf87 MZ |
147 | eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages) |
148 | AutocleanInterval=$DownloadUpgradeableInterval | |
149 | eval $(apt-config shell AutocleanInterval APT::Periodic::Autoclean) | |
0c132682 | 150 | |
0c132682 MZ |
151 | # laptop check, on_ac_power returns: |
152 | # 0 (true) System is on mains power | |
153 | # 1 (false) System is not on mains power | |
154 | # 255 (false) Power status could not be determined | |
155 | # Desktop systems always return 255 it seems | |
2288cf77 | 156 | if which on_ac_power >/dev/null; then |
05f6a46a | 157 | on_ac_power |
0c132682 MZ |
158 | if [ $? -eq 1 ]; then |
159 | exit 0 | |
160 | fi | |
161 | fi | |
162 | ||
05f6a46a MZ |
163 | UPDATE_STAMP=/var/lib/apt/periodic/update-stamp |
164 | if check_stamp $UPDATE_STAMP $UpdateInterval; then | |
165 | if apt-get -qq update 2>/dev/null; then | |
166 | if which dbus-send >/dev/null; then | |
167 | dbus-send --system / app.apt.dbus.updated boolean:true | |
168 | fi | |
169 | update_stamp $UPDATE_STAMP | |
170 | fi | |
0c132682 MZ |
171 | fi |
172 | ||
05f6a46a MZ |
173 | DOWNLOAD_UPGRADEABLE_STAMP=/var/lib/apt/periodic/download-upgradeable-stamp |
174 | if check_stamp $DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then | |
175 | apt-get -qq -d dist-upgrade 2>/dev/null | |
176 | update_stamp $DOWNLOAD_UPGRADEABLE_STAMP | |
0c132682 | 177 | fi |
9bd1cf87 MZ |
178 | |
179 | AUTOCLEAN_STAMP=/var/lib/apt/periodic/autoclean-stamp | |
180 | if check_stamp $AUTOCLEAN_STAMP $AutocleanInterval; then | |
181 | apt-get -qq autoclean | |
182 | update_stamp $AUTOCLEAN_STAMP | |
183 | fi | |
47536509 MV |
184 | |
185 | # check cache size | |
186 | check_size_constraints |