Annotation For xvfs-core.c

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

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