Annotation For xvfs-core.c

Origin for each line in xvfs-core.c from check-in 149aa89b7d:

69e476dcd5 2019-05-02    1: #include <xvfs-core.h>
d121970301 2019-05-02    2: #include <string.h>
69e476dcd5 2019-05-02    3: #include <tcl.h>
e5b6962adf 2019-05-02    4: 
d92ba3d36d 2019-05-08    5: #if defined(XVFS_MODE_FLEXIBLE) || defined(XVFS_MODE_SERVER)
d92ba3d36d 2019-05-08    6: #define XVFS_INTERNAL_SERVER_MAGIC "\xD4\xF3\x05\x96\x25\xCF\xAF\xFE"
d92ba3d36d 2019-05-08    7: #define XVFS_INTERNAL_SERVER_MAGIC_LEN 8
d92ba3d36d 2019-05-08    8: 
d92ba3d36d 2019-05-08    9: struct xvfs_tclfs_server_info {
b586d5b0a1 2019-05-08   10: 	char magic[XVFS_INTERNAL_SERVER_MAGIC_LEN];
d92ba3d36d 2019-05-08   11: 	int (*registerProc)(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo);
d92ba3d36d 2019-05-08   12: };
d92ba3d36d 2019-05-08   13: #endif /* XVFS_MODE_FLEXIBLE || XVFS_MODE_SERVER */
d92ba3d36d 2019-05-08   14: 
3e44e1def1 2019-05-02   15: #if defined(XVFS_MODE_SERVER) || defined(XVFS_MODE_STANDALONE) || defined(XVFS_MODE_FLEXIBLE)
d121970301 2019-05-02   16: #define XVFS_ROOT_MOUNTPOINT "//xvfs:/"
d121970301 2019-05-02   17: 
d121970301 2019-05-02   18: struct xvfs_tclfs_instance_info {
d121970301 2019-05-02   19: 	struct Xvfs_FSInfo *fsInfo;
d121970301 2019-05-02   20: 	Tcl_Obj            *mountpoint;
d121970301 2019-05-02   21: };
d121970301 2019-05-02   22: 
d121970301 2019-05-02   23: /*
d121970301 2019-05-02   24:  * Internal Core Utilities
d121970301 2019-05-02   25:  */
d121970301 2019-05-02   26: static const char *xvfs_relativePath(Tcl_Obj *path, struct xvfs_tclfs_instance_info *info) {
d121970301 2019-05-02   27: 	const char *pathStr, *rootStr;
d121970301 2019-05-02   28: 	int pathLen, rootLen;
d121970301 2019-05-02   29: 	
d121970301 2019-05-02   30: 	pathStr = Tcl_GetStringFromObj(path, &pathLen);
d121970301 2019-05-02   31: 	rootStr = Tcl_GetStringFromObj(info->mountpoint, &rootLen);
d121970301 2019-05-02   32: 	
d121970301 2019-05-02   33: 	if (pathLen < rootLen) {
d121970301 2019-05-02   34: 		return(NULL);
d121970301 2019-05-02   35: 	}
d121970301 2019-05-02   36: 	
d121970301 2019-05-02   37: 	if (memcmp(pathStr, rootStr, rootLen) != 0) {
d121970301 2019-05-02   38: 		return(NULL);
d121970301 2019-05-02   39: 	}
d121970301 2019-05-02   40: 	
d121970301 2019-05-02   41: 	if (pathLen == rootLen) {
d121970301 2019-05-02   42: 		return("");
d121970301 2019-05-02   43: 	}
d121970301 2019-05-02   44: 
d121970301 2019-05-02   45: 	/* XXX:TODO: Should this use the native OS path separator ? */
d121970301 2019-05-02   46: 	if (pathStr[rootLen] != '/') {
d121970301 2019-05-02   47: 		return(NULL);
d121970301 2019-05-02   48: 	}
d121970301 2019-05-02   49: 	
d121970301 2019-05-02   50: 	return(pathStr + rootLen + 1);
d121970301 2019-05-02   51: }
d121970301 2019-05-02   52: 
149aa89b7d 2019-09-13   53: static const char *xvfs_perror(int xvfs_error) {
149aa89b7d 2019-09-13   54: 	if (xvfs_error >= 0) {
149aa89b7d 2019-09-13   55: 		return("Not an error");
149aa89b7d 2019-09-13   56: 	}
149aa89b7d 2019-09-13   57: 
149aa89b7d 2019-09-13   58: 	switch (xvfs_error) {
149aa89b7d 2019-09-13   59: 		case XVFS_RV_ERR_ENOENT:
149aa89b7d 2019-09-13   60: 			return("No such file or directory");
149aa89b7d 2019-09-13   61: 		case XVFS_RV_ERR_EINVAL:
149aa89b7d 2019-09-13   62: 			return("Invalid argument");
149aa89b7d 2019-09-13   63: 		case XVFS_RV_ERR_EISDIR:
149aa89b7d 2019-09-13   64: 			return("Is a directory");
149aa89b7d 2019-09-13   65: 		case XVFS_RV_ERR_ENOTDIR:
149aa89b7d 2019-09-13   66: 			return("Not a directory");
149aa89b7d 2019-09-13   67: 		case XVFS_RV_ERR_EFAULT:
149aa89b7d 2019-09-13   68: 			return("Bad address");
149aa89b7d 2019-09-13   69: 		default:
149aa89b7d 2019-09-13   70: 			return("Unknown error");
149aa89b7d 2019-09-13   71: 	}
149aa89b7d 2019-09-13   72: }
149aa89b7d 2019-09-13   73: 
d121970301 2019-05-02   74: /*
d121970301 2019-05-02   75:  * Internal Tcl_Filesystem functions, with the appropriate instance info
d121970301 2019-05-02   76:  */
d121970301 2019-05-02   77: static int xvfs_tclfs_pathInFilesystem(Tcl_Obj *path, ClientData *dataPtr, struct xvfs_tclfs_instance_info *instanceInfo) {
d121970301 2019-05-02   78: 	const char *relativePath;
d121970301 2019-05-02   79: 	
d121970301 2019-05-02   80: 	relativePath = xvfs_relativePath(path, instanceInfo);
d121970301 2019-05-02   81: 	if (!relativePath) {
d121970301 2019-05-02   82: 		return(-1);
d121970301 2019-05-02   83: 	}
d121970301 2019-05-02   84: 	
d121970301 2019-05-02   85: 	return(TCL_OK);
d121970301 2019-05-02   86: }
d121970301 2019-05-02   87: 
d121970301 2019-05-02   88: static int xvfs_tclfs_stat(Tcl_Obj *path, Tcl_StatBuf *statBuf, struct xvfs_tclfs_instance_info *instanceInfo) {
d121970301 2019-05-02   89: 	const char *pathStr;
d121970301 2019-05-02   90: 	int retval;
d121970301 2019-05-02   91: 
d121970301 2019-05-02   92: 	pathStr = xvfs_relativePath(path, instanceInfo);
d121970301 2019-05-02   93: 	
daf25f5222 2019-05-03   94: 	retval = instanceInfo->fsInfo->getStatProc(pathStr, statBuf);
149aa89b7d 2019-09-13   95: 	if (retval < 0) {
149aa89b7d 2019-09-13   96: 		retval = -1;
149aa89b7d 2019-09-13   97: 	}
d121970301 2019-05-02   98: 	
d121970301 2019-05-02   99: 	return(retval);
d121970301 2019-05-02  100: }
d121970301 2019-05-02  101: 
d121970301 2019-05-02  102: static Tcl_Obj *xvfs_tclfs_listVolumes(struct xvfs_tclfs_instance_info *instanceInfo) {
d121970301 2019-05-02  103: 	return(NULL);
d121970301 2019-05-02  104: }
d121970301 2019-05-02  105: 
d121970301 2019-05-02  106: static Tcl_Channel xvfs_tclfs_openFileChannel(Tcl_Interp *interp, Tcl_Obj *path, int mode, int permissions, struct xvfs_tclfs_instance_info *instanceInfo) {
d121970301 2019-05-02  107: 	const char *pathStr;
149aa89b7d 2019-09-13  108: 	Tcl_WideInt length;
149aa89b7d 2019-09-13  109: 	const char *data;
d121970301 2019-05-02  110: 
d121970301 2019-05-02  111: 	pathStr = xvfs_relativePath(path, instanceInfo);
149aa89b7d 2019-09-13  112: 
9d3052c6f1 2019-05-08  113: 	/*
9d3052c6f1 2019-05-08  114: 	 * XXX:TODO: Do something to create the Tcl_Channel we
9d3052c6f1 2019-05-08  115: 	 * need to return here
9d3052c6f1 2019-05-08  116: 	 */
9d3052c6f1 2019-05-08  117: 
d121970301 2019-05-02  118: 	return(NULL);
d121970301 2019-05-02  119: }
3e44e1def1 2019-05-02  120: #endif /* XVFS_MODE_SERVER || XVFS_MODE_STANDALONE || XVFS_MODE_FLEIXBLE */
d121970301 2019-05-02  121: 
88f96696b7 2019-05-03  122: #if defined(XVFS_MODE_STANDALONE) || defined(XVFS_MODE_FLEXIBLE)
d121970301 2019-05-02  123: /*
d121970301 2019-05-02  124:  * Tcl_Filesystem handlers for the standalone implementation
d121970301 2019-05-02  125:  */
acfc5037c6 2019-05-02  126: static struct xvfs_tclfs_instance_info xvfs_tclfs_standalone_info;
d121970301 2019-05-02  127: static int xvfs_tclfs_standalone_pathInFilesystem(Tcl_Obj *path, ClientData *dataPtr) {
d121970301 2019-05-02  128: 	return(xvfs_tclfs_pathInFilesystem(path, dataPtr, &xvfs_tclfs_standalone_info));
d121970301 2019-05-02  129: }
d121970301 2019-05-02  130: 
d121970301 2019-05-02  131: static int xvfs_tclfs_standalone_stat(Tcl_Obj *path, Tcl_StatBuf *statBuf) {
d121970301 2019-05-02  132: 	return(xvfs_tclfs_stat(path, statBuf, &xvfs_tclfs_standalone_info));
e5b6962adf 2019-05-02  133: }
e5b6962adf 2019-05-02  134: 
d121970301 2019-05-02  135: static Tcl_Obj *xvfs_tclfs_standalone_listVolumes(void) {
d121970301 2019-05-02  136: 	return(xvfs_tclfs_listVolumes(&xvfs_tclfs_standalone_info));
e5b6962adf 2019-05-02  137: }
e5b6962adf 2019-05-02  138: 
d121970301 2019-05-02  139: static Tcl_Channel xvfs_tclfs_standalone_openFileChannel(Tcl_Interp *interp, Tcl_Obj *path, int mode, int permissions) {
d121970301 2019-05-02  140: 	return(xvfs_tclfs_openFileChannel(interp, path, mode, permissions, &xvfs_tclfs_standalone_info));
e5b6962adf 2019-05-02  141: }
32b55a907b 2019-05-02  142: 
32b55a907b 2019-05-02  143: /*
32b55a907b 2019-05-02  144:  * There are three (3) modes of operation for Xvfs_Register:
32b55a907b 2019-05-02  145:  *    1. standalone -- We register our own Tcl_Filesystem
32b55a907b 2019-05-02  146:  *                     and handle requests under `//xvfs:/<fsName>`
32b55a907b 2019-05-02  147:  *    2. client -- A single Tcl_Filesystem is registered for the
32b55a907b 2019-05-02  148:  *                 interp to handle requests under `//xvfs:/` which
32b55a907b 2019-05-02  149:  *                 then dispatches to the appropriate registered
32b55a907b 2019-05-02  150:  *                 handler
32b55a907b 2019-05-02  151:  *    3. flexible -- Attempts to find a core Xvfs instance for the
32b55a907b 2019-05-02  152:  *                   process at runtime, if found do #2, otherwise
32b55a907b 2019-05-02  153:  *                   fallback to #1
32b55a907b 2019-05-02  154:  *
32b55a907b 2019-05-02  155:  */
cb77ecfb24 2019-05-06  156: static Tcl_Filesystem xvfs_tclfs_standalone_fs;
b8cca3a6b4 2019-05-08  157: static int xvfs_standalone_register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
e5b6962adf 2019-05-02  158: 	int tcl_ret;
d121970301 2019-05-02  159: 	static int registered = 0;
e5b6962adf 2019-05-02  160: 	
d121970301 2019-05-02  161: 	/*
d121970301 2019-05-02  162: 	 * Ensure this instance is not already registered
d121970301 2019-05-02  163: 	 */
d121970301 2019-05-02  164: 	if (registered) {
d121970301 2019-05-02  165: 		return(TCL_OK);
d121970301 2019-05-02  166: 	}
d121970301 2019-05-02  167: 	registered = 1;
d121970301 2019-05-02  168: 
e5b6962adf 2019-05-02  169: 	/*
e5b6962adf 2019-05-02  170: 	 * In standalone mode, we only support the same protocol we are
e5b6962adf 2019-05-02  171: 	 * compiling for.
e5b6962adf 2019-05-02  172: 	 */
e5b6962adf 2019-05-02  173: 	if (fsInfo->protocolVersion != XVFS_PROTOCOL_VERSION) {
e5b6962adf 2019-05-02  174: 		if (interp) {
e5b6962adf 2019-05-02  175: 			Tcl_SetResult(interp, "Protocol mismatch", NULL);
e5b6962adf 2019-05-02  176: 		}
e5b6962adf 2019-05-02  177: 		return(TCL_ERROR);
e5b6962adf 2019-05-02  178: 	}
e5b6962adf 2019-05-02  179: 	
cb77ecfb24 2019-05-06  180: 	xvfs_tclfs_standalone_fs.typeName                   = "xvfs";
cb77ecfb24 2019-05-06  181: 	xvfs_tclfs_standalone_fs.structureLength            = sizeof(xvfs_tclfs_standalone_fs);
cb77ecfb24 2019-05-06  182: 	xvfs_tclfs_standalone_fs.version                    = TCL_FILESYSTEM_VERSION_1;
cb77ecfb24 2019-05-06  183: 	xvfs_tclfs_standalone_fs.pathInFilesystemProc       = xvfs_tclfs_standalone_pathInFilesystem;
cb77ecfb24 2019-05-06  184: 	xvfs_tclfs_standalone_fs.dupInternalRepProc         = NULL;
cb77ecfb24 2019-05-06  185: 	xvfs_tclfs_standalone_fs.freeInternalRepProc        = NULL;
cb77ecfb24 2019-05-06  186: 	xvfs_tclfs_standalone_fs.internalToNormalizedProc   = NULL;
cb77ecfb24 2019-05-06  187: 	xvfs_tclfs_standalone_fs.createInternalRepProc      = NULL;
cb77ecfb24 2019-05-06  188: 	xvfs_tclfs_standalone_fs.normalizePathProc          = NULL;
cb77ecfb24 2019-05-06  189: 	xvfs_tclfs_standalone_fs.filesystemPathTypeProc     = NULL;
cb77ecfb24 2019-05-06  190: 	xvfs_tclfs_standalone_fs.filesystemSeparatorProc    = NULL;
cb77ecfb24 2019-05-06  191: 	xvfs_tclfs_standalone_fs.statProc                   = xvfs_tclfs_standalone_stat;
cb77ecfb24 2019-05-06  192: 	xvfs_tclfs_standalone_fs.accessProc                 = NULL;
cb77ecfb24 2019-05-06  193: 	xvfs_tclfs_standalone_fs.openFileChannelProc        = xvfs_tclfs_standalone_openFileChannel;
cb77ecfb24 2019-05-06  194: 	xvfs_tclfs_standalone_fs.matchInDirectoryProc       = NULL;
cb77ecfb24 2019-05-06  195: 	xvfs_tclfs_standalone_fs.utimeProc                  = NULL;
cb77ecfb24 2019-05-06  196: 	xvfs_tclfs_standalone_fs.linkProc                   = NULL;
cb77ecfb24 2019-05-06  197: 	xvfs_tclfs_standalone_fs.listVolumesProc            = xvfs_tclfs_standalone_listVolumes;
cb77ecfb24 2019-05-06  198: 	xvfs_tclfs_standalone_fs.fileAttrStringsProc        = NULL;
cb77ecfb24 2019-05-06  199: 	xvfs_tclfs_standalone_fs.fileAttrsGetProc           = NULL;
cb77ecfb24 2019-05-06  200: 	xvfs_tclfs_standalone_fs.fileAttrsSetProc           = NULL;
cb77ecfb24 2019-05-06  201: 	xvfs_tclfs_standalone_fs.createDirectoryProc        = NULL;
cb77ecfb24 2019-05-06  202: 	xvfs_tclfs_standalone_fs.removeDirectoryProc        = NULL;
cb77ecfb24 2019-05-06  203: 	xvfs_tclfs_standalone_fs.deleteFileProc             = NULL;
cb77ecfb24 2019-05-06  204: 	xvfs_tclfs_standalone_fs.copyFileProc               = NULL;
cb77ecfb24 2019-05-06  205: 	xvfs_tclfs_standalone_fs.renameFileProc             = NULL;
cb77ecfb24 2019-05-06  206: 	xvfs_tclfs_standalone_fs.copyDirectoryProc          = NULL;
cb77ecfb24 2019-05-06  207: 	xvfs_tclfs_standalone_fs.lstatProc                  = NULL;
cb77ecfb24 2019-05-06  208: 	xvfs_tclfs_standalone_fs.loadFileProc               = NULL;
cb77ecfb24 2019-05-06  209: 	xvfs_tclfs_standalone_fs.getCwdProc                 = NULL;
cb77ecfb24 2019-05-06  210: 	xvfs_tclfs_standalone_fs.chdirProc                  = NULL;
d121970301 2019-05-02  211: 
d121970301 2019-05-02  212: 	xvfs_tclfs_standalone_info.fsInfo = fsInfo;
d121970301 2019-05-02  213: 	xvfs_tclfs_standalone_info.mountpoint = Tcl_NewObj();
d121970301 2019-05-02  214: 	Tcl_AppendStringsToObj(xvfs_tclfs_standalone_info.mountpoint, XVFS_ROOT_MOUNTPOINT, fsInfo->name, NULL);
d121970301 2019-05-02  215: 	
cb77ecfb24 2019-05-06  216: 	tcl_ret = Tcl_FSRegister(NULL, &xvfs_tclfs_standalone_fs);
e5b6962adf 2019-05-02  217: 	if (tcl_ret != TCL_OK) {
e5b6962adf 2019-05-02  218: 		if (interp) {
e5b6962adf 2019-05-02  219: 			Tcl_SetResult(interp, "Tcl_FSRegister() failed", NULL);
e5b6962adf 2019-05-02  220: 		}
e5b6962adf 2019-05-02  221: 		
e5b6962adf 2019-05-02  222: 		return(tcl_ret);
e5b6962adf 2019-05-02  223: 	}
e5b6962adf 2019-05-02  224: 	
e5b6962adf 2019-05-02  225: 	return(TCL_OK);
e5b6962adf 2019-05-02  226: }
d92ba3d36d 2019-05-08  227: #endif /* XVFS_MODE_STANDALONE || XVFS_MODE_FLEXIBLE */
88f96696b7 2019-05-03  228: 
88f96696b7 2019-05-03  229: #if defined(XVFS_MODE_FLEXIBLE)
b8cca3a6b4 2019-05-08  230: static int xvfs_flexible_register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
9bcf758fef 2019-05-08  231: 	ClientData fsHandlerDataRaw;
9bcf758fef 2019-05-08  232: 	struct xvfs_tclfs_server_info *fsHandlerData;
9bcf758fef 2019-05-08  233: 	const Tcl_Filesystem *fsHandler;
9bcf758fef 2019-05-08  234: 	int (*xvfs_register)(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo);
9bcf758fef 2019-05-08  235: 	Tcl_Obj *rootPathObj;
9bcf758fef 2019-05-08  236: 
9bcf758fef 2019-05-08  237: 	xvfs_register = &xvfs_standalone_register;
9bcf758fef 2019-05-08  238: 
9bcf758fef 2019-05-08  239: 	rootPathObj = Tcl_NewStringObj(XVFS_ROOT_MOUNTPOINT, -1);
9bcf758fef 2019-05-08  240: 	if (!rootPathObj) {
9bcf758fef 2019-05-08  241: 		return(xvfs_register(interp, fsInfo));
9bcf758fef 2019-05-08  242: 	}
9bcf758fef 2019-05-08  243: 
9bcf758fef 2019-05-08  244: 	Tcl_IncrRefCount(rootPathObj);
9bcf758fef 2019-05-08  245: 	fsHandler = Tcl_FSGetFileSystemForPath(rootPathObj);
9bcf758fef 2019-05-08  246: 	Tcl_DecrRefCount(rootPathObj);
9bcf758fef 2019-05-08  247: 
9bcf758fef 2019-05-08  248: 	if (!fsHandler) {
9bcf758fef 2019-05-08  249: 		return(xvfs_register(interp, fsInfo));
9bcf758fef 2019-05-08  250: 	}
9bcf758fef 2019-05-08  251: 
9bcf758fef 2019-05-08  252: 	fsHandlerDataRaw = Tcl_FSData(fsHandler);
9bcf758fef 2019-05-08  253: 	if (!fsHandlerDataRaw) {
9bcf758fef 2019-05-08  254: 		return(xvfs_register(interp, fsInfo));
9bcf758fef 2019-05-08  255: 	}
9bcf758fef 2019-05-08  256: 
9bcf758fef 2019-05-08  257: 	fsHandlerData = (struct xvfs_tclfs_server_info *) fsHandlerDataRaw;
88f96696b7 2019-05-03  258: 
88f96696b7 2019-05-03  259: 	/*
9bcf758fef 2019-05-08  260: 	 * XXX:TODO: What is the chance that the handler for //xvfs:/ hold
b586d5b0a1 2019-05-08  261: 	 * client data smaller than XVFS_INTERNAL_SERVER_MAGIC_LEN ?
88f96696b7 2019-05-03  262: 	 */
b586d5b0a1 2019-05-08  263: 	if (memcmp(fsHandlerData->magic, XVFS_INTERNAL_SERVER_MAGIC, sizeof(fsHandlerData->magic)) == 0) {
9bcf758fef 2019-05-08  264: 		xvfs_register = fsHandlerData->registerProc;
88f96696b7 2019-05-03  265: 	}
9bcf758fef 2019-05-08  266: 
88f96696b7 2019-05-03  267: 	return(xvfs_register(interp, fsInfo));
88f96696b7 2019-05-03  268: }
d92ba3d36d 2019-05-08  269: #endif /* XVFS_MODE_FLEXIBLE */
3e44e1def1 2019-05-02  270: 
3e44e1def1 2019-05-02  271: #if defined(XVFS_MODE_SERVER)
e5b6962adf 2019-05-02  272: int Xvfs_Register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
acfc5037c6 2019-05-02  273: 	return(TCL_ERROR);
69e476dcd5 2019-05-02  274: }
d92ba3d36d 2019-05-08  275: #endif /* XVFS_MODE_SERVER */