Annotation For xvfs-core.c

Lines of xvfs-core.c from check-in 0e05b2a8c7 that are changed by the sequence of edits moving toward check-in f1d16a3958:

                         1: #include <xvfs-core.h>
                         2: #include <string.h>
                         3: #include <sys/stat.h>
                         4: #include <errno.h>
                         5: #include <fcntl.h>
                         6: #include <tcl.h>
                         7: 
                         8: #ifdef XVFS_DEBUG
                         9: #include <stdio.h> /* Needed for XVFS_DEBUG_PRINTF */
                        10: static int xvfs_debug_depth = 0;
                        11: #define XVFS_DEBUG_PRINTF(fmt, ...) fprintf(stderr, "[XVFS:DEBUG:%-30s:%4i] %s" fmt "\n", __func__, __LINE__, "                                                                                " + (80 - (xvfs_debug_depth * 4)), __VA_ARGS__)
                        12: #define XVFS_DEBUG_PUTS(str) XVFS_DEBUG_PRINTF("%s", str);
                        13: #define XVFS_DEBUG_ENTER { xvfs_debug_depth++; XVFS_DEBUG_PUTS("Entered"); }
                        14: #define XVFS_DEBUG_LEAVE { XVFS_DEBUG_PUTS("Returning"); xvfs_debug_depth--; }
                        15: #else /* XVFS_DEBUG */
                        16: #define XVFS_DEBUG_PRINTF(fmt, ...) /**/
                        17: #define XVFS_DEBUG_PUTS(str) /**/
                        18: #define XVFS_DEBUG_ENTER /**/
                        19: #define XVFS_DEBUG_LEAVE /**/
                        20: #endif /* XVFS_DEBUG */
                        21: 
                        22: #if defined(XVFS_MODE_FLEXIBLE) || defined(XVFS_MODE_SERVER)
                        23: #define XVFS_INTERNAL_SERVER_MAGIC "\xD4\xF3\x05\x96\x25\xCF\xAF\xFE"
                        24: #define XVFS_INTERNAL_SERVER_MAGIC_LEN 8
                        25: 
                        26: struct xvfs_tclfs_server_info {
                        27: 	char magic[XVFS_INTERNAL_SERVER_MAGIC_LEN];
                        28: 	int (*registerProc)(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo);
                        29: };
                        30: #endif /* XVFS_MODE_FLEXIBLE || XVFS_MODE_SERVER */
                        31: 
                        32: #if defined(XVFS_MODE_SERVER) || defined(XVFS_MODE_STANDALONE) || defined(XVFS_MODE_FLEXIBLE)
                        33: #define XVFS_ROOT_MOUNTPOINT "//xvfs:/"
                        34: 
                        35: struct xvfs_tclfs_instance_info {
                        36: 	struct Xvfs_FSInfo *fsInfo;
                        37: 	Tcl_Obj            *mountpoint;
                        38: };
                        39: 
                        40: /*
                        41:  * Internal Core Utilities
                        42:  */
                        43: static Tcl_Obj *xvfs_absolutePath(Tcl_Obj *path) {
                        44: 	Tcl_Obj *currentDirectory;
                        45: 	const char *pathStr;
                        46: 
                        47: 	XVFS_DEBUG_ENTER;
                        48: 
                        49: 	pathStr = Tcl_GetString(path);
                        50: 
                        51: 	if (pathStr[0] != '/') {
                        52: 		currentDirectory = Tcl_FSGetCwd(NULL);
                        53: 		Tcl_IncrRefCount(currentDirectory);
                        54: 
                        55: 		path = Tcl_ObjPrintf("%s/%s", Tcl_GetString(currentDirectory), pathStr);
                        56: 		Tcl_IncrRefCount(path);
                        57: 		Tcl_DecrRefCount(currentDirectory);
                        58: 	} else {
                        59: 		Tcl_IncrRefCount(path);
                        60: 	}
                        61: 
                        62: 	XVFS_DEBUG_PRINTF("Converted path \"%s\" to absolute path: \"%s\"", pathStr, Tcl_GetString(path));
                        63: 
                        64: 	XVFS_DEBUG_LEAVE;
                        65: 	return(path);
                        66: }
                        67: 
                        68: static const char *xvfs_relativePath(Tcl_Obj *path, struct xvfs_tclfs_instance_info *info) {
                        69: 	const char *pathStr, *rootStr;
                        70: 	const char *pathFinal;
                        71: 	int pathLen, rootLen;
                        72: 
                        73: 	XVFS_DEBUG_ENTER;
                        74: 
                        75: 	rootStr = Tcl_GetStringFromObj(info->mountpoint, &rootLen);
                        76: 	pathStr = Tcl_GetStringFromObj(path, &pathLen);
                        77: 
                        78: 	XVFS_DEBUG_PRINTF("Finding relative path of \"%s\" from \"%s\" ...", pathStr, rootStr);
                        79: 
                        80: 	if (pathLen < rootLen) {
                        81: 		XVFS_DEBUG_PUTS("... none possible (length)");
                        82: 
                        83: 		XVFS_DEBUG_LEAVE;
                        84: 		return(NULL);
                        85: 	}
                        86: 
                        87: 	if (memcmp(pathStr, rootStr, rootLen) != 0) {
                        88: 		XVFS_DEBUG_PUTS("... none possible (prefix differs)");
                        89: 
                        90: 		XVFS_DEBUG_LEAVE;
                        91: 		return(NULL);
                        92: 	}
                        93: 
                        94: 	if (pathLen == rootLen) {
                        95: 		XVFS_DEBUG_PUTS("... short circuit: \"\"");
                        96: 
                        97: 		XVFS_DEBUG_LEAVE;
                        98: 		return("");
                        99: 	}
                       100: 
                       101: 	/* XXX:TODO: Should this use the native OS path separator ? */
                       102: 	if (pathStr[rootLen] != '/') {
                       103: 		XVFS_DEBUG_PUTS("... none possible (no seperator)");
                       104: 
                       105: 		XVFS_DEBUG_LEAVE;
                       106: 		return(NULL);
                       107: 	}
                       108: 
                       109: 	pathFinal = pathStr + rootLen + 1;
                       110: 	pathLen  -= rootLen + 1;
                       111: 
                       112: 	if (pathLen == 1 && memcmp(pathFinal, ".", 1) == 0) {
                       113: 		XVFS_DEBUG_PUTS("... short circuit: \".\" -> \"\"");
                       114: 
                       115: 		XVFS_DEBUG_LEAVE;
                       116: 		return("");
                       117: 	}
                       118: 
                       119: 	while (pathLen >= 2 && memcmp(pathFinal, "./", 2) == 0) {
                       120: 		pathFinal += 2;
                       121: 		pathLen   -= 2;
                       122: 	}
                       123: 
                       124: 	XVFS_DEBUG_PRINTF("... relative path: \"%s\"", pathFinal);
                       125: 
                       126: 	XVFS_DEBUG_LEAVE;
                       127: 	return(pathFinal);
                       128: }
                       129: 
                       130: static int xvfs_errorToErrno(int xvfs_error) {
                       131: 	if (xvfs_error >= 0) {
                       132: 		return(0);
                       133: 	}
                       134: 
                       135: 	switch (xvfs_error) {
                       136: 		case XVFS_RV_ERR_ENOENT:
                       137: 			return(ENOENT);
                       138: 		case XVFS_RV_ERR_EINVAL:
                       139: 			return(EINVAL);
                       140: 		case XVFS_RV_ERR_EISDIR:
                       141: 			return(EISDIR);
                       142: 		case XVFS_RV_ERR_ENOTDIR:
                       143: 			return(ENOTDIR);
                       144: 		case XVFS_RV_ERR_EFAULT:
                       145: 			return(EFAULT);
                       146: 		case XVFS_RV_ERR_EROFS:
                       147: 			return(EROFS);
                       148: 		case XVFS_RV_ERR_INTERNAL:
                       149: 			return(EINVAL);
                       150: 		default:
                       151: 			return(ERANGE);
                       152: 	}
                       153: }
                       154: 
                       155: static const char *xvfs_perror(int xvfs_error) {
                       156: 	if (xvfs_error >= 0) {
                       157: 		return("Not an error");
                       158: 	}
                       159: 
                       160: 	switch (xvfs_error) {
                       161: 		case XVFS_RV_ERR_ENOENT:
                       162: 		case XVFS_RV_ERR_EINVAL:
                       163: 		case XVFS_RV_ERR_EISDIR:
                       164: 		case XVFS_RV_ERR_ENOTDIR:
                       165: 		case XVFS_RV_ERR_EFAULT:
                       166: 		case XVFS_RV_ERR_EROFS:
                       167: 			return(Tcl_ErrnoMsg(xvfs_errorToErrno(xvfs_error)));
                       168: 		case XVFS_RV_ERR_INTERNAL:
                       169: 			return("Internal error");
                       170: 		default:
                       171: 			return("Unknown error");
                       172: 	}
                       173: }
                       174: 
                       175: static void xvfs_setresults_error(Tcl_Interp *interp, int xvfs_error) {
                       176: 	if (!interp) {
                       177: 		return;
                       178: 	}
                       179: 
                       180: 	Tcl_SetErrno(xvfs_errorToErrno(xvfs_error));
                       181: 	Tcl_SetResult(interp, (char *) xvfs_perror(xvfs_error), NULL);
                       182: 
                       183: 	return;
                       184: }
                       185: 
                       186: /*
                       187:  * Xvfs Memory Channel
                       188:  */
                       189: struct xvfs_tclfs_channel_id {
                       190: 	Tcl_Channel channel;
                       191: 	struct xvfs_tclfs_instance_info *fsInstanceInfo;
                       192: 	Tcl_Obj *path;
                       193: 	Tcl_WideInt currentOffset;
                       194: 	Tcl_WideInt fileSize;
                       195: 	int eofMarked;
                       196: 	int queuedEvents;
                       197: 	int closed;
                       198: };
                       199: struct xvfs_tclfs_channel_event {
                       200: 	Tcl_Event tcl;
                       201: 	struct xvfs_tclfs_channel_id *channelInstanceData;
                       202: };
                       203: static Tcl_ChannelType xvfs_tclfs_channelType;
                       204: 
                       205: static Tcl_Channel xvfs_tclfs_openChannel(Tcl_Interp *interp, Tcl_Obj *path, struct xvfs_tclfs_instance_info *instanceInfo) {
                       206: 	struct xvfs_tclfs_channel_id *channelInstanceData;
                       207: 	Tcl_Channel channel;
                       208: 	Tcl_StatBuf fileInfo;
                       209: 	Tcl_Obj *channelName;
                       210: 	int statRet;
                       211: 
                       212: 	XVFS_DEBUG_ENTER;
                       213: 	XVFS_DEBUG_PRINTF("Opening file \"%s\" ...", Tcl_GetString(path));
                       214: 
                       215: 	statRet = instanceInfo->fsInfo->getStatProc(Tcl_GetString(path), &fileInfo);
                       216: 	if (statRet < 0) {
                       217: 		XVFS_DEBUG_PRINTF("... failed: %s", xvfs_perror(statRet));
                       218: 
                       219: 		xvfs_setresults_error(interp, XVFS_RV_ERR_ENOENT);
                       220: 
                       221: 		XVFS_DEBUG_LEAVE;
                       222: 		return(NULL);
                       223: 	}
                       224: 
                       225: 	if (fileInfo.st_mode & 040000) {
                       226: 		XVFS_DEBUG_PUTS("... failed (cannot open directories)");
                       227: 
                       228: 		xvfs_setresults_error(interp, XVFS_RV_ERR_EISDIR);
                       229: 
                       230: 		XVFS_DEBUG_LEAVE;
                       231: 		return(NULL);
                       232: 	}
                       233: 
                       234: 	channelInstanceData = (struct xvfs_tclfs_channel_id *) Tcl_Alloc(sizeof(*channelInstanceData));
                       235: 	channelInstanceData->currentOffset = 0;
                       236: 	channelInstanceData->eofMarked = 0;
                       237: 	channelInstanceData->queuedEvents = 0;
                       238: 	channelInstanceData->closed = 0;
                       239: 	channelInstanceData->channel = NULL;
                       240: 
                       241: 	channelName = Tcl_ObjPrintf("xvfs0x%llx", (unsigned long long) channelInstanceData);
                       242: 	if (!channelName) {
                       243: 		XVFS_DEBUG_PUTS("... failed");
                       244: 
                       245: 		Tcl_Free((char *) channelInstanceData);
                       246: 
                       247: 		XVFS_DEBUG_LEAVE;
                       248: 		return(NULL);
                       249: 	}
                       250: 	Tcl_IncrRefCount(channelName);
                       251: 
                       252: 	channelInstanceData->fsInstanceInfo = instanceInfo;
                       253: 	channelInstanceData->fileSize = fileInfo.st_size;
                       254: 	channelInstanceData->path = path;
                       255: 	Tcl_IncrRefCount(path);
                       256: 
                       257: 	channel = Tcl_CreateChannel(&xvfs_tclfs_channelType, Tcl_GetString(channelName), channelInstanceData, TCL_READABLE);
                       258: 	Tcl_DecrRefCount(channelName);
                       259: 	if (!channel) {
                       260: 		XVFS_DEBUG_PUTS("... failed");
                       261: 
                       262: 		Tcl_DecrRefCount(path);
                       263: 		Tcl_Free((char *) channelInstanceData);
                       264: 
                       265: 		XVFS_DEBUG_LEAVE;
                       266: 		return(NULL);
                       267: 	}
                       268: 
                       269: 	channelInstanceData->channel = channel;
                       270: 
                       271: 	XVFS_DEBUG_PRINTF("... ok (%p)", channelInstanceData);
                       272: 
                       273: 	XVFS_DEBUG_LEAVE;
                       274: 	return(channel);
                       275: }
                       276: 
                       277: static int xvfs_tclfs_closeChannel(ClientData channelInstanceData_p, Tcl_Interp *interp);
                       278: static int xvfs_tclfs_closeChannelEvent(Tcl_Event *event_p, int flags) {
                       279: 	struct xvfs_tclfs_channel_id *channelInstanceData;
                       280: 	struct xvfs_tclfs_channel_event *event;
                       281: 
                       282: 	event = (struct xvfs_tclfs_channel_event *) event_p;
                       283: 	channelInstanceData = event->channelInstanceData;
                       284: 
                       285: 	channelInstanceData->queuedEvents--;
                       286: 
                       287: 	xvfs_tclfs_closeChannel((ClientData) channelInstanceData, NULL);
                       288: 
                       289: 	return(1);
                       290: }
                       291: 
                       292: static int xvfs_tclfs_closeChannel(ClientData channelInstanceData_p, Tcl_Interp *interp) {
                       293: 	struct xvfs_tclfs_channel_id *channelInstanceData;
                       294: 	struct xvfs_tclfs_channel_event *event;
                       295: 
                       296: 	XVFS_DEBUG_ENTER;
                       297: 	XVFS_DEBUG_PRINTF("Closing channel %p ...", channelInstanceData_p);
                       298: 
                       299: 	channelInstanceData = (struct xvfs_tclfs_channel_id *) channelInstanceData_p;
                       300: 
                       301: 	channelInstanceData->closed = 1;
                       302: 
                       303: 	if (channelInstanceData->queuedEvents != 0) {
                       304: 		XVFS_DEBUG_PUTS("... queued");
                       305: 
                       306: 		event = (struct xvfs_tclfs_channel_event *) Tcl_Alloc(sizeof(*event));
                       307: 		event->tcl.proc = xvfs_tclfs_closeChannelEvent;
                       308: 		event->tcl.nextPtr = NULL;
                       309: 		event->channelInstanceData = channelInstanceData;
                       310: 
                       311: 		channelInstanceData->queuedEvents++;
                       312: 
                       313: 		Tcl_QueueEvent((Tcl_Event *) event, TCL_QUEUE_TAIL);
                       314: 
                       315: 		XVFS_DEBUG_LEAVE;
                       316: 		return(0);
                       317: 	}
                       318: 
                       319: 	Tcl_DecrRefCount(channelInstanceData->path);
                       320: 	Tcl_Free((char *) channelInstanceData);
                       321: 
                       322: 	XVFS_DEBUG_PUTS("... ok");
                       323: 
                       324: 	XVFS_DEBUG_LEAVE;
                       325: 	return(0);
                       326: }
                       327: 
                       328: static int xvfs_tclfs_readChannel(ClientData channelInstanceData_p, char *buf, int bufSize, int *errorCodePtr) {
                       329: 	struct xvfs_tclfs_channel_id *channelInstanceData;
                       330: 	const unsigned char *data;
                       331: 	Tcl_WideInt offset, length;
                       332: 	char *path;
                       333: 
                       334: 	channelInstanceData = (struct xvfs_tclfs_channel_id *) channelInstanceData_p;
                       335: 
                       336: 	/*
                       337: 	 * If we are already at the end of the file we can skip
                       338: 	 * attempting to read it
                       339: 	 */
                       340: 	if (channelInstanceData->eofMarked) {
                       341: 		return(0);
                       342: 	}
                       343: 
                       344: 	path = Tcl_GetString(channelInstanceData->path);
                       345: 	offset = channelInstanceData->currentOffset;
                       346: 	length = bufSize;
                       347: 
                       348: 	data = channelInstanceData->fsInstanceInfo->fsInfo->getDataProc(path, offset, &length);
                       349: 
                       350: 	if (length < 0) {
                       351: 		*errorCodePtr = xvfs_errorToErrno(length);
                       352: 
                       353: 		return(-1);
                       354: 	}
                       355: 
                       356: 	if (length == 0) {
                       357: 		channelInstanceData->eofMarked = 1;
                       358: 	} else {
                       359: 		memcpy(buf, data, length);
                       360: 
                       361: 		channelInstanceData->currentOffset += length;
                       362: 	}
                       363: 
                       364: 	return(length);
                       365: }
                       366: 
                       367: static int xvfs_tclfs_watchChannelEvent(Tcl_Event *event_p, int flags) {
                       368: 	struct xvfs_tclfs_channel_id *channelInstanceData;
                       369: 	struct xvfs_tclfs_channel_event *event;
                       370: 
                       371: 	event = (struct xvfs_tclfs_channel_event *) event_p;
                       372: 	channelInstanceData = event->channelInstanceData;
                       373: 
                       374: 	channelInstanceData->queuedEvents--;
                       375: 
                       376: 	if (channelInstanceData->closed) {
                       377: 		return(1);
                       378: 	}
                       379: 
                       380: 	Tcl_NotifyChannel(channelInstanceData->channel, TCL_READABLE);
                       381: 
                       382: 	return(1);
                       383: }
                       384: 
                       385: static void xvfs_tclfs_watchChannel(ClientData channelInstanceData_p, int mask) {
                       386: 	struct xvfs_tclfs_channel_id *channelInstanceData;
                       387: 	struct xvfs_tclfs_channel_event *event;
                       388: 
                       389: 	if ((mask & TCL_READABLE) != TCL_READABLE) {
                       390: 		return;
                       391: 	}
                       392: 
                       393: 	channelInstanceData = (struct xvfs_tclfs_channel_id *) channelInstanceData_p;
                       394: 
                       395: 	/*
                       396: 	 * If the read call has marked that we have reached EOF,
                       397: 	 * do not signal any further
                       398: 	 */
                       399: 	if (channelInstanceData->eofMarked) {
                       400: 		return;
                       401: 	}
                       402: 
                       403: 	event = (struct xvfs_tclfs_channel_event *) Tcl_Alloc(sizeof(*event));
                       404: 	event->tcl.proc = xvfs_tclfs_watchChannelEvent;
                       405: 	event->tcl.nextPtr = NULL;
                       406: 	event->channelInstanceData = channelInstanceData;
                       407: 
                       408: 	channelInstanceData->queuedEvents++;
                       409: 
                       410: 	Tcl_QueueEvent((Tcl_Event *) event, TCL_QUEUE_TAIL);
                       411: 
                       412: 	return;
                       413: }
                       414: 
                       415: static int xvfs_tclfs_seekChannel(ClientData channelInstanceData_p, long offset, int mode, int *errorCodePtr) {
                       416: 	struct xvfs_tclfs_channel_id *channelInstanceData;
                       417: 	Tcl_WideInt newOffset, fileSize;
                       418: 
                       419: 	channelInstanceData = (struct xvfs_tclfs_channel_id *) channelInstanceData_p;
                       420: 
                       421: 	newOffset = channelInstanceData->currentOffset;
                       422: 	fileSize = channelInstanceData->fileSize;
                       423: 
                       424: 	switch (mode) {
                       425: 		case SEEK_CUR:
                       426: 			newOffset += offset;
                       427: 			break;
                       428: 		case SEEK_SET:
                       429: 			newOffset = offset;
                       430: 			break;
                       431: 		case SEEK_END:
                       432: 			newOffset = fileSize + offset;
                       433: 			break;
                       434: 		default:
                       435: 			*errorCodePtr = xvfs_errorToErrno(XVFS_RV_ERR_EINVAL);
                       436: 
                       437: 			return(-1);
                       438: 	}
                       439: 
                       440: 	/*
                       441: 	 * We allow users to seek right up to the end of the buffer, but
                       442: 	 * no further, this way if they want to seek backwards from there
                       443: 	 * it is possible to do so.
                       444: 	 */
                       445: 	if (newOffset < 0 || newOffset > fileSize) {
                       446: 		*errorCodePtr = xvfs_errorToErrno(XVFS_RV_ERR_EINVAL);
                       447: 
                       448: 		return(-1);
                       449: 	}
                       450: 
                       451: 	if (newOffset != channelInstanceData->currentOffset) {
                       452: 		channelInstanceData->eofMarked = 0;
                       453: 		channelInstanceData->currentOffset = newOffset;
                       454: 	}
                       455: 
                       456: 	return(channelInstanceData->currentOffset);
                       457: }
                       458: 
                       459: static void xvfs_tclfs_prepareChannelType(void) {
                       460: 	xvfs_tclfs_channelType.typeName = "xvfs";
                       461: 	xvfs_tclfs_channelType.version = TCL_CHANNEL_VERSION_2;
                       462: 	xvfs_tclfs_channelType.closeProc = xvfs_tclfs_closeChannel;
                       463: 	xvfs_tclfs_channelType.inputProc = xvfs_tclfs_readChannel;
                       464: 	xvfs_tclfs_channelType.outputProc = NULL;
                       465: 	xvfs_tclfs_channelType.watchProc = xvfs_tclfs_watchChannel;
                       466: 	xvfs_tclfs_channelType.getHandleProc = NULL;
                       467: 	xvfs_tclfs_channelType.seekProc = xvfs_tclfs_seekChannel;
                       468: 	xvfs_tclfs_channelType.setOptionProc = NULL;
                       469: 	xvfs_tclfs_channelType.getOptionProc = NULL;
                       470: 	xvfs_tclfs_channelType.close2Proc = NULL;
                       471: 	xvfs_tclfs_channelType.blockModeProc = NULL;
                       472: 	xvfs_tclfs_channelType.flushProc = NULL;
                       473: 	xvfs_tclfs_channelType.handlerProc = NULL;
                       474: 	xvfs_tclfs_channelType.wideSeekProc = NULL;
                       475: 	xvfs_tclfs_channelType.threadActionProc = NULL;
                       476: 	xvfs_tclfs_channelType.truncateProc = NULL;
                       477: }
                       478: 
                       479: /*
                       480:  * Internal Tcl_Filesystem functions, with the appropriate instance info
                       481:  */
                       482: static int xvfs_tclfs_pathInFilesystem(Tcl_Obj *path, ClientData *dataPtr, struct xvfs_tclfs_instance_info *instanceInfo) {
                       483: 	const char *relativePath;
                       484: 	int retval;
                       485: 
                       486: 	XVFS_DEBUG_ENTER;
                       487: 
                       488: 	XVFS_DEBUG_PRINTF("Checking to see if path \"%s\" is in the filesystem ...", Tcl_GetString(path));
                       489: 
                       490: 	path = xvfs_absolutePath(path);
                       491: 
                       492: 	relativePath = xvfs_relativePath(path, instanceInfo);
                       493: 
                       494: 	retval = TCL_OK;
                       495: 	if (!relativePath) {
                       496: 		retval = -1;
                       497: 	}
                       498: 
                       499: 	Tcl_DecrRefCount(path);
                       500: 
                       501: 	XVFS_DEBUG_PRINTF("... %s", retval == -1 ? "no" : "yes");
                       502: 
                       503: 	XVFS_DEBUG_LEAVE;
                       504: 	return(retval);
                       505: }
                       506: 
                       507: static int xvfs_tclfs_stat(Tcl_Obj *path, Tcl_StatBuf *statBuf, struct xvfs_tclfs_instance_info *instanceInfo) {
                       508: 	const char *pathStr;
                       509: 	int retval;
                       510: 
                       511: 	XVFS_DEBUG_ENTER;
                       512: 
                       513: 	XVFS_DEBUG_PRINTF("Getting stat() on \"%s\" ...", Tcl_GetString(path));
                       514: 
                       515: 	path = xvfs_absolutePath(path);
                       516: 
                       517: 	pathStr = xvfs_relativePath(path, instanceInfo);
                       518: 
                       519: 	retval = instanceInfo->fsInfo->getStatProc(pathStr, statBuf);
                       520: 	if (retval < 0) {
                       521: 		XVFS_DEBUG_PRINTF("... failed: %s", xvfs_perror(retval));
                       522: 		retval = -1;
                       523: 	} else {
                       524: 		XVFS_DEBUG_PUTS("... ok");
                       525: 	}
                       526: 
                       527: 	Tcl_DecrRefCount(path);
                       528: 
                       529: 	XVFS_DEBUG_LEAVE;
                       530: 	return(retval);
                       531: }
                       532: 
                       533: static int xvfs_tclfs_access(Tcl_Obj *path, int mode, struct xvfs_tclfs_instance_info *instanceInfo) {
                       534: 	const char *pathStr;
                       535: 	Tcl_StatBuf fileInfo;
                       536: 	int statRetVal;
                       537: 
                       538: 	XVFS_DEBUG_ENTER;
                       539: 
                       540: 	XVFS_DEBUG_PRINTF("Getting access(..., %i) on \"%s\" ...", mode, Tcl_GetString(path));
                       541: 
                       542: 	if (mode & W_OK) {
                       543: 		XVFS_DEBUG_PUTS("... no (not writable)");
                       544: 
                       545: 		XVFS_DEBUG_LEAVE;
                       546: 		return(-1);
                       547: 	}
                       548: 
                       549: 	path = xvfs_absolutePath(path);
                       550: 
                       551: 	pathStr = xvfs_relativePath(path, instanceInfo);
                       552: 	if (!pathStr) {
                       553: 		XVFS_DEBUG_PUTS("... no (not in our path)");
                       554: 
                       555: 		Tcl_DecrRefCount(path);
                       556: 
                       557: 		XVFS_DEBUG_LEAVE;
                       558: 		return(-1);
                       559: 	}
                       560: 
                       561: 	statRetVal = instanceInfo->fsInfo->getStatProc(pathStr, &fileInfo);
                       562: 	if (statRetVal < 0) {
                       563: 		XVFS_DEBUG_PUTS("... no (not statable)");
                       564: 
                       565: 		Tcl_DecrRefCount(path);
                       566: 
                       567: 		XVFS_DEBUG_LEAVE;
                       568: 		return(-1);
                       569: 	}
                       570: 
                       571: 	if (mode & X_OK) {
                       572: 		if (!(fileInfo.st_mode & 040000)) {
                       573: 			XVFS_DEBUG_PUTS("... no (not a directory and X_OK specified)");
                       574: 
                       575: 			Tcl_DecrRefCount(path);
                       576: 
                       577: 			XVFS_DEBUG_LEAVE;
                       578: 			return(-1);
                       579: 		}
                       580: 	}
                       581: 
                       582: 	Tcl_DecrRefCount(path);
                       583: 
                       584: 	XVFS_DEBUG_PUTS("... ok");
                       585: 
                       586: 	XVFS_DEBUG_LEAVE;
                       587: 	return(0);
                       588: }
                       589: 
                       590: static Tcl_Channel xvfs_tclfs_openFileChannel(Tcl_Interp *interp, Tcl_Obj *path, int mode, int permissions, struct xvfs_tclfs_instance_info *instanceInfo) {
                       591: 	Tcl_Channel retval;
                       592: 	Tcl_Obj *pathRel;
                       593: 	const char *pathStr;
                       594: 
                       595: 	XVFS_DEBUG_ENTER;
                       596: 
                       597: 	XVFS_DEBUG_PRINTF("Asked to open(\"%s\", %x)...", Tcl_GetString(path), mode);
                       598: 
                       599: 	if (mode & O_WRONLY) {
                       600: 		XVFS_DEBUG_PUTS("... failed (asked to open for writing)");
                       601: 
                       602: 		xvfs_setresults_error(interp, XVFS_RV_ERR_EROFS);
                       603: 
                       604: 		XVFS_DEBUG_LEAVE;
                       605: 		return(NULL);
                       606: 	}
                       607: 
                       608: 	path = xvfs_absolutePath(path);
                       609: 
                       610: 	pathStr = xvfs_relativePath(path, instanceInfo);
                       611: 
                       612: 	pathRel = Tcl_NewStringObj(pathStr, -1);
                       613: 
                       614: 	Tcl_DecrRefCount(path);
                       615: 
                       616: 	XVFS_DEBUG_PUTS("... done, passing off to channel handler");
                       617: 
                       618: 	retval = xvfs_tclfs_openChannel(interp, pathRel, instanceInfo);
                       619: 
                       620: 	XVFS_DEBUG_LEAVE;
                       621: 	return(retval);
                       622: }
                       623: 
                       624: static int xvfs_tclfs_verifyType(Tcl_Obj *path, Tcl_GlobTypeData *types, struct xvfs_tclfs_instance_info *instanceInfo) {
                       625: 	const char *pathStr;
                       626: 	Tcl_StatBuf fileInfo;
                       627: 	int statRetVal;
                       628: 
                       629: 	XVFS_DEBUG_ENTER;
                       630: 
                       631: 	if (types) {
                       632: 		XVFS_DEBUG_PRINTF("Asked to verify the existence and type of \"%s\" matches type=%i and perm=%i ...", Tcl_GetString(path), types->type, types->perm);
                       633: 	} else {
                       634: 		XVFS_DEBUG_PRINTF("Asked to verify the existence \"%s\" ...", Tcl_GetString(path));
                       635: 	}
                       636: 
                       637: 	statRetVal = xvfs_tclfs_stat(path, &fileInfo, instanceInfo);
                       638: 	if (statRetVal != 0) {
                       639: 		XVFS_DEBUG_PUTS("... no (cannot stat)");
                       640: 
                       641: 		XVFS_DEBUG_LEAVE;
                       642: 		return(0);
                       643: 	}
                       644: 
                       645: 	if (!types) {
                       646: 		XVFS_DEBUG_PUTS("... yes");
                       647: 
                       648: 		XVFS_DEBUG_LEAVE;
                       649: 		return(1);
                       650: 	}
                       651: 
                       652: 	if (types->perm != TCL_GLOB_PERM_RONLY) {
                       653: 		if (types->perm & (TCL_GLOB_PERM_W | TCL_GLOB_PERM_HIDDEN)) {
                       654: 			XVFS_DEBUG_PUTS("... no (checked for writable or hidden, not supported)");
                       655: 
                       656: 			XVFS_DEBUG_LEAVE;
                       657: 			return(0);
                       658: 		}
                       659: 
                       660: 		if ((types->perm & TCL_GLOB_PERM_X) == TCL_GLOB_PERM_X) {
                       661: 			if (!(fileInfo.st_mode & 040000)) {
                       662: 				XVFS_DEBUG_PUTS("... no (checked for executable but not a directory)");
                       663: 
                       664: 				XVFS_DEBUG_LEAVE;
                       665: 				return(0);
                       666: 			}
                       667: 		}
                       668: 	}
                       669: 
                       670: 	if (types->type & (TCL_GLOB_TYPE_BLOCK | TCL_GLOB_TYPE_CHAR | TCL_GLOB_TYPE_PIPE | TCL_GLOB_TYPE_SOCK | TCL_GLOB_TYPE_LINK)) {
                       671: 		XVFS_DEBUG_PUTS("... no (checked for block, char, pipe, sock, or link, not supported)");
                       672: 
                       673: 		XVFS_DEBUG_LEAVE;
                       674: 		return(0);
                       675: 	}
                       676: 
                       677: 	if ((types->type & TCL_GLOB_TYPE_DIR) == TCL_GLOB_TYPE_DIR) {
                       678: 		if (!(fileInfo.st_mode & 040000)) {
                       679: 			XVFS_DEBUG_PUTS("... no (checked for directory but not a directory)");
                       680: 
                       681: 			XVFS_DEBUG_LEAVE;
                       682: 			return(0);
                       683: 		}
                       684: 	}
                       685: 
                       686: 	if ((types->type & TCL_GLOB_TYPE_FILE) == TCL_GLOB_TYPE_FILE) {
                       687: 		if (!(fileInfo.st_mode & 0100000)) {
                       688: 			XVFS_DEBUG_PUTS("... no (checked for file but not a file)");
                       689: 
                       690: 			XVFS_DEBUG_LEAVE;
                       691: 			return(0);
                       692: 		}
                       693: 	}
                       694: 
                       695: 	if ((types->type & TCL_GLOB_TYPE_MOUNT) == TCL_GLOB_TYPE_MOUNT) {
                       696: 		path = xvfs_absolutePath(path);
                       697: 		pathStr = xvfs_relativePath(path, instanceInfo);
                       698: 		if (!pathStr) {
                       699: 			XVFS_DEBUG_PUTS("... no (checked for mount but not able to resolve path)");
                       700: 
                       701: 			Tcl_DecrRefCount(path);
                       702: 
                       703: 			XVFS_DEBUG_LEAVE;
                       704: 			return(0);
                       705: 		}
                       706: 
                       707: 		if (strlen(pathStr) != 0) {
                       708: 			XVFS_DEBUG_PUTS("... no (checked for mount but not our top-level directory)");
                       709: 
                       710: 			Tcl_DecrRefCount(path);
                       711: 
                       712: 			XVFS_DEBUG_LEAVE;
                       713: 			return(0);
                       714: 		}
                       715: 
                       716: 		Tcl_DecrRefCount(path);
                       717: 	}
                       718: 
                       719: 	XVFS_DEBUG_PUTS("... yes");
                       720: 
                       721: 	XVFS_DEBUG_LEAVE;
                       722: 	return(1);
                       723: }
                       724: 
                       725: static int xvfs_tclfs_matchInDir(Tcl_Interp *interp, Tcl_Obj *resultPtr, Tcl_Obj *path, const char *pattern, Tcl_GlobTypeData *types, struct xvfs_tclfs_instance_info *instanceInfo) {
                       726: 	const char *pathStr;
                       727: 	const char **children, *child;
                       728: 	Tcl_WideInt childrenCount, idx;
                       729: 	Tcl_Obj *childObj;
                       730: 	int tclRetVal;
                       731: 
                       732: 	if (pattern == NULL) {
                       733: 		if (xvfs_tclfs_verifyType(path, types, instanceInfo)) {
                       734: 			return(TCL_OK);
                       735: 		}
                       736: 
                       737: 		return(TCL_ERROR);
                       738: 	}
                       739: 
                       740: 	XVFS_DEBUG_ENTER;
                       741: 
                       742: 	path = xvfs_absolutePath(path);
                       743: 
                       744: 	if (types) {
                       745: 		XVFS_DEBUG_PRINTF("Checking for files matching %s in \"%s\" and type=%i and perm=%i ...", pattern, Tcl_GetString(path), types->type, types->perm);
                       746: 	} else {
                       747: 		XVFS_DEBUG_PRINTF("Checking for files matching %s in \"%s\" ...", pattern, Tcl_GetString(path));
                       748: 	}
                       749: 
                       750: 	pathStr = xvfs_relativePath(path, instanceInfo);
                       751: 	if (!pathStr) {
                       752: 		XVFS_DEBUG_PUTS("... error (not in our VFS)");
                       753: 
                       754: 		Tcl_DecrRefCount(path);
                       755: 
                       756: 		xvfs_setresults_error(interp, XVFS_RV_ERR_ENOENT);
                       757: 
                       758: 		XVFS_DEBUG_LEAVE;
                       759: 		return(TCL_OK);
                       760: 	}
                       761: 
                       762: 	childrenCount = 0;
                       763: 	children = instanceInfo->fsInfo->getChildrenProc(pathStr, &childrenCount);
                       764: 	if (childrenCount < 0) {
                       765: 		XVFS_DEBUG_PRINTF("... error: %s", xvfs_perror(childrenCount));
                       766: 
                       767: 		Tcl_DecrRefCount(path);
                       768: 
                       769: 		xvfs_setresults_error(interp, childrenCount);
                       770: 
                       771: 		XVFS_DEBUG_LEAVE;
                       772: 		return(TCL_ERROR);
                       773: 	}
                       774: 
                       775: 	for (idx = 0; idx < childrenCount; idx++) {
                       776: 		child = children[idx];
                       777: 
                       778: 		if (!Tcl_StringMatch(child, pattern)) {
                       779: 			continue;
                       780: 		}
                       781: 
                       782: 		childObj = Tcl_DuplicateObj(path);
                       783: 		Tcl_IncrRefCount(childObj);
                       784: 		Tcl_AppendStringsToObj(childObj, "/", child, NULL);
                       785: 
                       786: 		if (!xvfs_tclfs_verifyType(childObj, types, instanceInfo)) {
                       787: 			Tcl_DecrRefCount(childObj);
                       788: 
                       789: 			continue;
                       790: 		}
                       791: 
                       792: 		tclRetVal = Tcl_ListObjAppendElement(interp, resultPtr, childObj);
                       793: 		Tcl_DecrRefCount(childObj);
                       794: 
                       795: 		if (tclRetVal != TCL_OK) {
                       796: 			XVFS_DEBUG_PUTS("... error (lappend)");
                       797: 			Tcl_DecrRefCount(path);
                       798: 
                       799: 			XVFS_DEBUG_LEAVE;
                       800: 			return(tclRetVal);
                       801: 		}
                       802: 	}
                       803: 
                       804: 	Tcl_DecrRefCount(path);
                       805: 
                       806: 	XVFS_DEBUG_PRINTF("... ok (returning items: %s)", Tcl_GetString(resultPtr));
                       807: 
                       808: 	XVFS_DEBUG_LEAVE;
                       809: 	return(TCL_OK);
                       810: }
                       811: #endif /* XVFS_MODE_SERVER || XVFS_MODE_STANDALONE || XVFS_MODE_FLEIXBLE */
                       812: 
                       813: #if defined(XVFS_MODE_STANDALONE) || defined(XVFS_MODE_FLEXIBLE)
                       814: /*
                       815:  * Tcl_Filesystem handlers for the standalone implementation
                       816:  */
                       817: static struct xvfs_tclfs_instance_info xvfs_tclfs_standalone_info;
                       818: static int xvfs_tclfs_standalone_pathInFilesystem(Tcl_Obj *path, ClientData *dataPtr) {
                       819: 	return(xvfs_tclfs_pathInFilesystem(path, dataPtr, &xvfs_tclfs_standalone_info));
                       820: }
                       821: 
                       822: static int xvfs_tclfs_standalone_stat(Tcl_Obj *path, Tcl_StatBuf *statBuf) {
                       823: 	return(xvfs_tclfs_stat(path, statBuf, &xvfs_tclfs_standalone_info));
                       824: }
                       825: 
                       826: static int xvfs_tclfs_standalone_access(Tcl_Obj *path, int mode) {
                       827: 	return(xvfs_tclfs_access(path, mode, &xvfs_tclfs_standalone_info));
                       828: }
                       829: 
                       830: static Tcl_Channel xvfs_tclfs_standalone_openFileChannel(Tcl_Interp *interp, Tcl_Obj *path, int mode, int permissions) {
                       831: 	return(xvfs_tclfs_openFileChannel(interp, path, mode, permissions, &xvfs_tclfs_standalone_info));
                       832: }
                       833: 
                       834: static int xvfs_tclfs_standalone_matchInDir(Tcl_Interp *interp, Tcl_Obj *resultPtr, Tcl_Obj *pathPtr, const char *pattern, Tcl_GlobTypeData *types) {
                       835: 	return(xvfs_tclfs_matchInDir(interp, resultPtr, pathPtr, pattern, types, &xvfs_tclfs_standalone_info));
                       836: }
                       837: 
                       838: /*
                       839:  * There are three (3) modes of operation for Xvfs_Register:
                       840:  *    1. standalone -- We register our own Tcl_Filesystem
                       841:  *                     and handle requests under `//xvfs:/<fsName>`
                       842:  *    2. client -- A single Tcl_Filesystem is registered for the
                       843:  *                 interp to handle requests under `//xvfs:/` which
                       844:  *                 then dispatches to the appropriate registered
                       845:  *                 handler
                       846:  *    3. flexible -- Attempts to find a core Xvfs instance for the
                       847:  *                   process at runtime, if found do #2, otherwise
                       848:  *                   fallback to #1
                       849:  *
                       850:  */
                       851: static Tcl_Filesystem xvfs_tclfs_standalone_fs;
                       852: static int xvfs_standalone_register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
                       853: 	int tclRet;
                       854: 	static int registered = 0;
                       855: 
                       856: 	/*
                       857: 	 * Ensure this instance is not already registered
                       858: 	 */
                       859: 	if (registered) {
                       860: 		return(TCL_OK);
                       861: 	}
                       862: 	registered = 1;
                       863: 
                       864: 	/*
                       865: 	 * In standalone mode, we only support the same protocol we are
                       866: 	 * compiling for.
                       867: 	 */
                       868: 	if (fsInfo->protocolVersion != XVFS_PROTOCOL_VERSION) {
                       869: 		if (interp) {
                       870: 			Tcl_SetResult(interp, "Protocol mismatch", NULL);
                       871: 		}
                       872: 		return(TCL_ERROR);
                       873: 	}
                       874: 
                       875: 	xvfs_tclfs_standalone_fs.typeName                   = "xvfsInstance";
                       876: 	xvfs_tclfs_standalone_fs.structureLength            = sizeof(xvfs_tclfs_standalone_fs);
                       877: 	xvfs_tclfs_standalone_fs.version                    = TCL_FILESYSTEM_VERSION_1;
                       878: 	xvfs_tclfs_standalone_fs.pathInFilesystemProc       = xvfs_tclfs_standalone_pathInFilesystem;
                       879: 	xvfs_tclfs_standalone_fs.dupInternalRepProc         = NULL;
                       880: 	xvfs_tclfs_standalone_fs.freeInternalRepProc        = NULL;
                       881: 	xvfs_tclfs_standalone_fs.internalToNormalizedProc   = NULL;
                       882: 	xvfs_tclfs_standalone_fs.createInternalRepProc      = NULL;
                       883: 	xvfs_tclfs_standalone_fs.normalizePathProc          = NULL;
                       884: 	xvfs_tclfs_standalone_fs.filesystemPathTypeProc     = NULL;
                       885: 	xvfs_tclfs_standalone_fs.filesystemSeparatorProc    = NULL;
                       886: 	xvfs_tclfs_standalone_fs.statProc                   = xvfs_tclfs_standalone_stat;
                       887: 	xvfs_tclfs_standalone_fs.accessProc                 = xvfs_tclfs_standalone_access;
                       888: 	xvfs_tclfs_standalone_fs.openFileChannelProc        = xvfs_tclfs_standalone_openFileChannel;
                       889: 	xvfs_tclfs_standalone_fs.matchInDirectoryProc       = xvfs_tclfs_standalone_matchInDir;
                       890: 	xvfs_tclfs_standalone_fs.utimeProc                  = NULL;
                       891: 	xvfs_tclfs_standalone_fs.linkProc                   = NULL;
                       892: 	xvfs_tclfs_standalone_fs.listVolumesProc            = NULL;
                       893: 	xvfs_tclfs_standalone_fs.fileAttrStringsProc        = NULL;
                       894: 	xvfs_tclfs_standalone_fs.fileAttrsGetProc           = NULL;
                       895: 	xvfs_tclfs_standalone_fs.fileAttrsSetProc           = NULL;
                       896: 	xvfs_tclfs_standalone_fs.createDirectoryProc        = NULL;
                       897: 	xvfs_tclfs_standalone_fs.removeDirectoryProc        = NULL;
                       898: 	xvfs_tclfs_standalone_fs.deleteFileProc             = NULL;
                       899: 	xvfs_tclfs_standalone_fs.copyFileProc               = NULL;
                       900: 	xvfs_tclfs_standalone_fs.renameFileProc             = NULL;
                       901: 	xvfs_tclfs_standalone_fs.copyDirectoryProc          = NULL;
                       902: 	xvfs_tclfs_standalone_fs.lstatProc                  = NULL;
                       903: 	xvfs_tclfs_standalone_fs.loadFileProc               = NULL;
                       904: 	xvfs_tclfs_standalone_fs.getCwdProc                 = NULL;
                       905: 	xvfs_tclfs_standalone_fs.chdirProc                  = NULL;
                       906: 
                       907: 	xvfs_tclfs_standalone_info.fsInfo = fsInfo;
                       908: 	xvfs_tclfs_standalone_info.mountpoint = Tcl_NewObj();
                       909: 
                       910: 	Tcl_IncrRefCount(xvfs_tclfs_standalone_info.mountpoint);
                       911: 	Tcl_AppendStringsToObj(xvfs_tclfs_standalone_info.mountpoint, XVFS_ROOT_MOUNTPOINT, fsInfo->name, NULL);
                       912: 	
                       913: 	tclRet = Tcl_FSRegister(NULL, &xvfs_tclfs_standalone_fs);
                       914: 	if (tclRet != TCL_OK) {
                       915: 		Tcl_DecrRefCount(xvfs_tclfs_standalone_info.mountpoint);
                       916: 
                       917: 		if (interp) {
                       918: 			Tcl_SetResult(interp, "Tcl_FSRegister() failed", NULL);
                       919: 		}
                       920: 
                       921: 		return(tclRet);
                       922: 	}
                       923: 
                       924: 	xvfs_tclfs_prepareChannelType();
                       925: 
                       926: 	return(TCL_OK);
                       927: }
                       928: #endif /* XVFS_MODE_STANDALONE || XVFS_MODE_FLEXIBLE */
                       929: 
                       930: #if defined(XVFS_MODE_FLEXIBLE)
                       931: static int xvfs_flexible_register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
                       932: 	ClientData fsHandlerDataRaw;
                       933: 	struct xvfs_tclfs_server_info *fsHandlerData;
                       934: 	const Tcl_Filesystem *fsHandler;
                       935: 	int (*xvfs_register)(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo);
                       936: 	Tcl_Obj *rootPathObj;
                       937: 
                       938: 	xvfs_register = &xvfs_standalone_register;
                       939: 
                       940: 	rootPathObj = Tcl_NewStringObj(XVFS_ROOT_MOUNTPOINT, -1);
                       941: 	if (!rootPathObj) {
                       942: 		return(xvfs_register(interp, fsInfo));
                       943: 	}
                       944: 
                       945: 	Tcl_IncrRefCount(rootPathObj);
                       946: 	fsHandler = Tcl_FSGetFileSystemForPath(rootPathObj);
                       947: 	Tcl_DecrRefCount(rootPathObj);
                       948: 
                       949: 	if (!fsHandler) {
                       950: 		return(xvfs_register(interp, fsInfo));
                       951: 	}
                       952: 
                       953: 	fsHandlerDataRaw = Tcl_FSData(fsHandler);
                       954: 	if (!fsHandlerDataRaw) {
                       955: 		return(xvfs_register(interp, fsInfo));
                       956: 	}
                       957: 
                       958: 	fsHandlerData = (struct xvfs_tclfs_server_info *) fsHandlerDataRaw;
                       959: 
                       960: 	/*
                       961: 	 * XXX:TODO: What is the chance that the handler for //xvfs:/ hold
                       962: 	 * client data smaller than XVFS_INTERNAL_SERVER_MAGIC_LEN ?
                       963: 	 */
                       964: 	if (memcmp(fsHandlerData->magic, XVFS_INTERNAL_SERVER_MAGIC, sizeof(fsHandlerData->magic)) == 0) {
                       965: 		xvfs_register = fsHandlerData->registerProc;
                       966: 	}
                       967: 
                       968: 	return(xvfs_register(interp, fsInfo));
                       969: }
                       970: #endif /* XVFS_MODE_FLEXIBLE */
                       971: 
                       972: #if defined(XVFS_MODE_SERVER)
                       973: static Tcl_Filesystem xvfs_tclfs_dispatch_fs;
                       974: static Tcl_HashTable xvfs_tclfs_dispatch_map;
                       975: 
