Annotation For xvfs-core.c

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

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>
d92ba3d36d 2019-05-08    7: 
d92ba3d36d 2019-05-08    8: #if defined(XVFS_MODE_FLEXIBLE) || defined(XVFS_MODE_SERVER)
d92ba3d36d 2019-05-08    9: #define XVFS_INTERNAL_SERVER_MAGIC "\xD4\xF3\x05\x96\x25\xCF\xAF\xFE"
d92ba3d36d 2019-05-08   10: #define XVFS_INTERNAL_SERVER_MAGIC_LEN 8
d92ba3d36d 2019-05-08   11: 
d92ba3d36d 2019-05-08   12: struct xvfs_tclfs_server_info {
b586d5b0a1 2019-05-08   13: 	char magic[XVFS_INTERNAL_SERVER_MAGIC_LEN];
d92ba3d36d 2019-05-08   14: 	int (*registerProc)(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo);
d92ba3d36d 2019-05-08   15: };
d92ba3d36d 2019-05-08   16: #endif /* XVFS_MODE_FLEXIBLE || XVFS_MODE_SERVER */
3e44e1def1 2019-05-02   17: 
3e44e1def1 2019-05-02   18: #if defined(XVFS_MODE_SERVER) || defined(XVFS_MODE_STANDALONE) || defined(XVFS_MODE_FLEXIBLE)
d121970301 2019-05-02   19: #define XVFS_ROOT_MOUNTPOINT "//xvfs:/"
d121970301 2019-05-02   20: 
d121970301 2019-05-02   21: struct xvfs_tclfs_instance_info {
d121970301 2019-05-02   22: 	struct Xvfs_FSInfo *fsInfo;
d121970301 2019-05-02   23: 	Tcl_Obj            *mountpoint;
d121970301 2019-05-02   24: };
d121970301 2019-05-02   25: 
d121970301 2019-05-02   26: /*
d121970301 2019-05-02   27:  * Internal Core Utilities
d121970301 2019-05-02   28:  */
d121970301 2019-05-02   29: static const char *xvfs_relativePath(Tcl_Obj *path, struct xvfs_tclfs_instance_info *info) {
d121970301 2019-05-02   30: 	const char *pathStr, *rootStr;
d121970301 2019-05-02   31: 	int pathLen, rootLen;
38bed7cee0 2019-09-13   32: 
d121970301 2019-05-02   33: 	pathStr = Tcl_GetStringFromObj(path, &pathLen);
d121970301 2019-05-02   34: 	rootStr = Tcl_GetStringFromObj(info->mountpoint, &rootLen);
38bed7cee0 2019-09-13   35: 
d121970301 2019-05-02   36: 	if (pathLen < rootLen) {
d121970301 2019-05-02   37: 		return(NULL);
d121970301 2019-05-02   38: 	}
38bed7cee0 2019-09-13   39: 
d121970301 2019-05-02   40: 	if (memcmp(pathStr, rootStr, rootLen) != 0) {
d121970301 2019-05-02   41: 		return(NULL);
d121970301 2019-05-02   42: 	}
38bed7cee0 2019-09-13   43: 
d121970301 2019-05-02   44: 	if (pathLen == rootLen) {
d121970301 2019-05-02   45: 		return("");
d121970301 2019-05-02   46: 	}
d121970301 2019-05-02   47: 
d121970301 2019-05-02   48: 	/* XXX:TODO: Should this use the native OS path separator ? */
d121970301 2019-05-02   49: 	if (pathStr[rootLen] != '/') {
d121970301 2019-05-02   50: 		return(NULL);
d121970301 2019-05-02   51: 	}
38bed7cee0 2019-09-13   52: 
d121970301 2019-05-02   53: 	return(pathStr + rootLen + 1);
149aa89b7d 2019-09-13   54: }
149aa89b7d 2019-09-13   55: 
38bed7cee0 2019-09-13   56: #if 0
38bed7cee0 2019-09-13   57: /*
38bed7cee0 2019-09-13   58:  * Currently unused
38bed7cee0 2019-09-13   59:  */
149aa89b7d 2019-09-13   60: static const char *xvfs_perror(int xvfs_error) {
149aa89b7d 2019-09-13   61: 	if (xvfs_error >= 0) {
149aa89b7d 2019-09-13   62: 		return("Not an error");
149aa89b7d 2019-09-13   63: 	}
149aa89b7d 2019-09-13   64: 
149aa89b7d 2019-09-13   65: 	switch (xvfs_error) {
149aa89b7d 2019-09-13   66: 		case XVFS_RV_ERR_ENOENT:
149aa89b7d 2019-09-13   67: 			return("No such file or directory");
149aa89b7d 2019-09-13   68: 		case XVFS_RV_ERR_EINVAL:
149aa89b7d 2019-09-13   69: 			return("Invalid argument");
149aa89b7d 2019-09-13   70: 		case XVFS_RV_ERR_EISDIR:
149aa89b7d 2019-09-13   71: 			return("Is a directory");
149aa89b7d 2019-09-13   72: 		case XVFS_RV_ERR_ENOTDIR:
149aa89b7d 2019-09-13   73: 			return("Not a directory");
149aa89b7d 2019-09-13   74: 		case XVFS_RV_ERR_EFAULT:
149aa89b7d 2019-09-13   75: 			return("Bad address");
149aa89b7d 2019-09-13   76: 		default:
149aa89b7d 2019-09-13   77: 			return("Unknown error");
149aa89b7d 2019-09-13   78: 	}
149aa89b7d 2019-09-13   79: }
38bed7cee0 2019-09-13   80: #endif
38bed7cee0 2019-09-13   81: 
38bed7cee0 2019-09-13   82: static int xvfs_errorToErrno(int xvfs_error) {
38bed7cee0 2019-09-13   83: 	if (xvfs_error >= 0) {
38bed7cee0 2019-09-13   84: 		return(0);
38bed7cee0 2019-09-13   85: 	}
38bed7cee0 2019-09-13   86: 
38bed7cee0 2019-09-13   87: 	switch (xvfs_error) {
38bed7cee0 2019-09-13   88: 		case XVFS_RV_ERR_ENOENT:
38bed7cee0 2019-09-13   89: 			return(ENOENT);
38bed7cee0 2019-09-13   90: 		case XVFS_RV_ERR_EINVAL:
38bed7cee0 2019-09-13   91: 			return(EINVAL);
38bed7cee0 2019-09-13   92: 		case XVFS_RV_ERR_EISDIR:
38bed7cee0 2019-09-13   93: 			return(EISDIR);
38bed7cee0 2019-09-13   94: 		case XVFS_RV_ERR_ENOTDIR:
38bed7cee0 2019-09-13   95: 			return(ENOTDIR);
38bed7cee0 2019-09-13   96: 		case XVFS_RV_ERR_EFAULT:
38bed7cee0 2019-09-13   97: 			return(EFAULT);
38bed7cee0 2019-09-13   98: 		default:
38bed7cee0 2019-09-13   99: 			return(ERANGE);
38bed7cee0 2019-09-13  100: 	}
38bed7cee0 2019-09-13  101: }
38bed7cee0 2019-09-13  102: 
38bed7cee0 2019-09-13  103: /*
38bed7cee0 2019-09-13  104:  * Xvfs Memory Channel
38bed7cee0 2019-09-13  105:  */
38bed7cee0 2019-09-13  106: struct xvfs_tclfs_channel_id {
38bed7cee0 2019-09-13  107: 	Tcl_Channel channel;
38bed7cee0 2019-09-13  108: 	struct xvfs_tclfs_instance_info *fsInstanceInfo;
38bed7cee0 2019-09-13  109: 	Tcl_Obj *path;
38bed7cee0 2019-09-13  110: 	Tcl_WideInt currentOffset;
38bed7cee0 2019-09-13  111: 	Tcl_WideInt fileSize;
38bed7cee0 2019-09-13  112: };
38bed7cee0 2019-09-13  113: static Tcl_ChannelType xvfs_tclfs_channelType;
38bed7cee0 2019-09-13  114: 
38bed7cee0 2019-09-13  115: static Tcl_Channel xvfs_tclfs_openChannel(Tcl_Obj *path, struct xvfs_tclfs_instance_info *instanceInfo) {
38bed7cee0 2019-09-13  116: 	struct xvfs_tclfs_channel_id *channelInstanceData;
38bed7cee0 2019-09-13  117: 	Tcl_Channel channel;
38bed7cee0 2019-09-13  118: 	Tcl_StatBuf fileInfo;
38bed7cee0 2019-09-13  119: 	int statRet;
38bed7cee0 2019-09-13  120: 
38bed7cee0 2019-09-13  121: 	statRet = instanceInfo->fsInfo->getStatProc(Tcl_GetString(path), &fileInfo);
38bed7cee0 2019-09-13  122: 	if (statRet < 0) {
38bed7cee0 2019-09-13  123: 		return(NULL);
38bed7cee0 2019-09-13  124: 	}
38bed7cee0 2019-09-13  125: 
38bed7cee0 2019-09-13  126: 	channelInstanceData = (struct xvfs_tclfs_channel_id *) Tcl_Alloc(sizeof(*channelInstanceData));
38bed7cee0 2019-09-13  127: 	channelInstanceData->currentOffset = 0;
38bed7cee0 2019-09-13  128: 	channelInstanceData->channel = NULL;
38bed7cee0 2019-09-13  129: 
38bed7cee0 2019-09-13  130: 	channelInstanceData->fsInstanceInfo = instanceInfo;
38bed7cee0 2019-09-13  131: 	channelInstanceData->path = path;
38bed7cee0 2019-09-13  132: 	channelInstanceData->fileSize = fileInfo.st_size;
38bed7cee0 2019-09-13  133: 	Tcl_IncrRefCount(path);
38bed7cee0 2019-09-13  134: 
38bed7cee0 2019-09-13  135: 	channel = Tcl_CreateChannel(&xvfs_tclfs_channelType, Tcl_GetString(path), channelInstanceData, TCL_READABLE);
38bed7cee0 2019-09-13  136: 	if (!channel) {
38bed7cee0 2019-09-13  137: 		Tcl_DecrRefCount(path);
38bed7cee0 2019-09-13  138: 		Tcl_Free((char *) channelInstanceData);
38bed7cee0 2019-09-13  139: 
38bed7cee0 2019-09-13  140: 		return(NULL);
38bed7cee0 2019-09-13  141: 	}
38bed7cee0 2019-09-13  142: 
38bed7cee0 2019-09-13  143: 	channelInstanceData->channel = channel;
38bed7cee0 2019-09-13  144: 
38bed7cee0 2019-09-13  145: 	return(channel);
38bed7cee0 2019-09-13  146: }
38bed7cee0 2019-09-13  147: 
38bed7cee0 2019-09-13  148: static int xvfs_tclfs_closeChannel(ClientData channelInstanceData_p, Tcl_Interp *interp) {
38bed7cee0 2019-09-13  149: 	struct xvfs_tclfs_channel_id *channelInstanceData;
38bed7cee0 2019-09-13  150: 
38bed7cee0 2019-09-13  151: 	channelInstanceData = (struct xvfs_tclfs_channel_id *) channelInstanceData_p;
38bed7cee0 2019-09-13  152: 
38bed7cee0 2019-09-13  153: 	Tcl_DecrRefCount(channelInstanceData->path);
38bed7cee0 2019-09-13  154: 	Tcl_Free((char *) channelInstanceData);
38bed7cee0 2019-09-13  155: 
38bed7cee0 2019-09-13  156: 	return(0);
38bed7cee0 2019-09-13  157: }
38bed7cee0 2019-09-13  158: 
38bed7cee0 2019-09-13  159: static int xvfs_tclfs_readChannel(ClientData channelInstanceData_p, char *buf, int bufSize, int *errorCodePtr) {
38bed7cee0 2019-09-13  160: 	struct xvfs_tclfs_channel_id *channelInstanceData;
38bed7cee0 2019-09-13  161: 	const unsigned char *data;
38bed7cee0 2019-09-13  162: 	Tcl_WideInt offset, length;
38bed7cee0 2019-09-13  163: 	char *path;
38bed7cee0 2019-09-13  164: 
38bed7cee0 2019-09-13  165: 	channelInstanceData = (struct xvfs_tclfs_channel_id *) channelInstanceData_p;
38bed7cee0 2019-09-13  166: 
38bed7cee0 2019-09-13  167: 	path = Tcl_GetString(channelInstanceData->path);
38bed7cee0 2019-09-13  168: 	offset = channelInstanceData->currentOffset;
38bed7cee0 2019-09-13  169: 	length = bufSize;
38bed7cee0 2019-09-13  170: 
38bed7cee0 2019-09-13  171: 	data = channelInstanceData->fsInstanceInfo->fsInfo->getDataProc(path, offset, &length);
38bed7cee0 2019-09-13  172: 
38bed7cee0 2019-09-13  173: 	if (length < 0) {
38bed7cee0 2019-09-13  174: 		*errorCodePtr = xvfs_errorToErrno(length);
38bed7cee0 2019-09-13  175: 
38bed7cee0 2019-09-13  176: 		return(-1);
38bed7cee0 2019-09-13  177: 	}
38bed7cee0 2019-09-13  178: 
38bed7cee0 2019-09-13  179: 	memcpy(buf, data, length);
38bed7cee0 2019-09-13  180: 
38bed7cee0 2019-09-13  181: 	channelInstanceData->currentOffset += length;
38bed7cee0 2019-09-13  182: 
38bed7cee0 2019-09-13  183: 	return(length);
38bed7cee0 2019-09-13  184: }
38bed7cee0 2019-09-13  185: 
38bed7cee0 2019-09-13  186: static void xvfs_tclfs_watchChannel(ClientData channelInstanceData_p, int mask) {
38bed7cee0 2019-09-13  187: 	if ((mask & TCL_READABLE) != TCL_READABLE) {
38bed7cee0 2019-09-13  188: 		return;
38bed7cee0 2019-09-13  189: 	}
38bed7cee0 2019-09-13  190: 
38bed7cee0 2019-09-13  191: /* XXX:TODO: fprintf(stderr, "Incomplete watch called, mask = %i\n", mask); */
38bed7cee0 2019-09-13  192: 	return;
38bed7cee0 2019-09-13  193: }
38bed7cee0 2019-09-13  194: 
38bed7cee0 2019-09-13  195: static int xvfs_tclfs_seekChannel(ClientData channelInstanceData_p, long offset, int mode, int *errorCodePtr) {
38bed7cee0 2019-09-13  196: 	struct xvfs_tclfs_channel_id *channelInstanceData;
38bed7cee0 2019-09-13  197: 	Tcl_WideInt newOffset, fileSize;
38bed7cee0 2019-09-13  198: 
38bed7cee0 2019-09-13  199: 	channelInstanceData = (struct xvfs_tclfs_channel_id *) channelInstanceData_p;
38bed7cee0 2019-09-13  200: 
38bed7cee0 2019-09-13  201: 	newOffset = channelInstanceData->currentOffset;
38bed7cee0 2019-09-13  202: 	fileSize = channelInstanceData->fileSize;
38bed7cee0 2019-09-13  203: 
38bed7cee0 2019-09-13  204: 	switch (mode) {
38bed7cee0 2019-09-13  205: 		case SEEK_CUR:
38bed7cee0 2019-09-13  206: 			newOffset += offset;
38bed7cee0 2019-09-13  207: 			break;
38bed7cee0 2019-09-13  208: 		case SEEK_SET:
38bed7cee0 2019-09-13  209: 			newOffset = offset;
38bed7cee0 2019-09-13  210: 			break;
38bed7cee0 2019-09-13  211: 		case SEEK_END:
38bed7cee0 2019-09-13  212: 			newOffset = fileSize + offset;
38bed7cee0 2019-09-13  213: 			break;
38bed7cee0 2019-09-13  214: 		default:
38bed7cee0 2019-09-13  215: 			*errorCodePtr = xvfs_errorToErrno(XVFS_RV_ERR_EINVAL);
38bed7cee0 2019-09-13  216: 
38bed7cee0 2019-09-13  217: 			return(-1);
38bed7cee0 2019-09-13  218: 	}
38bed7cee0 2019-09-13  219: 
38bed7cee0 2019-09-13  220: 	/*
38bed7cee0 2019-09-13  221: 	 * We allow users to seek right up to the end of the buffer, but
38bed7cee0 2019-09-13  222: 	 * no further, this way if they want to seek backwards from there
38bed7cee0 2019-09-13  223: 	 * it is possible to do so.
38bed7cee0 2019-09-13  224: 	 */
38bed7cee0 2019-09-13  225: 	if (newOffset < 0 || newOffset > fileSize) {
38bed7cee0 2019-09-13  226: 		*errorCodePtr = xvfs_errorToErrno(XVFS_RV_ERR_EINVAL);
38bed7cee0 2019-09-13  227: 
38bed7cee0 2019-09-13  228: 		return(-1);
38bed7cee0 2019-09-13  229: 	}
38bed7cee0 2019-09-13  230: 
38bed7cee0 2019-09-13  231: 	channelInstanceData->currentOffset = newOffset;
38bed7cee0 2019-09-13  232: 
38bed7cee0 2019-09-13  233: 	return(channelInstanceData->currentOffset);
38bed7cee0 2019-09-13  234: }
38bed7cee0 2019-09-13  235: 
38bed7cee0 2019-09-13  236: static void xvfs_tclfs_prepareChannelType(void) {
38bed7cee0 2019-09-13  237: 	xvfs_tclfs_channelType.typeName = "xvfs";
38bed7cee0 2019-09-13  238: 	xvfs_tclfs_channelType.version = TCL_CHANNEL_VERSION_2;
38bed7cee0 2019-09-13  239: 	xvfs_tclfs_channelType.closeProc = xvfs_tclfs_closeChannel;
38bed7cee0 2019-09-13  240: 	xvfs_tclfs_channelType.inputProc = xvfs_tclfs_readChannel;
38bed7cee0 2019-09-13  241: 	xvfs_tclfs_channelType.outputProc = NULL;
38bed7cee0 2019-09-13  242: 	xvfs_tclfs_channelType.watchProc = xvfs_tclfs_watchChannel;
38bed7cee0 2019-09-13  243: 	xvfs_tclfs_channelType.getHandleProc = NULL;
38bed7cee0 2019-09-13  244: 	xvfs_tclfs_channelType.seekProc = xvfs_tclfs_seekChannel;
38bed7cee0 2019-09-13  245: 	xvfs_tclfs_channelType.setOptionProc = NULL;
38bed7cee0 2019-09-13  246: 	xvfs_tclfs_channelType.getOptionProc = NULL;
38bed7cee0 2019-09-13  247: 	xvfs_tclfs_channelType.close2Proc = NULL;
38bed7cee0 2019-09-13  248: 	xvfs_tclfs_channelType.blockModeProc = NULL;
38bed7cee0 2019-09-13  249: 	xvfs_tclfs_channelType.flushProc = NULL;
38bed7cee0 2019-09-13  250: 	xvfs_tclfs_channelType.handlerProc = NULL;
38bed7cee0 2019-09-13  251: 	xvfs_tclfs_channelType.wideSeekProc = NULL;
38bed7cee0 2019-09-13  252: 	xvfs_tclfs_channelType.threadActionProc = NULL;
38bed7cee0 2019-09-13  253: 	xvfs_tclfs_channelType.truncateProc = NULL;
38bed7cee0 2019-09-13  254: }
d121970301 2019-05-02  255: 
d121970301 2019-05-02  256: /*
d121970301 2019-05-02  257:  * Internal Tcl_Filesystem functions, with the appropriate instance info
d121970301 2019-05-02  258:  */
d121970301 2019-05-02  259: static int xvfs_tclfs_pathInFilesystem(Tcl_Obj *path, ClientData *dataPtr, struct xvfs_tclfs_instance_info *instanceInfo) {
d121970301 2019-05-02  260: 	const char *relativePath;
38bed7cee0 2019-09-13  261: 
d121970301 2019-05-02  262: 	relativePath = xvfs_relativePath(path, instanceInfo);
d121970301 2019-05-02  263: 	if (!relativePath) {
d121970301 2019-05-02  264: 		return(-1);
d121970301 2019-05-02  265: 	}
38bed7cee0 2019-09-13  266: 
d121970301 2019-05-02  267: 	return(TCL_OK);
d121970301 2019-05-02  268: }
d121970301 2019-05-02  269: 
d121970301 2019-05-02  270: static int xvfs_tclfs_stat(Tcl_Obj *path, Tcl_StatBuf *statBuf, struct xvfs_tclfs_instance_info *instanceInfo) {
d121970301 2019-05-02  271: 	const char *pathStr;
d121970301 2019-05-02  272: 	int retval;
d121970301 2019-05-02  273: 
d121970301 2019-05-02  274: 	pathStr = xvfs_relativePath(path, instanceInfo);
38bed7cee0 2019-09-13  275: 
daf25f5222 2019-05-03  276: 	retval = instanceInfo->fsInfo->getStatProc(pathStr, statBuf);
149aa89b7d 2019-09-13  277: 	if (retval < 0) {
149aa89b7d 2019-09-13  278: 		retval = -1;
149aa89b7d 2019-09-13  279: 	}
38bed7cee0 2019-09-13  280: 
d121970301 2019-05-02  281: 	return(retval);
d121970301 2019-05-02  282: }
d121970301 2019-05-02  283: 
d121970301 2019-05-02  284: static Tcl_Obj *xvfs_tclfs_listVolumes(struct xvfs_tclfs_instance_info *instanceInfo) {
d121970301 2019-05-02  285: 	return(NULL);
d121970301 2019-05-02  286: }
d121970301 2019-05-02  287: 
d121970301 2019-05-02  288: static Tcl_Channel xvfs_tclfs_openFileChannel(Tcl_Interp *interp, Tcl_Obj *path, int mode, int permissions, struct xvfs_tclfs_instance_info *instanceInfo) {
d121970301 2019-05-02  289: 	const char *pathStr;
d121970301 2019-05-02  290: 
d121970301 2019-05-02  291: 	pathStr = xvfs_relativePath(path, instanceInfo);
149aa89b7d 2019-09-13  292: 
38bed7cee0 2019-09-13  293: 	if (mode & O_WRONLY) {
38bed7cee0 2019-09-13  294: 		return(NULL);
38bed7cee0 2019-09-13  295: 	}
9d3052c6f1 2019-05-08  296: 
38bed7cee0 2019-09-13  297: 	return(xvfs_tclfs_openChannel(Tcl_NewStringObj(pathStr, -1), instanceInfo));
d121970301 2019-05-02  298: }
3e44e1def1 2019-05-02  299: #endif /* XVFS_MODE_SERVER || XVFS_MODE_STANDALONE || XVFS_MODE_FLEIXBLE */
d121970301 2019-05-02  300: 
88f96696b7 2019-05-03  301: #if defined(XVFS_MODE_STANDALONE) || defined(XVFS_MODE_FLEXIBLE)
d121970301 2019-05-02  302: /*
d121970301 2019-05-02  303:  * Tcl_Filesystem handlers for the standalone implementation
d121970301 2019-05-02  304:  */
acfc5037c6 2019-05-02  305: static struct xvfs_tclfs_instance_info xvfs_tclfs_standalone_info;
d121970301 2019-05-02  306: static int xvfs_tclfs_standalone_pathInFilesystem(Tcl_Obj *path, ClientData *dataPtr) {
d121970301 2019-05-02  307: 	return(xvfs_tclfs_pathInFilesystem(path, dataPtr, &xvfs_tclfs_standalone_info));
d121970301 2019-05-02  308: }
d121970301 2019-05-02  309: 
d121970301 2019-05-02  310: static int xvfs_tclfs_standalone_stat(Tcl_Obj *path, Tcl_StatBuf *statBuf) {
d121970301 2019-05-02  311: 	return(xvfs_tclfs_stat(path, statBuf, &xvfs_tclfs_standalone_info));
e5b6962adf 2019-05-02  312: }
e5b6962adf 2019-05-02  313: 
d121970301 2019-05-02  314: static Tcl_Obj *xvfs_tclfs_standalone_listVolumes(void) {
d121970301 2019-05-02  315: 	return(xvfs_tclfs_listVolumes(&xvfs_tclfs_standalone_info));
e5b6962adf 2019-05-02  316: }
e5b6962adf 2019-05-02  317: 
d121970301 2019-05-02  318: static Tcl_Channel xvfs_tclfs_standalone_openFileChannel(Tcl_Interp *interp, Tcl_Obj *path, int mode, int permissions) {
d121970301 2019-05-02  319: 	return(xvfs_tclfs_openFileChannel(interp, path, mode, permissions, &xvfs_tclfs_standalone_info));
e5b6962adf 2019-05-02  320: }
32b55a907b 2019-05-02  321: 
32b55a907b 2019-05-02  322: /*
32b55a907b 2019-05-02  323:  * There are three (3) modes of operation for Xvfs_Register:
32b55a907b 2019-05-02  324:  *    1. standalone -- We register our own Tcl_Filesystem
32b55a907b 2019-05-02  325:  *                     and handle requests under `//xvfs:/<fsName>`
32b55a907b 2019-05-02  326:  *    2. client -- A single Tcl_Filesystem is registered for the
32b55a907b 2019-05-02  327:  *                 interp to handle requests under `//xvfs:/` which
32b55a907b 2019-05-02  328:  *                 then dispatches to the appropriate registered
32b55a907b 2019-05-02  329:  *                 handler
32b55a907b 2019-05-02  330:  *    3. flexible -- Attempts to find a core Xvfs instance for the
32b55a907b 2019-05-02  331:  *                   process at runtime, if found do #2, otherwise
32b55a907b 2019-05-02  332:  *                   fallback to #1
32b55a907b 2019-05-02  333:  *
32b55a907b 2019-05-02  334:  */
cb77ecfb24 2019-05-06  335: static Tcl_Filesystem xvfs_tclfs_standalone_fs;
b8cca3a6b4 2019-05-08  336: static int xvfs_standalone_register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
e5b6962adf 2019-05-02  337: 	int tcl_ret;
d121970301 2019-05-02  338: 	static int registered = 0;
38bed7cee0 2019-09-13  339: 
d121970301 2019-05-02  340: 	/*
d121970301 2019-05-02  341: 	 * Ensure this instance is not already registered
d121970301 2019-05-02  342: 	 */
d121970301 2019-05-02  343: 	if (registered) {
d121970301 2019-05-02  344: 		return(TCL_OK);
d121970301 2019-05-02  345: 	}
d121970301 2019-05-02  346: 	registered = 1;
d121970301 2019-05-02  347: 
e5b6962adf 2019-05-02  348: 	/*
e5b6962adf 2019-05-02  349: 	 * In standalone mode, we only support the same protocol we are
e5b6962adf 2019-05-02  350: 	 * compiling for.
e5b6962adf 2019-05-02  351: 	 */
e5b6962adf 2019-05-02  352: 	if (fsInfo->protocolVersion != XVFS_PROTOCOL_VERSION) {
e5b6962adf 2019-05-02  353: 		if (interp) {
e5b6962adf 2019-05-02  354: 			Tcl_SetResult(interp, "Protocol mismatch", NULL);
e5b6962adf 2019-05-02  355: 		}
e5b6962adf 2019-05-02  356: 		return(TCL_ERROR);
e5b6962adf 2019-05-02  357: 	}
38bed7cee0 2019-09-13  358: 
cb77ecfb24 2019-05-06  359: 	xvfs_tclfs_standalone_fs.typeName                   = "xvfs";
cb77ecfb24 2019-05-06  360: 	xvfs_tclfs_standalone_fs.structureLength            = sizeof(xvfs_tclfs_standalone_fs);
cb77ecfb24 2019-05-06  361: 	xvfs_tclfs_standalone_fs.version                    = TCL_FILESYSTEM_VERSION_1;
cb77ecfb24 2019-05-06  362: 	xvfs_tclfs_standalone_fs.pathInFilesystemProc       = xvfs_tclfs_standalone_pathInFilesystem;
cb77ecfb24 2019-05-06  363: 	xvfs_tclfs_standalone_fs.dupInternalRepProc         = NULL;
cb77ecfb24 2019-05-06  364: 	xvfs_tclfs_standalone_fs.freeInternalRepProc        = NULL;
cb77ecfb24 2019-05-06  365: 	xvfs_tclfs_standalone_fs.internalToNormalizedProc   = NULL;
cb77ecfb24 2019-05-06  366: 	xvfs_tclfs_standalone_fs.createInternalRepProc      = NULL;
cb77ecfb24 2019-05-06  367: 	xvfs_tclfs_standalone_fs.normalizePathProc          = NULL;
cb77ecfb24 2019-05-06  368: 	xvfs_tclfs_standalone_fs.filesystemPathTypeProc     = NULL;
cb77ecfb24 2019-05-06  369: 	xvfs_tclfs_standalone_fs.filesystemSeparatorProc    = NULL;
cb77ecfb24 2019-05-06  370: 	xvfs_tclfs_standalone_fs.statProc                   = xvfs_tclfs_standalone_stat;
cb77ecfb24 2019-05-06  371: 	xvfs_tclfs_standalone_fs.accessProc                 = NULL;
cb77ecfb24 2019-05-06  372: 	xvfs_tclfs_standalone_fs.openFileChannelProc        = xvfs_tclfs_standalone_openFileChannel;
38bed7cee0 2019-09-13  373: 	xvfs_tclfs_standalone_fs.matchInDirectoryProc       = NULL; /* XXX:TODO */
cb77ecfb24 2019-05-06  374: 	xvfs_tclfs_standalone_fs.utimeProc                  = NULL;
cb77ecfb24 2019-05-06  375: 	xvfs_tclfs_standalone_fs.linkProc                   = NULL;
cb77ecfb24 2019-05-06  376: 	xvfs_tclfs_standalone_fs.listVolumesProc            = xvfs_tclfs_standalone_listVolumes;
cb77ecfb24 2019-05-06  377: 	xvfs_tclfs_standalone_fs.fileAttrStringsProc        = NULL;
cb77ecfb24 2019-05-06  378: 	xvfs_tclfs_standalone_fs.fileAttrsGetProc           = NULL;
cb77ecfb24 2019-05-06  379: 	xvfs_tclfs_standalone_fs.fileAttrsSetProc           = NULL;
cb77ecfb24 2019-05-06  380: 	xvfs_tclfs_standalone_fs.createDirectoryProc        = NULL;
cb77ecfb24 2019-05-06  381: 	xvfs_tclfs_standalone_fs.removeDirectoryProc        = NULL;
cb77ecfb24 2019-05-06  382: 	xvfs_tclfs_standalone_fs.deleteFileProc             = NULL;
cb77ecfb24 2019-05-06  383: 	xvfs_tclfs_standalone_fs.copyFileProc               = NULL;
cb77ecfb24 2019-05-06  384: 	xvfs_tclfs_standalone_fs.renameFileProc             = NULL;
cb77ecfb24 2019-05-06  385: 	xvfs_tclfs_standalone_fs.copyDirectoryProc          = NULL;
cb77ecfb24 2019-05-06  386: 	xvfs_tclfs_standalone_fs.lstatProc                  = NULL;
cb77ecfb24 2019-05-06  387: 	xvfs_tclfs_standalone_fs.loadFileProc               = NULL;
cb77ecfb24 2019-05-06  388: 	xvfs_tclfs_standalone_fs.getCwdProc                 = NULL;
cb77ecfb24 2019-05-06  389: 	xvfs_tclfs_standalone_fs.chdirProc                  = NULL;
d121970301 2019-05-02  390: 
d121970301 2019-05-02  391: 	xvfs_tclfs_standalone_info.fsInfo = fsInfo;
d121970301 2019-05-02  392: 	xvfs_tclfs_standalone_info.mountpoint = Tcl_NewObj();
d121970301 2019-05-02  393: 	Tcl_AppendStringsToObj(xvfs_tclfs_standalone_info.mountpoint, XVFS_ROOT_MOUNTPOINT, fsInfo->name, NULL);
38bed7cee0 2019-09-13  394: 
cb77ecfb24 2019-05-06  395: 	tcl_ret = Tcl_FSRegister(NULL, &xvfs_tclfs_standalone_fs);
e5b6962adf 2019-05-02  396: 	if (tcl_ret != TCL_OK) {
e5b6962adf 2019-05-02  397: 		if (interp) {
e5b6962adf 2019-05-02  398: 			Tcl_SetResult(interp, "Tcl_FSRegister() failed", NULL);
e5b6962adf 2019-05-02  399: 		}
38bed7cee0 2019-09-13  400: 
e5b6962adf 2019-05-02  401: 		return(tcl_ret);
e5b6962adf 2019-05-02  402: 	}
38bed7cee0 2019-09-13  403: 
38bed7cee0 2019-09-13  404: 	xvfs_tclfs_prepareChannelType();
38bed7cee0 2019-09-13  405: 
e5b6962adf 2019-05-02  406: 	return(TCL_OK);
e5b6962adf 2019-05-02  407: }
d92ba3d36d 2019-05-08  408: #endif /* XVFS_MODE_STANDALONE || XVFS_MODE_FLEXIBLE */
88f96696b7 2019-05-03  409: 
88f96696b7 2019-05-03  410: #if defined(XVFS_MODE_FLEXIBLE)
b8cca3a6b4 2019-05-08  411: static int xvfs_flexible_register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
9bcf758fef 2019-05-08  412: 	ClientData fsHandlerDataRaw;
9bcf758fef 2019-05-08  413: 	struct xvfs_tclfs_server_info *fsHandlerData;
9bcf758fef 2019-05-08  414: 	const Tcl_Filesystem *fsHandler;
9bcf758fef 2019-05-08  415: 	int (*xvfs_register)(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo);
9bcf758fef 2019-05-08  416: 	Tcl_Obj *rootPathObj;
9bcf758fef 2019-05-08  417: 
9bcf758fef 2019-05-08  418: 	xvfs_register = &xvfs_standalone_register;
9bcf758fef 2019-05-08  419: 
9bcf758fef 2019-05-08  420: 	rootPathObj = Tcl_NewStringObj(XVFS_ROOT_MOUNTPOINT, -1);
9bcf758fef 2019-05-08  421: 	if (!rootPathObj) {
9bcf758fef 2019-05-08  422: 		return(xvfs_register(interp, fsInfo));
9bcf758fef 2019-05-08  423: 	}
9bcf758fef 2019-05-08  424: 
9bcf758fef 2019-05-08  425: 	Tcl_IncrRefCount(rootPathObj);
9bcf758fef 2019-05-08  426: 	fsHandler = Tcl_FSGetFileSystemForPath(rootPathObj);
9bcf758fef 2019-05-08  427: 	Tcl_DecrRefCount(rootPathObj);
9bcf758fef 2019-05-08  428: 
9bcf758fef 2019-05-08  429: 	if (!fsHandler) {
9bcf758fef 2019-05-08  430: 		return(xvfs_register(interp, fsInfo));
9bcf758fef 2019-05-08  431: 	}
9bcf758fef 2019-05-08  432: 
9bcf758fef 2019-05-08  433: 	fsHandlerDataRaw = Tcl_FSData(fsHandler);
9bcf758fef 2019-05-08  434: 	if (!fsHandlerDataRaw) {
9bcf758fef 2019-05-08  435: 		return(xvfs_register(interp, fsInfo));
9bcf758fef 2019-05-08  436: 	}
9bcf758fef 2019-05-08  437: 
9bcf758fef 2019-05-08  438: 	fsHandlerData = (struct xvfs_tclfs_server_info *) fsHandlerDataRaw;
88f96696b7 2019-05-03  439: 
88f96696b7 2019-05-03  440: 	/*
9bcf758fef 2019-05-08  441: 	 * XXX:TODO: What is the chance that the handler for //xvfs:/ hold
b586d5b0a1 2019-05-08  442: 	 * client data smaller than XVFS_INTERNAL_SERVER_MAGIC_LEN ?
88f96696b7 2019-05-03  443: 	 */
b586d5b0a1 2019-05-08  444: 	if (memcmp(fsHandlerData->magic, XVFS_INTERNAL_SERVER_MAGIC, sizeof(fsHandlerData->magic)) == 0) {
9bcf758fef 2019-05-08  445: 		xvfs_register = fsHandlerData->registerProc;
88f96696b7 2019-05-03  446: 	}
9bcf758fef 2019-05-08  447: 
88f96696b7 2019-05-03  448: 	return(xvfs_register(interp, fsInfo));
88f96696b7 2019-05-03  449: }
d92ba3d36d 2019-05-08  450: #endif /* XVFS_MODE_FLEXIBLE */
3e44e1def1 2019-05-02  451: 
3e44e1def1 2019-05-02  452: #if defined(XVFS_MODE_SERVER)
e5b6962adf 2019-05-02  453: int Xvfs_Register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
acfc5037c6 2019-05-02  454: 	return(TCL_ERROR);
69e476dcd5 2019-05-02  455: }
d92ba3d36d 2019-05-08  456: #endif /* XVFS_MODE_SERVER */