Diff

Differences From Artifact [20368f703a]:

To Artifact [15909aa1a5]:


846
847
848
849
850
851
852
853

854
855
856
857
858
859
860
846
847
848
849
850
851
852

853
854
855
856
857
858
859
860







-
+







 *    3. flexible -- Attempts to find a core Xvfs instance for the
 *                   process at runtime, if found do #2, otherwise
 *                   fallback to #1
 *
 */
static Tcl_Filesystem xvfs_tclfs_standalone_fs;
static int xvfs_standalone_register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
	int tcl_ret;
	int tclRet;
	static int registered = 0;

	/*
	 * Ensure this instance is not already registered
	 */
	if (registered) {
		return(TCL_OK);
868
869
870
871
872
873
874
875

876
877
878
879
880
881
882
868
869
870
871
872
873
874

875
876
877
878
879
880
881
882







-
+







	if (fsInfo->protocolVersion != XVFS_PROTOCOL_VERSION) {
		if (interp) {
			Tcl_SetResult(interp, "Protocol mismatch", NULL);
		}
		return(TCL_ERROR);
	}

	xvfs_tclfs_standalone_fs.typeName                   = "xvfs";
	xvfs_tclfs_standalone_fs.typeName                   = "xvfsInstance";
	xvfs_tclfs_standalone_fs.structureLength            = sizeof(xvfs_tclfs_standalone_fs);
	xvfs_tclfs_standalone_fs.version                    = TCL_FILESYSTEM_VERSION_1;
	xvfs_tclfs_standalone_fs.pathInFilesystemProc       = xvfs_tclfs_standalone_pathInFilesystem;
	xvfs_tclfs_standalone_fs.dupInternalRepProc         = NULL;
	xvfs_tclfs_standalone_fs.freeInternalRepProc        = NULL;
	xvfs_tclfs_standalone_fs.internalToNormalizedProc   = NULL;
	xvfs_tclfs_standalone_fs.createInternalRepProc      = NULL;
906
907
908
909
910
911
912
913
914


915
916
917
918
919
920
921

922
923
924
925
926
927
928
906
907
908
909
910
911
912


913
914
915
916
917
918
919
920

921
922
923
924
925
926
927
928







-
-
+
+






-
+








	xvfs_tclfs_standalone_info.fsInfo = fsInfo;
	xvfs_tclfs_standalone_info.mountpoint = Tcl_NewObj();

	Tcl_IncrRefCount(xvfs_tclfs_standalone_info.mountpoint);
	Tcl_AppendStringsToObj(xvfs_tclfs_standalone_info.mountpoint, XVFS_ROOT_MOUNTPOINT, fsInfo->name, NULL);
	
	tcl_ret = Tcl_FSRegister(NULL, &xvfs_tclfs_standalone_fs);
	if (tcl_ret != TCL_OK) {
	tclRet = Tcl_FSRegister(NULL, &xvfs_tclfs_standalone_fs);
	if (tclRet != TCL_OK) {
		Tcl_DecrRefCount(xvfs_tclfs_standalone_info.mountpoint);

		if (interp) {
			Tcl_SetResult(interp, "Tcl_FSRegister() failed", NULL);
		}

		return(tcl_ret);
		return(tclRet);
	}

	xvfs_tclfs_prepareChannelType();

	return(TCL_OK);
}
#endif /* XVFS_MODE_STANDALONE || XVFS_MODE_FLEXIBLE */
966
967
968
969
970
971
972



















































































































































































973

















974
975




















976
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169


1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

	}

	return(xvfs_register(interp, fsInfo));
}
#endif /* XVFS_MODE_FLEXIBLE */

#if defined(XVFS_MODE_SERVER)
static Tcl_Filesystem xvfs_tclfs_dispatch_fs;
static Tcl_HashTable xvfs_tclfs_dispatch_map;

static struct xvfs_tclfs_instance_info *xvfs_tclfs_dispatch_pathToInstanceInfo(Tcl_Obj *path) {
	Tcl_HashEntry *mapEntry;
	struct xvfs_tclfs_instance_info *retval;
	const char *pathStr, *rootStr;
	char *fsName, *fsNameEnds, origSep;
	int pathLen, rootLen;

	XVFS_DEBUG_ENTER;

	XVFS_DEBUG_PRINTF("Looking up path \"%s\" ...", Tcl_GetString(path));
	
	rootStr = XVFS_ROOT_MOUNTPOINT;
	rootLen = strlen(XVFS_ROOT_MOUNTPOINT);

