Annotation For xvfs-core.c

Lines of xvfs-core.c from check-in 88f96696b7 that are changed by the sequence of edits moving toward check-in daf25f5222:

                         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: 	
88f96696b7 2019-05-03   63: 	retval = instanceInfo->fsInfo->getInfoProc(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: int xvfs_standalone_register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
                       117: 	Tcl_Filesystem *xvfs_tclfs_Info;
                       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_Info = (Tcl_Filesystem *) Tcl_AttemptAlloc(sizeof(*xvfs_tclfs_Info));
                       141: 	if (!xvfs_tclfs_Info) {
                       142: 		if (interp) {
                       143: 			Tcl_SetResult(interp, "Unable to allocate Tcl_Filesystem object", NULL);
                       144: 		}
                       145: 		return(TCL_ERROR);
                       146: 	}
                       147: 	
                       148: 	xvfs_tclfs_Info->typeName                   = "xvfs";
                       149: 	xvfs_tclfs_Info->structureLength            = sizeof(*xvfs_tclfs_Info);
                       150: 	xvfs_tclfs_Info->version                    = TCL_FILESYSTEM_VERSION_1;
                       151: 	xvfs_tclfs_Info->pathInFilesystemProc       = xvfs_tclfs_standalone_pathInFilesystem;
                       152: 	xvfs_tclfs_Info->dupInternalRepProc         = NULL;
                       153: 	xvfs_tclfs_Info->freeInternalRepProc        = NULL;
                       154: 	xvfs_tclfs_Info->internalToNormalizedProc   = NULL;
                       155: 	xvfs_tclfs_Info->createInternalRepProc      = NULL;
                       156: 	xvfs_tclfs_Info->normalizePathProc          = NULL;
                       157: 	xvfs_tclfs_Info->filesystemPathTypeProc     = NULL;
                       158: 	xvfs_tclfs_Info->filesystemSeparatorProc    = NULL;
                       159: 	xvfs_tclfs_Info->statProc                   = xvfs_tclfs_standalone_stat;
                       160: 	xvfs_tclfs_Info->accessProc                 = NULL;
                       161: 	xvfs_tclfs_Info->openFileChannelProc        = xvfs_tclfs_standalone_openFileChannel;
                       162: 	xvfs_tclfs_Info->matchInDirectoryProc       = NULL;
                       163: 	xvfs_tclfs_Info->utimeProc                  = NULL;
                       164: 	xvfs_tclfs_Info->linkProc                   = NULL;
                       165: 	xvfs_tclfs_Info->listVolumesProc            = xvfs_tclfs_standalone_listVolumes;
                       166: 	xvfs_tclfs_Info->fileAttrStringsProc        = NULL;
                       167: 	xvfs_tclfs_Info->fileAttrsGetProc           = NULL;
                       168: 	xvfs_tclfs_Info->fileAttrsSetProc           = NULL;
                       169: 	xvfs_tclfs_Info->createDirectoryProc        = NULL;
                       170: 	xvfs_tclfs_Info->removeDirectoryProc        = NULL;
                       171: 	xvfs_tclfs_Info->deleteFileProc             = NULL;
                       172: 	xvfs_tclfs_Info->copyFileProc               = NULL;
                       173: 	xvfs_tclfs_Info->renameFileProc             = NULL;
                       174: 	xvfs_tclfs_Info->copyDirectoryProc          = NULL;
                       175: 	xvfs_tclfs_Info->lstatProc                  = NULL;
                       176: 	xvfs_tclfs_Info->loadFileProc               = NULL;
                       177: 	xvfs_tclfs_Info->getCwdProc                 = NULL;
                       178: 	xvfs_tclfs_Info->chdirProc                  = NULL;
                       179: 
                       180: 	xvfs_tclfs_standalone_info.fsInfo = fsInfo;
                       181: 	xvfs_tclfs_standalone_info.mountpoint = Tcl_NewObj();
                       182: 	Tcl_AppendStringsToObj(xvfs_tclfs_standalone_info.mountpoint, XVFS_ROOT_MOUNTPOINT, fsInfo->name, NULL);
                       183: 	
                       184: 	tcl_ret = Tcl_FSRegister(NULL, xvfs_tclfs_Info);
                       185: 	if (tcl_ret != TCL_OK) {
                       186: 		if (interp) {
                       187: 			Tcl_SetResult(interp, "Tcl_FSRegister() failed", NULL);
                       188: 		}
                       189: 		
                       190: 		return(tcl_ret);
                       191: 	}
                       192: 	
                       193: 	return(TCL_OK);
                       194: }
                       195: #endif
                       196: 
                       197: #if defined(XVFS_MODE_FLEXIBLE)
                       198: #include <dlfcn.h>
                       199: int xvfs_flexible_register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
                       200: 	int (*xvfs_register)(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) = NULL;
                       201: 
                       202: 	/*
                       203: 	 * XXX:TODO: Find some way to use Tcl_FindSymbol() to do this
                       204: 	 */
                       205: 	xvfs_register = dlsym(NULL, "Xvfs_Register");
                       206: 	if (!xvfs_register) {
                       207: 		xvfs_register = &xvfs_standalone_register;
                       208: 	}
                       209: 	
                       210: 	return(xvfs_register(interp, fsInfo));
                       211: }
                       212: #endif
                       213: 
                       214: #if defined(XVFS_MODE_SERVER)
                       215: int Xvfs_Register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
                       216: 	return(TCL_ERROR);
                       217: }
                       218: #endif