Annotation For xvfs-core.c

Lines of xvfs-core.c from check-in 12ff77016f that are changed by the sequence of edits moving toward check-in 5ae034e55e:

                         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: #if defined(XVFS_MODE_FLEXIBLE) || defined(XVFS_MODE_SERVER)
                         9: #define XVFS_INTERNAL_SERVER_MAGIC "\xD4\xF3\x05\x96\x25\xCF\xAF\xFE"
                        10: #define XVFS_INTERNAL_SERVER_MAGIC_LEN 8
                        11: 
                        12: struct xvfs_tclfs_server_info {
                        13: 	char magic[XVFS_INTERNAL_SERVER_MAGIC_LEN];
                        14: 	int (*registerProc)(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo);
                        15: };
                        16: #endif /* XVFS_MODE_FLEXIBLE || XVFS_MODE_SERVER */
                        17: 
                        18: #if defined(XVFS_MODE_SERVER) || defined(XVFS_MODE_STANDALONE) || defined(XVFS_MODE_FLEXIBLE)
                        19: #define XVFS_ROOT_MOUNTPOINT "//xvfs:/"
                        20: 
                        21: struct xvfs_tclfs_instance_info {
                        22: 	struct Xvfs_FSInfo *fsInfo;
                        23: 	Tcl_Obj            *mountpoint;
                        24: };
                        25: 
                        26: /*
                        27:  * Internal Core Utilities
                        28:  */
                        29: static const char *xvfs_relativePath(Tcl_Obj *path, struct xvfs_tclfs_instance_info *info) {
                        30: 	const char *pathStr, *rootStr;
                        31: 	const char *pathFinal;
                        32: 	int pathLen, rootLen;
                        33: 
                        34: 	rootStr = Tcl_GetStringFromObj(info->mountpoint, &rootLen);
                        35: 	pathStr = Tcl_GetStringFromObj(path, &pathLen);
                        36: 	if (pathStr[0] != '/') {
12ff77016f 2019-09-14   37: 		path = Tcl_ObjPrintf("%s/%s", Tcl_GetString(Tcl_FSGetCwd(NULL)), pathStr);
                        38: 		pathStr = Tcl_GetStringFromObj(path, &pathLen);
                        39: 	}
                        40: 
                        41: 	if (pathLen < rootLen) {
                        42: 		return(NULL);
                        43: 	}
                        44: 
                        45: 	if (memcmp(pathStr, rootStr, rootLen) != 0) {
                        46: 		return(NULL);
                        47: 	}
                        48: 
                        49: 	if (pathLen == rootLen) {
                        50: 		return("");
                        51: 	}
                        52: 
                        53: 	/* XXX:TODO: Should this use the native OS path separator ? */
                        54: 	if (pathStr[rootLen] != '/') {
                        55: 		return(NULL);
                        56: 	}
                        57: 
                        58: 	pathFinal = pathStr + rootLen + 1;
                        59: 	pathLen  -= rootLen + 1;
                        60: 
                        61: 	if (pathLen == 1 && memcmp(pathFinal, ".", 1) == 0) {
                        62: 		return("");
                        63: 	}
                        64: 
                        65: 	while (pathLen >= 2 && memcmp(pathFinal, "./", 2) == 0) {
                        66: 		pathFinal += 2;
                        67: 		pathLen   -= 2;
                        68: 	}
                        69: 
                        70: 	return(pathFinal);
                        71: }
                        72: 
                        73: static const char *xvfs_perror(int xvfs_error) {
                        74: 	if (xvfs_error >= 0) {
                        75: 		return("Not an error");
                        76: 	}
                        77: 
                        78: 	switch (xvfs_error) {
                        79: 		case XVFS_RV_ERR_ENOENT:
                        80: 			return("No such file or directory");
                        81: 		case XVFS_RV_ERR_EINVAL:
                        82: 			return("Invalid argument");
                        83: 		case XVFS_RV_ERR_EISDIR:
                        84: 			return("Is a directory");
                        85: 		case XVFS_RV_ERR_ENOTDIR:
                        86: 			return("Not a directory");
                        87: 		case XVFS_RV_ERR_EFAULT:
                        88: 			return("Bad address");
                        89: 		case XVFS_RV_ERR_INTERNAL:
                        90: 			return("Internal error");
                        91: 		default:
                        92: 			return("Unknown error");
                        93: 	}
                        94: }
                        95: 
                        96: static int xvfs_errorToErrno(int xvfs_error) {
                        97: 	if (xvfs_error >= 0) {
                        98: 		return(0);
                        99: 	}
                       100: 
                       101: 	switch (xvfs_error) {
                       102: 		case XVFS_RV_ERR_ENOENT:
                       103: 			return(ENOENT);
                       104: 		case XVFS_RV_ERR_EINVAL:
                       105: 			return(EINVAL);
                       106: 		case XVFS_RV_ERR_EISDIR:
                       107: 			return(EISDIR);
                       108: 		case XVFS_RV_ERR_ENOTDIR:
                       109: 			return(ENOTDIR);
                       110: 		case XVFS_RV_ERR_EFAULT:
                       111: 			return(EFAULT);
                       112: 		case XVFS_RV_ERR_INTERNAL:
                       113: 			return(EINVAL);
                       114: 		default:
                       115: 			return(ERANGE);
                       116: 	}
                       117: }
                       118: 
                       119: /*
                       120:  * Xvfs Memory Channel
                       121:  */
                       122: struct xvfs_tclfs_channel_id {
                       123: 	Tcl_Channel channel;
                       124: 	struct xvfs_tclfs_instance_info *fsInstanceInfo;
                       125: 	Tcl_Obj *path;
                       126: 	Tcl_WideInt currentOffset;
                       127: 	Tcl_WideInt fileSize;
                       128: 	int eofMarked;
                       129: 	int queuedEvents;
                       130: 	int closed;
                       131: };
                       132: struct xvfs_tclfs_channel_event {
                       133: 	Tcl_Event tcl;
                       134: 	struct xvfs_tclfs_channel_id *channelInstanceData;
                       135: };
                       136: static Tcl_ChannelType xvfs_tclfs_channelType;
                       137: 
                       138: static Tcl_Channel xvfs_tclfs_openChannel(Tcl_Obj *path, struct xvfs_tclfs_instance_info *instanceInfo) {
                       139: 	struct xvfs_tclfs_channel_id *channelInstanceData;
                       140: 	Tcl_Channel channel;
                       141: 	Tcl_StatBuf fileInfo;
                       142: 	Tcl_Obj *channelName;
                       143: 	int statRet;
                       144: 
                       145: 	statRet = instanceInfo->fsInfo->getStatProc(Tcl_GetString(path), &fileInfo);
                       146: 	if (statRet < 0) {
                       147: 		return(NULL);
                       148: 	}
                       149: 
                       150: 	channelInstanceData = (struct xvfs_tclfs_channel_id *) Tcl_Alloc(sizeof(*channelInstanceData));
                       151: 	channelInstanceData->currentOffset = 0;
                       152: 	channelInstanceData->eofMarked = 0;
                       153: 	channelInstanceData->queuedEvents = 0;
                       154: 	channelInstanceData->closed = 0;
                       155: 	channelInstanceData->channel = NULL;
                       156: 
                       157: 	channelName = Tcl_ObjPrintf("xvfs0x%llx", (unsigned long long) channelInstanceData);
                       158: 	if (!channelName) {
                       159: 		Tcl_Free((char *) channelInstanceData);
                       160: 
                       161: 		return(NULL);
                       162: 	}
                       163: 	Tcl_IncrRefCount(channelName);
                       164: 
                       165: 	channelInstanceData->fsInstanceInfo = instanceInfo;
                       166: 	channelInstanceData->fileSize = fileInfo.st_size;
                       167: 	channelInstanceData->path = path;
                       168: 	Tcl_IncrRefCount(path);
                       169: 
                       170: 	channel = Tcl_CreateChannel(&xvfs_tclfs_channelType, Tcl_GetString(channelName), channelInstanceData, TCL_READABLE);
                       171: 	Tcl_DecrRefCount(channelName);
                       172: 	if (!channel) {
                       173: 		Tcl_DecrRefCount(path);
                       174: 		Tcl_Free((char *) channelInstanceData);
                       175: 
                       176: 		return(NULL);
                       177: 	}
                       178: 
                       179: 	channelInstanceData->channel = channel;
                       180: 
                       181: 	return(channel);
                       182: }
                       183: 
                       184: static int xvfs_tclfs_closeChannel(ClientData channelInstanceData_p, Tcl_Interp *interp);
                       185: static int xvfs_tclfs_closeChannelEvent(Tcl_Event *event_p, int flags) {
                       186: 	struct xvfs_tclfs_channel_id *channelInstanceData;
                       187: 	struct xvfs_tclfs_channel_event *event;
                       188: 
                       189: 	event = (struct xvfs_tclfs_channel_event *) event_p;
                       190: 	channelInstanceData = event->channelInstanceData;
                       191: 
                       192: 	channelInstanceData->queuedEvents--;
                       193: 
                       194: 	xvfs_tclfs_closeChannel((ClientData) channelInstanceData, NULL);
                       195: 
                       196: 	return(1);
                       197: }
                       198: 
                       199: static int xvfs_tclfs_closeChannel(ClientData channelInstanceData_p, Tcl_Interp *interp) {
                       200: 	struct xvfs_tclfs_channel_id *channelInstanceData;
                       201: 	struct xvfs_tclfs_channel_event *event;
                       202: 
                       203: 	channelInstanceData = (struct xvfs_tclfs_channel_id *) channelInstanceData_p;
                       204: 
                       205: 	channelInstanceData->closed = 1;
                       206: 
                       207: 	if (channelInstanceData->queuedEvents != 0) {
                       208: 		event = (struct xvfs_tclfs_channel_event *) Tcl_Alloc(sizeof(*event));
                       209: 		event->tcl.proc = xvfs_tclfs_closeChannelEvent;
                       210: 		event->tcl.nextPtr = NULL;
                       211: 		event->channelInstanceData = channelInstanceData;
                       212: 
                       213: 		channelInstanceData->queuedEvents++;
                       214: 
                       215: 		Tcl_QueueEvent((Tcl_Event *) event, TCL_QUEUE_TAIL);
                       216: 
                       217: 		return(0);
                       218: 	}
                       219: 
                       220: 	Tcl_DecrRefCount(channelInstanceData->path);
                       221: 	Tcl_Free((char *) channelInstanceData);
                       222: 
                       223: 	return(0);
                       224: }
                       225: 
                       226: static int xvfs_tclfs_readChannel(ClientData channelInstanceData_p, char *buf, int bufSize, int *errorCodePtr) {
                       227: 	struct xvfs_tclfs_channel_id *channelInstanceData;
                       228: 	const unsigned char *data;
                       229: 	Tcl_WideInt offset, length;
                       230: 	char *path;
                       231: 
                       232: 	channelInstanceData = (struct xvfs_tclfs_channel_id *) channelInstanceData_p;
                       233: 
                       234: 	/*
                       235: 	 * If we are already at the end of the file we can skip
                       236: 	 * attempting to read it
                       237: 	 */
                       238: 	if (channelInstanceData->eofMarked) {
                       239: 		return(0);
                       240: 	}
                       241: 
                       242: 	path = Tcl_GetString(channelInstanceData->path);
                       243: 	offset = channelInstanceData->currentOffset;
                       244: 	length = bufSize;
                       245: 
                       246: 	data = channelInstanceData->fsInstanceInfo->fsInfo->getDataProc(path, offset, &length);
                       247: 
                       248: 	if (length < 0) {
                       249: 		*errorCodePtr = xvfs_errorToErrno(length);
                       250: 
                       251: 		return(-1);
                       252: 	}
                       253: 
                       254: 	if (length == 0) {
                       255: 		channelInstanceData->eofMarked = 1;
                       256: 	} else {
                       257: 		memcpy(buf, data, length);
                       258: 
                       259: 		channelInstanceData->currentOffset += length;
                       260: 	}
                       261: 
                       262: 	return(length);
                       263: }
                       264: 
                       265: static int xvfs_tclfs_watchChannelEvent(Tcl_Event *event_p, int flags) {
                       266: 	struct xvfs_tclfs_channel_id *channelInstanceData;
                       267: 	struct xvfs_tclfs_channel_event *event;
                       268: 
                       269: 	event = (struct xvfs_tclfs_channel_event *) event_p;
                       270: 	channelInstanceData = event->channelInstanceData;
                       271: 
                       272: 	channelInstanceData->queuedEvents--;
                       273: 
                       274: 	if (channelInstanceData->closed) {
                       275: 		return(1);
                       276: 	}
                       277: 
                       278: 	Tcl_NotifyChannel(channelInstanceData->channel, TCL_READABLE);
                       279: 
                       280: 	return(1);
                       281: }
                       282: 
                       283: static void xvfs_tclfs_watchChannel(ClientData channelInstanceData_p, int mask) {
                       284: 	struct xvfs_tclfs_channel_id *channelInstanceData;
                       285: 	struct xvfs_tclfs_channel_event *event;
                       286: 
                       287: 	if ((mask & TCL_READABLE) != TCL_READABLE) {
                       288: 		return;
                       289: 	}
                       290: 
                       291: 	channelInstanceData = (struct xvfs_tclfs_channel_id *) channelInstanceData_p;
                       292: 
                       293: 	/*
                       294: 	 * If the read call has marked that we have reached EOF,
                       295: 	 * do not signal any further
                       296: 	 */
                       297: 	if (channelInstanceData->eofMarked) {
                       298: 		return;
                       299: 	}
                       300: 
                       301: 	event = (struct xvfs_tclfs_channel_event *) Tcl_Alloc(sizeof(*event));
                       302: 	event->tcl.proc = xvfs_tclfs_watchChannelEvent;
                       303: 	event->tcl.nextPtr = NULL;
                       304: 	event->channelInstanceData = channelInstanceData;
                       305: 
                       306: 	channelInstanceData->queuedEvents++;
                       307: 
                       308: 	Tcl_QueueEvent((Tcl_Event *) event, TCL_QUEUE_TAIL);
                       309: 
                       310: 	return;
                       311: }
                       312: 
                       313: static int xvfs_tclfs_seekChannel(ClientData channelInstanceData_p, long offset, int mode, int *errorCodePtr) {
                       314: 	struct xvfs_tclfs_channel_id *channelInstanceData;
                       315: 	Tcl_WideInt newOffset, fileSize;
                       316: 
                       317: 	channelInstanceData = (struct xvfs_tclfs_channel_id *) channelInstanceData_p;
                       318: 
                       319: 	newOffset = channelInstanceData->currentOffset;
                       320: 	fileSize = channelInstanceData->fileSize;
                       321: 
                       322: 	switch (mode) {
                       323: 		case SEEK_CUR:
                       324: 			newOffset += offset;
                       325: 			break;
                       326: 		case SEEK_SET:
                       327: 			newOffset = offset;
                       328: 			break;
                       329: 		case SEEK_END:
                       330: 			newOffset = fileSize + offset;
                       331: 			break;
                       332: 		default:
                       333: 			*errorCodePtr = xvfs_errorToErrno(XVFS_RV_ERR_EINVAL);
                       334: 
                       335: 			return(-1);
                       336: 	}
                       337: 
                       338: 	/*
                       339: 	 * We allow users to seek right up to the end of the buffer, but
                       340: 	 * no further, this way if they want to seek backwards from there
                       341: 	 * it is possible to do so.
                       342: 	 */
                       343: 	if (newOffset < 0 || newOffset > fileSize) {
                       344: 		*errorCodePtr = xvfs_errorToErrno(XVFS_RV_ERR_EINVAL);
                       345: 
                       346: 		return(-1);
                       347: 	}
                       348: 
                       349: 	if (newOffset != channelInstanceData->currentOffset) {
                       350: 		channelInstanceData->eofMarked = 0;
                       351: 		channelInstanceData->currentOffset = newOffset;
                       352: 	}
                       353: 
                       354: 	return(channelInstanceData->currentOffset);
                       355: }
                       356: 
                       357: static void xvfs_tclfs_prepareChannelType(void) {
                       358: 	xvfs_tclfs_channelType.typeName = "xvfs";
                       359: 	xvfs_tclfs_channelType.version = TCL_CHANNEL_VERSION_2;
                       360: 	xvfs_tclfs_channelType.closeProc = xvfs_tclfs_closeChannel;
                       361: 	xvfs_tclfs_channelType.inputProc = xvfs_tclfs_readChannel;
                       362: 	xvfs_tclfs_channelType.outputProc = NULL;
                       363: 	xvfs_tclfs_channelType.watchProc = xvfs_tclfs_watchChannel;
                       364: 	xvfs_tclfs_channelType.getHandleProc = NULL;
                       365: 	xvfs_tclfs_channelType.seekProc = xvfs_tclfs_seekChannel;
                       366: 	xvfs_tclfs_channelType.setOptionProc = NULL;
                       367: 	xvfs_tclfs_channelType.getOptionProc = NULL;
                       368: 	xvfs_tclfs_channelType.close2Proc = NULL;
                       369: 	xvfs_tclfs_channelType.blockModeProc = NULL;
                       370: 	xvfs_tclfs_channelType.flushProc = NULL;
                       371: 	xvfs_tclfs_channelType.handlerProc = NULL;
                       372: 	xvfs_tclfs_channelType.wideSeekProc = NULL;
                       373: 	xvfs_tclfs_channelType.threadActionProc = NULL;
                       374: 	xvfs_tclfs_channelType.truncateProc = NULL;
                       375: }
                       376: 
                       377: /*
                       378:  * Internal Tcl_Filesystem functions, with the appropriate instance info
                       379:  */
                       380: static int xvfs_tclfs_pathInFilesystem(Tcl_Obj *path, ClientData *dataPtr, struct xvfs_tclfs_instance_info *instanceInfo) {
                       381: 	const char *relativePath;
                       382: 
                       383: 	relativePath = xvfs_relativePath(path, instanceInfo);
                       384: 	if (!relativePath) {
                       385: 		return(-1);
                       386: 	}
                       387: 
                       388: 	return(TCL_OK);
                       389: }
                       390: 
                       391: static int xvfs_tclfs_stat(Tcl_Obj *path, Tcl_StatBuf *statBuf, struct xvfs_tclfs_instance_info *instanceInfo) {
                       392: 	const char *pathStr;
                       393: 	int retval;
                       394: 
                       395: 	pathStr = xvfs_relativePath(path, instanceInfo);
                       396: 
                       397: 	retval = instanceInfo->fsInfo->getStatProc(pathStr, statBuf);
                       398: 	if (retval < 0) {
                       399: 		retval = -1;
                       400: 	}
                       401: 
                       402: 	return(retval);
                       403: }
                       404: 
                       405: static int xvfs_tclfs_access(Tcl_Obj *path, int mode, struct xvfs_tclfs_instance_info *instanceInfo) {
                       406: 	const char *pathStr;
                       407: 	Tcl_StatBuf fileInfo;
                       408: 	int statRetVal;
                       409: 
                       410: 	pathStr = xvfs_relativePath(path, instanceInfo);
                       411: 
                       412: 	if (mode & W_OK) {
                       413: 		return(-1);
                       414: 	}
                       415: 
                       416: 	statRetVal = instanceInfo->fsInfo->getStatProc(pathStr, &fileInfo);
                       417: 	if (statRetVal < 0) {
                       418: 		return(-1);
                       419: 	}
                       420: 
                       421: 	if (mode & X_OK) {
                       422: 		if (!(fileInfo.st_mode & 040000)) {
                       423: 			return(-1);
                       424: 		}
                       425: 	}
                       426: 
                       427: 	return(0);
                       428: }
                       429: 
                       430: static Tcl_Obj *xvfs_tclfs_listVolumes(struct xvfs_tclfs_instance_info *instanceInfo) {
                       431: 	return(NULL);
                       432: }
                       433: 
                       434: static Tcl_Channel xvfs_tclfs_openFileChannel(Tcl_Interp *interp, Tcl_Obj *path, int mode, int permissions, struct xvfs_tclfs_instance_info *instanceInfo) {
                       435: 	const char *pathStr;
                       436: 
                       437: 	pathStr = xvfs_relativePath(path, instanceInfo);
                       438: 
                       439: 	if (mode & O_WRONLY) {
                       440: 		return(NULL);
                       441: 	}
                       442: 
                       443: 	return(xvfs_tclfs_openChannel(Tcl_NewStringObj(pathStr, -1), instanceInfo));
                       444: }
                       445: 
                       446: static int xvfs_tclfs_verifyType(Tcl_Obj *path, Tcl_GlobTypeData *types, struct xvfs_tclfs_instance_info *instanceInfo) {
                       447: 	const char *pathStr;
                       448: 	Tcl_StatBuf fileInfo;
                       449: 	int statRetVal;
                       450: 
                       451: 	statRetVal = xvfs_tclfs_stat(path, &fileInfo, instanceInfo);
                       452: 	if (statRetVal != 0) {
                       453: 		return(0);
                       454: 	}
                       455: 
                       456: 	if (!types) {
                       457: 		return(1);
                       458: 	}
                       459: 
                       460: 	if (types->perm != TCL_GLOB_PERM_RONLY) {
                       461: 		if (types->perm & (TCL_GLOB_PERM_W | TCL_GLOB_PERM_HIDDEN)) {
                       462: 			return(0);
                       463: 		}
                       464: 
                       465: 		if ((types->perm & TCL_GLOB_PERM_X) == TCL_GLOB_PERM_X) {
                       466: 			if (!(fileInfo.st_mode & 040000)) {
                       467: 				return(0);
                       468: 			}
                       469: 		}
                       470: 	}
                       471: 
                       472: 	if (types->type & (TCL_GLOB_TYPE_BLOCK | TCL_GLOB_TYPE_CHAR | TCL_GLOB_TYPE_PIPE | TCL_GLOB_TYPE_SOCK | TCL_GLOB_TYPE_LINK)) {
                       473: 		return(0);
                       474: 	}
                       475: 
                       476: 	if ((types->type & TCL_GLOB_TYPE_DIR) == TCL_GLOB_TYPE_DIR) {
                       477: 		if (!(fileInfo.st_mode & 040000)) {
                       478: 			return(0);
                       479: 		}
                       480: 	}
                       481: 
                       482: 	if ((types->type & TCL_GLOB_TYPE_FILE) == TCL_GLOB_TYPE_FILE) {
                       483: 		if (!(fileInfo.st_mode & 0100000)) {
                       484: 			return(0);
                       485: 		}
                       486: 	}
                       487: 
                       488: 	if ((types->type & TCL_GLOB_TYPE_MOUNT) == TCL_GLOB_TYPE_MOUNT) {
                       489: 		pathStr = xvfs_relativePath(path, instanceInfo);
                       490: 		if (!pathStr) {
                       491: 			return(0);
                       492: 		}
                       493: 
                       494: 		if (strlen(pathStr) != 0) {
                       495: 			return(0);
                       496: 		}
                       497: 	}
                       498: 
                       499: 	return(1);
                       500: }
                       501: 
                       502: 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) {
                       503: 	const char *pathStr;
                       504: 	const char **children, *child;
                       505: 	Tcl_WideInt childrenCount, idx;
                       506: 	Tcl_Obj *childObj;
                       507: 	int tclRetVal;
                       508: 
                       509: 	if (pattern == NULL) {
                       510: 		if (xvfs_tclfs_verifyType(path, types, instanceInfo)) {
                       511: 			return(TCL_OK);
                       512: 		}
                       513: 
                       514: 		return(TCL_ERROR);
                       515: 	}
                       516: 
                       517: 	pathStr = xvfs_relativePath(path, instanceInfo);
                       518: 	if (!pathStr) {
                       519: 		if (interp) {
                       520: 			Tcl_SetResult(interp, (char *) xvfs_perror(XVFS_RV_ERR_ENOENT), NULL);
                       521: 		}
                       522: 
                       523: 		return(TCL_ERROR);
                       524: 	}
                       525: 
                       526: 	childrenCount = 0;
                       527: 	children = instanceInfo->fsInfo->getChildrenProc(pathStr, &childrenCount);
                       528: 	if (childrenCount < 0) {
                       529: 		if (interp) {
                       530: 			Tcl_SetResult(interp, (char *) xvfs_perror(childrenCount), NULL);
                       531: 		}
                       532: 
                       533: 		return(TCL_ERROR);
                       534: 	}
                       535: 
                       536: 	for (idx = 0; idx < childrenCount; idx++) {
                       537: 		child = children[idx];
                       538: 
                       539: 		if (!Tcl_StringMatch(child, pattern)) {
                       540: 			continue;
                       541: 		}
                       542: 
                       543: 		childObj = Tcl_DuplicateObj(path);
                       544: 		Tcl_AppendStringsToObj(childObj, "/", child, NULL);
12ff77016f 2019-09-14  545: 		Tcl_IncrRefCount(childObj);
                       546: 
                       547: 		if (!xvfs_tclfs_verifyType(childObj, types, instanceInfo)) {
                       548: 			Tcl_DecrRefCount(childObj);
                       549: 
                       550: 			continue;
                       551: 		}
                       552: 
                       553: 		tclRetVal = Tcl_ListObjAppendElement(interp, resultPtr, childObj);
                       554: 		Tcl_DecrRefCount(childObj);
                       555: 
                       556: 		if (tclRetVal != TCL_OK) {
                       557: 			return(tclRetVal);
                       558: 		}
                       559: 	}
                       560: 
                       561: 	return(TCL_OK);
                       562: }
                       563: #endif /* XVFS_MODE_SERVER || XVFS_MODE_STANDALONE || XVFS_MODE_FLEIXBLE */
                       564: 
                       565: #if defined(XVFS_MODE_STANDALONE) || defined(XVFS_MODE_FLEXIBLE)
                       566: /*
                       567:  * Tcl_Filesystem handlers for the standalone implementation
                       568:  */
                       569: static struct xvfs_tclfs_instance_info xvfs_tclfs_standalone_info;
                       570: static int xvfs_tclfs_standalone_pathInFilesystem(Tcl_Obj *path, ClientData *dataPtr) {
                       571: 	return(xvfs_tclfs_pathInFilesystem(path, dataPtr, &xvfs_tclfs_standalone_info));
                       572: }
                       573: 
                       574: static int xvfs_tclfs_standalone_stat(Tcl_Obj *path, Tcl_StatBuf *statBuf) {
                       575: 	return(xvfs_tclfs_stat(path, statBuf, &xvfs_tclfs_standalone_info));
                       576: }
                       577: 
                       578: static int xvfs_tclfs_standalone_access(Tcl_Obj *path, int mode) {
                       579: 	return(xvfs_tclfs_access(path, mode, &xvfs_tclfs_standalone_info));
                       580: }
                       581: 
                       582: static Tcl_Obj *xvfs_tclfs_standalone_listVolumes(void) {
                       583: 	return(xvfs_tclfs_listVolumes(&xvfs_tclfs_standalone_info));
                       584: }
                       585: 
                       586: static Tcl_Channel xvfs_tclfs_standalone_openFileChannel(Tcl_Interp *interp, Tcl_Obj *path, int mode, int permissions) {
                       587: 	return(xvfs_tclfs_openFileChannel(interp, path, mode, permissions, &xvfs_tclfs_standalone_info));
                       588: }
                       589: 
                       590: static int xvfs_tclfs_standalone_matchInDir(Tcl_Interp *interp, Tcl_Obj *resultPtr, Tcl_Obj *pathPtr, const char *pattern, Tcl_GlobTypeData *types) {
                       591: 	return(xvfs_tclfs_matchInDir(interp, resultPtr, pathPtr, pattern, types, &xvfs_tclfs_standalone_info));
                       592: }
                       593: 
                       594: /*
                       595:  * There are three (3) modes of operation for Xvfs_Register:
                       596:  *    1. standalone -- We register our own Tcl_Filesystem
                       597:  *                     and handle requests under `//xvfs:/<fsName>`
                       598:  *    2. client -- A single Tcl_Filesystem is registered for the
                       599:  *                 interp to handle requests under `//xvfs:/` which
                       600:  *                 then dispatches to the appropriate registered
                       601:  *                 handler
                       602:  *    3. flexible -- Attempts to find a core Xvfs instance for the
                       603:  *                   process at runtime, if found do #2, otherwise
                       604:  *                   fallback to #1
                       605:  *
                       606:  */
                       607: static Tcl_Filesystem xvfs_tclfs_standalone_fs;
                       608: static int xvfs_standalone_register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
                       609: 	int tcl_ret;
                       610: 	static int registered = 0;
                       611: 
                       612: 	/*
                       613: 	 * Ensure this instance is not already registered
                       614: 	 */
                       615: 	if (registered) {
                       616: 		return(TCL_OK);
                       617: 	}
                       618: 	registered = 1;
                       619: 
                       620: 	/*
                       621: 	 * In standalone mode, we only support the same protocol we are
                       622: 	 * compiling for.
                       623: 	 */
                       624: 	if (fsInfo->protocolVersion != XVFS_PROTOCOL_VERSION) {
                       625: 		if (interp) {
                       626: 			Tcl_SetResult(interp, "Protocol mismatch", NULL);
                       627: 		}
                       628: 		return(TCL_ERROR);
                       629: 	}
                       630: 
                       631: 	xvfs_tclfs_standalone_fs.typeName                   = "xvfs";
                       632: 	xvfs_tclfs_standalone_fs.structureLength            = sizeof(xvfs_tclfs_standalone_fs);
                       633: 	xvfs_tclfs_standalone_fs.version                    = TCL_FILESYSTEM_VERSION_1;
                       634: 	xvfs_tclfs_standalone_fs.pathInFilesystemProc       = xvfs_tclfs_standalone_pathInFilesystem;
                       635: 	xvfs_tclfs_standalone_fs.dupInternalRepProc         = NULL;
                       636: 	xvfs_tclfs_standalone_fs.freeInternalRepProc        = NULL;
                       637: 	xvfs_tclfs_standalone_fs.internalToNormalizedProc   = NULL;
                       638: 	xvfs_tclfs_standalone_fs.createInternalRepProc      = NULL;
                       639: 	xvfs_tclfs_standalone_fs.normalizePathProc          = NULL;
                       640: 	xvfs_tclfs_standalone_fs.filesystemPathTypeProc     = NULL;
                       641: 	xvfs_tclfs_standalone_fs.filesystemSeparatorProc    = NULL;
                       642: 	xvfs_tclfs_standalone_fs.statProc                   = xvfs_tclfs_standalone_stat;
                       643: 	xvfs_tclfs_standalone_fs.accessProc                 = xvfs_tclfs_standalone_access;
                       644: 	xvfs_tclfs_standalone_fs.openFileChannelProc        = xvfs_tclfs_standalone_openFileChannel;
                       645: 	xvfs_tclfs_standalone_fs.matchInDirectoryProc       = xvfs_tclfs_standalone_matchInDir;
                       646: 	xvfs_tclfs_standalone_fs.utimeProc                  = NULL;
                       647: 	xvfs_tclfs_standalone_fs.linkProc                   = NULL;
                       648: 	xvfs_tclfs_standalone_fs.listVolumesProc            = xvfs_tclfs_standalone_listVolumes;
                       649: 	xvfs_tclfs_standalone_fs.fileAttrStringsProc        = NULL;
                       650: 	xvfs_tclfs_standalone_fs.fileAttrsGetProc           = NULL;
                       651: 	xvfs_tclfs_standalone_fs.fileAttrsSetProc           = NULL;
                       652: 	xvfs_tclfs_standalone_fs.createDirectoryProc        = NULL;
                       653: 	xvfs_tclfs_standalone_fs.removeDirectoryProc        = NULL;
                       654: 	xvfs_tclfs_standalone_fs.deleteFileProc             = NULL;
                       655: 	xvfs_tclfs_standalone_fs.copyFileProc               = NULL;
                       656: 	xvfs_tclfs_standalone_fs.renameFileProc             = NULL;
                       657: 	xvfs_tclfs_standalone_fs.copyDirectoryProc          = NULL;
                       658: 	xvfs_tclfs_standalone_fs.lstatProc                  = NULL;
                       659: 	xvfs_tclfs_standalone_fs.loadFileProc               = NULL;
                       660: 	xvfs_tclfs_standalone_fs.getCwdProc                 = NULL;
                       661: 	xvfs_tclfs_standalone_fs.chdirProc                  = NULL;
                       662: 
                       663: 	xvfs_tclfs_standalone_info.fsInfo = fsInfo;
                       664: 	xvfs_tclfs_standalone_info.mountpoint = Tcl_NewObj();
                       665: 	Tcl_AppendStringsToObj(xvfs_tclfs_standalone_info.mountpoint, XVFS_ROOT_MOUNTPOINT, fsInfo->name, NULL);