	pathStr = Tcl_GetStringFromObj(path, &pathLen);

	if (pathLen < rootLen) {
		XVFS_DEBUG_PUTS("... failed (length too short)");
		XVFS_DEBUG_LEAVE;
		return(NULL);
	}

	if (memcmp(pathStr, rootStr, rootLen) != 0) {
		XVFS_DEBUG_PUTS("... failed (incorrect prefix)");
		XVFS_DEBUG_LEAVE;
		return(NULL);
	}

	fsName = ((char *) pathStr) + rootLen;

	fsNameEnds = strchr(fsName, '/');
	if (fsNameEnds) {
		origSep = *fsNameEnds;
		*fsNameEnds = '\0';
	}

	XVFS_DEBUG_PRINTF("... fsName = %s...", fsName);

	mapEntry = Tcl_FindHashEntry(&xvfs_tclfs_dispatch_map, fsName);

	if (fsNameEnds) {
		*fsNameEnds = origSep;
	}

	if (mapEntry) {
		retval = (struct xvfs_tclfs_instance_info *) Tcl_GetHashValue(mapEntry);
		XVFS_DEBUG_PRINTF("... found a registered filesystem: %p", retval);
	} else {
		retval = NULL;
		XVFS_DEBUG_PUTS("... found no registered filesystem.");
	}

	XVFS_DEBUG_LEAVE;
	return(retval);
}

static int xvfs_tclfs_dispatch_pathInFilesystem(Tcl_Obj *path, ClientData *dataPtr) {
	static struct xvfs_tclfs_instance_info *instanceInfo;

	instanceInfo = xvfs_tclfs_dispatch_pathToInstanceInfo(path);
	if (!instanceInfo) {
		return(-1);
	}

	return(xvfs_tclfs_pathInFilesystem(path, dataPtr, instanceInfo));
}

static int xvfs_tclfs_dispatch_stat(Tcl_Obj *path, Tcl_StatBuf *statBuf) {
	struct xvfs_tclfs_instance_info *instanceInfo;

	instanceInfo = xvfs_tclfs_dispatch_pathToInstanceInfo(path);
	if (!instanceInfo) {
		return(0);
	}

	return(xvfs_tclfs_stat(path, statBuf, instanceInfo));
}

static int xvfs_tclfs_dispatch_access(Tcl_Obj *path, int mode) {
	struct xvfs_tclfs_instance_info *instanceInfo;

	instanceInfo = xvfs_tclfs_dispatch_pathToInstanceInfo(path);
	if (!instanceInfo) {
		return(-1);
	}

	return(xvfs_tclfs_access(path, mode, instanceInfo));
}

static Tcl_Channel xvfs_tclfs_dispatch_openFileChannel(Tcl_Interp *interp, Tcl_Obj *path, int mode, int permissions) {
	struct xvfs_tclfs_instance_info *instanceInfo;

	instanceInfo = xvfs_tclfs_dispatch_pathToInstanceInfo(path);
	if (!instanceInfo) {
		return(NULL);
	}

	return(xvfs_tclfs_openFileChannel(interp, path, mode, permissions, instanceInfo));
}

static int xvfs_tclfs_dispatch_matchInDir(Tcl_Interp *interp, Tcl_Obj *resultPtr, Tcl_Obj *pathPtr, const char *pattern, Tcl_GlobTypeData *types) {
	struct xvfs_tclfs_instance_info *instanceInfo;

	instanceInfo = xvfs_tclfs_dispatch_pathToInstanceInfo(pathPtr);
	if (!instanceInfo) {
		return(TCL_ERROR);
	}

	return(xvfs_tclfs_matchInDir(interp, resultPtr, pathPtr, pattern, types, instanceInfo));
}

