Annotation For xvfs-core.c

Lines of xvfs-core.c from check-in cb77ecfb24 that are changed by the sequence of edits moving toward check-in 9bcf758fef:

                         1: #include <xvfs-core.h>
                         2: #include <string.h>
                         3: #include <tcl.h>
                         4: 
                         5: #if defined(XVFS_MODE_SERVER) || defined(XVFS_MODE_STANDALONE) || defined(XVFS_MODE_FLEXIBLE)
                         6: #define XVFS_ROOT_MOUNTPOINT "//xvfs:/"
                         7: 
                         8: struct xvfs_tclfs_instance_info {
                         9: 	struct Xvfs_FSInfo *fsInfo;
                        10: 	Tcl_Obj            *mountpoint;
                        11: };
                        12: 
                        13: /*
                        14:  * Internal Core Utilities
                        15:  */
                        16: static const char *xvfs_relativePath(Tcl_Obj *path, struct xvfs_tclfs_instance_info *info) {
                        17: 	const char *pathStr, *rootStr;
                        18: 	int pathLen, rootLen;
                        19: 	
                        20: 	pathStr = Tcl_GetStringFromObj(path, &pathLen);
                        21: 	rootStr = Tcl_GetStringFromObj(info->mountpoint, &rootLen);
                        22: 	
                        23: 	if (pathLen < rootLen) {
                        24: 		return(NULL);
                        25: 	}
                        26: 	
                        27: 	if (memcmp(pathStr, rootStr, rootLen) != 0) {
                        28: 		return(NULL);
                        29: 	}
                        30: 	
                        31: 	if (pathLen == rootLen) {
                        32: 		return("");
                        33: 	}
                        34: 
                        35: 	/* XXX:TODO: Should this use the native OS path separator ? */
                        36: 	if (pathStr[rootLen] != '/') {
                        37: 		return(NULL);
                        38: 	}
                        39: 	
                        40: 	return(pathStr + rootLen + 1);
                        41: }
                        42: 
                        43: /*
                        44:  * Internal Tcl_Filesystem functions, with the appropriate instance info
                        45:  */
                        46: static int xvfs_tclfs_pathInFilesystem(Tcl_Obj *path, ClientData *dataPtr, struct xvfs_tclfs_instance_info *instanceInfo) {
                        47: 	const char *relativePath;
                        48: 	
                        49: 	relativePath = xvfs_relativePath(path, instanceInfo);
                        50: 	if (!relativePath) {
                        51: 		return(-1);
                        52: 	}
                        53: 	
                        54: 	return(TCL_OK);
                        55: }
                        56: 
                        57: static int xvfs_tclfs_stat(Tcl_Obj *path, Tcl_StatBuf *statBuf, struct xvfs_tclfs_instance_info *instanceInfo) {
                        58: 	const char *pathStr;
                        59: 	int retval;
                        60: 
                        61: 	pathStr = xvfs_relativePath(path, instanceInfo);
                        62: 	
                        63: 	retval = instanceInfo->fsInfo->getStatProc(pathStr, statBuf);
                        64: 	
                        65: 	return(retval);
                        66: }
                        67: 
                        68: static Tcl_Obj *xvfs_tclfs_listVolumes(struct xvfs_tclfs_instance_info *instanceInfo) {
                        69: 	return(NULL);
                        70: }
                        71: 
                        72: static Tcl_Channel xvfs_tclfs_openFileChannel(Tcl_Interp *interp, Tcl_Obj *path, int mode, int permissions, struct xvfs_tclfs_instance_info *instanceInfo) {
                        73: 	const char *pathStr;
                        74: 
                        75: 	pathStr = xvfs_relativePath(path, instanceInfo);
                        76: fprintf(stderr, "Called open(%s)!\n", pathStr);
                        77: 	
                        78: 	return(NULL);
                        79: }
                        80: #endif /* XVFS_MODE_SERVER || XVFS_MODE_STANDALONE || XVFS_MODE_FLEIXBLE */
                        81: 
                        82: #if defined(XVFS_MODE_STANDALONE) || defined(XVFS_MODE_FLEXIBLE)
                        83: /*
                        84:  * Tcl_Filesystem handlers for the standalone implementation
                        85:  */
                        86: static struct xvfs_tclfs_instance_info xvfs_tclfs_standalone_info;
                        87: static int xvfs_tclfs_standalone_pathInFilesystem(Tcl_Obj *path, ClientData *dataPtr) {
                        88: 	return(xvfs_tclfs_pathInFilesystem(path, dataPtr, &xvfs_tclfs_standalone_info));
                        89: }
                        90: 
                        91: static int xvfs_tclfs_standalone_stat(Tcl_Obj *path, Tcl_StatBuf *statBuf) {
                        92: 	return(xvfs_tclfs_stat(path, statBuf, &xvfs_tclfs_standalone_info));
                        93: }
                        94: 
                        95: static Tcl_Obj *xvfs_tclfs_standalone_listVolumes(void) {
                        96: 	return(xvfs_tclfs_listVolumes(&xvfs_tclfs_standalone_info));
                        97: }
                        98: 
                        99: static Tcl_Channel xvfs_tclfs_standalone_openFileChannel(Tcl_Interp *interp, Tcl_Obj *path, int mode, int permissions) {
                       100: 	return(xvfs_tclfs_openFileChannel(interp, path, mode, permissions, &xvfs_tclfs_standalone_info));
                       101: }
                       102: 
                       103: /*
                       104:  * There are three (3) modes of operation for Xvfs_Register:
                       105:  *    1. standalone -- We register our own Tcl_Filesystem
                       106:  *                     and handle requests under `//xvfs:/<fsName>`
                       107:  *    2. client -- A single Tcl_Filesystem is registered for the
                       108:  *                 interp to handle requests under `//xvfs:/` which
                       109:  *                 then dispatches to the appropriate registered
                       110:  *                 handler
                       111:  *    3. flexible -- Attempts to find a core Xvfs instance for the
                       112:  *                   process at runtime, if found do #2, otherwise
                       113:  *                   fallback to #1
                       114:  *
                       115:  */
                       116: static Tcl_Filesystem xvfs_tclfs_standalone_fs;
                       117: int xvfs_standalone_register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
                       118: 	int tcl_ret;
                       119: 	static int registered = 0;
                       120: 	
                       121: 	/*
                       122: 	 * Ensure this instance is not already registered
                       123: 	 */
                       124: 	if (registered) {
                       125: 		return(TCL_OK);
                       126: 	}
                       127: 	registered = 1;
                       128: 
                       129: 	/*
                       130: 	 * In standalone mode, we only support the same protocol we are
                       131: 	 * compiling for.
                       132: 	 */
                       133: 	if (fsInfo->protocolVersion != XVFS_PROTOCOL_VERSION) {
                       134: 		if (interp) {
                       135: 			Tcl_SetResult(interp, "Protocol mismatch", NULL);
                       136: 		}
                       137: 		return(TCL_ERROR);
                       138: 	}
                       139: 	
                       140: 	xvfs_tclfs_standalone_fs.typeName                   = "xvfs";
                       141: 	xvfs_tclfs_standalone_fs.structureLength            = sizeof(xvfs_tclfs_standalone_fs);
                       142: 	xvfs_tclfs_standalone_fs.version                    = TCL_FILESYSTEM_VERSION_1;
                       143: 	xvfs_tclfs_standalone_fs.pathInFilesystemProc       = xvfs_tclfs_standalone_pathInFilesystem;
                       144: 	xvfs_tclfs_standalone_fs.dupInternalRepProc         = NULL;
                       145: 	xvfs_tclfs_standalone_fs.freeInternalRepProc        = NULL;
                       146: 	xvfs_tclfs_standalone_fs.internalToNormalizedProc   = NULL;
                       147: 	xvfs_tclfs_standalone_fs.createInternalRepProc      = NULL;
                       148: 	xvfs_tclfs_standalone_fs.normalizePathProc          = NULL;
                       149: 	xvfs_tclfs_standalone_fs.filesystemPathTypeProc     = NULL;
                       150: 	xvfs_tclfs_standalone_fs.filesystemSeparatorProc    = NULL;
                       151: 	xvfs_tclfs_standalone_fs.statProc                   = xvfs_tclfs_standalone_stat;
                       152: 	xvfs_tclfs_standalone_fs.accessProc                 = NULL;
                       153: 	xvfs_tclfs_standalone_fs.openFileChannelProc        = xvfs_tclfs_standalone_openFileChannel;
                       154: 	xvfs_tclfs_standalone_fs.matchInDirectoryProc       = NULL;
                       155: 	xvfs_tclfs_standalone_fs.utimeProc                  = NULL;
                       156: 	xvfs_tclfs_standalone_fs.linkProc                   = NULL;
                       157: 	xvfs_tclfs_standalone_fs.listVolumesProc            = xvfs_tclfs_standalone_listVolumes;
                       158: 	xvfs_tclfs_standalone_fs.fileAttrStringsProc        = NULL;
                       159: 	xvfs_tclfs_standalone_fs.fileAttrsGetProc           = NULL;
                       160: 	xvfs_tclfs_standalone_fs.fileAttrsSetProc           = NULL;
                       161: 	xvfs_tclfs_standalone_fs.createDirectoryProc        = NULL;
                       162: 	xvfs_tclfs_standalone_fs.removeDirectoryProc        = NULL;
                       163: 	xvfs_tclfs_standalone_fs.deleteFileProc             = NULL;
                       164: 	xvfs_tclfs_standalone_fs.copyFileProc               = NULL;
                       165: 	xvfs_tclfs_standalone_fs.renameFileProc             = NULL;
                       166: 	xvfs_tclfs_standalone_fs.copyDirectoryProc          = NULL;
                       167: 	xvfs_tclfs_standalone_fs.lstatProc                  = NULL;
                       168: 	xvfs_tclfs_standalone_fs.loadFileProc               = NULL;
                       169: 	xvfs_tclfs_standalone_fs.getCwdProc                 = NULL;
                       170: 	xvfs_tclfs_standalone_fs.chdirProc                  = NULL;
                       171: 
                       172: 	xvfs_tclfs_standalone_info.fsInfo = fsInfo;
                       173: 	xvfs_tclfs_standalone_info.mountpoint = Tcl_NewObj();
                       174: 	Tcl_AppendStringsToObj(xvfs_tclfs_standalone_info.mountpoint, XVFS_ROOT_MOUNTPOINT, fsInfo->name, NULL);
                       175: 	
                       176: 	tcl_ret = Tcl_FSRegister(NULL, &xvfs_tclfs_standalone_fs);
                       177: 	if (tcl_ret != TCL_OK) {
                       178: 		if (interp) {
                       179: 			Tcl_SetResult(interp, "Tcl_FSRegister() failed", NULL);
                       180: 		}
                       181: 		
                       182: 		return(tcl_ret);
                       183: 	}
                       184: 	
                       185: 	return(TCL_OK);
                       186: }
                       187: #endif
                       188: 
                       189: #if defined(XVFS_MODE_FLEXIBLE)
cb77ecfb24 2019-05-06  190: #include <dlfcn.h>
                       191: int xvfs_flexible_register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
cb77ecfb24 2019-05-06  192: 	int (*xvfs_register)(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) = NULL;
                       193: 
                       194: 	/*
cb77ecfb24 2019-05-06  195: 	 * XXX:TODO: Find some way to use Tcl_FindSymbol() to do this
                       196: 	 */
cb77ecfb24 2019-05-06  197: 	xvfs_register = dlsym(NULL, "Xvfs_Register");
cb77ecfb24 2019-05-06  198: 	if (!xvfs_register) {
cb77ecfb24 2019-05-06  199: 		xvfs_register = &xvfs_standalone_register;
                       200: 	}
cb77ecfb24 2019-05-06  201: 	
                       202: 	return(xvfs_register(interp, fsInfo));
                       203: }
                       204: #endif
                       205: 
                       206: #if defined(XVFS_MODE_SERVER)
                       207: int Xvfs_Register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
                       208: 	return(TCL_ERROR);
                       209: }
                       210: #endif