Check-in [2160c4189b]
Overview
Comment:Nearly completely working write support
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | tcl-ops
Files: files | file ages | folders
SHA1: 2160c4189bf2872e7143399ced1c46b08cadf08d
User & Date: rkeene on 2014-11-10 03:11:18
Other Links: branch diff | manifest | tags
Context
2014-11-10
03:34
Updated to cache last home directory looked up and to setfsuid()/setfsgid() before accessing the filesystem so that we may be the right user check-in: 63e41c262c user: rkeene tags: tcl-ops
03:11
Nearly completely working write support check-in: 2160c4189b user: rkeene tags: tcl-ops
2014-11-09
09:10
Removed spurious deletes and fixed permissions on version directory check-in: 7d728e1078 user: rkeene tags: tcl-ops
Changes

Modified appfsd.c from [070b2bd8e3] to [227a45ec34].

627
628
629
630
631
632
633

634
635
636
637
638
639
640
	memset(stbuf, 0, sizeof(struct stat));

	stbuf->st_mtime = pathinfo.time;
	stbuf->st_ctime = pathinfo.time;
	stbuf->st_atime = pathinfo.time;
	stbuf->st_ino   = pathinfo.inode;
	stbuf->st_mode  = 0;


	switch (pathinfo.type) {
		case APPFS_PATHTYPE_DIRECTORY:
			stbuf->st_mode = S_IFDIR | 0555;
			stbuf->st_nlink = 2 + pathinfo.typeinfo.dir.childcount;
			break;
		case APPFS_PATHTYPE_FILE:







>







627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
	memset(stbuf, 0, sizeof(struct stat));

	stbuf->st_mtime = pathinfo.time;
	stbuf->st_ctime = pathinfo.time;
	stbuf->st_atime = pathinfo.time;
	stbuf->st_ino   = pathinfo.inode;
	stbuf->st_mode  = 0;
	stbuf->st_uid   = appfs_get_fsuid();

	switch (pathinfo.type) {
		case APPFS_PATHTYPE_DIRECTORY:
			stbuf->st_mode = S_IFDIR | 0555;
			stbuf->st_nlink = 2 + pathinfo.typeinfo.dir.childcount;
			break;
		case APPFS_PATHTYPE_FILE:
841
842
843
844
845
846
847

848
849
850
851
852



853



854


855
856
857
858
859
860
861

862
863
864


865
866
867
868
869
870
871
872
		return(errno * -1);
	}

	return(0);
}

static int appfs_fuse_create(const char *path, mode_t mode, struct fuse_file_info *fi) {

	int fd;
	int chmod_ret;

	APPFS_DEBUG("Enter (path = %s, ...)", path);




	fd = appfs_fuse_open(path, fi);



	if (fd < 0) {


		return(fd);
	}

	chmod_ret = fchmod(fd, mode);
	if (chmod_ret != 0) {
		close(fd);


		return(-EIO);
	}



	return(fd);
}

static int appfs_fuse_truncate(const char *path, off_t size) {
	char *real_path;
	int truncate_ret;

	APPFS_DEBUG("Enter (path = %s, ...)", path);







>

<



>
>
>
|
>
>
>
|
>
>
|


|
|
|

>
|


>
>
|







842
843
844
845
846
847
848
849
850

851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
		return(errno * -1);
	}

	return(0);
}

static int appfs_fuse_create(const char *path, mode_t mode, struct fuse_file_info *fi) {
	char *real_path;
	int fd;


	APPFS_DEBUG("Enter (path = %s, ...)", path);

	if ((mode & S_IFCHR) == S_IFCHR) {
		return(-EPERM);
	}

	if ((mode & S_IFBLK) == S_IFBLK) {
		return(-EPERM);
	}

	real_path = appfs_prepare_to_create(path);
	if (real_path == NULL) {
		return(-EIO);
	}

	fd = creat(real_path, mode);

	free(real_path);

	if (fd < 0) {
		return(errno * -1);
	}

	fi->fh = fd;

	return(0);
}

static int appfs_fuse_truncate(const char *path, off_t size) {
	char *real_path;
	int truncate_ret;

	APPFS_DEBUG("Enter (path = %s, ...)", path);
928
929
930
931
932
933
934






























935
936
937
938
939
940
941
		if (errno != EEXIST) {
			return(errno * -1);
		}
	}

	return(0);
}































/*
 * SQLite3 mode: Execute raw SQL and return success or failure
 */
static int appfs_sqlite3(const char *sql) {
	Tcl_Interp *interp;
	const char *sql_ret;







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







940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
		if (errno != EEXIST) {
			return(errno * -1);
		}
	}

	return(0);
}

