Diff

Differences From Artifact [649eb3a6a1]:

To Artifact [969f157ddb]:


119
120
121
122
123
124
125










126
127
128
129
130
131
132
			"noarch" - "none" - "any" - "all" {
				return "noarch"
			}
		}

		return -code error "Unable to normalize CPU: $cpu"
	}











	proc init {} {
		if {[info exists ::appfs::init_called]} {
			return
		}

		# Force [parray] to be loaded







>
>
>
>
>
>
>
>
>
>







119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
			"noarch" - "none" - "any" - "all" {
				return "noarch"
			}
		}

		return -code error "Unable to normalize CPU: $cpu"
	}

	proc _as_user {code} {
		::appfsd::simulate_user_fs_enter

		set retcode [catch [list uplevel $code] retstr]

		::appfsd::simulate_user_fs_leave

		return -code $retcode $retstr
	}

	proc init {} {
		if {[info exists ::appfs::init_called]} {
			return
		}

		# Force [parray] to be loaded
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
		# Create indexes
		db eval {CREATE INDEX IF NOT EXISTS sites_index ON sites (hostname);}
		db eval {CREATE INDEX IF NOT EXISTS packages_index ON packages (hostname, package, version, os, cpuArch);}
		db eval {CREATE INDEX IF NOT EXISTS files_index ON files (package_sha1, file_name, file_directory);}
	}

	proc download {hostname hash {method sha1}} {
		::appfsd::simulate_user_fs_leave

		set url [_construct_url $hostname $hash $method]
		set file [_cachefile $url $hash]

		::appfsd::simulate_user_fs_enter

		if {![file exists $file]} {
			return -code error "Unable to fetch (file does not exist: $file)"
		}

		return $file
	}








<
<



<
<







166
167
168
169
170
171
172


173
174
175


176
177
178
179
180
181
182
		# Create indexes
		db eval {CREATE INDEX IF NOT EXISTS sites_index ON sites (hostname);}
		db eval {CREATE INDEX IF NOT EXISTS packages_index ON packages (hostname, package, version, os, cpuArch);}
		db eval {CREATE INDEX IF NOT EXISTS files_index ON files (package_sha1, file_name, file_directory);}
	}

	proc download {hostname hash {method sha1}} {


		set url [_construct_url $hostname $hash $method]
		set file [_cachefile $url $hash]



		if {![file exists $file]} {
			return -code error "Unable to fetch (file does not exist: $file)"
		}

		return $file
	}

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
				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)]
					set whiteoutdir [string range [_whiteoutpath $pathinfo(package) $pathinfo(hostname) $pathinfo(file)] 0 end-15]

					foreach file [glob -nocomplain -tails -directory $whiteoutdir {{.,}*.APPFS.WHITEOUT}] {
						set remove [string range $file 0 end-15]
						set idx [lsearch -exact $retval $remove]
						if {$idx != -1} {
							set retval [lreplace $retval $idx $idx]
						}
					}

					foreach file [glob -nocomplain -tails -directory $dir -types {d f l p s} {{.,}*}] {
						if {$file == "." || $file == ".."} {
							continue
						}

						if {$file == ".APPFS.WHITEOUT"} {
							continue
						}

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

						lappend retval $file

					}
				}

				return $retval
			}
		}








>
|
|

|
|
|
|
|
|
|

|
|
|
|

|
|
|

|
|
|

|
>







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
				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)]} {
					_as_user {
						set dir [_localpath $pathinfo(package) $pathinfo(hostname) $pathinfo(file)]
						set whiteoutdir [string range [_whiteoutpath $pathinfo(package) $pathinfo(hostname) $pathinfo(file)] 0 end-15]

						foreach file [glob -nocomplain -tails -directory $whiteoutdir {{.,}*.APPFS.WHITEOUT}] {
							set remove [string range $file 0 end-15]
							set idx [lsearch -exact $retval $remove]
							if {$idx != -1} {
								set retval [lreplace $retval $idx $idx]
							}
						}

						foreach file [glob -nocomplain -tails -directory $dir -types {d f l p s} {{.,}*}] {
							if {$file == "." || $file == ".."} {
								continue
							}

							if {$file == ".APPFS.WHITEOUT"} {
								continue
							}

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

							lappend retval $file
						}
					}
				}

				return $retval
			}
		}

