-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathappfs-cache
More file actions
executable file
·209 lines (163 loc) · 4.61 KB
/
appfs-cache
File metadata and controls
executable file
·209 lines (163 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#! /usr/bin/env bash
#
# Copyright (c) 2014 Roy Keene
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
PATH="${PATH}:$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
appfsd_options=()
if [ "$1" == "--cachedir" ]; then
appfsd_options=("${appfsd_options[@]}" '--cachedir' "$2")
shift; shift;
fi
function call_appfsd() {
appfsd "${appfsd_options[@]}" "$@"
}
function invalidate() {
call_appfsd --sqlite3 'UPDATE sites SET ttl = "0";'
}
function remove_site() {
local site
site="$1"
call_appfsd --sqlite3 'DELETE FROM sites WHERE hostname = '"'$site'"'; DELETE FROM packages WHERE hostname = '"'$site'"';' || return 1
clean
}
function mirror() {
local site destinationDir
local baseURL index
local packageList
local marker
site="$1"
destinationDir="$2"
baseURL="http://${site}/appfs"
index="${baseURL}/index"
if [ ! -e "${destinationDir}" ]; then
mkdir "${destinationDir}" || return 1
fi
marker="$(openssl rand 20 -hex)"
(
cd "${destinationDir}" || exit 1
# XXX:TODO: This does not validate the signature
curl -sSL "${index}" > "index.${marker}"
packageListHash="$(cat "index.${marker}" | cut -f 1 -d ,)"
packageListHashMethod="$(cat "index.${marker}" | cut -f 2 -d ,)"
#curl -sSL "${...}"
mv "index.${marker}" index
) || return 1
return 0
}
function clean() {
call_appfsd --tcl "$(cat <<\_EOF_
unset -nocomplain row
::appfs::db eval {SELECT sha1, hostname FROM packages;} row {
set hostname [::appfs::db onecolumn {SELECT hostname FROM sites WHERE hostname = $row(hostname) LIMIT 1;}]
if {$hostname == ""} {
continue
}
set valid_sha1($row(sha1)) 1
::appfs::db eval {SELECT file_sha1 FROM files WHERE file_sha1 IS NOT NULL AND file_sha1 != '' AND package_sha1 = $row(sha1);} subrow {
set valid_sha1($subrow(file_sha1)) 1
}
}
foreach file [glob -nocomplain -tails -directory $::appfs::cachedir {[0-9a-f][0-9a-f]/*/*/*/*}] {
set sha1 [string map [list "/" "" "\\" ""] $file]
set file [file join $::appfs::cachedir $file]
if {[info exists valid_sha1($sha1)]} {
continue
}
puts "Cleaning $file"
file delete -force -- $file
}
_EOF_
)"
}
function clear() {
local package
package="$1"
if [ -n "${package}" ]; then
echo "not implemented" >&2
return 1
fi
call_appfsd --tcl 'file delete -force -- {*}[glob -directory $::appfs::cachedir {[0-9a-f][0-9a-f]}]' || return 1
call_appfsd --sqlite3 'DELETE FROM sites; DELETE FROM packages; DELETE FROM files; VACUUM;' || return 1
}
function install() {
local site packages
local package packagedir
local includeLib
includeLib='0'
if [ "$1" = '-lib' ]; then
shift
includeLib='1'
fi
site="$1"
shift
packages=("$@")
if [ -z "${site}" -o -z "${packages[*]}" ]; then
echo "usage: appfs-cache install <site> <package>..." >&2
return 1
fi
for package in "${packages[@]}"; do
packagedir="/opt/appfs/${site}/${package}/platform/latest"
## XXX:TODO: Allow installation to other locations
ln -fs "${packagedir}"/bin/* /bin/
if [ "${includeLib}" = '1' ]; then
ln -fs "${packagedir}"/lib/* /lib/
fi
done
return 0
}
operation="$1"
shift
case "$operation" in
install)
install "$@" || exit 1
exit 0
;;
invalidate)
invalidate || exit 1
;;
remove-site)
remove_site "$@" || exit 1
;;
clean)
clean || exit 1
;;
clear)
clear "$@" || exit 1
;;
hoard)
echo "not implemented" >&2
exit 1
hoard "$@" || exit 1
exit 0
;;
mirror)
echo "not implemented" >&2
exit 1
mirror "$@" || exit 1
exit 0
;;
*)
echo "Usage: appfs-cache {invalidate|clean|clear|clear <package>|remove-site <site>|hoard <package>|mirror <site> <dir>}" >&2
exit 1
;;
esac
pkill -HUP appfsd
exit 0