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>
d61a265cd6 2019-09-17 rkeene: #include <unistd.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>
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: #ifdef XVFS_DEBUG
d80c88cee0 2019-09-16 rkeene: #include <stdio.h> /* Needed for XVFS_DEBUG_PRINTF */
d80c88cee0 2019-09-16 rkeene: static int xvfs_debug_depth = 0;
d80c88cee0 2019-09-16 rkeene: #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 rkeene: #define XVFS_DEBUG_PUTS(str) XVFS_DEBUG_PRINTF("%s", str);
d80c88cee0 2019-09-16 rkeene: #define XVFS_DEBUG_ENTER { xvfs_debug_depth++; XVFS_DEBUG_PUTS("Entered"); }
d80c88cee0 2019-09-16 rkeene: #define XVFS_DEBUG_LEAVE { XVFS_DEBUG_PUTS("Returning"); xvfs_debug_depth--; }
d80c88cee0 2019-09-16 rkeene: #else /* XVFS_DEBUG */
d80c88cee0 2019-09-16 rkeene: #define XVFS_DEBUG_PRINTF(fmt, ...) /**/
d80c88cee0 2019-09-16 rkeene: #define XVFS_DEBUG_PUTS(str) /**/
d80c88cee0 2019-09-16 rkeene: #define XVFS_DEBUG_ENTER /**/
d80c88cee0 2019-09-16 rkeene: #define XVFS_DEBUG_LEAVE /**/
d80c88cee0 2019-09-16 rkeene: #endif /* XVFS_DEBUG */
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: */
d80c88cee0 2019-09-16 rkeene: static Tcl_Obj *xvfs_absolutePath(Tcl_Obj *path) {
5ae034e55e 2019-09-14 rkeene: Tcl_Obj *currentDirectory;
d80c88cee0 2019-09-16 rkeene: const char *pathStr;
5ae034e55e 2019-09-14 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_ENTER;
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: pathStr = Tcl_GetString(path);
d80c88cee0 2019-09-16 rkeene:
5ae034e55e 2019-09-14 rkeene: if (pathStr[0] != '/') {
5ae034e55e 2019-09-14 rkeene: currentDirectory = Tcl_FSGetCwd(NULL);
5ae034e55e 2019-09-14 rkeene: Tcl_IncrRefCount(currentDirectory);
5ae034e55e 2019-09-14 rkeene:
5ae034e55e 2019-09-14 rkeene: path = Tcl_ObjPrintf("%s/%s", Tcl_GetString(currentDirectory), pathStr);
5ae034e55e 2019-09-14 rkeene: Tcl_IncrRefCount(path);
5ae034e55e 2019-09-14 rkeene: Tcl_DecrRefCount(currentDirectory);
d80c88cee0 2019-09-16 rkeene: } else {
d80c88cee0 2019-09-16 rkeene: Tcl_IncrRefCount(path);
d80c88cee0 2019-09-16 rkeene: }
5ae034e55e 2019-09-14 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PRINTF("Converted path \"%s\" to absolute path: \"%s\"", pathStr, Tcl_GetString(path));
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
d80c88cee0 2019-09-16 rkeene: return(path);
d80c88cee0 2019-09-16 rkeene: }
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: static const char *xvfs_relativePath(Tcl_Obj *path, struct xvfs_tclfs_instance_info *info) {
d80c88cee0 2019-09-16 rkeene: const char *pathStr, *rootStr;
d80c88cee0 2019-09-16 rkeene: const char *pathFinal;
d80c88cee0 2019-09-16 rkeene: int pathLen, rootLen;
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_ENTER;
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: rootStr = Tcl_GetStringFromObj(info->mountpoint, &rootLen);
d80c88cee0 2019-09-16 rkeene: pathStr = Tcl_GetStringFromObj(path, &pathLen);
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PRINTF("Finding relative path of \"%s\" from \"%s\" ...", pathStr, rootStr);
38bed7cee0 2019-09-13 rkeene:
d121970301 2019-05-02 rkeene: if (pathLen < rootLen) {
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PUTS("... none possible (length)");
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
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) {
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PUTS("... none possible (prefix differs)");
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
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) {
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PUTS("... short circuit: \"\"");
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
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] != '/') {
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PUTS("... none possible (no seperator)");
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
149aa89b7d 2019-09-13 rkeene: return(NULL);
149aa89b7d 2019-09-13 rkeene: }
38bed7cee0 2019-09-13 rkeene:
5583d77f1c 2019-09-14 rkeene: pathFinal = pathStr + rootLen + 1;
5583d77f1c 2019-09-14 rkeene: pathLen -= rootLen + 1;
5583d77f1c 2019-09-14 rkeene:
5583d77f1c 2019-09-14 rkeene: if (pathLen == 1 && memcmp(pathFinal, ".", 1) == 0) {
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PUTS("... short circuit: \".\" -> \"\"");
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 rkeene: return("");
5583d77f1c 2019-09-14 rkeene: }
5583d77f1c 2019-09-14 rkeene:
5583d77f1c 2019-09-14 rkeene: while (pathLen >= 2 && memcmp(pathFinal, "./", 2) == 0) {
5583d77f1c 2019-09-14 rkeene: pathFinal += 2;
5583d77f1c 2019-09-14 rkeene: pathLen -= 2;
5583d77f1c 2019-09-14 rkeene: }
5583d77f1c 2019-09-14 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PRINTF("... relative path: \"%s\"", pathFinal);
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 rkeene: return(pathFinal);
5583d77f1c 2019-09-14 rkeene: }
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);
10f67b2ced 2019-09-16 rkeene: case XVFS_RV_ERR_EROFS:
10f67b2ced 2019-09-16 rkeene: return(EROFS);
5583d77f1c 2019-09-14 rkeene: case XVFS_RV_ERR_INTERNAL:
5583d77f1c 2019-09-14 rkeene: return(EINVAL);
38bed7cee0 2019-09-13 rkeene: default:
38bed7cee0 2019-09-13 rkeene: return(ERANGE);
38bed7cee0 2019-09-13 rkeene: }
10f67b2ced 2019-09-16 rkeene: }
10f67b2ced 2019-09-16 rkeene:
cf56967f97 2019-09-17 rkeene: static const char *xvfs_strerror(int xvfs_error) {
10f67b2ced 2019-09-16 rkeene: if (xvfs_error >= 0) {
10f67b2ced 2019-09-16 rkeene: return("Not an error");
10f67b2ced 2019-09-16 rkeene: }
10f67b2ced 2019-09-16 rkeene:
10f67b2ced 2019-09-16 rkeene: switch (xvfs_error) {
10f67b2ced 2019-09-16 rkeene: case XVFS_RV_ERR_ENOENT:
10f67b2ced 2019-09-16 rkeene: case XVFS_RV_ERR_EINVAL:
10f67b2ced 2019-09-16 rkeene: case XVFS_RV_ERR_EISDIR:
10f67b2ced 2019-09-16 rkeene: case XVFS_RV_ERR_ENOTDIR:
10f67b2ced 2019-09-16 rkeene: case XVFS_RV_ERR_EFAULT:
10f67b2ced 2019-09-16 rkeene: case XVFS_RV_ERR_EROFS:
10f67b2ced 2019-09-16 rkeene: return(Tcl_ErrnoMsg(xvfs_errorToErrno(xvfs_error)));
10f67b2ced 2019-09-16 rkeene: case XVFS_RV_ERR_INTERNAL:
10f67b2ced 2019-09-16 rkeene: return("Internal error");
10f67b2ced 2019-09-16 rkeene: default:
10f67b2ced 2019-09-16 rkeene: return("Unknown error");
10f67b2ced 2019-09-16 rkeene: }
e786b9e07b 2019-09-16 rkeene: }
e786b9e07b 2019-09-16 rkeene:
e786b9e07b 2019-09-16 rkeene: static void xvfs_setresults_error(Tcl_Interp *interp, int xvfs_error) {
e786b9e07b 2019-09-16 rkeene: if (!interp) {
e786b9e07b 2019-09-16 rkeene: return;
e786b9e07b 2019-09-16 rkeene: }
e786b9e07b 2019-09-16 rkeene:
e786b9e07b 2019-09-16 rkeene: Tcl_SetErrno(xvfs_errorToErrno(xvfs_error));
cf56967f97 2019-09-17 rkeene: Tcl_SetResult(interp, (char *) xvfs_strerror(xvfs_error), NULL);
e786b9e07b 2019-09-16 rkeene:
e786b9e07b 2019-09-16 rkeene: return;
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;
aa08a4a749 2019-09-14 rkeene: int queuedEvents;
aa08a4a749 2019-09-14 rkeene: int closed;
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:
e786b9e07b 2019-09-16 rkeene: static Tcl_Channel xvfs_tclfs_openChannel(Tcl_Interp *interp, 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;
7d74392642 2019-09-13 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_ENTER;
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PRINTF("Opening file \"%s\" ...", Tcl_GetString(path));
d80c88cee0 2019-09-16 rkeene:
38bed7cee0 2019-09-13 rkeene: statRet = instanceInfo->fsInfo->getStatProc(Tcl_GetString(path), &fileInfo);
38bed7cee0 2019-09-13 rkeene: if (statRet < 0) {
cf56967f97 2019-09-17 rkeene: XVFS_DEBUG_PRINTF("... failed: %s", xvfs_strerror(statRet));
e786b9e07b 2019-09-16 rkeene:
e786b9e07b 2019-09-16 rkeene: xvfs_setresults_error(interp, XVFS_RV_ERR_ENOENT);
e786b9e07b 2019-09-16 rkeene:
e786b9e07b 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
e786b9e07b 2019-09-16 rkeene: return(NULL);
e786b9e07b 2019-09-16 rkeene: }
e786b9e07b 2019-09-16 rkeene:
e786b9e07b 2019-09-16 rkeene: if (fileInfo.st_mode & 040000) {
e786b9e07b 2019-09-16 rkeene: XVFS_DEBUG_PUTS("... failed (cannot open directories)");
e786b9e07b 2019-09-16 rkeene:
e786b9e07b 2019-09-16 rkeene: xvfs_setresults_error(interp, XVFS_RV_ERR_EISDIR);
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
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;
aa08a4a749 2019-09-14 rkeene: channelInstanceData->queuedEvents = 0;
aa08a4a749 2019-09-14 rkeene: channelInstanceData->closed = 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) {
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PUTS("... failed");
d80c88cee0 2019-09-16 rkeene:
7d74392642 2019-09-13 rkeene: Tcl_Free((char *) channelInstanceData);
7d74392642 2019-09-13 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
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) {
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PUTS("... failed");
d80c88cee0 2019-09-16 rkeene:
38bed7cee0 2019-09-13 rkeene: Tcl_DecrRefCount(path);
38bed7cee0 2019-09-13 rkeene: Tcl_Free((char *) channelInstanceData);
38bed7cee0 2019-09-13 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
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:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PRINTF("... ok (%p)", channelInstanceData);
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
38bed7cee0 2019-09-13 rkeene: return(channel);
aa08a4a749 2019-09-14 rkeene: }
aa08a4a749 2019-09-14 rkeene:
aa08a4a749 2019-09-14 rkeene: static int xvfs_tclfs_closeChannel(ClientData channelInstanceData_p, Tcl_Interp *interp);
aa08a4a749 2019-09-14 rkeene: static int xvfs_tclfs_closeChannelEvent(Tcl_Event *event_p, int flags) {
aa08a4a749 2019-09-14 rkeene: struct xvfs_tclfs_channel_id *channelInstanceData;
aa08a4a749 2019-09-14 rkeene: struct xvfs_tclfs_channel_event *event;
aa08a4a749 2019-09-14 rkeene:
aa08a4a749 2019-09-14 rkeene: event = (struct xvfs_tclfs_channel_event *) event_p;
aa08a4a749 2019-09-14 rkeene: channelInstanceData = event->channelInstanceData;
aa08a4a749 2019-09-14 rkeene:
aa08a4a749 2019-09-14 rkeene: channelInstanceData->queuedEvents--;
aa08a4a749 2019-09-14 rkeene:
aa08a4a749 2019-09-14 rkeene: xvfs_tclfs_closeChannel((ClientData) channelInstanceData, NULL);
aa08a4a749 2019-09-14 rkeene:
aa08a4a749 2019-09-14 rkeene: return(1);
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;
aa08a4a749 2019-09-14 rkeene: struct xvfs_tclfs_channel_event *event;
aa08a4a749 2019-09-14 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_ENTER;
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PRINTF("Closing channel %p ...", channelInstanceData_p);
d80c88cee0 2019-09-16 rkeene:
38bed7cee0 2019-09-13 rkeene: channelInstanceData = (struct xvfs_tclfs_channel_id *) channelInstanceData_p;
aa08a4a749 2019-09-14 rkeene:
aa08a4a749 2019-09-14 rkeene: channelInstanceData->closed = 1;
aa08a4a749 2019-09-14 rkeene:
aa08a4a749 2019-09-14 rkeene: if (channelInstanceData->queuedEvents != 0) {
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PUTS("... queued");
d80c88cee0 2019-09-16 rkeene:
aa08a4a749 2019-09-14 rkeene: event = (struct xvfs_tclfs_channel_event *) Tcl_Alloc(sizeof(*event));
aa08a4a749 2019-09-14 rkeene: event->tcl.proc = xvfs_tclfs_closeChannelEvent;
aa08a4a749 2019-09-14 rkeene: event->tcl.nextPtr = NULL;
aa08a4a749 2019-09-14 rkeene: event->channelInstanceData = channelInstanceData;
aa08a4a749 2019-09-14 rkeene:
aa08a4a749 2019-09-14 rkeene: channelInstanceData->queuedEvents++;
aa08a4a749 2019-09-14 rkeene:
aa08a4a749 2019-09-14 rkeene: Tcl_QueueEvent((Tcl_Event *) event, TCL_QUEUE_TAIL);
aa08a4a749 2019-09-14 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
aa08a4a749 2019-09-14 rkeene: return(0);
aa08a4a749 2019-09-14 rkeene: }
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:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PUTS("... ok");
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
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;
7d74392642 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: }
38bed7cee0 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:
aa08a4a749 2019-09-14 rkeene: channelInstanceData->queuedEvents--;
aa08a4a749 2019-09-14 rkeene:
aa08a4a749 2019-09-14 rkeene: if (channelInstanceData->closed) {
aa08a4a749 2019-09-14 rkeene: return(1);
aa08a4a749 2019-09-14 rkeene: }
aa08a4a749 2019-09-14 rkeene:
7d74392642 2019-09-13 rkeene: Tcl_NotifyChannel(channelInstanceData->channel, TCL_READABLE);
7d74392642 2019-09-13 rkeene:
aa08a4a749 2019-09-14 rkeene: return(1);
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;
aa08a4a749 2019-09-14 rkeene:
aa08a4a749 2019-09-14 rkeene: channelInstanceData->queuedEvents++;
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;
d121970301 2019-05-02 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;
d80c88cee0 2019-09-16 rkeene: int retval;
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_ENTER;
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PRINTF("Checking to see if path \"%s\" is in the filesystem ...", Tcl_GetString(path));
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: path = xvfs_absolutePath(path);
38bed7cee0 2019-09-13 rkeene:
d121970301 2019-05-02 rkeene: relativePath = xvfs_relativePath(path, instanceInfo);
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: retval = TCL_OK;
d121970301 2019-05-02 rkeene: if (!relativePath) {
d80c88cee0 2019-09-16 rkeene: retval = -1;
d121970301 2019-05-02 rkeene: }
38bed7cee0 2019-09-13 rkeene:
d80c88cee0 2019-09-16 rkeene: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PRINTF("... %s", retval == -1 ? "no" : "yes");
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
d80c88cee0 2019-09-16 rkeene: return(retval);
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:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_ENTER;
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PRINTF("Getting stat() on \"%s\" ...", Tcl_GetString(path));
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: path = xvfs_absolutePath(path);
d80c88cee0 2019-09-16 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) {
cf56967f97 2019-09-17 rkeene: XVFS_DEBUG_PRINTF("... failed: %s", xvfs_strerror(retval));
f1d16a3958 2019-09-17 rkeene:
f1d16a3958 2019-09-17 rkeene: Tcl_SetErrno(xvfs_errorToErrno(retval));
f1d16a3958 2019-09-17 rkeene:
149aa89b7d 2019-09-13 rkeene: retval = -1;
d80c88cee0 2019-09-16 rkeene: } else {
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PUTS("... ok");
149aa89b7d 2019-09-13 rkeene: }
38bed7cee0 2019-09-13 rkeene:
d80c88cee0 2019-09-16 rkeene: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
d121970301 2019-05-02 rkeene: return(retval);
d121970301 2019-05-02 rkeene: }
d121970301 2019-05-02 rkeene:
12ff77016f 2019-09-14 rkeene: static int xvfs_tclfs_access(Tcl_Obj *path, int mode, struct xvfs_tclfs_instance_info *instanceInfo) {
12ff77016f 2019-09-14 rkeene: const char *pathStr;
12ff77016f 2019-09-14 rkeene: Tcl_StatBuf fileInfo;
12ff77016f 2019-09-14 rkeene: int statRetVal;
12ff77016f 2019-09-14 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_ENTER;
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PRINTF("Getting access(..., %i) on \"%s\" ...", mode, Tcl_GetString(path));
12ff77016f 2019-09-14 rkeene:
12ff77016f 2019-09-14 rkeene: if (mode & W_OK) {
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PUTS("... no (not writable)");
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
d80c88cee0 2019-09-16 rkeene: return(-1);
d80c88cee0 2019-09-16 rkeene: }
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: path = xvfs_absolutePath(path);
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: pathStr = xvfs_relativePath(path, instanceInfo);
d80c88cee0 2019-09-16 rkeene: if (!pathStr) {
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PUTS("... no (not in our path)");
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
12ff77016f 2019-09-14 rkeene: return(-1);
12ff77016f 2019-09-14 rkeene: }
12ff77016f 2019-09-14 rkeene:
12ff77016f 2019-09-14 rkeene: statRetVal = instanceInfo->fsInfo->getStatProc(pathStr, &fileInfo);
12ff77016f 2019-09-14 rkeene: if (statRetVal < 0) {
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PUTS("... no (not statable)");
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
12ff77016f 2019-09-14 rkeene: return(-1);
12ff77016f 2019-09-14 rkeene: }
12ff77016f 2019-09-14 rkeene:
12ff77016f 2019-09-14 rkeene: if (mode & X_OK) {
12ff77016f 2019-09-14 rkeene: if (!(fileInfo.st_mode & 040000)) {
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PUTS("... no (not a directory and X_OK specified)");
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
12ff77016f 2019-09-14 rkeene: return(-1);
12ff77016f 2019-09-14 rkeene: }
12ff77016f 2019-09-14 rkeene: }
12ff77016f 2019-09-14 rkeene:
d80c88cee0 2019-09-16 rkeene: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PUTS("... ok");
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
12ff77016f 2019-09-14 rkeene: return(0);
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) {
d80c88cee0 2019-09-16 rkeene: Tcl_Channel retval;
d80c88cee0 2019-09-16 rkeene: Tcl_Obj *pathRel;
38bed7cee0 2019-09-13 rkeene: const char *pathStr;
38bed7cee0 2019-09-13 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_ENTER;
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PRINTF("Asked to open(\"%s\", %x)...", Tcl_GetString(path), mode);
38bed7cee0 2019-09-13 rkeene:
38bed7cee0 2019-09-13 rkeene: if (mode & O_WRONLY) {
10f67b2ced 2019-09-16 rkeene: XVFS_DEBUG_PUTS("... failed (asked to open for writing)");
10f67b2ced 2019-09-16 rkeene:
e786b9e07b 2019-09-16 rkeene: xvfs_setresults_error(interp, XVFS_RV_ERR_EROFS);
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
38bed7cee0 2019-09-13 rkeene: return(NULL);
38bed7cee0 2019-09-13 rkeene: }
38bed7cee0 2019-09-13 rkeene:
d80c88cee0 2019-09-16 rkeene: path = xvfs_absolutePath(path);
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: pathStr = xvfs_relativePath(path, instanceInfo);
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: pathRel = Tcl_NewStringObj(pathStr, -1);
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PUTS("... done, passing off to channel handler");
d80c88cee0 2019-09-16 rkeene:
e786b9e07b 2019-09-16 rkeene: retval = xvfs_tclfs_openChannel(interp, pathRel, instanceInfo);
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
d80c88cee0 2019-09-16 rkeene: return(retval);
5583d77f1c 2019-09-14 rkeene: }
5583d77f1c 2019-09-14 rkeene:
5583d77f1c 2019-09-14 rkeene: static int xvfs_tclfs_verifyType(Tcl_Obj *path, Tcl_GlobTypeData *types, struct xvfs_tclfs_instance_info *instanceInfo) {
5583d77f1c 2019-09-14 rkeene: const char *pathStr;
5583d77f1c 2019-09-14 rkeene: Tcl_StatBuf fileInfo;
5583d77f1c 2019-09-14 rkeene: int statRetVal;
5583d77f1c 2019-09-14 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_ENTER;
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: if (types) {
d80c88cee0 2019-09-16 rkeene: 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 rkeene: } else {
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PRINTF("Asked to verify the existence \"%s\" ...", Tcl_GetString(path));
d80c88cee0 2019-09-16 rkeene: }
d80c88cee0 2019-09-16 rkeene:
5583d77f1c 2019-09-14 rkeene: statRetVal = xvfs_tclfs_stat(path, &fileInfo, instanceInfo);
5583d77f1c 2019-09-14 rkeene: if (statRetVal != 0) {
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PUTS("... no (cannot stat)");
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 rkeene: return(0);
5583d77f1c 2019-09-14 rkeene: }
5583d77f1c 2019-09-14 rkeene:
5583d77f1c 2019-09-14 rkeene: if (!types) {
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PUTS("... yes");
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 rkeene: return(1);
5583d77f1c 2019-09-14 rkeene: }
5583d77f1c 2019-09-14 rkeene:
5583d77f1c 2019-09-14 rkeene: if (types->perm != TCL_GLOB_PERM_RONLY) {
12ff77016f 2019-09-14 rkeene: if (types->perm & (TCL_GLOB_PERM_W | TCL_GLOB_PERM_HIDDEN)) {
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PUTS("... no (checked for writable or hidden, not supported)");
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 rkeene: return(0);
12ff77016f 2019-09-14 rkeene: }
12ff77016f 2019-09-14 rkeene:
12ff77016f 2019-09-14 rkeene: if ((types->perm & TCL_GLOB_PERM_X) == TCL_GLOB_PERM_X) {
12ff77016f 2019-09-14 rkeene: if (!(fileInfo.st_mode & 040000)) {
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PUTS("... no (checked for executable but not a directory)");
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
12ff77016f 2019-09-14 rkeene: return(0);
12ff77016f 2019-09-14 rkeene: }
5583d77f1c 2019-09-14 rkeene: }
5583d77f1c 2019-09-14 rkeene: }
5583d77f1c 2019-09-14 rkeene:
5583d77f1c 2019-09-14 rkeene: 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 rkeene: XVFS_DEBUG_PUTS("... no (checked for block, char, pipe, sock, or link, not supported)");
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 rkeene: return(0);
5583d77f1c 2019-09-14 rkeene: }
5583d77f1c 2019-09-14 rkeene:
5583d77f1c 2019-09-14 rkeene: if ((types->type & TCL_GLOB_TYPE_DIR) == TCL_GLOB_TYPE_DIR) {
5583d77f1c 2019-09-14 rkeene: if (!(fileInfo.st_mode & 040000)) {
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PUTS("... no (checked for directory but not a directory)");
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 rkeene: return(0);
5583d77f1c 2019-09-14 rkeene: }
5583d77f1c 2019-09-14 rkeene: }
5583d77f1c 2019-09-14 rkeene:
5583d77f1c 2019-09-14 rkeene: if ((types->type & TCL_GLOB_TYPE_FILE) == TCL_GLOB_TYPE_FILE) {
5583d77f1c 2019-09-14 rkeene: if (!(fileInfo.st_mode & 0100000)) {
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PUTS("... no (checked for file but not a file)");
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 rkeene: return(0);
5583d77f1c 2019-09-14 rkeene: }
5583d77f1c 2019-09-14 rkeene: }
5583d77f1c 2019-09-14 rkeene:
5583d77f1c 2019-09-14 rkeene: if ((types->type & TCL_GLOB_TYPE_MOUNT) == TCL_GLOB_TYPE_MOUNT) {
d80c88cee0 2019-09-16 rkeene: path = xvfs_absolutePath(path);
5583d77f1c 2019-09-14 rkeene: pathStr = xvfs_relativePath(path, instanceInfo);
5583d77f1c 2019-09-14 rkeene: if (!pathStr) {
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PUTS("... no (checked for mount but not able to resolve path)");
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 rkeene: return(0);
5583d77f1c 2019-09-14 rkeene: }
5583d77f1c 2019-09-14 rkeene:
5583d77f1c 2019-09-14 rkeene: if (strlen(pathStr) != 0) {
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PUTS("... no (checked for mount but not our top-level directory)");
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 rkeene: return(0);
5583d77f1c 2019-09-14 rkeene: }
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: Tcl_DecrRefCount(path);
5583d77f1c 2019-09-14 rkeene: }
5583d77f1c 2019-09-14 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PUTS("... yes");
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 rkeene: return(1);
5583d77f1c 2019-09-14 rkeene: }
5583d77f1c 2019-09-14 rkeene:
5583d77f1c 2019-09-14 rkeene: 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 rkeene: const char *pathStr;
5583d77f1c 2019-09-14 rkeene: const char **children, *child;
5583d77f1c 2019-09-14 rkeene: Tcl_WideInt childrenCount, idx;
5583d77f1c 2019-09-14 rkeene: Tcl_Obj *childObj;
5583d77f1c 2019-09-14 rkeene: int tclRetVal;
5583d77f1c 2019-09-14 rkeene:
5583d77f1c 2019-09-14 rkeene: if (pattern == NULL) {
5583d77f1c 2019-09-14 rkeene: if (xvfs_tclfs_verifyType(path, types, instanceInfo)) {
5583d77f1c 2019-09-14 rkeene: return(TCL_OK);
5583d77f1c 2019-09-14 rkeene: }
5583d77f1c 2019-09-14 rkeene:
5583d77f1c 2019-09-14 rkeene: return(TCL_ERROR);
5583d77f1c 2019-09-14 rkeene: }
5583d77f1c 2019-09-14 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_ENTER;
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: path = xvfs_absolutePath(path);
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: if (types) {
d80c88cee0 2019-09-16 rkeene: 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 rkeene: } else {
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PRINTF("Checking for files matching %s in \"%s\" ...", pattern, Tcl_GetString(path));
d80c88cee0 2019-09-16 rkeene: }
d80c88cee0 2019-09-16 rkeene:
5583d77f1c 2019-09-14 rkeene: pathStr = xvfs_relativePath(path, instanceInfo);
5583d77f1c 2019-09-14 rkeene: if (!pathStr) {
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PUTS("... error (not in our VFS)");
5583d77f1c 2019-09-14 rkeene:
d80c88cee0 2019-09-16 rkeene: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 rkeene:
e786b9e07b 2019-09-16 rkeene: xvfs_setresults_error(interp, XVFS_RV_ERR_ENOENT);
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
d80c88cee0 2019-09-16 rkeene: return(TCL_OK);
5583d77f1c 2019-09-14 rkeene: }
5583d77f1c 2019-09-14 rkeene:
5583d77f1c 2019-09-14 rkeene: childrenCount = 0;
5583d77f1c 2019-09-14 rkeene: children = instanceInfo->fsInfo->getChildrenProc(pathStr, &childrenCount);
5583d77f1c 2019-09-14 rkeene: if (childrenCount < 0) {
cf56967f97 2019-09-17 rkeene: XVFS_DEBUG_PRINTF("... error: %s", xvfs_strerror(childrenCount));
5583d77f1c 2019-09-14 rkeene:
d80c88cee0 2019-09-16 rkeene: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 rkeene:
e786b9e07b 2019-09-16 rkeene: xvfs_setresults_error(interp, childrenCount);
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 rkeene: return(TCL_ERROR);
5583d77f1c 2019-09-14 rkeene: }
5583d77f1c 2019-09-14 rkeene:
5583d77f1c 2019-09-14 rkeene: for (idx = 0; idx < childrenCount; idx++) {
5583d77f1c 2019-09-14 rkeene: child = children[idx];
5583d77f1c 2019-09-14 rkeene:
5583d77f1c 2019-09-14 rkeene: if (!Tcl_StringMatch(child, pattern)) {
5583d77f1c 2019-09-14 rkeene: continue;
5583d77f1c 2019-09-14 rkeene: }
5583d77f1c 2019-09-14 rkeene:
5583d77f1c 2019-09-14 rkeene: childObj = Tcl_DuplicateObj(path);
5583d77f1c 2019-09-14 rkeene: Tcl_IncrRefCount(childObj);
5ae034e55e 2019-09-14 rkeene: Tcl_AppendStringsToObj(childObj, "/", child, NULL);
5583d77f1c 2019-09-14 rkeene:
5583d77f1c 2019-09-14 rkeene: if (!xvfs_tclfs_verifyType(childObj, types, instanceInfo)) {
5583d77f1c 2019-09-14 rkeene: Tcl_DecrRefCount(childObj);
5583d77f1c 2019-09-14 rkeene:
5583d77f1c 2019-09-14 rkeene: continue;
5583d77f1c 2019-09-14 rkeene: }
5583d77f1c 2019-09-14 rkeene:
5583d77f1c 2019-09-14 rkeene: tclRetVal = Tcl_ListObjAppendElement(interp, resultPtr, childObj);
5583d77f1c 2019-09-14 rkeene: Tcl_DecrRefCount(childObj);
5583d77f1c 2019-09-14 rkeene:
5583d77f1c 2019-09-14 rkeene: if (tclRetVal != TCL_OK) {
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PUTS("... error (lappend)");
d80c88cee0 2019-09-16 rkeene: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 rkeene: return(tclRetVal);
5583d77f1c 2019-09-14 rkeene: }
5583d77f1c 2019-09-14 rkeene: }
5583d77f1c 2019-09-14 rkeene:
d80c88cee0 2019-09-16 rkeene: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_PRINTF("... ok (returning items: %s)", Tcl_GetString(resultPtr));
d80c88cee0 2019-09-16 rkeene:
d80c88cee0 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 rkeene: return(TCL_OK);
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));
d121970301 2019-05-02 rkeene: }
d121970301 2019-05-02 rkeene:
12ff77016f 2019-09-14 rkeene: static int xvfs_tclfs_standalone_access(Tcl_Obj *path, int mode) {
12ff77016f 2019-09-14 rkeene: return(xvfs_tclfs_access(path, mode, &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));
5583d77f1c 2019-09-14 rkeene: }
5583d77f1c 2019-09-14 rkeene:
5583d77f1c 2019-09-14 rkeene: 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 rkeene: return(xvfs_tclfs_matchInDir(interp, resultPtr, pathPtr, pattern, types, &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) {
0e05b2a8c7 2019-09-16 rkeene: int tclRet;
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:
0e05b2a8c7 2019-09-16 rkeene: xvfs_tclfs_standalone_fs.typeName = "xvfsInstance";
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;
12ff77016f 2019-09-14 rkeene: xvfs_tclfs_standalone_fs.accessProc = xvfs_tclfs_standalone_access;
cb77ecfb24 2019-05-06 rkeene: xvfs_tclfs_standalone_fs.openFileChannelProc = xvfs_tclfs_standalone_openFileChannel;
5583d77f1c 2019-09-14 rkeene: xvfs_tclfs_standalone_fs.matchInDirectoryProc = xvfs_tclfs_standalone_matchInDir;
cb77ecfb24 2019-05-06 rkeene: xvfs_tclfs_standalone_fs.utimeProc = NULL;
cb77ecfb24 2019-05-06 rkeene: xvfs_tclfs_standalone_fs.linkProc = NULL;
d80c88cee0 2019-09-16 rkeene: xvfs_tclfs_standalone_fs.listVolumesProc = NULL;
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();
5ae034e55e 2019-09-14 rkeene:
5ae034e55e 2019-09-14 rkeene: Tcl_IncrRefCount(xvfs_tclfs_standalone_info.mountpoint);
d121970301 2019-05-02 rkeene: Tcl_AppendStringsToObj(xvfs_tclfs_standalone_info.mountpoint, XVFS_ROOT_MOUNTPOINT, fsInfo->name, NULL);
5ae034e55e 2019-09-14 rkeene:
0e05b2a8c7 2019-09-16 rkeene: tclRet = Tcl_FSRegister(NULL, &xvfs_tclfs_standalone_fs);
0e05b2a8c7 2019-09-16 rkeene: if (tclRet != TCL_OK) {
5ae034e55e 2019-09-14 rkeene: Tcl_DecrRefCount(xvfs_tclfs_standalone_info.mountpoint);
38bed7cee0 2019-09-13 rkeene:
9bcf758fef 2019-05-08 rkeene: if (interp) {
9bcf758fef 2019-05-08 rkeene: Tcl_SetResult(interp, "Tcl_FSRegister() failed", NULL);
9bcf758fef 2019-05-08 rkeene: }
38bed7cee0 2019-09-13 rkeene:
0e05b2a8c7 2019-09-16 rkeene: return(tclRet);
9bcf758fef 2019-05-08 rkeene: }
38bed7cee0 2019-09-13 rkeene:
38bed7cee0 2019-09-13 rkeene: xvfs_tclfs_prepareChannelType();
38bed7cee0 2019-09-13 rkeene:
9bcf758fef 2019-05-08 rkeene: return(TCL_OK);
9bcf758fef 2019-05-08 rkeene: }
d92ba3d36d 2019-05-08 rkeene: #endif /* XVFS_MODE_STANDALONE || XVFS_MODE_FLEXIBLE */
9bcf758fef 2019-05-08 rkeene:
9bcf758fef 2019-05-08 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:
f1d16a3958 2019-09-17 rkeene: XVFS_DEBUG_ENTER;
f1d16a3958 2019-09-17 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) {
f1d16a3958 2019-09-17 rkeene: XVFS_DEBUG_LEAVE;
f1d16a3958 2019-09-17 rkeene:
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) {
f1d16a3958 2019-09-17 rkeene: XVFS_DEBUG_LEAVE;
f1d16a3958 2019-09-17 rkeene:
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) {
f1d16a3958 2019-09-17 rkeene: XVFS_DEBUG_LEAVE;
f1d16a3958 2019-09-17 rkeene:
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;
9bcf758fef 2019-05-08 rkeene:
9bcf758fef 2019-05-08 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 ?
9bcf758fef 2019-05-08 rkeene: */
b586d5b0a1 2019-05-08 rkeene: if (memcmp(fsHandlerData->magic, XVFS_INTERNAL_SERVER_MAGIC, sizeof(fsHandlerData->magic)) == 0) {
f1d16a3958 2019-09-17 rkeene: XVFS_DEBUG_PUTS("Found a server handler");
9bcf758fef 2019-05-08 rkeene: xvfs_register = fsHandlerData->registerProc;
9bcf758fef 2019-05-08 rkeene: }
f1d16a3958 2019-09-17 rkeene:
f1d16a3958 2019-09-17 rkeene: XVFS_DEBUG_LEAVE;
d92ba3d36d 2019-05-08 rkeene:
9bcf758fef 2019-05-08 rkeene: return(xvfs_register(interp, fsInfo));
9bcf758fef 2019-05-08 rkeene: }
d92ba3d36d 2019-05-08 rkeene: #endif /* XVFS_MODE_FLEXIBLE */
9bcf758fef 2019-05-08 rkeene:
9bcf758fef 2019-05-08 rkeene: #if defined(XVFS_MODE_SERVER)
0e05b2a8c7 2019-09-16 rkeene: static Tcl_Filesystem xvfs_tclfs_dispatch_fs;
0e05b2a8c7 2019-09-16 rkeene: static Tcl_HashTable xvfs_tclfs_dispatch_map;
f1d16a3958 2019-09-17 rkeene: static struct xvfs_tclfs_server_info xvfs_tclfs_dispatch_fsdata;
0e05b2a8c7 2019-09-16 rkeene:
a101629cc6 2019-09-17 rkeene: static int xvfs_tclfs_dispatch_pathInFS(Tcl_Obj *path, ClientData *dataPtr) {
0e05b2a8c7 2019-09-16 rkeene: const char *pathStr, *rootStr;
0e05b2a8c7 2019-09-16 rkeene: int pathLen, rootLen;
0e05b2a8c7 2019-09-16 rkeene:
0e05b2a8c7 2019-09-16 rkeene: XVFS_DEBUG_ENTER;
0e05b2a8c7 2019-09-16 rkeene:
f1d16a3958 2019-09-17 rkeene: XVFS_DEBUG_PRINTF("Verifying that \"%s\" belongs in XVFS ...", Tcl_GetString(path));
0e05b2a8c7 2019-09-16 rkeene:
0e05b2a8c7 2019-09-16 rkeene: rootStr = XVFS_ROOT_MOUNTPOINT;
0e05b2a8c7 2019-09-16 rkeene: rootLen = strlen(XVFS_ROOT_MOUNTPOINT);
0e05b2a8c7 2019-09-16 rkeene:
0e05b2a8c7 2019-09-16 rkeene: pathStr = Tcl_GetStringFromObj(path, &pathLen);
0e05b2a8c7 2019-09-16 rkeene:
0e05b2a8c7 2019-09-16 rkeene: if (pathLen < rootLen) {
0e05b2a8c7 2019-09-16 rkeene: XVFS_DEBUG_PUTS("... failed (length too short)");
0e05b2a8c7 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
f1d16a3958 2019-09-17 rkeene: return(-1);
0e05b2a8c7 2019-09-16 rkeene: }
0e05b2a8c7 2019-09-16 rkeene:
0e05b2a8c7 2019-09-16 rkeene: if (memcmp(pathStr, rootStr, rootLen) != 0) {
0e05b2a8c7 2019-09-16 rkeene: XVFS_DEBUG_PUTS("... failed (incorrect prefix)");
0e05b2a8c7 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
f1d16a3958 2019-09-17 rkeene: return(-1);
f1d16a3958 2019-09-17 rkeene: }
f1d16a3958 2019-09-17 rkeene:
f1d16a3958 2019-09-17 rkeene: XVFS_DEBUG_PUTS("... yes");
f1d16a3958 2019-09-17 rkeene:
f1d16a3958 2019-09-17 rkeene: XVFS_DEBUG_LEAVE;
f1d16a3958 2019-09-17 rkeene:
f1d16a3958 2019-09-17 rkeene: return(TCL_OK);
f1d16a3958 2019-09-17 rkeene: }
f1d16a3958 2019-09-17 rkeene:
f1d16a3958 2019-09-17 rkeene: static struct xvfs_tclfs_instance_info *xvfs_tclfs_dispatch_pathToInfo(Tcl_Obj *path) {
f1d16a3958 2019-09-17 rkeene: Tcl_HashEntry *mapEntry;
f1d16a3958 2019-09-17 rkeene: struct xvfs_tclfs_instance_info *retval;
f1d16a3958 2019-09-17 rkeene: int rootLen;
f1d16a3958 2019-09-17 rkeene: char *pathStr, *fsName, *fsNameEnds, origSep;
f1d16a3958 2019-09-17 rkeene:
f1d16a3958 2019-09-17 rkeene: XVFS_DEBUG_ENTER;
f1d16a3958 2019-09-17 rkeene:
a101629cc6 2019-09-17 rkeene: if (xvfs_tclfs_dispatch_pathInFS(path, NULL) != TCL_OK) {
f1d16a3958 2019-09-17 rkeene: XVFS_DEBUG_LEAVE;
f1d16a3958 2019-09-17 rkeene:
0e05b2a8c7 2019-09-16 rkeene: return(NULL);
0e05b2a8c7 2019-09-16 rkeene: }
f1d16a3958 2019-09-17 rkeene:
f1d16a3958 2019-09-17 rkeene: rootLen = strlen(XVFS_ROOT_MOUNTPOINT);
f1d16a3958 2019-09-17 rkeene: pathStr = Tcl_GetString(path);
0e05b2a8c7 2019-09-16 rkeene:
0e05b2a8c7 2019-09-16 rkeene: fsName = ((char *) pathStr) + rootLen;
0e05b2a8c7 2019-09-16 rkeene:
0e05b2a8c7 2019-09-16 rkeene: fsNameEnds = strchr(fsName, '/');
0e05b2a8c7 2019-09-16 rkeene: if (fsNameEnds) {
0e05b2a8c7 2019-09-16 rkeene: origSep = *fsNameEnds;
0e05b2a8c7 2019-09-16 rkeene: *fsNameEnds = '\0';
0e05b2a8c7 2019-09-16 rkeene: }
0e05b2a8c7 2019-09-16 rkeene:
0e05b2a8c7 2019-09-16 rkeene: XVFS_DEBUG_PRINTF("... fsName = %s...", fsName);
0e05b2a8c7 2019-09-16 rkeene:
0e05b2a8c7 2019-09-16 rkeene: mapEntry = Tcl_FindHashEntry(&xvfs_tclfs_dispatch_map, fsName);
0e05b2a8c7 2019-09-16 rkeene:
0e05b2a8c7 2019-09-16 rkeene: if (fsNameEnds) {
0e05b2a8c7 2019-09-16 rkeene: *fsNameEnds = origSep;
0e05b2a8c7 2019-09-16 rkeene: }
0e05b2a8c7 2019-09-16 rkeene:
0e05b2a8c7 2019-09-16 rkeene: if (mapEntry) {
0e05b2a8c7 2019-09-16 rkeene: retval = (struct xvfs_tclfs_instance_info *) Tcl_GetHashValue(mapEntry);
0e05b2a8c7 2019-09-16 rkeene: XVFS_DEBUG_PRINTF("... found a registered filesystem: %p", retval);
0e05b2a8c7 2019-09-16 rkeene: } else {
0e05b2a8c7 2019-09-16 rkeene: retval = NULL;
0e05b2a8c7 2019-09-16 rkeene: XVFS_DEBUG_PUTS("... found no registered filesystem.");
0e05b2a8c7 2019-09-16 rkeene: }
0e05b2a8c7 2019-09-16 rkeene:
0e05b2a8c7 2019-09-16 rkeene: XVFS_DEBUG_LEAVE;
0e05b2a8c7 2019-09-16 rkeene: return(retval);
0e05b2a8c7 2019-09-16 rkeene: }
0e05b2a8c7 2019-09-16 rkeene:
0e05b2a8c7 2019-09-16 rkeene: static int xvfs_tclfs_dispatch_stat(Tcl_Obj *path, Tcl_StatBuf *statBuf) {
0e05b2a8c7 2019-09-16 rkeene: struct xvfs_tclfs_instance_info *instanceInfo;
0e05b2a8c7 2019-09-16 rkeene:
f1d16a3958 2019-09-17 rkeene: instanceInfo = xvfs_tclfs_dispatch_pathToInfo(path);
0e05b2a8c7 2019-09-16 rkeene: if (!instanceInfo) {
f1d16a3958 2019-09-17 rkeene: Tcl_SetErrno(xvfs_errorToErrno(XVFS_RV_ERR_ENOENT));
f1d16a3958 2019-09-17 rkeene:
f1d16a3958 2019-09-17 rkeene: return(-1);
0e05b2a8c7 2019-09-16 rkeene: }
0e05b2a8c7 2019-09-16 rkeene:
0e05b2a8c7 2019-09-16 rkeene: return(xvfs_tclfs_stat(path, statBuf, instanceInfo));
0e05b2a8c7 2019-09-16 rkeene: }
0e05b2a8c7 2019-09-16 rkeene:
0e05b2a8c7 2019-09-16 rkeene: static int xvfs_tclfs_dispatch_access(Tcl_Obj *path, int mode) {
0e05b2a8c7 2019-09-16 rkeene: struct xvfs_tclfs_instance_info *instanceInfo;
0e05b2a8c7 2019-09-16 rkeene:
f1d16a3958 2019-09-17 rkeene: instanceInfo = xvfs_tclfs_dispatch_pathToInfo(path);
0e05b2a8c7 2019-09-16 rkeene: if (!instanceInfo) {
0e05b2a8c7 2019-09-16 rkeene: return(-1);
0e05b2a8c7 2019-09-16 rkeene: }
0e05b2a8c7 2019-09-16 rkeene:
0e05b2a8c7 2019-09-16 rkeene: return(xvfs_tclfs_access(path, mode, instanceInfo));
0e05b2a8c7 2019-09-16 rkeene: }
0e05b2a8c7 2019-09-16 rkeene:
0e05b2a8c7 2019-09-16 rkeene: static Tcl_Channel xvfs_tclfs_dispatch_openFileChannel(Tcl_Interp *interp, Tcl_Obj *path, int mode, int permissions) {
0e05b2a8c7 2019-09-16 rkeene: struct xvfs_tclfs_instance_info *instanceInfo;
0e05b2a8c7 2019-09-16 rkeene:
f1d16a3958 2019-09-17 rkeene: instanceInfo = xvfs_tclfs_dispatch_pathToInfo(path);
0e05b2a8c7 2019-09-16 rkeene: if (!instanceInfo) {
0e05b2a8c7 2019-09-16 rkeene: return(NULL);
0e05b2a8c7 2019-09-16 rkeene: }
0e05b2a8c7 2019-09-16 rkeene:
0e05b2a8c7 2019-09-16 rkeene: return(xvfs_tclfs_openFileChannel(interp, path, mode, permissions, instanceInfo));
0e05b2a8c7 2019-09-16 rkeene: }
0e05b2a8c7 2019-09-16 rkeene:
0e05b2a8c7 2019-09-16 rkeene: 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 rkeene: struct xvfs_tclfs_instance_info *instanceInfo;
0e05b2a8c7 2019-09-16 rkeene:
f1d16a3958 2019-09-17 rkeene: instanceInfo = xvfs_tclfs_dispatch_pathToInfo(pathPtr);
0e05b2a8c7 2019-09-16 rkeene: if (!instanceInfo) {
0e05b2a8c7 2019-09-16 rkeene: return(TCL_ERROR);
0e05b2a8c7 2019-09-16 rkeene: }
0e05b2a8c7 2019-09-16 rkeene:
0e05b2a8c7 2019-09-16 rkeene: return(xvfs_tclfs_matchInDir(interp, resultPtr, pathPtr, pattern, types, instanceInfo));
0e05b2a8c7 2019-09-16 rkeene: }
0e05b2a8c7 2019-09-16 rkeene:
f1d16a3958 2019-09-17 rkeene: int Xvfs_Init(Tcl_Interp *interp) {
0e05b2a8c7 2019-09-16 rkeene: static int registered = 0;
0e05b2a8c7 2019-09-16 rkeene: int tclRet;
f1d16a3958 2019-09-17 rkeene:
a101629cc6 2019-09-17 rkeene: #ifdef USE_TCL_STUBS
a101629cc6 2019-09-17 rkeene: const char *tclInitStubs_ret;
a101629cc6 2019-09-17 rkeene: /* Initialize Stubs */
a101629cc6 2019-09-17 rkeene: tclInitStubs_ret = Tcl_InitStubs(interp, TCL_PATCH_LEVEL, 0);
a101629cc6 2019-09-17 rkeene: if (!tclInitStubs_ret) {
a101629cc6 2019-09-17 rkeene: return(TCL_ERROR);
a101629cc6 2019-09-17 rkeene: }
a101629cc6 2019-09-17 rkeene: #endif
a101629cc6 2019-09-17 rkeene:
0e05b2a8c7 2019-09-16 rkeene: /* XXX:TODO: Make this thread-safe */
0e05b2a8c7 2019-09-16 rkeene: if (registered) {
0e05b2a8c7 2019-09-16 rkeene: return(TCL_OK);
0e05b2a8c7 2019-09-16 rkeene: }
0e05b2a8c7 2019-09-16 rkeene: registered = 1;
0e05b2a8c7 2019-09-16 rkeene:
0e05b2a8c7 2019-09-16 rkeene: xvfs_tclfs_dispatch_fs.typeName = "xvfsDispatch";
0e05b2a8c7 2019-09-16 rkeene: xvfs_tclfs_dispatch_fs.structureLength = sizeof(xvfs_tclfs_dispatch_fs);
0e05b2a8c7 2019-09-16 rkeene: xvfs_tclfs_dispatch_fs.version = TCL_FILESYSTEM_VERSION_1;
a101629cc6 2019-09-17 rkeene: xvfs_tclfs_dispatch_fs.pathInFilesystemProc = xvfs_tclfs_dispatch_pathInFS;
0e05b2a8c7 2019-09-16 rkeene: xvfs_tclfs_dispatch_fs.dupInternalRepProc = NULL;
0e05b2a8c7 2019-09-16 rkeene: xvfs_tclfs_dispatch_fs.freeInternalRepProc = NULL;
0e05b2a8c7 2019-09-16 rkeene: xvfs_tclfs_dispatch_fs.internalToNormalizedProc = NULL;
0e05b2a8c7 2019-09-16 rkeene: xvfs_tclfs_dispatch_fs.createInternalRepProc = NULL;
0e05b2a8c7 2019-09-16 rkeene: xvfs_tclfs_dispatch_fs.normalizePathProc = NULL;
0e05b2a8c7 2019-09-16 rkeene: xvfs_tclfs_dispatch_fs.filesystemPathTypeProc = NULL;
0e05b2a8c7 2019-09-16 rkeene: xvfs_tclfs_dispatch_fs.filesystemSeparatorProc = NULL;
0e05b2a8c7 2019-09-16 rkeene: xvfs_tclfs_dispatch_fs.statProc = xvfs_tclfs_dispatch_stat;
0e05b2a8c7 2019-09-16 rkeene: xvfs_tclfs_dispatch_fs.accessProc = xvfs_tclfs_dispatch_access;
0e05b2a8c7 2019-09-16 rkeene: xvfs_tclfs_dispatch_fs.openFileChannelProc = xvfs_tclfs_dispatch_openFileChannel;
0e05b2a8c7 2019-09-16 rkeene: xvfs_tclfs_dispatch_fs.matchInDirectoryProc = xvfs_tclfs_dispatch_matchInDir;
0e05b2a8c7 2019-09-16 rkeene: xvfs_tclfs_dispatch_fs.utimeProc = NULL;
0e05b2a8c7 2019-09-16 rkeene: xvfs_tclfs_dispatch_fs.linkProc = NULL;
0e05b2a8c7 2019-09-16 rkeene: xvfs_tclfs_dispatch_fs.listVolumesProc = NULL;
0e05b2a8c7 2019-09-16 rkeene: xvfs_tclfs_dispatch_fs.fileAttrStringsProc = NULL;
0e05b2a8c7 2019-09-16 rkeene: xvfs_tclfs_dispatch_fs.fileAttrsGetProc = NULL;
0e05b2a8c7 2019-09-16 rkeene: xvfs_tclfs_dispatch_fs.fileAttrsSetProc = NULL;
0e05b2a8c7 2019-09-16 rkeene: xvfs_tclfs_dispatch_fs.createDirectoryProc = NULL;
0e05b2a8c7 2019-09-16 rkeene: xvfs_tclfs_dispatch_fs.removeDirectoryProc = NULL;
0e05b2a8c7 2019-09-16 rkeene: xvfs_tclfs_dispatch_fs.deleteFileProc = NULL;
0e05b2a8c7 2019-09-16 rkeene: xvfs_tclfs_dispatch_fs.copyFileProc = NULL;
0e05b2a8c7 2019-09-16 rkeene: xvfs_tclfs_dispatch_fs.renameFileProc = NULL;
0e05b2a8c7 2019-09-16 rkeene: xvfs_tclfs_dispatch_fs.copyDirectoryProc = NULL;
0e05b2a8c7 2019-09-16 rkeene: xvfs_tclfs_dispatch_fs.lstatProc = NULL;
0e05b2a8c7 2019-09-16 rkeene: xvfs_tclfs_dispatch_fs.loadFileProc = NULL;
0e05b2a8c7 2019-09-16 rkeene: xvfs_tclfs_dispatch_fs.getCwdProc = NULL;
0e05b2a8c7 2019-09-16 rkeene: xvfs_tclfs_dispatch_fs.chdirProc = NULL;
0e05b2a8c7 2019-09-16 rkeene:
f1d16a3958 2019-09-17 rkeene: memcpy(xvfs_tclfs_dispatch_fsdata.magic, XVFS_INTERNAL_SERVER_MAGIC, XVFS_INTERNAL_SERVER_MAGIC_LEN);
f1d16a3958 2019-09-17 rkeene: xvfs_tclfs_dispatch_fsdata.registerProc = Xvfs_Register;
f1d16a3958 2019-09-17 rkeene:
f1d16a3958 2019-09-17 rkeene: tclRet = Tcl_FSRegister((ClientData) &xvfs_tclfs_dispatch_fsdata, &xvfs_tclfs_dispatch_fs);
0e05b2a8c7 2019-09-16 rkeene: if (tclRet != TCL_OK) {
0e05b2a8c7 2019-09-16 rkeene: if (interp) {
0e05b2a8c7 2019-09-16 rkeene: Tcl_SetResult(interp, "Tcl_FSRegister() failed", NULL);
0e05b2a8c7 2019-09-16 rkeene: }
0e05b2a8c7 2019-09-16 rkeene:
0e05b2a8c7 2019-09-16 rkeene: return(tclRet);
0e05b2a8c7 2019-09-16 rkeene: }
0e05b2a8c7 2019-09-16 rkeene:
0e05b2a8c7 2019-09-16 rkeene: /*
0e05b2a8c7 2019-09-16 rkeene: * Initialize the channel type we will use for I/O
0e05b2a8c7 2019-09-16 rkeene: */
0e05b2a8c7 2019-09-16 rkeene: xvfs_tclfs_prepareChannelType();
0e05b2a8c7 2019-09-16 rkeene:
0e05b2a8c7 2019-09-16 rkeene: /*
0e05b2a8c7 2019-09-16 rkeene: * Initialize the map to lookup paths to registered
0e05b2a8c7 2019-09-16 rkeene: * filesystems
0e05b2a8c7 2019-09-16 rkeene: */
0e05b2a8c7 2019-09-16 rkeene: Tcl_InitHashTable(&xvfs_tclfs_dispatch_map, TCL_STRING_KEYS);
0e05b2a8c7 2019-09-16 rkeene:
0e05b2a8c7 2019-09-16 rkeene: return(TCL_OK);
0e05b2a8c7 2019-09-16 rkeene: }
0e05b2a8c7 2019-09-16 rkeene:
d92ba3d36d 2019-05-08 rkeene: int Xvfs_Register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
0e05b2a8c7 2019-09-16 rkeene: Tcl_HashEntry *mapEntry;
0e05b2a8c7 2019-09-16 rkeene: struct xvfs_tclfs_instance_info *instanceInfo;
0e05b2a8c7 2019-09-16 rkeene: int dispatchInitRet;
0e05b2a8c7 2019-09-16 rkeene: int new;
0e05b2a8c7 2019-09-16 rkeene:
f1d16a3958 2019-09-17 rkeene: dispatchInitRet = Xvfs_Init(interp);
0e05b2a8c7 2019-09-16 rkeene: if (dispatchInitRet != TCL_OK) {
0e05b2a8c7 2019-09-16 rkeene: return(dispatchInitRet);
0e05b2a8c7 2019-09-16 rkeene: }
0e05b2a8c7 2019-09-16 rkeene:
0e05b2a8c7 2019-09-16 rkeene: /*
0e05b2a8c7 2019-09-16 rkeene: * Verify this is for a protocol we support
0e05b2a8c7 2019-09-16 rkeene: */
0e05b2a8c7 2019-09-16 rkeene: if (fsInfo->protocolVersion != XVFS_PROTOCOL_VERSION) {
0e05b2a8c7 2019-09-16 rkeene: if (interp) {
0e05b2a8c7 2019-09-16 rkeene: Tcl_SetResult(interp, "Protocol mismatch", NULL);
0e05b2a8c7 2019-09-16 rkeene: }
0e05b2a8c7 2019-09-16 rkeene: return(TCL_ERROR);
0e05b2a8c7 2019-09-16 rkeene: }
0e05b2a8c7 2019-09-16 rkeene:
0e05b2a8c7 2019-09-16 rkeene: /*
0e05b2a8c7 2019-09-16 rkeene: * Create the structure needed
0e05b2a8c7 2019-09-16 rkeene: */
0e05b2a8c7 2019-09-16 rkeene: instanceInfo = (struct xvfs_tclfs_instance_info *) Tcl_Alloc(sizeof(*instanceInfo));
0e05b2a8c7 2019-09-16 rkeene: instanceInfo->fsInfo = fsInfo;
0e05b2a8c7 2019-09-16 rkeene: instanceInfo->mountpoint = Tcl_ObjPrintf("%s%s", XVFS_ROOT_MOUNTPOINT, fsInfo->name);
0e05b2a8c7 2019-09-16 rkeene: Tcl_IncrRefCount(instanceInfo->mountpoint);
0e05b2a8c7 2019-09-16 rkeene:
0e05b2a8c7 2019-09-16 rkeene: /*
0e05b2a8c7 2019-09-16 rkeene: * Register a hash table entry for this name
0e05b2a8c7 2019-09-16 rkeene: */
0e05b2a8c7 2019-09-16 rkeene: new = 0;
0e05b2a8c7 2019-09-16 rkeene: mapEntry = Tcl_CreateHashEntry(&xvfs_tclfs_dispatch_map, fsInfo->name, &new);
0e05b2a8c7 2019-09-16 rkeene: Tcl_SetHashValue(mapEntry, instanceInfo);
0e05b2a8c7 2019-09-16 rkeene:
0e05b2a8c7 2019-09-16 rkeene: return(TCL_OK);
d92ba3d36d 2019-05-08 rkeene: }
d92ba3d36d 2019-05-08 rkeene: #endif /* XVFS_MODE_SERVER */
5f2895faba 2019-09-17 rkeene: #undef XVFS_DEBUG_PRINTF
5f2895faba 2019-09-17 rkeene: #undef XVFS_DEBUG_PUTS
5f2895faba 2019-09-17 rkeene: #undef XVFS_DEBUG_ENTER
5f2895faba 2019-09-17 rkeene: #undef XVFS_DEBUG_LEAVE
5f2895faba 2019-09-17 rkeene: #undef XVFS_INTERNAL_SERVER_MAGIC
5f2895faba 2019-09-17 rkeene: #undef XVFS_INTERNAL_SERVER_MAGIC_LEN
5f2895faba 2019-09-17 rkeene: #undef XVFS_ROOT_MOUNTPOINT