0e05b2a8c7 2019-09-16  976: static struct xvfs_tclfs_instance_info *xvfs_tclfs_dispatch_pathToInstanceInfo(Tcl_Obj *path) {
0e05b2a8c7 2019-09-16  977: 	Tcl_HashEntry *mapEntry;
0e05b2a8c7 2019-09-16  978: 	struct xvfs_tclfs_instance_info *retval;
                       979: 	const char *pathStr, *rootStr;
0e05b2a8c7 2019-09-16  980: 	char *fsName, *fsNameEnds, origSep;
                       981: 	int pathLen, rootLen;
                       982: 
                       983: 	XVFS_DEBUG_ENTER;
                       984: 
0e05b2a8c7 2019-09-16  985: 	XVFS_DEBUG_PRINTF("Looking up path \"%s\" ...", Tcl_GetString(path));
                       986: 	
                       987: 	rootStr = XVFS_ROOT_MOUNTPOINT;
                       988: 	rootLen = strlen(XVFS_ROOT_MOUNTPOINT);
                       989: 
                       990: 	pathStr = Tcl_GetStringFromObj(path, &pathLen);
                       991: 
                       992: 	if (pathLen < rootLen) {
                       993: 		XVFS_DEBUG_PUTS("... failed (length too short)");
                       994: 		XVFS_DEBUG_LEAVE;
0e05b2a8c7 2019-09-16  995: 		return(NULL);
                       996: 	}
                       997: 
                       998: 	if (memcmp(pathStr, rootStr, rootLen) != 0) {
                       999: 		XVFS_DEBUG_PUTS("... failed (incorrect prefix)");
                      1000: 		XVFS_DEBUG_LEAVE;
                      1001: 		return(NULL);
                      1002: 	}
                      1003: 
                      1004: 	fsName = ((char *) pathStr) + rootLen;
                      1005: 
                      1006: 	fsNameEnds = strchr(fsName, '/');
                      1007: 	if (fsNameEnds) {
                      1008: 		origSep = *fsNameEnds;
                      1009: 		*fsNameEnds = '\0';
                      1010: 	}
                      1011: 
                      1012: 	XVFS_DEBUG_PRINTF("... fsName = %s...", fsName);
                      1013: 
                      1014: 	mapEntry = Tcl_FindHashEntry(&xvfs_tclfs_dispatch_map, fsName);
                      1015: 
                      1016: 	if (fsNameEnds) {
                      1017: 		*fsNameEnds = origSep;
                      1018: 	}
                      1019: 
                      1020: 	if (mapEntry) {
                      1021: 		retval = (struct xvfs_tclfs_instance_info *) Tcl_GetHashValue(mapEntry);
                      1022: 		XVFS_DEBUG_PRINTF("... found a registered filesystem: %p", retval);
                      1023: 	} else {
                      1024: 		retval = NULL;
                      1025: 		XVFS_DEBUG_PUTS("... found no registered filesystem.");
                      1026: 	}
                      1027: 
                      1028: 	XVFS_DEBUG_LEAVE;
                      1029: 	return(retval);
                      1030: }
                      1031: 
