Annotation For xvfs-core.c

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

69e476dcd5 2019-05-02        rkeene: #include <xvfs-core.h>
d121970301 2019-05-02        rkeene: #include <string.h>
38bed7cee0 2019-09-13        rkeene: #include <sys/stat.h>
38bed7cee0 2019-09-13        rkeene: #include <errno.h>
38bed7cee0 2019-09-13        rkeene: #include <fcntl.h>
69e476dcd5 2019-05-02        rkeene: #include <tcl.h>
d92ba3d36d 2019-05-08        rkeene: 
d92ba3d36d 2019-05-08        rkeene: #if defined(XVFS_MODE_FLEXIBLE) || defined(XVFS_MODE_SERVER)
d92ba3d36d 2019-05-08        rkeene: #define XVFS_INTERNAL_SERVER_MAGIC "\xD4\xF3\x05\x96\x25\xCF\xAF\xFE"
d92ba3d36d 2019-05-08        rkeene: #define XVFS_INTERNAL_SERVER_MAGIC_LEN 8
d92ba3d36d 2019-05-08        rkeene: 
d92ba3d36d 2019-05-08        rkeene: struct xvfs_tclfs_server_info {
b586d5b0a1 2019-05-08        rkeene: 	char magic[XVFS_INTERNAL_SERVER_MAGIC_LEN];
d92ba3d36d 2019-05-08        rkeene: 	int (*registerProc)(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo);
d92ba3d36d 2019-05-08        rkeene: };
d92ba3d36d 2019-05-08        rkeene: #endif /* XVFS_MODE_FLEXIBLE || XVFS_MODE_SERVER */
3e44e1def1 2019-05-02        rkeene: 
3e44e1def1 2019-05-02        rkeene: #if defined(XVFS_MODE_SERVER) || defined(XVFS_MODE_STANDALONE) || defined(XVFS_MODE_FLEXIBLE)
d121970301 2019-05-02        rkeene: #define XVFS_ROOT_MOUNTPOINT "//xvfs:/"
d121970301 2019-05-02        rkeene: 
d121970301 2019-05-02        rkeene: struct xvfs_tclfs_instance_info {
d121970301 2019-05-02        rkeene: 	struct Xvfs_FSInfo *fsInfo;
d121970301 2019-05-02        rkeene: 	Tcl_Obj            *mountpoint;
d121970301 2019-05-02        rkeene: };
d121970301 2019-05-02        rkeene: 
d121970301 2019-05-02        rkeene: /*
d121970301 2019-05-02        rkeene:  * Internal Core Utilities
d121970301 2019-05-02        rkeene:  */
d121970301 2019-05-02        rkeene: static const char *xvfs_relativePath(Tcl_Obj *path, struct xvfs_tclfs_instance_info *info) {
d121970301 2019-05-02        rkeene: 	const char *pathStr, *rootStr;
d121970301 2019-05-02        rkeene: 	int pathLen, rootLen;
38bed7cee0 2019-09-13        rkeene: 
d121970301 2019-05-02        rkeene: 	pathStr = Tcl_GetStringFromObj(path, &pathLen);
d121970301 2019-05-02        rkeene: 	rootStr = Tcl_GetStringFromObj(info->mountpoint, &rootLen);
38bed7cee0 2019-09-13        rkeene: 
d121970301 2019-05-02        rkeene: 	if (pathLen < rootLen) {
d121970301 2019-05-02        rkeene: 		return(NULL);
d121970301 2019-05-02        rkeene: 	}
38bed7cee0 2019-09-13        rkeene: 
d121970301 2019-05-02        rkeene: 	if (memcmp(pathStr, rootStr, rootLen) != 0) {
d121970301 2019-05-02        rkeene: 		return(NULL);
d121970301 2019-05-02        rkeene: 	}
38bed7cee0 2019-09-13        rkeene: 
d121970301 2019-05-02        rkeene: 	if (pathLen == rootLen) {
d121970301 2019-05-02        rkeene: 		return("");
d121970301 2019-05-02        rkeene: 	}
d121970301 2019-05-02        rkeene: 
d121970301 2019-05-02        rkeene: 	/* XXX:TODO: Should this use the native OS path separator ? */
d121970301 2019-05-02        rkeene: 	if (pathStr[rootLen] != '/') {
d121970301 2019-05-02        rkeene: 		return(NULL);
d121970301 2019-05-02        rkeene: 	}
38bed7cee0 2019-09-13        rkeene: 
d121970301 2019-05-02        rkeene: 	return(pathStr + rootLen + 1);
149aa89b7d 2019-09-13        rkeene: }
149aa89b7d 2019-09-13        rkeene: 
38bed7cee0 2019-09-13        rkeene: #if 0
38bed7cee0 2019-09-13        rkeene: /*
38bed7cee0 2019-09-13        rkeene:  * Currently unused
38bed7cee0 2019-09-13        rkeene:  */
149aa89b7d 2019-09-13        rkeene: static const char *xvfs_perror(int xvfs_error) {
149aa89b7d 2019-09-13        rkeene: 	if (xvfs_error >= 0) {
149aa89b7d 2019-09-13        rkeene: 		return("Not an error");
149aa89b7d 2019-09-13        rkeene: 	}
149aa89b7d 2019-09-13        rkeene: 
149aa89b7d 2019-09-13        rkeene: 	switch (xvfs_error) {
149aa89b7d 2019-09-13        rkeene: 		case XVFS_RV_ERR_ENOENT:
149aa89b7d 2019-09-13        rkeene: 			return("No such file or directory");
149aa89b7d 2019-09-13        rkeene: 		case XVFS_RV_ERR_EINVAL:
149aa89b7d 2019-09-13        rkeene: 			return("Invalid argument");
149aa89b7d 2019-09-13        rkeene: 		case XVFS_RV_ERR_EISDIR:
149aa89b7d 2019-09-13        rkeene: 			return("Is a directory");
149aa89b7d 2019-09-13        rkeene: 		case XVFS_RV_ERR_ENOTDIR:
149aa89b7d 2019-09-13        rkeene: 			return("Not a directory");
149aa89b7d 2019-09-13        rkeene: 		case XVFS_RV_ERR_EFAULT:
149aa89b7d 2019-09-13        rkeene: 			return("Bad address");
149aa89b7d 2019-09-13        rkeene: 		default:
149aa89b7d 2019-09-13        rkeene: 			return("Unknown error");
149aa89b7d 2019-09-13        rkeene: 	}
149aa89b7d 2019-09-13        rkeene: }
38bed7cee0 2019-09-13        rkeene: #endif
38bed7cee0 2019-09-13        rkeene: 
38bed7cee0 2019-09-13        rkeene: static int xvfs_errorToErrno(int xvfs_error) {
38bed7cee0 2019-09-13        rkeene: 	if (xvfs_error >= 0) {
38bed7cee0 2019-09-13        rkeene: 		return(0);
38bed7cee0 2019-09-13        rkeene: 	}
38bed7cee0 2019-09-13        rkeene: 
38bed7cee0 2019-09-13        rkeene: 	switch (xvfs_error) {
38bed7cee0 2019-09-13        rkeene: 		case XVFS_RV_ERR_ENOENT:
38bed7cee0 2019-09-13        rkeene: 			return(ENOENT);
38bed7cee0 2019-09-13        rkeene: 		case XVFS_RV_ERR_EINVAL:
38bed7cee0 2019-09-13        rkeene: 			return(EINVAL);
38bed7cee0 2019-09-13        rkeene: 		case XVFS_RV_ERR_EISDIR:
38bed7cee0 2019-09-13        rkeene: 			return(EISDIR);
38bed7cee0 2019-09-13        rkeene: 		case XVFS_RV_ERR_ENOTDIR:
38bed7cee0 2019-09-13        rkeene: 			return(ENOTDIR);
38bed7cee0 2019-09-13        rkeene: 		case XVFS_RV_ERR_EFAULT:
38bed7cee0 2019-09-13        rkeene: 			return(EFAULT);
38bed7cee0 2019-09-13        rkeene: 		default:
38bed7cee0 2019-09-13        rkeene: 			return(ERANGE);
38bed7cee0 2019-09-13        rkeene: 	}
38bed7cee0 2019-09-13        rkeene: }
38bed7cee0 2019-09-13        rkeene: 
38bed7cee0 2019-09-13        rkeene: /*
38bed7cee0 2019-09-13        rkeene:  * Xvfs Memory Channel
38bed7cee0 2019-09-13        rkeene:  */
38bed7cee0 2019-09-13        rkeene: struct xvfs_tclfs_channel_id {
38bed7cee0 2019-09-13        rkeene: 	Tcl_Channel channel;
38bed7cee0 2019-09-13        rkeene: 	struct xvfs_tclfs_instance_info *fsInstanceInfo;
38bed7cee0 2019-09-13        rkeene: 	Tcl_Obj *path;
38bed7cee0 2019-09-13        rkeene: 	Tcl_WideInt currentOffset;
38bed7cee0 2019-09-13        rkeene: 	Tcl_WideInt fileSize;
7d74392642 2019-09-13        rkeene: 	int eofMarked;
7d74392642 2019-09-13        rkeene: };
7d74392642 2019-09-13        rkeene: struct xvfs_tclfs_channel_event {
7d74392642 2019-09-13        rkeene: 	Tcl_Event tcl;
7d74392642 2019-09-13        rkeene: 	struct xvfs_tclfs_channel_id *channelInstanceData;
38bed7cee0 2019-09-13        rkeene: };
38bed7cee0 2019-09-13        rkeene: static Tcl_ChannelType xvfs_tclfs_channelType;
38bed7cee0 2019-09-13        rkeene: 
38bed7cee0 2019-09-13        rkeene: static Tcl_Channel xvfs_tclfs_openChannel(Tcl_Obj *path, struct xvfs_tclfs_instance_info *instanceInfo) {
38bed7cee0 2019-09-13        rkeene: 	struct xvfs_tclfs_channel_id *channelInstanceData;
38bed7cee0 2019-09-13        rkeene: 	Tcl_Channel channel;
38bed7cee0 2019-09-13        rkeene: 	Tcl_StatBuf fileInfo;
7d74392642 2019-09-13        rkeene: 	Tcl_Obj *channelName;
38bed7cee0 2019-09-13        rkeene: 	int statRet;
38bed7cee0 2019-09-13        rkeene: 
38bed7cee0 2019-09-13        rkeene: 	statRet = instanceInfo->fsInfo->getStatProc(Tcl_GetString(path), &fileInfo);
38bed7cee0 2019-09-13        rkeene: 	if (statRet < 0) {
38bed7cee0 2019-09-13        rkeene: 		return(NULL);
38bed7cee0 2019-09-13        rkeene: 	}
38bed7cee0 2019-09-13        rkeene: 
38bed7cee0 2019-09-13        rkeene: 	channelInstanceData = (struct xvfs_tclfs_channel_id *) Tcl_Alloc(sizeof(*channelInstanceData));
38bed7cee0 2019-09-13        rkeene: 	channelInstanceData->currentOffset = 0;
7d74392642 2019-09-13        rkeene: 	channelInstanceData->eofMarked = 0;
38bed7cee0 2019-09-13        rkeene: 	channelInstanceData->channel = NULL;
38bed7cee0 2019-09-13        rkeene: 
7d74392642 2019-09-13        rkeene: 	channelName = Tcl_ObjPrintf("xvfs0x%llx", (unsigned long long) channelInstanceData);
7d74392642 2019-09-13        rkeene: 	if (!channelName) {
7d74392642 2019-09-13        rkeene: 		Tcl_Free((char *) channelInstanceData);
7d74392642 2019-09-13        rkeene: 
7d74392642 2019-09-13        rkeene: 		return(NULL);
7d74392642 2019-09-13        rkeene: 	}
7d74392642 2019-09-13        rkeene: 	Tcl_IncrRefCount(channelName);
7d74392642 2019-09-13        rkeene: 
38bed7cee0 2019-09-13        rkeene: 	channelInstanceData->fsInstanceInfo = instanceInfo;
38bed7cee0 2019-09-13        rkeene: 	channelInstanceData->fileSize = fileInfo.st_size;
7d74392642 2019-09-13        rkeene: 	channelInstanceData->path = path;
38bed7cee0 2019-09-13        rkeene: 	Tcl_IncrRefCount(path);
38bed7cee0 2019-09-13        rkeene: 
7d74392642 2019-09-13        rkeene: 	channel = Tcl_CreateChannel(&xvfs_tclfs_channelType, Tcl_GetString(channelName), channelInstanceData, TCL_READABLE);
7d74392642 2019-09-13        rkeene: 	Tcl_DecrRefCount(channelName);
38bed7cee0 2019-09-13        rkeene: 	if (!channel) {
38bed7cee0 2019-09-13        rkeene: 		Tcl_DecrRefCount(path);
38bed7cee0 2019-09-13        rkeene: 		Tcl_Free((char *) channelInstanceData);
38bed7cee0 2019-09-13        rkeene: 
38bed7cee0 2019-09-13        rkeene: 		return(NULL);
38bed7cee0 2019-09-13        rkeene: 	}
38bed7cee0 2019-09-13        rkeene: 
38bed7cee0 2019-09-13        rkeene: 	channelInstanceData->channel = channel;
38bed7cee0 2019-09-13        rkeene: 
38bed7cee0 2019-09-13        rkeene: 	return(channel);
38bed7cee0 2019-09-13        rkeene: }
38bed7cee0 2019-09-13        rkeene: 
38bed7cee0 2019-09-13        rkeene: static int xvfs_tclfs_closeChannel(ClientData channelInstanceData_p, Tcl_Interp *interp) {
38bed7cee0 2019-09-13        rkeene: 	struct xvfs_tclfs_channel_id *channelInstanceData;
38bed7cee0 2019-09-13        rkeene: 
38bed7cee0 2019-09-13        rkeene: 	channelInstanceData = (struct xvfs_tclfs_channel_id *) channelInstanceData_p;
38bed7cee0 2019-09-13        rkeene: 
38bed7cee0 2019-09-13        rkeene: 	Tcl_DecrRefCount(channelInstanceData->path);
38bed7cee0 2019-09-13        rkeene: 	Tcl_Free((char *) channelInstanceData);
38bed7cee0 2019-09-13        rkeene: 
38bed7cee0 2019-09-13        rkeene: 	return(0);
38bed7cee0 2019-09-13        rkeene: }
38bed7cee0 2019-09-13        rkeene: 
38bed7cee0 2019-09-13        rkeene: static int xvfs_tclfs_readChannel(ClientData channelInstanceData_p, char *buf, int bufSize, int *errorCodePtr) {
38bed7cee0 2019-09-13        rkeene: 	struct xvfs_tclfs_channel_id *channelInstanceData;
38bed7cee0 2019-09-13        rkeene: 	const unsigned char *data;
38bed7cee0 2019-09-13        rkeene: 	Tcl_WideInt offset, length;
38bed7cee0 2019-09-13        rkeene: 	char *path;
38bed7cee0 2019-09-13        rkeene: 
38bed7cee0 2019-09-13        rkeene: 	channelInstanceData = (struct xvfs_tclfs_channel_id *) channelInstanceData_p;
38bed7cee0 2019-09-13        rkeene: 
7d74392642 2019-09-13        rkeene: 	/*
7d74392642 2019-09-13        rkeene: 	 * If we are already at the end of the file we can skip
7d74392642 2019-09-13        rkeene: 	 * attempting to read it
7d74392642 2019-09-13        rkeene: 	 */
7d74392642 2019-09-13        rkeene: 	if (channelInstanceData->eofMarked) {
7d74392642 2019-09-13        rkeene: 		return(0);
7d74392642 2019-09-13        rkeene: 	}
7d74392642 2019-09-13        rkeene: 
38bed7cee0 2019-09-13        rkeene: 	path = Tcl_GetString(channelInstanceData->path);
38bed7cee0 2019-09-13        rkeene: 	offset = channelInstanceData->currentOffset;
38bed7cee0 2019-09-13        rkeene: 	length = bufSize;
38bed7cee0 2019-09-13        rkeene: 
38bed7cee0 2019-09-13        rkeene: 	data = channelInstanceData->fsInstanceInfo->fsInfo->getDataProc(path, offset, &length);
38bed7cee0 2019-09-13        rkeene: 
38bed7cee0 2019-09-13        rkeene: 	if (length < 0) {
38bed7cee0 2019-09-13        rkeene: 		*errorCodePtr = xvfs_errorToErrno(length);
38bed7cee0 2019-09-13        rkeene: 
38bed7cee0 2019-09-13        rkeene: 		return(-1);
38bed7cee0 2019-09-13        rkeene: 	}
38bed7cee0 2019-09-13        rkeene: 
7d74392642 2019-09-13        rkeene: 	if (length == 0) {
7d74392642 2019-09-13        rkeene: 		channelInstanceData->eofMarked = 1;
7d74392642 2019-09-13        rkeene: 	} else {
7d74392642 2019-09-13        rkeene: 		memcpy(buf, data, length);
38bed7cee0 2019-09-13        rkeene: 
7d74392642 2019-09-13        rkeene: 		channelInstanceData->currentOffset += length;
7d74392642 2019-09-13        rkeene: 	}
38bed7cee0 2019-09-13        rkeene: 
38bed7cee0 2019-09-13        rkeene: 	return(length);
38bed7cee0 2019-09-13        rkeene: }
38bed7cee0 2019-09-13        rkeene: 
7d74392642 2019-09-13        rkeene: static int xvfs_tclfs_watchChannelEvent(Tcl_Event *event_p, int flags) {
7d74392642 2019-09-13        rkeene: 	struct xvfs_tclfs_channel_id *channelInstanceData;
7d74392642 2019-09-13        rkeene: 	struct xvfs_tclfs_channel_event *event;
7d74392642 2019-09-13        rkeene: 
7d74392642 2019-09-13        rkeene: 	event = (struct xvfs_tclfs_channel_event *) event_p;
7d74392642 2019-09-13        rkeene: 	channelInstanceData = event->channelInstanceData;
7d74392642 2019-09-13        rkeene: 
7d74392642 2019-09-13        rkeene: 	Tcl_NotifyChannel(channelInstanceData->channel, TCL_READABLE);
7d74392642 2019-09-13        rkeene: 
7d74392642 2019-09-13        rkeene: 	return(0);
7d74392642 2019-09-13        rkeene: }
7d74392642 2019-09-13        rkeene: 
38bed7cee0 2019-09-13        rkeene: static void xvfs_tclfs_watchChannel(ClientData channelInstanceData_p, int mask) {
7d74392642 2019-09-13        rkeene: 	struct xvfs_tclfs_channel_id *channelInstanceData;
7d74392642 2019-09-13        rkeene: 	struct xvfs_tclfs_channel_event *event;
7d74392642 2019-09-13        rkeene: 
38bed7cee0 2019-09-13        rkeene: 	if ((mask & TCL_READABLE) != TCL_READABLE) {
38bed7cee0 2019-09-13        rkeene: 		return;
38bed7cee0 2019-09-13        rkeene: 	}
38bed7cee0 2019-09-13        rkeene: 
7d74392642 2019-09-13        rkeene: 	channelInstanceData = (struct xvfs_tclfs_channel_id *) channelInstanceData_p;
7d74392642 2019-09-13        rkeene: 
7d74392642 2019-09-13        rkeene: 	/*
7d74392642 2019-09-13        rkeene: 	 * If the read call has marked that we have reached EOF,
7d74392642 2019-09-13        rkeene: 	 * do not signal any further
7d74392642 2019-09-13        rkeene: 	 */
7d74392642 2019-09-13        rkeene: 	if (channelInstanceData->eofMarked) {
7d74392642 2019-09-13        rkeene: 		return;
7d74392642 2019-09-13        rkeene: 	}
7d74392642 2019-09-13        rkeene: 
7d74392642 2019-09-13        rkeene: 	event = (struct xvfs_tclfs_channel_event *) Tcl_Alloc(sizeof(*event));
7d74392642 2019-09-13        rkeene: 	event->tcl.proc = xvfs_tclfs_watchChannelEvent;
7d74392642 2019-09-13        rkeene: 	event->tcl.nextPtr = NULL;
7d74392642 2019-09-13        rkeene: 	event->channelInstanceData = channelInstanceData;
7d74392642 2019-09-13        rkeene: 
7d74392642 2019-09-13        rkeene: 	Tcl_QueueEvent((Tcl_Event *) event, TCL_QUEUE_TAIL);
7d74392642 2019-09-13        rkeene: 
38bed7cee0 2019-09-13        rkeene: 	return;
38bed7cee0 2019-09-13        rkeene: }
38bed7cee0 2019-09-13        rkeene: 
38bed7cee0 2019-09-13        rkeene: static int xvfs_tclfs_seekChannel(ClientData channelInstanceData_p, long offset, int mode, int *errorCodePtr) {
38bed7cee0 2019-09-13        rkeene: 	struct xvfs_tclfs_channel_id *channelInstanceData;
38bed7cee0 2019-09-13        rkeene: 	Tcl_WideInt newOffset, fileSize;
38bed7cee0 2019-09-13        rkeene: 
38bed7cee0 2019-09-13        rkeene: 	channelInstanceData = (struct xvfs_tclfs_channel_id *) channelInstanceData_p;
38bed7cee0 2019-09-13        rkeene: 
38bed7cee0 2019-09-13        rkeene: 	newOffset = channelInstanceData->currentOffset;
38bed7cee0 2019-09-13        rkeene: 	fileSize = channelInstanceData->fileSize;
38bed7cee0 2019-09-13        rkeene: 
38bed7cee0 2019-09-13        rkeene: 	switch (mode) {
38bed7cee0 2019-09-13        rkeene: 		case SEEK_CUR:
38bed7cee0 2019-09-13        rkeene: 			newOffset += offset;
38bed7cee0 2019-09-13        rkeene: 			break;
38bed7cee0 2019-09-13        rkeene: 		case SEEK_SET:
38bed7cee0 2019-09-13        rkeene: 			newOffset = offset;
38bed7cee0 2019-09-13        rkeene: 			break;
38bed7cee0 2019-09-13        rkeene: 		case SEEK_END:
38bed7cee0 2019-09-13        rkeene: 			newOffset = fileSize + offset;
38bed7cee0 2019-09-13        rkeene: 			break;
38bed7cee0 2019-09-13        rkeene: 		default:
38bed7cee0 2019-09-13        rkeene: 			*errorCodePtr = xvfs_errorToErrno(XVFS_RV_ERR_EINVAL);
38bed7cee0 2019-09-13        rkeene: 
38bed7cee0 2019-09-13        rkeene: 			return(-1);
38bed7cee0 2019-09-13        rkeene: 	}
38bed7cee0 2019-09-13        rkeene: 
38bed7cee0 2019-09-13        rkeene: 	/*
38bed7cee0 2019-09-13        rkeene: 	 * We allow users to seek right up to the end of the buffer, but
38bed7cee0 2019-09-13        rkeene: 	 * no further, this way if they want to seek backwards from there
38bed7cee0 2019-09-13        rkeene: 	 * it is possible to do so.
38bed7cee0 2019-09-13        rkeene: 	 */
38bed7cee0 2019-09-13        rkeene: 	if (newOffset < 0 || newOffset > fileSize) {
38bed7cee0 2019-09-13        rkeene: 		*errorCodePtr = xvfs_errorToErrno(XVFS_RV_ERR_EINVAL);
38bed7cee0 2019-09-13        rkeene: 
38bed7cee0 2019-09-13        rkeene: 		return(-1);
38bed7cee0 2019-09-13        rkeene: 	}
38bed7cee0 2019-09-13        rkeene: 
7d74392642 2019-09-13        rkeene: 	if (newOffset != channelInstanceData->currentOffset) {
7d74392642 2019-09-13        rkeene: 		channelInstanceData->eofMarked = 0;
7d74392642 2019-09-13        rkeene: 		channelInstanceData->currentOffset = newOffset;
7d74392642 2019-09-13        rkeene: 	}
38bed7cee0 2019-09-13        rkeene: 
38bed7cee0 2019-09-13        rkeene: 	return(channelInstanceData->currentOffset);
38bed7cee0 2019-09-13        rkeene: }
38bed7cee0 2019-09-13        rkeene: 
38bed7cee0 2019-09-13        rkeene: static void xvfs_tclfs_prepareChannelType(void) {
38bed7cee0 2019-09-13        rkeene: 	xvfs_tclfs_channelType.typeName = "xvfs";
38bed7cee0 2019-09-13        rkeene: 	xvfs_tclfs_channelType.version = TCL_CHANNEL_VERSION_2;
38bed7cee0 2019-09-13        rkeene: 	xvfs_tclfs_channelType.closeProc = xvfs_tclfs_closeChannel;
38bed7cee0 2019-09-13        rkeene: 	xvfs_tclfs_channelType.inputProc = xvfs_tclfs_readChannel;
38bed7cee0 2019-09-13        rkeene: 	xvfs_tclfs_channelType.outputProc = NULL;
38bed7cee0 2019-09-13        rkeene: 	xvfs_tclfs_channelType.watchProc = xvfs_tclfs_watchChannel;
38bed7cee0 2019-09-13        rkeene: 	xvfs_tclfs_channelType.getHandleProc = NULL;
38bed7cee0 2019-09-13        rkeene: 	xvfs_tclfs_channelType.seekProc = xvfs_tclfs_seekChannel;
38bed7cee0 2019-09-13        rkeene: 	xvfs_tclfs_channelType.setOptionProc = NULL;
38bed7cee0 2019-09-13        rkeene: 	xvfs_tclfs_channelType.getOptionProc = NULL;
38bed7cee0 2019-09-13        rkeene: 	xvfs_tclfs_channelType.close2Proc = NULL;
38bed7cee0 2019-09-13        rkeene: 	xvfs_tclfs_channelType.blockModeProc = NULL;
38bed7cee0 2019-09-13        rkeene: 	xvfs_tclfs_channelType.flushProc = NULL;
38bed7cee0 2019-09-13        rkeene: 	xvfs_tclfs_channelType.handlerProc = NULL;
38bed7cee0 2019-09-13        rkeene: 	xvfs_tclfs_channelType.wideSeekProc = NULL;
38bed7cee0 2019-09-13        rkeene: 	xvfs_tclfs_channelType.threadActionProc = NULL;
38bed7cee0 2019-09-13        rkeene: 	xvfs_tclfs_channelType.truncateProc = NULL;
38bed7cee0 2019-09-13        rkeene: }
d121970301 2019-05-02        rkeene: 
d121970301 2019-05-02        rkeene: /*
d121970301 2019-05-02        rkeene:  * Internal Tcl_Filesystem functions, with the appropriate instance info
d121970301 2019-05-02        rkeene:  */
d121970301 2019-05-02        rkeene: static int xvfs_tclfs_pathInFilesystem(Tcl_Obj *path, ClientData *dataPtr, struct xvfs_tclfs_instance_info *instanceInfo) {
d121970301 2019-05-02        rkeene: 	const char *relativePath;
38bed7cee0 2019-09-13        rkeene: 
d121970301 2019-05-02        rkeene: 	relativePath = xvfs_relativePath(path, instanceInfo);
d121970301 2019-05-02        rkeene: 	if (!relativePath) {
d121970301 2019-05-02        rkeene: 		return(-1);
d121970301 2019-05-02        rkeene: 	}
38bed7cee0 2019-09-13        rkeene: 
d121970301 2019-05-02        rkeene: 	return(TCL_OK);
d121970301 2019-05-02        rkeene: }
d121970301 2019-05-02        rkeene: 
d121970301 2019-05-02        rkeene: static int xvfs_tclfs_stat(Tcl_Obj *path, Tcl_StatBuf *statBuf, struct xvfs_tclfs_instance_info *instanceInfo) {
d121970301 2019-05-02        rkeene: 	const char *pathStr;
d121970301 2019-05-02        rkeene: 	int retval;
d121970301 2019-05-02        rkeene: 
d121970301 2019-05-02        rkeene: 	pathStr = xvfs_relativePath(path, instanceInfo);
38bed7cee0 2019-09-13        rkeene: 
daf25f5222 2019-05-03        rkeene: 	retval = instanceInfo->fsInfo->getStatProc(pathStr, statBuf);
149aa89b7d 2019-09-13        rkeene: 	if (retval < 0) {
149aa89b7d 2019-09-13        rkeene: 		retval = -1;
149aa89b7d 2019-09-13        rkeene: 	}
38bed7cee0 2019-09-13        rkeene: 
d121970301 2019-05-02        rkeene: 	return(retval);
d121970301 2019-05-02        rkeene: }
d121970301 2019-05-02        rkeene: 
d121970301 2019-05-02        rkeene: static Tcl_Obj *xvfs_tclfs_listVolumes(struct xvfs_tclfs_instance_info *instanceInfo) {
d121970301 2019-05-02        rkeene: 	return(NULL);
d121970301 2019-05-02        rkeene: }
d121970301 2019-05-02        rkeene: 
d121970301 2019-05-02        rkeene: 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        rkeene: 	const char *pathStr;
d121970301 2019-05-02        rkeene: 
d121970301 2019-05-02        rkeene: 	pathStr = xvfs_relativePath(path, instanceInfo);
149aa89b7d 2019-09-13        rkeene: 
38bed7cee0 2019-09-13        rkeene: 	if (mode & O_WRONLY) {
38bed7cee0 2019-09-13        rkeene: 		return(NULL);
38bed7cee0 2019-09-13        rkeene: 	}
9d3052c6f1 2019-05-08        rkeene: 
38bed7cee0 2019-09-13        rkeene: 	return(xvfs_tclfs_openChannel(Tcl_NewStringObj(pathStr, -1), instanceInfo));
d121970301 2019-05-02        rkeene: }
3e44e1def1 2019-05-02        rkeene: #endif /* XVFS_MODE_SERVER || XVFS_MODE_STANDALONE || XVFS_MODE_FLEIXBLE */
d121970301 2019-05-02        rkeene: 
88f96696b7 2019-05-03        rkeene: #if defined(XVFS_MODE_STANDALONE) || defined(XVFS_MODE_FLEXIBLE)
d121970301 2019-05-02        rkeene: /*
d121970301 2019-05-02        rkeene:  * Tcl_Filesystem handlers for the standalone implementation
d121970301 2019-05-02        rkeene:  */
acfc5037c6 2019-05-02        rkeene: static struct xvfs_tclfs_instance_info xvfs_tclfs_standalone_info;
d121970301 2019-05-02        rkeene: static int xvfs_tclfs_standalone_pathInFilesystem(Tcl_Obj *path, ClientData *dataPtr) {
d121970301 2019-05-02        rkeene: 	return(xvfs_tclfs_pathInFilesystem(path, dataPtr, &xvfs_tclfs_standalone_info));
d121970301 2019-05-02        rkeene: }
d121970301 2019-05-02        rkeene: 
d121970301 2019-05-02        rkeene: static int xvfs_tclfs_standalone_stat(Tcl_Obj *path, Tcl_StatBuf *statBuf) {
d121970301 2019-05-02        rkeene: 	return(xvfs_tclfs_stat(path, statBuf, &xvfs_tclfs_standalone_info));
e5b6962adf 2019-05-02        rkeene: }
e5b6962adf 2019-05-02        rkeene: 
d121970301 2019-05-02        rkeene: static Tcl_Obj *xvfs_tclfs_standalone_listVolumes(void) {
d121970301 2019-05-02        rkeene: 	return(xvfs_tclfs_listVolumes(&xvfs_tclfs_standalone_info));
e5b6962adf 2019-05-02        rkeene: }
e5b6962adf 2019-05-02        rkeene: 
d121970301 2019-05-02        rkeene: static Tcl_Channel xvfs_tclfs_standalone_openFileChannel(Tcl_Interp *interp, Tcl_Obj *path, int mode, int permissions) {
d121970301 2019-05-02        rkeene: 	return(xvfs_tclfs_openFileChannel(interp, path, mode, permissions, &xvfs_tclfs_standalone_info));
e5b6962adf 2019-05-02        rkeene: }
32b55a907b 2019-05-02        rkeene: 
32b55a907b 2019-05-02        rkeene: /*
32b55a907b 2019-05-02        rkeene:  * There are three (3) modes of operation for Xvfs_Register:
32b55a907b 2019-05-02        rkeene:  *    1. standalone -- We register our own Tcl_Filesystem
32b55a907b 2019-05-02        rkeene:  *                     and handle requests under `//xvfs:/<fsName>`
32b55a907b 2019-05-02        rkeene:  *    2. client -- A single Tcl_Filesystem is registered for the
32b55a907b 2019-05-02        rkeene:  *                 interp to handle requests under `//xvfs:/` which
32b55a907b 2019-05-02        rkeene:  *                 then dispatches to the appropriate registered
32b55a907b 2019-05-02        rkeene:  *                 handler
32b55a907b 2019-05-02        rkeene:  *    3. flexible -- Attempts to find a core Xvfs instance for the
32b55a907b 2019-05-02        rkeene:  *                   process at runtime, if found do #2, otherwise
32b55a907b 2019-05-02        rkeene:  *                   fallback to #1
32b55a907b 2019-05-02        rkeene:  *
32b55a907b 2019-05-02        rkeene:  */
cb77ecfb24 2019-05-06        rkeene: static Tcl_Filesystem xvfs_tclfs_standalone_fs;
b8cca3a6b4 2019-05-08        rkeene: static int xvfs_standalone_register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
e5b6962adf 2019-05-02        rkeene: 	int tcl_ret;
d121970301 2019-05-02        rkeene: 	static int registered = 0;
38bed7cee0 2019-09-13        rkeene: 
d121970301 2019-05-02        rkeene: 	/*
d121970301 2019-05-02        rkeene: 	 * Ensure this instance is not already registered
d121970301 2019-05-02        rkeene: 	 */
d121970301 2019-05-02        rkeene: 	if (registered) {
d121970301 2019-05-02        rkeene: 		return(TCL_OK);
d121970301 2019-05-02        rkeene: 	}
d121970301 2019-05-02        rkeene: 	registered = 1;
d121970301 2019-05-02        rkeene: 
e5b6962adf 2019-05-02        rkeene: 	/*
e5b6962adf 2019-05-02        rkeene: 	 * In standalone mode, we only support the same protocol we are
e5b6962adf 2019-05-02        rkeene: 	 * compiling for.
e5b6962adf 2019-05-02        rkeene: 	 */
e5b6962adf 2019-05-02        rkeene: 	if (fsInfo->protocolVersion != XVFS_PROTOCOL_VERSION) {
e5b6962adf 2019-05-02        rkeene: 		if (interp) {
e5b6962adf 2019-05-02        rkeene: 			Tcl_SetResult(interp, "Protocol mismatch", NULL);
e5b6962adf 2019-05-02        rkeene: 		}
e5b6962adf 2019-05-02        rkeene: 		return(TCL_ERROR);
e5b6962adf 2019-05-02        rkeene: 	}
38bed7cee0 2019-09-13        rkeene: 
cb77ecfb24 2019-05-06        rkeene: 	xvfs_tclfs_standalone_fs.typeName                   = "xvfs";
cb77ecfb24 2019-05-06        rkeene: 	xvfs_tclfs_standalone_fs.structureLength            = sizeof(xvfs_tclfs_standalone_fs);
cb77ecfb24 2019-05-06        rkeene: 	xvfs_tclfs_standalone_fs.version                    = TCL_FILESYSTEM_VERSION_1;
cb77ecfb24 2019-05-06        rkeene: 	xvfs_tclfs_standalone_fs.pathInFilesystemProc       = xvfs_tclfs_standalone_pathInFilesystem;
cb77ecfb24 2019-05-06        rkeene: 	xvfs_tclfs_standalone_fs.dupInternalRepProc         = NULL;
cb77ecfb24 2019-05-06        rkeene: 	xvfs_tclfs_standalone_fs.freeInternalRepProc        = NULL;
cb77ecfb24 2019-05-06        rkeene: 	xvfs_tclfs_standalone_fs.internalToNormalizedProc   = NULL;
cb77ecfb24 2019-05-06        rkeene: 	xvfs_tclfs_standalone_fs.createInternalRepProc      = NULL;
cb77ecfb24 2019-05-06        rkeene: 	xvfs_tclfs_standalone_fs.normalizePathProc          = NULL;
cb77ecfb24 2019-05-06        rkeene: 	xvfs_tclfs_standalone_fs.filesystemPathTypeProc     = NULL;
cb77ecfb24 2019-05-06        rkeene: 	xvfs_tclfs_standalone_fs.filesystemSeparatorProc    = NULL;
cb77ecfb24 2019-05-06        rkeene: 	xvfs_tclfs_standalone_fs.statProc                   = xvfs_tclfs_standalone_stat;
cb77ecfb24 2019-05-06        rkeene: 	xvfs_tclfs_standalone_fs.accessProc                 = NULL;
cb77ecfb24 2019-05-06        rkeene: 	xvfs_tclfs_standalone_fs.openFileChannelProc        = xvfs_tclfs_standalone_openFileChannel;
38bed7cee0 2019-09-13        rkeene: 	xvfs_tclfs_standalone_fs.matchInDirectoryProc       = NULL; /* XXX:TODO */
cb77ecfb24 2019-05-06        rkeene: 	xvfs_tclfs_standalone_fs.utimeProc                  = NULL;
cb77ecfb24 2019-05-06        rkeene: 	xvfs_tclfs_standalone_fs.linkProc                   = NULL;
cb77ecfb24 2019-05-06        rkeene: 	xvfs_tclfs_standalone_fs.listVolumesProc            = xvfs_tclfs_standalone_listVolumes;
cb77ecfb24 2019-05-06        rkeene: 	xvfs_tclfs_standalone_fs.fileAttrStringsProc        = NULL;
cb77ecfb24 2019-05-06        rkeene: 	xvfs_tclfs_standalone_fs.fileAttrsGetProc           = NULL;
cb77ecfb24 2019-05-06        rkeene: 	xvfs_tclfs_standalone_fs.fileAttrsSetProc           = NULL;
cb77ecfb24 2019-05-06        rkeene: 	xvfs_tclfs_standalone_fs.createDirectoryProc        = NULL;
cb77ecfb24 2019-05-06        rkeene: 	xvfs_tclfs_standalone_fs.removeDirectoryProc        = NULL;
cb77ecfb24 2019-05-06        rkeene: 	xvfs_tclfs_standalone_fs.deleteFileProc             = NULL;
cb77ecfb24 2019-05-06        rkeene: 	xvfs_tclfs_standalone_fs.copyFileProc               = NULL;
cb77ecfb24 2019-05-06        rkeene: 	xvfs_tclfs_standalone_fs.renameFileProc             = NULL;
cb77ecfb24 2019-05-06        rkeene: 	xvfs_tclfs_standalone_fs.copyDirectoryProc          = NULL;
cb77ecfb24 2019-05-06        rkeene: 	xvfs_tclfs_standalone_fs.lstatProc                  = NULL;
cb77ecfb24 2019-05-06        rkeene: 	xvfs_tclfs_standalone_fs.loadFileProc               = NULL;
cb77ecfb24 2019-05-06        rkeene: 	xvfs_tclfs_standalone_fs.getCwdProc                 = NULL;
cb77ecfb24 2019-05-06        rkeene: 	xvfs_tclfs_standalone_fs.chdirProc                  = NULL;
d121970301 2019-05-02        rkeene: 
d121970301 2019-05-02        rkeene: 	xvfs_tclfs_standalone_info.fsInfo = fsInfo;
d121970301 2019-05-02        rkeene: 	xvfs_tclfs_standalone_info.mountpoint = Tcl_NewObj();
d121970301 2019-05-02        rkeene: 	Tcl_AppendStringsToObj(xvfs_tclfs_standalone_info.mountpoint, XVFS_ROOT_MOUNTPOINT, fsInfo->name, NULL);
38bed7cee0 2019-09-13        rkeene: 
cb77ecfb24 2019-05-06        rkeene: 	tcl_ret = Tcl_FSRegister(NULL, &xvfs_tclfs_standalone_fs);
e5b6962adf 2019-05-02        rkeene: 	if (tcl_ret != TCL_OK) {
e5b6962adf 2019-05-02        rkeene: 		if (interp) {
e5b6962adf 2019-05-02        rkeene: 			Tcl_SetResult(interp, "Tcl_FSRegister() failed", NULL);
e5b6962adf 2019-05-02        rkeene: 		}
38bed7cee0 2019-09-13        rkeene: 
e5b6962adf 2019-05-02        rkeene: 		return(tcl_ret);
e5b6962adf 2019-05-02        rkeene: 	}
38bed7cee0 2019-09-13        rkeene: 
38bed7cee0 2019-09-13        rkeene: 	xvfs_tclfs_prepareChannelType();
38bed7cee0 2019-09-13        rkeene: 
e5b6962adf 2019-05-02        rkeene: 	return(TCL_OK);
e5b6962adf 2019-05-02        rkeene: }
d92ba3d36d 2019-05-08        rkeene: #endif /* XVFS_MODE_STANDALONE || XVFS_MODE_FLEXIBLE */
88f96696b7 2019-05-03        rkeene: 
88f96696b7 2019-05-03        rkeene: #if defined(XVFS_MODE_FLEXIBLE)
b8cca3a6b4 2019-05-08        rkeene: static int xvfs_flexible_register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
9bcf758fef 2019-05-08        rkeene: 	ClientData fsHandlerDataRaw;
9bcf758fef 2019-05-08        rkeene: 	struct xvfs_tclfs_server_info *fsHandlerData;
9bcf758fef 2019-05-08        rkeene: 	const Tcl_Filesystem *fsHandler;
9bcf758fef 2019-05-08        rkeene: 	int (*xvfs_register)(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo);
9bcf758fef 2019-05-08        rkeene: 	Tcl_Obj *rootPathObj;
9bcf758fef 2019-05-08        rkeene: 
9bcf758fef 2019-05-08        rkeene: 	xvfs_register = &xvfs_standalone_register;
9bcf758fef 2019-05-08        rkeene: 
9bcf758fef 2019-05-08        rkeene: 	rootPathObj = Tcl_NewStringObj(XVFS_ROOT_MOUNTPOINT, -1);
9bcf758fef 2019-05-08        rkeene: 	if (!rootPathObj) {
9bcf758fef 2019-05-08        rkeene: 		return(xvfs_register(interp, fsInfo));
9bcf758fef 2019-05-08        rkeene: 	}
9bcf758fef 2019-05-08        rkeene: 
9bcf758fef 2019-05-08        rkeene: 	Tcl_IncrRefCount(rootPathObj);
9bcf758fef 2019-05-08        rkeene: 	fsHandler = Tcl_FSGetFileSystemForPath(rootPathObj);
9bcf758fef 2019-05-08        rkeene: 	Tcl_DecrRefCount(rootPathObj);
9bcf758fef 2019-05-08        rkeene: 
9bcf758fef 2019-05-08        rkeene: 	if (!fsHandler) {
9bcf758fef 2019-05-08        rkeene: 		return(xvfs_register(interp, fsInfo));
9bcf758fef 2019-05-08        rkeene: 	}
9bcf758fef 2019-05-08        rkeene: 
9bcf758fef 2019-05-08        rkeene: 	fsHandlerDataRaw = Tcl_FSData(fsHandler);
9bcf758fef 2019-05-08        rkeene: 	if (!fsHandlerDataRaw) {
9bcf758fef 2019-05-08        rkeene: 		return(xvfs_register(interp, fsInfo));
9bcf758fef 2019-05-08        rkeene: 	}
9bcf758fef 2019-05-08        rkeene: 
9bcf758fef 2019-05-08        rkeene: 	fsHandlerData = (struct xvfs_tclfs_server_info *) fsHandlerDataRaw;
88f96696b7 2019-05-03        rkeene: 
88f96696b7 2019-05-03        rkeene: 	/*
9bcf758fef 2019-05-08        rkeene: 	 * XXX:TODO: What is the chance that the handler for //xvfs:/ hold
b586d5b0a1 2019-05-08        rkeene: 	 * client data smaller than XVFS_INTERNAL_SERVER_MAGIC_LEN ?
88f96696b7 2019-05-03        rkeene: 	 */
b586d5b0a1 2019-05-08        rkeene: 	if (memcmp(fsHandlerData->magic, XVFS_INTERNAL_SERVER_MAGIC, sizeof(fsHandlerData->magic)) == 0) {
9bcf758fef 2019-05-08        rkeene: 		xvfs_register = fsHandlerData->registerProc;
88f96696b7 2019-05-03        rkeene: 	}
9bcf758fef 2019-05-08        rkeene: 
88f96696b7 2019-05-03        rkeene: 	return(xvfs_register(interp, fsInfo));
88f96696b7 2019-05-03        rkeene: }
d92ba3d36d 2019-05-08        rkeene: #endif /* XVFS_MODE_FLEXIBLE */
3e44e1def1 2019-05-02        rkeene: 
3e44e1def1 2019-05-02        rkeene: #if defined(XVFS_MODE_SERVER)
e5b6962adf 2019-05-02        rkeene: int Xvfs_Register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
acfc5037c6 2019-05-02        rkeene: 	return(TCL_ERROR);
69e476dcd5 2019-05-02        rkeene: }
d92ba3d36d 2019-05-08        rkeene: #endif /* XVFS_MODE_SERVER */