12ff77016f 2019-09-14  666: 
                       667: 	tcl_ret = Tcl_FSRegister(NULL, &xvfs_tclfs_standalone_fs);
                       668: 	if (tcl_ret != TCL_OK) {
                       669: 		if (interp) {
                       670: 			Tcl_SetResult(interp, "Tcl_FSRegister() failed", NULL);
                       671: 		}
                       672: 
                       673: 		return(tcl_ret);
                       674: 	}
                       675: 
                       676: 	xvfs_tclfs_prepareChannelType();
                       677: 
                       678: 	return(TCL_OK);
                       679: }
                       680: #endif /* XVFS_MODE_STANDALONE || XVFS_MODE_FLEXIBLE */
                       681: 
                       682: #if defined(XVFS_MODE_FLEXIBLE)
                       683: static int xvfs_flexible_register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
                       684: 	ClientData fsHandlerDataRaw;
                       685: 	struct xvfs_tclfs_server_info *fsHandlerData;
                       686: 	const Tcl_Filesystem *fsHandler;
                       687: 	int (*xvfs_register)(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo);
                       688: 	Tcl_Obj *rootPathObj;
                       689: 
                       690: 	xvfs_register = &xvfs_standalone_register;
                       691: 
                       692: 	rootPathObj = Tcl_NewStringObj(XVFS_ROOT_MOUNTPOINT, -1);
                       693: 	if (!rootPathObj) {
                       694: 		return(xvfs_register(interp, fsInfo));
                       695: 	}
                       696: 
                       697: 	Tcl_IncrRefCount(rootPathObj);
                       698: 	fsHandler = Tcl_FSGetFileSystemForPath(rootPathObj);
                       699: 	Tcl_DecrRefCount(rootPathObj);
                       700: 
                       701: 	if (!fsHandler) {
                       702: 		return(xvfs_register(interp, fsInfo));
                       703: 	}
                       704: 
                       705: 	fsHandlerDataRaw = Tcl_FSData(fsHandler);
                       706: 	if (!fsHandlerDataRaw) {
                       707: 		return(xvfs_register(interp, fsInfo));
                       708: 	}
                       709: 
                       710: 	fsHandlerData = (struct xvfs_tclfs_server_info *) fsHandlerDataRaw;
                       711: 
                       712: 	/*
                       713: 	 * XXX:TODO: What is the chance that the handler for //xvfs:/ hold
                       714: 	 * client data smaller than XVFS_INTERNAL_SERVER_MAGIC_LEN ?
                       715: 	 */
                       716: 	if (memcmp(fsHandlerData->magic, XVFS_INTERNAL_SERVER_MAGIC, sizeof(fsHandlerData->magic)) == 0) {
                       717: 		xvfs_register = fsHandlerData->registerProc;
                       718: 	}
                       719: 
                       720: 	return(xvfs_register(interp, fsInfo));
                       721: }
                       722: #endif /* XVFS_MODE_FLEXIBLE */
                       723: 
                       724: #if defined(XVFS_MODE_SERVER)
                       725: int Xvfs_Register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
                       726: 	return(TCL_ERROR);
                       727: }
                       728: #endif /* XVFS_MODE_SERVER */