0e05b2a8c7 2019-09-16 1032: static int xvfs_tclfs_dispatch_pathInFilesystem(Tcl_Obj *path, ClientData *dataPtr) {
0e05b2a8c7 2019-09-16 1033: 	static struct xvfs_tclfs_instance_info *instanceInfo;
0e05b2a8c7 2019-09-16 1034: 
0e05b2a8c7 2019-09-16 1035: 	instanceInfo = xvfs_tclfs_dispatch_pathToInstanceInfo(path);
0e05b2a8c7 2019-09-16 1036: 	if (!instanceInfo) {
0e05b2a8c7 2019-09-16 1037: 		return(-1);
0e05b2a8c7 2019-09-16 1038: 	}
0e05b2a8c7 2019-09-16 1039: 
0e05b2a8c7 2019-09-16 1040: 	return(xvfs_tclfs_pathInFilesystem(path, dataPtr, instanceInfo));
0e05b2a8c7 2019-09-16 1041: }
0e05b2a8c7 2019-09-16 1042: 
                      1043: static int xvfs_tclfs_dispatch_stat(Tcl_Obj *path, Tcl_StatBuf *statBuf) {
                      1044: 	struct xvfs_tclfs_instance_info *instanceInfo;
                      1045: 
0e05b2a8c7 2019-09-16 1046: 	instanceInfo = xvfs_tclfs_dispatch_pathToInstanceInfo(path);
                      1047: 	if (!instanceInfo) {
0e05b2a8c7 2019-09-16 1048: 		return(0);
                      1049: 	}
                      1050: 
                      1051: 	return(xvfs_tclfs_stat(path, statBuf, instanceInfo));
                      1052: }
                      1053: 
                      1054: static int xvfs_tclfs_dispatch_access(Tcl_Obj *path, int mode) {
                      1055: 	struct xvfs_tclfs_instance_info *instanceInfo;
                      1056: 
0e05b2a8c7 2019-09-16 1057: 	instanceInfo = xvfs_tclfs_dispatch_pathToInstanceInfo(path);
                      1058: 	if (!instanceInfo) {
                      1059: 		return(-1);
                      1060: 	}
                      1061: 
                      1062: 	return(xvfs_tclfs_access(path, mode, instanceInfo));
                      1063: }
                      1064: 
                      1065: static Tcl_Channel xvfs_tclfs_dispatch_openFileChannel(Tcl_Interp *interp, Tcl_Obj *path, int mode, int permissions) {
                      1066: 	struct xvfs_tclfs_instance_info *instanceInfo;
                      1067: 
0e05b2a8c7 2019-09-16 1068: 	instanceInfo = xvfs_tclfs_dispatch_pathToInstanceInfo(path);
                      1069: 	if (!instanceInfo) {
                      1070: 		return(NULL);
                      1071: 	}
                      1072: 
                      1073: 	return(xvfs_tclfs_openFileChannel(interp, path, mode, permissions, instanceInfo));
                      1074: }
                      1075: 
                      1076: static int xvfs_tclfs_dispatch_matchInDir(Tcl_Interp *interp, Tcl_Obj *resultPtr, Tcl_Obj *pathPtr, const char *pattern, Tcl_GlobTypeData *types) {
                      1077: 	struct xvfs_tclfs_instance_info *instanceInfo;
                      1078: 
0e05b2a8c7 2019-09-16 1079: 	instanceInfo = xvfs_tclfs_dispatch_pathToInstanceInfo(pathPtr);
                      1080: 	if (!instanceInfo) {
                      1081: 		return(TCL_ERROR);
                      1082: 	}
                      1083: 
                      1084: 	return(xvfs_tclfs_matchInDir(interp, resultPtr, pathPtr, pattern, types, instanceInfo));
                      1085: }
                      1086: 