static int appfs_fuse_chmod(const char *path, mode_t mode) {
	Tcl_Interp *interp;
	const char *real_path;
	int tcl_ret, chmod_ret;

	APPFS_DEBUG("Enter (path = %s, ...)", path);

	interp = appfs_TclInterp();
	if (interp == NULL) {
		return(-EIO);
	}

	tcl_ret = appfs_Tcl_Eval(interp, 3, "::appfs::openpath", path, "write");
	if (tcl_ret != TCL_OK) {
		APPFS_DEBUG("::appfs::openpath(%s, %s) failed.", path, "write");
		APPFS_DEBUG("Tcl Error is: %s", Tcl_GetStringResult(interp));

		return(-EIO);
	}

	real_path = Tcl_GetStringResult(interp);
	if (real_path == NULL) {
		return(-EIO);
	}

	chmod_ret = chmod(real_path, mode);

	return(chmod_ret);
}

/*
 * SQLite3 mode: Execute raw SQL and return success or failure
 */
static int appfs_sqlite3(const char *sql) {
	Tcl_Interp *interp;
	const char *sql_ret;
1027
1028
1029
1030
1031
1032
1033

1034
1035
1036
1037
1038
1039
1040
	.write     = appfs_fuse_write,
	.mknod     = appfs_fuse_mknod,
	.create    = appfs_fuse_create,
	.truncate  = appfs_fuse_truncate,
	.unlink    = appfs_fuse_unlink_rmdir,
	.rmdir     = appfs_fuse_unlink_rmdir,
	.mkdir     = appfs_fuse_mkdir,

};

/*
 * FUSE option parsing callback
 */
static int appfs_fuse_opt_cb(void *data, const char *arg, int key, struct fuse_args *outargs) {
	static int seen_cachedir = 0;







>







1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
	.write     = appfs_fuse_write,
	.mknod     = appfs_fuse_mknod,
	.create    = appfs_fuse_create,
	.truncate  = appfs_fuse_truncate,
	.unlink    = appfs_fuse_unlink_rmdir,
	.rmdir     = appfs_fuse_unlink_rmdir,
	.mkdir     = appfs_fuse_mkdir,
	.chmod     = appfs_fuse_chmod,
};

/*
 * FUSE option parsing callback
 */
static int appfs_fuse_opt_cb(void *data, const char *arg, int key, struct fuse_args *outargs) {
	static int seen_cachedir = 0;

Modified appfsd.tcl from [251a7402cc] to [3ec5e46dc5].

343
344
345
346
347
348
349






350
351
352
353
354
355
356
	}

	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 "/"]
		set pathlen [llength $path]

		array set retval [list _children sites _type toplevel]







>
>
>
>
>
>







343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
	}

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

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

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

		array set retval [list _children sites _type toplevel]
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
					::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 {[string match "*.APPFS.WHITEOUT" $file]} {
							set remove [string range $file 0 end-15]
							set idx [lsearch -exact $retval $remove]
							if {$idx != -1} {
								set retval [lreplace $retval $idx $idx]
							}
							continue
						}

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








>
>
>
>
>
>
>
>
>
>





|
<
<
<
<
<







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
					::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} {{.,}*}] {
						if {$file == "." || $file == ".."} {
							continue
						}

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





							continue
						}

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

540
541
542
543
544
545
546

547
548

549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
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
					}
				}
			}
			"files" {
				set retval(packaged) 1

				set localpath [_localpath $pathinfo(package) $pathinfo(hostname) $pathinfo(file)]


				set retval(localpath) $localpath


				if {![file exists "${localpath}.APPFS.WHITEOUT"]} {
					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]
								}
							}
						} err
					} else {

						set retval(is_remotefile) 1

						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(*)
					}
				}

			}
		}







>


>

<
|
|
|
|
|

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





>
>
>
>
>







551
552
553
554
555
556
557
558
559
560
561
562

563
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
598
599
600
601
602
603
604
605
606
607
					}
				}
			}
			"files" {
				set retval(packaged) 1

				set localpath [_localpath $pathinfo(package) $pathinfo(hostname) $pathinfo(file)]
				set whiteoutpath  [_whiteoutpath $pathinfo(package) $pathinfo(hostname) $pathinfo(file)]

				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]
							}
						}
					} err
				} else {
					if {![file exists $whiteoutpath]} {
						set retval(is_remotefile) 1

						set work [split $pathinfo(file) "/"]
						set directory [join [lrange $work 0 end-1] "/"]
						set file [lindex $work end]

						if {$directory == "" && $file == ""} {
							array set retval [list type directory childcount 2]
						}

						::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(*)
					}
				}

			}
		}
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
		if {$pathinfo(_type) != "files"} {
			return -code error "invalid type"
		}

		set localpath [_localpath $pathinfo(package) $pathinfo(hostname) $pathinfo(file)]

		if {$mode == "create"} {
			file delete -- "${localpath}.APPFS.WHITEOUT"

			return $localpath
		}

		if {[file exists $localpath]} {
			return $localpath
		}








