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