0e05b2a8c7 2019-09-16 1087: static int xvfs_tclfs_dispatch_init(Tcl_Interp *interp) {
                      1088: 	static int registered = 0;
                      1089: 	int tclRet;
                      1090: 
                      1091: 	/* XXX:TODO: Make this thread-safe */
                      1092: 	if (registered) {
                      1093: 		return(TCL_OK);
                      1094: 	}
                      1095: 	registered = 1;
                      1096: 
                      1097: 	xvfs_tclfs_dispatch_fs.typeName                   = "xvfsDispatch";
                      1098: 	xvfs_tclfs_dispatch_fs.structureLength            = sizeof(xvfs_tclfs_dispatch_fs);
                      1099: 	xvfs_tclfs_dispatch_fs.version                    = TCL_FILESYSTEM_VERSION_1;
                      1100: 	xvfs_tclfs_dispatch_fs.pathInFilesystemProc       = xvfs_tclfs_dispatch_pathInFilesystem;
                      1101: 	xvfs_tclfs_dispatch_fs.dupInternalRepProc         = NULL;
                      1102: 	xvfs_tclfs_dispatch_fs.freeInternalRepProc        = NULL;
                      1103: 	xvfs_tclfs_dispatch_fs.internalToNormalizedProc   = NULL;
                      1104: 	xvfs_tclfs_dispatch_fs.createInternalRepProc      = NULL;
                      1105: 	xvfs_tclfs_dispatch_fs.normalizePathProc          = NULL;
                      1106: 	xvfs_tclfs_dispatch_fs.filesystemPathTypeProc     = NULL;
                      1107: 	xvfs_tclfs_dispatch_fs.filesystemSeparatorProc    = NULL;
                      1108: 	xvfs_tclfs_dispatch_fs.statProc                   = xvfs_tclfs_dispatch_stat;
                      1109: 	xvfs_tclfs_dispatch_fs.accessProc                 = xvfs_tclfs_dispatch_access;
                      1110: 	xvfs_tclfs_dispatch_fs.openFileChannelProc        = xvfs_tclfs_dispatch_openFileChannel;
                      1111: 	xvfs_tclfs_dispatch_fs.matchInDirectoryProc       = xvfs_tclfs_dispatch_matchInDir;
                      1112: 	xvfs_tclfs_dispatch_fs.utimeProc                  = NULL;
                      1113: 	xvfs_tclfs_dispatch_fs.linkProc                   = NULL;
                      1114: 	xvfs_tclfs_dispatch_fs.listVolumesProc            = NULL;
                      1115: 	xvfs_tclfs_dispatch_fs.fileAttrStringsProc        = NULL;
                      1116: 	xvfs_tclfs_dispatch_fs.fileAttrsGetProc           = NULL;
                      1117: 	xvfs_tclfs_dispatch_fs.fileAttrsSetProc           = NULL;
                      1118: 	xvfs_tclfs_dispatch_fs.createDirectoryProc        = NULL;
                      1119: 	xvfs_tclfs_dispatch_fs.removeDirectoryProc        = NULL;
                      1120: 	xvfs_tclfs_dispatch_fs.deleteFileProc             = NULL;
                      1121: 	xvfs_tclfs_dispatch_fs.copyFileProc               = NULL;
                      1122: 	xvfs_tclfs_dispatch_fs.renameFileProc             = NULL;
                      1123: 	xvfs_tclfs_dispatch_fs.copyDirectoryProc          = NULL;
                      1124: 	xvfs_tclfs_dispatch_fs.lstatProc                  = NULL;
                      1125: 	xvfs_tclfs_dispatch_fs.loadFileProc               = NULL;
                      1126: 	xvfs_tclfs_dispatch_fs.getCwdProc                 = NULL;
                      1127: 	xvfs_tclfs_dispatch_fs.chdirProc                  = NULL;
                      1128: 
0e05b2a8c7 2019-09-16 1129: 	tclRet = Tcl_FSRegister(NULL, &xvfs_tclfs_dispatch_fs);
                      1130: 	if (tclRet != TCL_OK) {
                      1131: 		if (interp) {
                      1132: 			Tcl_SetResult(interp, "Tcl_FSRegister() failed", NULL);
                      1133: 		}
                      1134: 
                      1135: 		return(tclRet);
                      1136: 	}
                      1137: 
                      1138: 	/*
                      1139: 	 * Initialize the channel type we will use for I/O
                      1140: 	 */
                      1141: 	xvfs_tclfs_prepareChannelType();
                      1142: 
                      1143: 	/*
                      1144: 	 * Initialize the map to lookup paths to registered
                      1145: 	 * filesystems
                      1146: 	 */
                      1147: 	Tcl_InitHashTable(&xvfs_tclfs_dispatch_map, TCL_STRING_KEYS);
                      1148: 
                      1149: 	return(TCL_OK);
                      1150: }
                      1151: 
                      1152: int Xvfs_Register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
                      1153: 	Tcl_HashEntry *mapEntry;
                      1154: 	struct xvfs_tclfs_instance_info *instanceInfo;
                      1155: 	int dispatchInitRet;
                      1156: 	int new;
                      1157: 
