Diff

Differences From Artifact [8b965c29ef]:

To Artifact [7312652049]:


     1      1   #! /usr/bin/env tclsh
     2      2   
     3      3   package require http 2.7
     4      4   package require sqlite3
     5      5   
     6      6   namespace eval ::appfs {
     7      7   	variable cachedir "/tmp/appfs-cache"
            8  +	variable ttl 3600
     8      9   
     9     10   	proc _hash_sep {hash {seps 4}} {
    10     11   		for {set idx 0} {$idx < $seps} {incr idx} {
    11     12   			append retval "[string range $hash [expr {$idx * 2}] [expr {($idx * 2) + 1}]]/"
    12     13   		}
    13     14   		append retval "[string range $hash [expr {$idx * 2}] end]"
    14     15   
................................................................................
    76     77   
    77     78   		if {![info exists ::appfs::db]} {
    78     79   			file mkdir $::appfs::cachedir
    79     80   
    80     81   			sqlite3 ::appfs::db [file join $::appfs::cachedir cache.db]
    81     82   		}
    82     83   
           84  +		_db eval {CREATE TABLE IF NOT EXISTS sites(hostname PRIMARY KEY, lastUpdate);}
    83     85   		_db eval {CREATE TABLE IF NOT EXISTS packages(hostname, sha1, package, version, os, cpuArch, isLatest, haveManifest);}
    84     86   		_db eval {CREATE TABLE IF NOT EXISTS files(package_sha1, type, time, source, size, perms, file_sha1, file_name, file_directory);}
    85     87   	}
    86     88   
    87     89   	proc download {hostname hash {method sha1}} {
    88     90   		set url "http://$hostname/appfs/$method/$hash"
    89     91   		set file [_cachefile $url $hash]
................................................................................
    92     94   			return -code error "Unable to fetch"
    93     95   		}
    94     96   
    95     97   		return $file
    96     98   	}
    97     99   
    98    100   	proc getindex {hostname} {
          101  +		set now [clock seconds]
          102  +
          103  +		set lastUpdates [_db eval {SELECT lastUpdate FROM sites WHERE hostname = $hostname LIMIT 1;}]
          104  +		if {[llength $lastUpdates] == 0} {
          105  +			set lastUpdate 0
          106  +		} else {
          107  +			set lastUpdate [lindex $lastUpdates 0]
          108  +		}
          109  +
          110  +		if {$now < ($lastUpdate + $::appfs::ttl)} {
          111  +			return COMPLETE
          112  +		}
          113  +
    99    114   		if {[string match "*\[/~\]*" $hostname]} {
   100    115   			return -code error "Invalid hostname"
   101    116   		}
   102    117   
   103    118   		set url "http://$hostname/appfs/index"
   104    119   
   105         -		set indexcachefile [_cachefile $url "SERVERS/[string tolower $hostname]" 0]
          120  +		catch {
          121  +			set token [::http::geturl $url]
          122  +			if {[::http::ncode $token] == "200"} {
          123  +				set indexhash_data [::http::data $token]
          124  +			}
          125  +			::http::reset $token
          126  +			$token cleanup
          127  +		}
   106    128   
   107         -		if {![file exists $indexcachefile]} {
          129  +		if {![info exists indexhash_data]} {
   108    130   			return -code error "Unable to fetch $url"
   109    131   		}
   110    132   
   111         -		set fd [open $indexcachefile]
   112         -		gets $fd indexhash_data
   113    133   		set indexhash [lindex [split $indexhash_data ","] 0]
   114         -		close $fd
   115    134   
   116    135   		set file [download $hostname $indexhash]
   117    136   		set fd [open $file]
   118    137   		set data [read $fd]
   119    138   		close $fd
   120    139   
   121    140   		array set packages [list]
................................................................................
   160    179   			if {$pkgInfo(isLatest)} {
   161    180   				_db eval {UPDATE packages SET isLatest = 0 WHERE hostname = $hostname AND package = $pkgInfo($package) AND os = $pkgInfo($package) AND cpuArch = $pkgInfo(cpuArch);}
   162    181   			}
   163    182   
   164    183   			_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);}
   165    184   
   166    185   		}
          186  +
          187  +		_db eval {INSERT OR REPLACE INTO sites (hostname, lastUpdate) VALUES ($hostname, $now);}
   167    188   
   168    189   		return COMPLETE
   169    190   	}
   170    191   
   171    192   	proc getpkgmanifest {hostname package_sha1} {
   172    193   		set haveManifests [_db eval {SELECT haveManifest FROM packages WHERE sha1 = $package_sha1 LIMIT 1;}]
   173    194   		set haveManifest [lindex $haveManifests 0]