Diff

Differences From Artifact [8c72230216]:

To Artifact [1d195c7296]:


     1      1   #! /usr/bin/env bash
            2  +
            3  +function invalidate() {
            4  +	appfsd -sqlite3 'UPDATE sites SET ttl = "0";'
            5  +}
            6  +
            7  +function remove_site() {
            8  +	local site
            9  +
           10  +	site="$1"
           11  +
           12  +	appfsd -sqlite3 'DELETE FROM sites WHERE hostname = '"'$site'"'; DELETE FROM packages WHERE hostname = '"'$site'"';' || return 1
           13  +
           14  +	clean
           15  +}
           16  +
           17  +function clean() {
           18  +	appfsd -tcl "$(cat <<\_EOF_
           19  +		unset -nocomplain row
           20  +		::appfs::db eval {SELECT sha1, hostname FROM packages;} row {
           21  +			set hostname [::appfs::db onecolumn {SELECT hostname FROM sites WHERE hostname = $row(hostname) LIMIT 1;}]
           22  +			if {$hostname == ""} {
           23  +				continue
           24  +			}
           25  +
           26  +			set valid_sha1($row(sha1)) 1
           27  +			::appfs::db eval {SELECT file_sha1 FROM files WHERE file_sha1 IS NOT NULL AND file_sha1 != '' AND package_sha1 = $row(sha1);} subrow {
           28  +				set valid_sha1($subrow(file_sha1)) 1
           29  +			}
           30  +		}
           31  +
           32  +		foreach file [glob -nocomplain -tails -directory $::appfs::cachedir {[0-9a-f][0-9a-f]/*/*/*/*}] {
           33  +			set sha1 [string map [list "/" "" "\\" ""] $file]
           34  +			set file [file join $::appfs::cachedir $file]
           35  +
           36  +			if {[info exists valid_sha1($sha1)]} {
           37  +				continue
           38  +			}
           39  +
           40  +			puts "Cleaning $file"
           41  +			file delete -force -- $file
           42  +		}
           43  +_EOF_
           44  +	)"
           45  +}
           46  +
           47  +function clear() {
           48  +	local package
           49  +
           50  +	package="$1"
           51  +
           52  +	if [ -n "${package}" ]; then
           53  +		echo "not implemented" >&2
           54  +
           55  +		return 1
           56  +	fi
           57  +
           58  +	appfsd -tcl 'file delete -force -- {*}[glob -directory $::appfs::cachedir {[0-9a-f][0-9a-f]}]' || return 1
           59  +	appfsd -sqlite3 'DELETE FROM sites; DELETE FROM packages; DELETE FROM files; VACUUM;' || return 1
           60  +}
     2     61   
     3     62   case "$1" in
     4     63   	invalidate)
     5         -		statement='UPDATE sites SET ttl = "0";'
           64  +		invalidate || exit 1
           65  +		;;
           66  +	remove-site)
           67  +		remove_site "$2" || exit 1
           68  +		;;
           69  +	clean)
           70  +		clean || exit 1
     6     71   		;;
     7     72   	clear)
     8         -		rm -rf 
     9         -		statement='DELETE FROM sites; DELETE FROM packages; DELETE FROM files;'
           73  +		clear "$2" || exit 1
    10     74   		;;
    11     75   	*)
    12         -		echo "Usage: appfs-cache {invalidate|clear}" >&2
           76  +		echo "Usage: appfs-cache {invalidate|clean|clear|clear <package>|remove-site <site>}" >&2
    13     77   
    14     78   		exit 1
    15     79   		;;
    16     80   esac
    17     81   
    18         -exec appfsd -sqlite3 "${statement}"
           82  +exit 0