Diff

Differences From Artifact [8c72230216]:

To Artifact [1d195c7296]:


1
2



























































3
4


5


6



7
8
9

10
11
12

13
14
15
16
17
18

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


+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+


+
+
-
+
+

+
+
+

-
-
+


-
+





-
+
#! /usr/bin/env bash

function invalidate() {
	appfsd -sqlite3 'UPDATE sites SET ttl = "0";'
}

function remove_site() {
	local site

	site="$1"

	appfsd -sqlite3 'DELETE FROM sites WHERE hostname = '"'$site'"'; DELETE FROM packages WHERE hostname = '"'$site'"';' || return 1

	clean
}

function clean() {
	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

	appfsd -tcl 'file delete -force -- {*}[glob -directory $::appfs::cachedir {[0-9a-f][0-9a-f]}]' || return 1
	appfsd -sqlite3 'DELETE FROM sites; DELETE FROM packages; DELETE FROM files; VACUUM;' || return 1
}

case "$1" in
	invalidate)
		invalidate || exit 1
		;;
		statement='UPDATE sites SET ttl = "0";'
	remove-site)
		remove_site "$2" || exit 1
		;;
	clean)
		clean || exit 1
		;;
	clear)
		rm -rf 
		statement='DELETE FROM sites; DELETE FROM packages; DELETE FROM files;'
		clear "$2" || exit 1
		;;
	*)
		echo "Usage: appfs-cache {invalidate|clear}" >&2
		echo "Usage: appfs-cache {invalidate|clean|clear|clear <package>|remove-site <site>}" >&2

		exit 1
		;;
esac

exec appfsd -sqlite3 "${statement}"
exit 0