Diff

Differences From Artifact [88b17c4a86]:

To Artifact [c564230658]:


1
2
3
4
5

6
7
8
9
10
11
12
#! /usr/bin/env tclsh

package require http 2.7
package require sqlite3
package require sha1


namespace eval ::appfs {
	variable cachedir "/tmp/appfs-cache"
	variable ttl 3600
	variable nttl 60

	# User-replacable function to convert a hostname/hash/method to an URL





>







1
2
3
4
5
6
7
8
9
10
11
12
13
#! /usr/bin/env tclsh

package require http 2.7
package require sqlite3
package require sha1
package require appfsd

namespace eval ::appfs {
	variable cachedir "/tmp/appfs-cache"
	variable ttl 3600
	variable nttl 60

	# User-replacable function to convert a hostname/hash/method to an URL
335
336
337
338
339
340
341





342
343
344
345
346
347
348
349
350
351
352

353
354
355
356
357
358







359
360
361
362
363
364
365
366
367
368
369

370
371
372
373
374
375
376
377
378

379
380
381
382

383
384
385
386
387
388
389

390
391
392

393
394
395
396
397
398
399
				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) );}
				db eval {UPDATE packages SET haveManifest = 1 WHERE sha1 = $package_sha1;}
			}
		}

		return COMPLETE
	}






	proc _parsepath {path} {
		set path [string trim $path "/"]
		set path [split $path "/"]
		set pathlen [llength $path]

		array set retval [list _children sites]

		if {$pathlen > 0} {
			set retval(hostname) [lindex $path 0]
			set retval(_children) packages


			if {$pathlen > 1} {
				set package [lindex $path 1]
				if {[string length $package] == "40" && [regexp {^[a-fA-F0-9]*$} $package]} {
					set retval(package_sha1) $package
					set retval(_children) files








					if {$pathlen > 2} {
						set retval(file) [join [lrange $path 2 end] "/"]
					} else {
						set retval(file) ""
					}

					return [array get retval]
				} else {
					set retval(package) $package
					set retval(_children) os-cpu

				}

				if {$pathlen > 2} {
					set os_cpu [lindex $path 2]
					set os_cpu [split $os_cpu "-"]

					set retval(os) [lindex $os_cpu 0]
					set retval(cpu) [lindex $os_cpu 1]
					set retval(_children) versions


					if {$pathlen > 3} {
						set retval(version) [lindex $path 3]
						set retval(_children) files


						set retval(package_sha1) [::appfs::db onecolumn {SELECT sha1 FROM packages WHERE hostname = $retval(hostname) AND os = $retval(os) AND cpuArch = $retval(cpu) AND version = $retval(version);}]
						if {$retval(package_sha1) == ""} {
							return [list]
						}

						if {$pathlen > 4} {

							set retval(file) [join [lrange $path 4 end] "/"]
						} else {
							set retval(file) ""

						}
					}
				}
			}
		}

		return [array get retval]







>
>
>
>
>






|




>






>
>
>
>
>
>
>






<
<



>
|
<
|
|
|

|
|
|
>

|
|
|
>

|
|
|
|

|
>
|
|
|
>







336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378


379
380
381
382
383

384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
				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) );}
				db eval {UPDATE packages SET haveManifest = 1 WHERE sha1 = $package_sha1;}
			}
		}

		return COMPLETE
	}

	proc _localpath {package hostname file} {
		set homedir [::appfsd::get_homedir]
		set dir [file join $homedir .appfs "./${package}@${hostname}" "./${file}"]
	}

	proc _parsepath {path} {
		set path [string trim $path "/"]
		set path [split $path "/"]
		set pathlen [llength $path]

		array set retval [list _children sites _type toplevel]

		if {$pathlen > 0} {
			set retval(hostname) [lindex $path 0]
			set retval(_children) packages
			set retval(_type) sites

			if {$pathlen > 1} {
				set package [lindex $path 1]
				if {[string length $package] == "40" && [regexp {^[a-fA-F0-9]*$} $package]} {
					set retval(package_sha1) $package
					set retval(_children) files
					set retval(_type) files

					::appfs::db eval {SELECT package, os, cpuArch, version FROM packages WHERE sha1 = $retval(package_sha1);} pkginfo {}
					set retval(package) $pkginfo(package)
					set retval(os) $pkginfo(os)
					set retval(cpu) $pkginfo(cpuArch)
					set retval(version) $pkginfo(version)

					if {$pathlen > 2} {
						set retval(file) [join [lrange $path 2 end] "/"]
					} else {
						set retval(file) ""
					}


				} else {
					set retval(package) $package
					set retval(_children) os-cpu
					set retval(_type) packages


					if {$pathlen > 2} {
						set os_cpu [lindex $path 2]
						set os_cpu [split $os_cpu "-"]

						set retval(os) [lindex $os_cpu 0]
						set retval(cpu) [lindex $os_cpu 1]
						set retval(_children) versions
						set retval(_type) os-cpu

						if {$pathlen > 3} {
							set retval(version) [lindex $path 3]
							set retval(_children) files
							set retval(_type) versions

							set retval(package_sha1) [::appfs::db onecolumn {SELECT sha1 FROM packages WHERE hostname = $retval(hostname) AND os = $retval(os) AND cpuArch = $retval(cpu) AND version = $retval(version);}]
							if {$retval(package_sha1) == ""} {
								return [list]
							}

							if {$pathlen > 4} {
								set retval(_type) files
								set retval(file) [join [lrange $path 4 end] "/"]
							} else {
								set retval(file) ""
							}
						}
					}
				}
			}
		}

		return [array get retval]
410
411
412
413
414
415
416
417
418




419
420
421
422




423
424
425
426
427
428
429
430





431
432












433
434
435
436


437




438













































439
440
441
				catch {
					::appfs::getindex $pathinfo(hostname)
				}

				return [::appfs::db eval {SELECT DISTINCT package FROM packages WHERE hostname = $pathinfo(hostname);}]
			}
			"os-cpu" {
				return [::appfs::db eval {SELECT DISTINCT os || "-" || cpuArch FROM packages WHERE hostname = $pathinfo(hostname) AND package = $pathinfo(package);}]
			}




			"versions" {
				return [::appfs::db eval {
					SELECT DISTINCT version FROM packages WHERE hostname = $pathinfo(hostname) AND package = $pathinfo(package) AND os = $pathinfo(os) AND cpuArch = $pathinfo(cpu);
				}]




			}
			"files" {
				catch {
					::appfs::getpkgmanifest $pathinfo(hostname) $pathinfo(package_sha1)
				}

				return [::appfs::db eval {SELECT DISTINCT file_name FROM files WHERE package_sha1 = $pathinfo(package_sha1) AND file_directory = $pathinfo(file);}]
			}





		}













		return -code error "Invalid or unacceptable path: $dir"
	}

	proc getattr {path} {


	}


















































	proc openpath {path mode} {
	}
}







