Annotation For xvfs-core.c

Origin for each line in xvfs-core.c from check-in 0e05b2a8c7:

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