<
<







619
620
621
622
623
624
625


626
627
628
629
630
631
632
		if {$pathinfo(_type) != "files"} {
			return -code error "invalid type"
		}

		set localpath [_localpath $pathinfo(package) $pathinfo(hostname) $pathinfo(file)]

		if {$mode == "create"} {


			return $localpath
		}

		if {[file exists $localpath]} {
			return $localpath
		}

650
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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715

716
717

718
719
720

721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
			}

			return $localpath
		}

		return $localcachefile
	}













	proc exists {path} {
		catch {
			set info [getattr $path]
		} err

		if {![info exists info]} {
			if {$err == "No such file or directory"} {
				return [list]
			} else {
				return -code error $err
			}
		}

		return $info
	}

	proc prepare_to_create {path} {

		if {[exists $path] != ""} {
			return -code error "File already exists"
		}


		set filename [openpath $path "create"]

		set dirname [file dirname $filename]

		file mkdir $dirname

		return $filename
	}

	proc localpath {path} {
		array set pathinfo [_parsepath $path]

		if {$pathinfo(_type) != "files"} {
			return -code error "invalid type"
		}

		set localpath [_localpath $pathinfo(package) $pathinfo(hostname) $pathinfo(file)]

		return $localpath
	}

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

		if {![info exists pathattrs(packaged)]} {
			return -code error "invalid type"
		}

		set localpath $pathattrs(localpath)

		set whiteout 0
		set isdirectory 0
		if {[info exists pathattrs(is_localfile)]} {
			if {[file isdirectory $localpath]} {
				set isdirectory 1
				set whiteout 1
			} else {

				file delete -force -- $localpath
			}

		} elseif {[info exists pathattrs(is_remotefile)]} {
			if {$pathattrs(type) == "directory"} {
				set isdirectory 1

			}

			set whiteout 1
		} else {
			return -code error "Unknown if file is remote or local !?"
		}

		if {$isdirectory} {
			set children [getchildren $path]
			if {$children != [list]} {
				return -code error "Asked to delete non-empty directory"
			}
		}

		if {$whiteout} {
			set whiteoutfile "${localpath}.APPFS.WHITEOUT"
			set whiteoutdir [file dirname $whiteoutfile]
			file mkdir $whiteoutdir
			close [open $whiteoutfile w]
		}
	}
}







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

















|
>
|
|
|
|
>
|








<
<
<
<
<
<
<
<
<
<
<
<













<

|
>
|

>



>








<






|






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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717












718
719
720
721
722
723
724
725
726
727
728
729
730

731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748

749
750
751
752
753
754
755
756
757
758
759
760
761
			}

			return $localpath
		}

		return $localcachefile
	}

	proc localpath {path} {
		array set pathinfo [_parsepath $path]

		if {$pathinfo(_type) != "files"} {
			return -code error "invalid type"
		}

		set localpath [_localpath $pathinfo(package) $pathinfo(hostname) $pathinfo(file)]

		return $localpath
	}

	proc exists {path} {
		catch {
			set info [getattr $path]
		} err

		if {![info exists info]} {
			if {$err == "No such file or directory"} {
				return [list]
			} else {
				return -code error $err
			}
		}

		return $info
	}

	proc prepare_to_create {path {must_not_exist 1}} {
		if {$must_not_exist} {
			if {[exists $path] != ""} {
				return -code error "File already exists"
			}
		}

		set filename [localpath $path]

		set dirname [file dirname $filename]

		file mkdir $dirname

		return $filename
	}













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

		if {![info exists pathattrs(packaged)]} {
			return -code error "invalid type"
		}

		set localpath $pathattrs(localpath)

		set whiteout 0
		set isdirectory 0
		if {[info exists pathattrs(is_localfile)]} {
			if {[file isdirectory $localpath]} {

				set whiteout 1

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

			set whiteout 1
		} else {
			return -code error "Unknown if file is remote or local !?"
		}

		if {$isdirectory} {

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

		if {$whiteout} {
			set whiteoutfile $pathattrs(whiteoutpath)
			set whiteoutdir [file dirname $whiteoutfile]
			file mkdir $whiteoutdir
			close [open $whiteoutfile w]
		}
	}
}