Diff

Differences From Artifact [1a0526f9cf]:

To Artifact [e0ad1bdba0]:


    76     76   		if {![regexp {^[0-9a-f]*$} $value]} {
    77     77   			return false
    78     78   		}
    79     79   
    80     80   		return true
    81     81   	}
    82     82   
    83         -	proc _db {args} {
    84         -		return [uplevel 1 [list ::appfs::db {*}$args]]
    85         -	}
    86         -
    87     83   	proc _normalizeOS {os} {
    88     84   		set os [string tolower [string trim $os]]
    89     85   
    90     86   		switch -- $os {
    91     87   			"linux" - "freebsd" - "openbsd" - "netbsd" {
    92     88   				return $os
    93     89   			}
................................................................................
   120    116   		return -code error "Unable to normalize CPU: $cpu"
   121    117   	}
   122    118   
   123    119   	proc init {} {
   124    120   		if {[info exists ::appfs::init_called]} {
   125    121   			return
   126    122   		}
          123  +
          124  +		# Force [parray] to be loaded
          125  +		catch {
          126  +			parray does_not_exist
          127  +		}
   127    128   
   128    129   		set ::appfs::init_called 1
   129    130   
   130    131   		if {![info exists ::appfs::db]} {
   131    132   			file mkdir $::appfs::cachedir
   132    133   
   133    134   			sqlite3 ::appfs::db [file join $::appfs::cachedir cache.db]
   134    135   		}
   135    136   
   136    137   		# Create tables
   137         -		_db eval {CREATE TABLE IF NOT EXISTS sites(hostname PRIMARY KEY, lastUpdate, ttl);}
   138         -		_db eval {CREATE TABLE IF NOT EXISTS packages(hostname, sha1, package, version, os, cpuArch, isLatest, haveManifest);}
   139         -		_db eval {CREATE TABLE IF NOT EXISTS files(package_sha1, type, time, source, size, perms, file_sha1, file_name, file_directory);}
          138  +		db eval {CREATE TABLE IF NOT EXISTS sites(hostname PRIMARY KEY, lastUpdate, ttl);}
          139  +		db eval {CREATE TABLE IF NOT EXISTS packages(hostname, sha1, package, version, os, cpuArch, isLatest, haveManifest);}
          140  +		db eval {CREATE TABLE IF NOT EXISTS files(package_sha1, type, time, source, size, perms, file_sha1, file_name, file_directory);}
   140    141   
   141    142   		# Create indexes
   142         -		_db eval {CREATE INDEX IF NOT EXISTS sites_index ON sites (hostname);}
   143         -		_db eval {CREATE INDEX IF NOT EXISTS packages_index ON packages (hostname, package, version, os, cpuArch);}
   144         -		_db eval {CREATE INDEX IF NOT EXISTS files_index ON files (package_sha1, file_name, file_directory);}
          143  +		db eval {CREATE INDEX IF NOT EXISTS sites_index ON sites (hostname);}
          144  +		db eval {CREATE INDEX IF NOT EXISTS packages_index ON packages (hostname, package, version, os, cpuArch);}
          145  +		db eval {CREATE INDEX IF NOT EXISTS files_index ON files (package_sha1, file_name, file_directory);}
   145    146   	}
   146    147   
   147    148   	proc download {hostname hash {method sha1}} {
   148    149   		set url "http://$hostname/appfs/$method/$hash"
   149    150   		set file [_cachefile $url $hash]
   150    151   
   151    152   		if {![file exists $file]} {
................................................................................
   154    155   
   155    156   		return $file
   156    157   	}
   157    158   
   158    159   	proc getindex {hostname} {
   159    160   		set now [clock seconds]
   160    161   
   161         -		set lastUpdates [_db eval {SELECT lastUpdate, ttl FROM sites WHERE hostname = $hostname LIMIT 1;}]
          162  +		set lastUpdates [db eval {SELECT lastUpdate, ttl FROM sites WHERE hostname = $hostname LIMIT 1;}]
   162    163   		if {[llength $lastUpdates] == 0} {
   163    164   			set lastUpdate 0
   164    165   			set ttl 0
   165    166   		} else {
   166    167   			set lastUpdate [lindex $lastUpdates 0]
   167    168   			set ttl [lindex $lastUpdates 1]
   168    169   		}
................................................................................
   184    185   			}
   185    186   			::http::reset $token
   186    187   			::http::cleanup $token
   187    188   		}
   188    189   
   189    190   		if {![info exists indexhash_data]} {
   190    191   			# Cache this result for 60 seconds
   191         -			_db eval {INSERT OR REPLACE INTO sites (hostname, lastUpdate, ttl) VALUES ($hostname, $now, $::appfs::nttl);}
          192  +			db eval {INSERT OR REPLACE INTO sites (hostname, lastUpdate, ttl) VALUES ($hostname, $now, $::appfs::nttl);}
   192    193   
   193    194   			return -code error "Unable to fetch $url"
   194    195   		}
   195    196   
   196    197   		set indexhash [lindex [split $indexhash_data ","] 0]
   197    198   
   198    199   		if {![_isHash $indexhash]} {
................................................................................
   234    235   			if {![_isHash $pkgInfo(hash)]} {
   235    236   				continue
   236    237   			}
   237    238   
   238    239   			lappend curr_packages $pkgInfo(hash)
   239    240   
   240    241   			# Do not do any additional work if we already have this package
   241         -			set existing_packages [_db eval {SELECT package FROM packages WHERE hostname = $hostname AND sha1 = $pkgInfo(hash);}]
          242  +			set existing_packages [db eval {SELECT package FROM packages WHERE hostname = $hostname AND sha1 = $pkgInfo(hash);}]
   242    243   			if {[lsearch -exact $existing_packages $pkgInfo(package)] != -1} {
   243    244   				continue
   244    245   			}
   245    246   
   246    247   			if {$pkgInfo(isLatest)} {
   247         -				_db eval {UPDATE packages SET isLatest = 0 WHERE hostname = $hostname AND package = $pkgInfo($package) AND os = $pkgInfo($package) AND cpuArch = $pkgInfo(cpuArch);}
          248  +				db eval {UPDATE packages SET isLatest = 0 WHERE hostname = $hostname AND package = $pkgInfo($package) AND os = $pkgInfo($package) AND cpuArch = $pkgInfo(cpuArch);}
   248    249   			}
   249    250   
   250         -			_db eval {INSERT INTO packages (hostname, sha1, package, version, os, cpuArch, isLatest, haveManifest) VALUES ($hostname, $pkgInfo(hash), $pkgInfo(package), $pkgInfo(version), $pkgInfo(os), $pkgInfo(cpuArch), $pkgInfo(isLatest), 0);}
          251  +			db eval {INSERT INTO packages (hostname, sha1, package, version, os, cpuArch, isLatest, haveManifest) VALUES ($hostname, $pkgInfo(hash), $pkgInfo(package), $pkgInfo(version), $pkgInfo(os), $pkgInfo(cpuArch), $pkgInfo(isLatest), 0);}
   251    252   		}
   252    253   
   253    254   		# Look for packages that have been deleted
   254         -		set found_packages [_db eval {SELECT sha1 FROM packages WHERE hostname = $hostname;}]
          255  +		set found_packages [db eval {SELECT sha1 FROM packages WHERE hostname = $hostname;}]
   255    256   		foreach package $found_packages {
   256    257   			set found_packages_arr($package) 1
   257    258   		}
   258    259   
   259    260   		foreach package $curr_packages {
   260    261   			unset -nocomplain found_packages_arr($package)
   261    262   		}
   262    263   
   263    264   		foreach package [array names found_packages_arr] {
   264         -			_db eval {DELETE FROM packages WHERE hostname = $hostname AND sha1 = $package;}
          265  +			db eval {DELETE FROM packages WHERE hostname = $hostname AND sha1 = $package;}
   265    266   		}
   266    267   
   267         -		_db eval {INSERT OR REPLACE INTO sites (hostname, lastUpdate, ttl) VALUES ($hostname, $now, $::appfs::ttl);}
          268  +		db eval {INSERT OR REPLACE INTO sites (hostname, lastUpdate, ttl) VALUES ($hostname, $now, $::appfs::ttl);}
   268    269   
   269    270   		return COMPLETE
   270    271   	}
   271    272   
   272    273   	proc getpkgmanifest {hostname package_sha1} {
   273         -		set haveManifests [_db eval {SELECT haveManifest FROM packages WHERE sha1 = $package_sha1 LIMIT 1;}]
          274  +		set haveManifests [db eval {SELECT haveManifest FROM packages WHERE sha1 = $package_sha1 LIMIT 1;}]
   274    275   		set haveManifest [lindex $haveManifests 0]
   275    276   
   276    277   		if {$haveManifest} {
   277    278   			return COMPLETE
   278    279   		}
   279    280   
   280    281   		if {![_isHash $package_sha1]} {
................................................................................
   282    283   		}
   283    284   
   284    285   		set file [download $hostname $package_sha1]
   285    286   		set fd [open $file]
   286    287   		set pkgdata [read $fd]
   287    288   		close $fd
   288    289   
   289         -		_db transaction {
          290  +		db transaction {
   290    291   			foreach line [split $pkgdata "\n"] {
   291    292   				set line [string trim $line]
   292    293   
   293    294   				if {$line == ""} {
   294    295   					continue
   295    296   				}
   296    297   
................................................................................
   316    317   				}
   317    318   
   318    319   				set fileInfo(name) [join $work ","]
   319    320   				set fileInfo(name) [split [string trim $fileInfo(name) "/"] "/"]
   320    321   				set fileInfo(directory) [join [lrange $fileInfo(name) 0 end-1] "/"]
   321    322   				set fileInfo(name) [lindex $fileInfo(name) end]
   322    323   
   323         -				_db eval {INSERT INTO files (package_sha1, type, time, source, size, perms, file_sha1, file_name, file_directory) VALUES ($package_sha1, $fileInfo(type), $fileInfo(time), $fileInfo(source), $fileInfo(size), $fileInfo(perms), $fileInfo(sha1), $fileInfo(name), $fileInfo(directory) );}
   324         -				_db eval {UPDATE packages SET haveManifest = 1 WHERE sha1 = $package_sha1;}
          324  +				db eval {INSERT INTO files (package_sha1, type, time, source, size, perms, file_sha1, file_name, file_directory) VALUES ($package_sha1, $fileInfo(type), $fileInfo(time), $fileInfo(source), $fileInfo(size), $fileInfo(perms), $fileInfo(sha1), $fileInfo(name), $fileInfo(directory) );}
          325  +				db eval {UPDATE packages SET haveManifest = 1 WHERE sha1 = $package_sha1;}
   325    326   			}
   326    327   		}
   327    328   
   328    329   		return COMPLETE
   329    330   	}
   330    331   }