@@ -36,11 +36,11 @@ if {[file exists $file]} { return $file } - set tmpfile "${file}.[expr {rand()}]" + set tmpfile "${file}.[expr {rand()}][clock clicks]" set fd [open $tmpfile "w"] fconfigure $fd -translation binary catch { @@ -342,10 +342,11 @@ } proc _localpath {package hostname file} { set homedir [::appfsd::get_homedir] set dir [file join $homedir .appfs "./${package}@${hostname}" "./${file}"] + return $dir } proc _parsepath {path} { set path [string trim $path "/"] set path [split $path "/"] @@ -527,7 +528,39 @@ return [array get retval] } proc openpath {path mode} { + array set pathinfo [_parsepath $path] + + if {$pathinfo(_type) != "files"} { + return -code error "invalid type" + } + + set localpath [_localpath $pathinfo(package) $pathinfo(hostname) $pathinfo(file)] + + if {[file exists $localpath]} { + return $localpath + } + + set work [split $pathinfo(file) "/"] + set directory [join [lrange $work 0 end-1] "/"] + set file [lindex $work end] + set file_sha1 [::appfs::db onecolumn {SELECT file_sha1 FROM files WHERE package_sha1 = $pathinfo(package_sha1) AND file_name = $file AND file_directory = $directory;}] + + if {$file_sha1 == ""} { + return -code error "No such file or directory" + } + + set localcachefile [download $pathinfo(hostname) $file_sha1] + + if {$mode == "create"} { + set tmplocalpath "${localpath}.[expr rand()][clock clicks]" + file copy -force -- $localcachefile $tmplocalpath + file rename -force -- $tmplocalpath $localpath + + return $localpath + } + + return $localcachefile } }