|
|
>
>
>
>

|


>
>
>
>






|
|
>
>
>
>
>
|

>
>
>
>
>
>
>
>
>
>
>
>




>
>
|
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
				catch {
					::appfs::getindex $pathinfo(hostname)
				}

				return [::appfs::db eval {SELECT DISTINCT package FROM packages WHERE hostname = $pathinfo(hostname);}]
			}
			"os-cpu" {
				set retval [::appfs::db eval {SELECT DISTINCT os || "-" || cpuArch FROM packages WHERE hostname = $pathinfo(hostname) AND package = $pathinfo(package);}]

				lappend retval "platform"

				return $retval
			}
			"versions" {
				set retval [::appfs::db eval {
					SELECT DISTINCT version FROM packages WHERE hostname = $pathinfo(hostname) AND package = $pathinfo(package) AND os = $pathinfo(os) AND cpuArch = $pathinfo(cpu);
				}]

				lappend retval "latest"

				return $retval
			}
			"files" {
				catch {
					::appfs::getpkgmanifest $pathinfo(hostname) $pathinfo(package_sha1)
				}

				set retval [::appfs::db eval {SELECT DISTINCT file_name FROM files WHERE package_sha1 = $pathinfo(package_sha1) AND file_directory = $pathinfo(file);}]

				if {[info exists pathinfo(package)] && [info exists pathinfo(hostname)] && [info exists pathinfo(file)]} {
					set dir [_localpath $pathinfo(package) $pathinfo(hostname) $pathinfo(file)]
					foreach file [glob -nocomplain -tails -directory $dir -types {d f l} {{.,}*}] {
						if {$file == "." || $file == ".."} {
							continue
						}

						if {[lsearch -exact $retval $file] != -1} {
							continue
						}

						lappend retval $file
					}
				}

				return $retval
			}
		}

		return -code error "Invalid or unacceptable path: $dir"
	}

	proc getattr {path} {
		array set pathinfo [_parsepath $path]
		array set retval [list]

		switch -- $pathinfo(_type) {
			"toplevel" - "sites" - "packages" - "os-cpu" - "versions" {
				set retval(type) directory
				set retval(childcount) 2;
			}
			"files" {
				set localpath [_localpath $pathinfo(package) $pathinfo(hostname) $pathinfo(file)]
				if {[file exists $localpath]} {
					catch {
						file lstat $localpath localpathinfo
						set retval(time) $localpathinfo(mtime)

						switch -- $localpathinfo(type) {
							"directory" {
								set retval(type) "directory"
								set retval(childcount) 2
							}
							"file" {
								set retval(type) "file"
								set retval(size) $localpathinfo(size)
								if {[file executable $localpath]} {
									set retval(perms) "x"
								} else {
									set retval(perms) ""
								}
							}
							"link" {
								set retval(type) "symlink"
								set retval(source) [file readlink $localpath]
							}
						}
					} err
				} else {
					set work [split $pathinfo(file) "/"]
					set directory [join [lrange $work 0 end-1] "/"]
					set file [lindex $work end]
					::appfs::db eval {SELECT type, time, source, size, perms FROM files WHERE package_sha1 = $pathinfo(package_sha1) AND file_directory = $directory AND file_name = $file;} retval {}
					unset -nocomplain retval(*)
				}

			}
		}

		if {![info exists retval(type)]} {
			return -code error "No such file or directory"
		}

		return [array get retval]
	}

	proc openpath {path mode} {
	}
}