0e05b2a8c7 2019-09-16 1158: 	dispatchInitRet = xvfs_tclfs_dispatch_init(interp);
                      1159: 	if (dispatchInitRet != TCL_OK) {
                      1160: 		return(dispatchInitRet);
                      1161: 	}
                      1162: 
                      1163: 	/*
                      1164: 	 * Verify this is for a protocol we support
                      1165: 	 */
                      1166: 	if (fsInfo->protocolVersion != XVFS_PROTOCOL_VERSION) {
                      1167: 		if (interp) {
                      1168: 			Tcl_SetResult(interp, "Protocol mismatch", NULL);
                      1169: 		}
                      1170: 		return(TCL_ERROR);
                      1171: 	}
                      1172: 
                      1173: 	/*
                      1174: 	 * Create the structure needed
                      1175: 	 */
                      1176: 	instanceInfo = (struct xvfs_tclfs_instance_info *) Tcl_Alloc(sizeof(*instanceInfo));
                      1177: 	instanceInfo->fsInfo = fsInfo;
                      1178: 	instanceInfo->mountpoint = Tcl_ObjPrintf("%s%s", XVFS_ROOT_MOUNTPOINT, fsInfo->name);
                      1179: 	Tcl_IncrRefCount(instanceInfo->mountpoint);
                      1180: 
                      1181: 	/*
                      1182: 	 * Register a hash table entry for this name
                      1183: 	 */
                      1184: 	new = 0;
                      1185: 	mapEntry = Tcl_CreateHashEntry(&xvfs_tclfs_dispatch_map, fsInfo->name, &new);
                      1186: 	Tcl_SetHashValue(mapEntry, instanceInfo);
                      1187: 
                      1188: 	return(TCL_OK);
                      1189: }
                      1190: #endif /* XVFS_MODE_SERVER */