511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
			::appfs::getindex $pathinfo(hostname)
			::appfs::getpkgmanifest $pathinfo(hostname) $pathinfo(package_sha1)
		}

		switch -- $pathinfo(_type) {
			"toplevel" {
				set retval(type) directory
				set retval(childcount) 2;
			}
			"sites" {
				set check [::appfs::db onecolumn {SELECT 1 FROM packages WHERE hostname = $pathinfo(hostname);}]
				if {$check == "1"} {
					set retval(type) directory
					set retval(childcount) 2;
				}







|







519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
			::appfs::getindex $pathinfo(hostname)
			::appfs::getpkgmanifest $pathinfo(hostname) $pathinfo(package_sha1)
		}

		switch -- $pathinfo(_type) {
			"toplevel" {
				set retval(type) directory
				set retval(childcount) 2
			}
			"sites" {
				set check [::appfs::db onecolumn {SELECT 1 FROM packages WHERE hostname = $pathinfo(hostname);}]
				if {$check == "1"} {
					set retval(type) directory
					set retval(childcount) 2;
				}
564
565
566
567
568
569
570

571

572
573
574
575
576
577
578
579
580
581

582
583
584
585

586
587
588
589


590

591
592
593
594
595
596
597

				set retval(localpath) $localpath
				set retval(whiteoutpath) $whiteoutpath

				if {[file exists $localpath]} {
					set retval(is_localfile) 1
					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]

							}
							"fifo" {
								# Capitalized so that the first char is unique
								set retval(type) "Fifo"
							}
							"socket" {
								# Capitalized so that the first char is unique







>
|
>










>
|
|
|
|
>




>
>
|
>







572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612

				set retval(localpath) $localpath
				set retval(whiteoutpath) $whiteoutpath

				if {[file exists $localpath]} {
					set retval(is_localfile) 1
					catch {
						_as_user {
							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)
								_as_user {
									if {[file executable $localpath]} {
										set retval(perms) "x"
									} else {
										set retval(perms) ""
									}
								}
							}
							"link" {
								set retval(type) "symlink"

								_as_user {
									set retval(source) [file readlink $localpath]
								}
							}
							"fifo" {
								# Capitalized so that the first char is unique
								set retval(type) "Fifo"
							}
							"socket" {
								# Capitalized so that the first char is unique
651
652
653
654
655
656
657

658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674

675
676
677
678
679
680
681
		if {$pkgpathinfo(file_sha1) == ""} {
			return -code error "No such file or directory"
		}

		set localcachefile [download $pathinfo(hostname) $pkgpathinfo(file_sha1)]

		if {$mode == "write"} {

			set tmplocalpath "${localpath}.[expr rand()][clock clicks]"

			set failed 0
			if {[catch {
				file mkdir [file dirname $localpath]
				file copy -force -- $localcachefile $tmplocalpath

				if {$pkgpathinfo(perms) == "x"} {
					file attributes $tmplocalpath -permissions +x
				}

				file rename -force -- $tmplocalpath $localpath
			} err]} {
				set failed 1
			}
			catch {
				file delete -force -- $tmplocalpath

			}

			if {$failed} {
				return -code error $err
			}

			return $localpath







>
|

|
|
|
|

|
|
|

|
|
|
|
|
|
>







666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
		if {$pkgpathinfo(file_sha1) == ""} {
			return -code error "No such file or directory"
		}

		set localcachefile [download $pathinfo(hostname) $pkgpathinfo(file_sha1)]

		if {$mode == "write"} {
			_as_user {
				set tmplocalpath "${localpath}.[expr rand()][clock clicks]"

				set failed 0
				if {[catch {
					file mkdir [file dirname $localpath]
					file copy -force -- $localcachefile $tmplocalpath

					if {$pkgpathinfo(perms) == "x"} {
						file attributes $tmplocalpath -permissions +x
					}

					file rename -force -- $tmplocalpath $localpath
				} err]} {
					set failed 1
				}
				catch {
					file delete -force -- $tmplocalpath
				}
			}

			if {$failed} {
				return -code error $err
			}

			return $localpath
719
720
721
722
723
724
725

726

727
728
729
730
731
732
733
			}
		}

		set filename [localpath $path]

		set dirname [file dirname $filename]


		file mkdir $dirname


		return $filename
	}

	proc unlinkpath {path} {
		array set pathattrs [exists $path]








>
|
>







736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
			}
		}

		set filename [localpath $path]

		set dirname [file dirname $filename]

		_as_user {
			file mkdir $dirname
		}

		return $filename
	}

	proc unlinkpath {path} {
		array set pathattrs [exists $path]

742
743
744
745
746
747
748

749

750
751
752
753
754
755
756
757
758
759
760
761
762
763


764
765
766
767

				set children [getchildren $path]

				if {[llength $children] != 0} {
					return -code error "Asked to delete non-empty directory"
				}
			}


			file delete -force -- $localpath

		} elseif {[info exists pathattrs(is_remotefile)]} {
			if {$pathattrs(type) == "directory"} {
				set children [getchildren $path]

				if {[llength $children] != 0} {
					return -code error "Asked to delete non-empty directory"
				}
			}
		} else {
			return -code error "Unknown if file is remote or local !?"
		}

		set whiteoutfile $pathattrs(whiteoutpath)
		set whiteoutdir [file dirname $whiteoutfile]


		file mkdir $whiteoutdir
		close [open $whiteoutfile w]
	}
}








>
|
>














>
>
|
|
|
|
>
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
				set children [getchildren $path]

				if {[llength $children] != 0} {
					return -code error "Asked to delete non-empty directory"
				}
			}

			_as_user {
				file delete -force -- $localpath
			}
		} elseif {[info exists pathattrs(is_remotefile)]} {
			if {$pathattrs(type) == "directory"} {
				set children [getchildren $path]

				if {[llength $children] != 0} {
					return -code error "Asked to delete non-empty directory"
				}
			}
		} else {
			return -code error "Unknown if file is remote or local !?"
		}

		set whiteoutfile $pathattrs(whiteoutpath)
		set whiteoutdir [file dirname $whiteoutfile]

		_as_user {
			file mkdir $whiteoutdir
			close [open $whiteoutfile w]
		}
	}
}