static int xvfs_tclfs_dispatch_init(Tcl_Interp *interp) {
	static int registered = 0;
	int tclRet;

	/* XXX:TODO: Make this thread-safe */
	if (registered) {
		return(TCL_OK);
	}
	registered = 1;

	xvfs_tclfs_dispatch_fs.typeName                   = "xvfsDispatch";
	xvfs_tclfs_dispatch_fs.structureLength            = sizeof(xvfs_tclfs_dispatch_fs);
	xvfs_tclfs_dispatch_fs.version                    = TCL_FILESYSTEM_VERSION_1;
	xvfs_tclfs_dispatch_fs.pathInFilesystemProc       = xvfs_tclfs_dispatch_pathInFilesystem;
	xvfs_tclfs_dispatch_fs.dupInternalRepProc         = NULL;
	xvfs_tclfs_dispatch_fs.freeInternalRepProc        = NULL;
	xvfs_tclfs_dispatch_fs.internalToNormalizedProc   = NULL;
	xvfs_tclfs_dispatch_fs.createInternalRepProc      = NULL;
	xvfs_tclfs_dispatch_fs.normalizePathProc          = NULL;
	xvfs_tclfs_dispatch_fs.filesystemPathTypeProc     = NULL;
	xvfs_tclfs_dispatch_fs.filesystemSeparatorProc    = NULL;
	xvfs_tclfs_dispatch_fs.statProc                   = xvfs_tclfs_dispatch_stat;
	xvfs_tclfs_dispatch_fs.accessProc                 = xvfs_tclfs_dispatch_access;
	xvfs_tclfs_dispatch_fs.openFileChannelProc        = xvfs_tclfs_dispatch_openFileChannel;
	xvfs_tclfs_dispatch_fs.matchInDirectoryProc       = xvfs_tclfs_dispatch_matchInDir;
	xvfs_tclfs_dispatch_fs.utimeProc                  = NULL;
	xvfs_tclfs_dispatch_fs.linkProc                   = NULL;
	xvfs_tclfs_dispatch_fs.listVolumesProc            = NULL;
	xvfs_tclfs_dispatch_fs.fileAttrStringsProc        = NULL;
	xvfs_tclfs_dispatch_fs.fileAttrsGetProc           = NULL;
	xvfs_tclfs_dispatch_fs.fileAttrsSetProc           = NULL;
	xvfs_tclfs_dispatch_fs.createDirectoryProc        = NULL;
	xvfs_tclfs_dispatch_fs.removeDirectoryProc        = NULL;
	xvfs_tclfs_dispatch_fs.deleteFileProc             = NULL;
	xvfs_tclfs_dispatch_fs.copyFileProc               = NULL;
	xvfs_tclfs_dispatch_fs.renameFileProc             = NULL;
	xvfs_tclfs_dispatch_fs.copyDirectoryProc          = NULL;
	xvfs_tclfs_dispatch_fs.lstatProc                  = NULL;
	xvfs_tclfs_dispatch_fs.loadFileProc               = NULL;
	xvfs_tclfs_dispatch_fs.getCwdProc                 = NULL;
	xvfs_tclfs_dispatch_fs.chdirProc                  = NULL;

	tclRet = Tcl_FSRegister(NULL, &xvfs_tclfs_dispatch_fs);
	if (tclRet != TCL_OK) {
		if (interp) {
			Tcl_SetResult(interp, "Tcl_FSRegister() failed", NULL);
		}

		return(tclRet);
	}

	/*
	 * Initialize the channel type we will use for I/O
	 */
	xvfs_tclfs_prepareChannelType();

	/*
	 * Initialize the map to lookup paths to registered
	 * filesystems
	 */
	Tcl_InitHashTable(&xvfs_tclfs_dispatch_map, TCL_STRING_KEYS);

	return(TCL_OK);
}

int Xvfs_Register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
	Tcl_HashEntry *mapEntry;
	struct xvfs_tclfs_instance_info *instanceInfo;
	int dispatchInitRet;
	int new;

	dispatchInitRet = xvfs_tclfs_dispatch_init(interp);
	if (dispatchInitRet != TCL_OK) {
		return(dispatchInitRet);
	}

	/*
	 * Verify this is for a protocol we support
	 */
	if (fsInfo->protocolVersion != XVFS_PROTOCOL_VERSION) {
		if (interp) {
			Tcl_SetResult(interp, "Protocol mismatch", NULL);
		}
	return(TCL_ERROR);
}
		return(TCL_ERROR);
	}

	/*
	 * Create the structure needed
	 */
	instanceInfo = (struct xvfs_tclfs_instance_info *) Tcl_Alloc(sizeof(*instanceInfo));
	instanceInfo->fsInfo = fsInfo;
	instanceInfo->mountpoint = Tcl_ObjPrintf("%s%s", XVFS_ROOT_MOUNTPOINT, fsInfo->name);
	Tcl_IncrRefCount(instanceInfo->mountpoint);

	/*
	 * Register a hash table entry for this name
	 */
	new = 0;
	mapEntry = Tcl_CreateHashEntry(&xvfs_tclfs_dispatch_map, fsInfo->name, &new);
	Tcl_SetHashValue(mapEntry, instanceInfo);

	return(TCL_OK);
}
#endif /* XVFS_MODE_SERVER */