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: /*
7d74392642 2019-09-13 397: * If the read call has marked that we have reached EOF,
7d74392642 2019-09-13 398: * do not signal any further
7d74392642 2019-09-13 399: */
7d74392642 2019-09-13 400: if (channelInstanceData->eofMarked) {
7d74392642 2019-09-13 401: return;
7d74392642 2019-09-13 402: }
7d74392642 2019-09-13 403:
7d74392642 2019-09-13 404: event = (struct xvfs_tclfs_channel_event *) Tcl_Alloc(sizeof(*event));
7d74392642 2019-09-13 405: event->tcl.proc = xvfs_tclfs_watchChannelEvent;
7d74392642 2019-09-13 406: event->tcl.nextPtr = NULL;
7d74392642 2019-09-13 407: event->channelInstanceData = channelInstanceData;
aa08a4a749 2019-09-14 408:
aa08a4a749 2019-09-14 409: channelInstanceData->queuedEvents++;
7d74392642 2019-09-13 410:
7d74392642 2019-09-13 411: Tcl_QueueEvent((Tcl_Event *) event, TCL_QUEUE_TAIL);
7d74392642 2019-09-13 412:
38bed7cee0 2019-09-13 413: return;
38bed7cee0 2019-09-13 414: }
38bed7cee0 2019-09-13 415:
38bed7cee0 2019-09-13 416: static int xvfs_tclfs_seekChannel(ClientData channelInstanceData_p, long offset, int mode, int *errorCodePtr) {
38bed7cee0 2019-09-13 417: struct xvfs_tclfs_channel_id *channelInstanceData;
38bed7cee0 2019-09-13 418: Tcl_WideInt newOffset, fileSize;
38bed7cee0 2019-09-13 419:
38bed7cee0 2019-09-13 420: channelInstanceData = (struct xvfs_tclfs_channel_id *) channelInstanceData_p;
38bed7cee0 2019-09-13 421:
38bed7cee0 2019-09-13 422: newOffset = channelInstanceData->currentOffset;
38bed7cee0 2019-09-13 423: fileSize = channelInstanceData->fileSize;
38bed7cee0 2019-09-13 424:
38bed7cee0 2019-09-13 425: switch (mode) {
38bed7cee0 2019-09-13 426: case SEEK_CUR:
38bed7cee0 2019-09-13 427: newOffset += offset;
38bed7cee0 2019-09-13 428: break;
38bed7cee0 2019-09-13 429: case SEEK_SET:
38bed7cee0 2019-09-13 430: newOffset = offset;
38bed7cee0 2019-09-13 431: break;
38bed7cee0 2019-09-13 432: case SEEK_END:
38bed7cee0 2019-09-13 433: newOffset = fileSize + offset;
38bed7cee0 2019-09-13 434: break;
38bed7cee0 2019-09-13 435: default:
38bed7cee0 2019-09-13 436: *errorCodePtr = xvfs_errorToErrno(XVFS_RV_ERR_EINVAL);
38bed7cee0 2019-09-13 437:
38bed7cee0 2019-09-13 438: return(-1);
38bed7cee0 2019-09-13 439: }
38bed7cee0 2019-09-13 440:
38bed7cee0 2019-09-13 441: /*
38bed7cee0 2019-09-13 442: * We allow users to seek right up to the end of the buffer, but
38bed7cee0 2019-09-13 443: * no further, this way if they want to seek backwards from there
38bed7cee0 2019-09-13 444: * it is possible to do so.
38bed7cee0 2019-09-13 445: */
38bed7cee0 2019-09-13 446: if (newOffset < 0 || newOffset > fileSize) {
38bed7cee0 2019-09-13 447: *errorCodePtr = xvfs_errorToErrno(XVFS_RV_ERR_EINVAL);
38bed7cee0 2019-09-13 448:
38bed7cee0 2019-09-13 449: return(-1);
38bed7cee0 2019-09-13 450: }
38bed7cee0 2019-09-13 451:
7d74392642 2019-09-13 452: if (newOffset != channelInstanceData->currentOffset) {
7d74392642 2019-09-13 453: channelInstanceData->eofMarked = 0;
7d74392642 2019-09-13 454: channelInstanceData->currentOffset = newOffset;
7d74392642 2019-09-13 455: }
38bed7cee0 2019-09-13 456:
38bed7cee0 2019-09-13 457: return(channelInstanceData->currentOffset);
38bed7cee0 2019-09-13 458: }
38bed7cee0 2019-09-13 459:
38bed7cee0 2019-09-13 460: static void xvfs_tclfs_prepareChannelType(void) {
38bed7cee0 2019-09-13 461: xvfs_tclfs_channelType.typeName = "xvfs";
38bed7cee0 2019-09-13 462: xvfs_tclfs_channelType.version = TCL_CHANNEL_VERSION_2;
38bed7cee0 2019-09-13 463: xvfs_tclfs_channelType.closeProc = xvfs_tclfs_closeChannel;
38bed7cee0 2019-09-13 464: xvfs_tclfs_channelType.inputProc = xvfs_tclfs_readChannel;
38bed7cee0 2019-09-13 465: xvfs_tclfs_channelType.outputProc = NULL;
38bed7cee0 2019-09-13 466: xvfs_tclfs_channelType.watchProc = xvfs_tclfs_watchChannel;
38bed7cee0 2019-09-13 467: xvfs_tclfs_channelType.getHandleProc = NULL;
38bed7cee0 2019-09-13 468: xvfs_tclfs_channelType.seekProc = xvfs_tclfs_seekChannel;
38bed7cee0 2019-09-13 469: xvfs_tclfs_channelType.setOptionProc = NULL;
38bed7cee0 2019-09-13 470: xvfs_tclfs_channelType.getOptionProc = NULL;
38bed7cee0 2019-09-13 471: xvfs_tclfs_channelType.close2Proc = NULL;
38bed7cee0 2019-09-13 472: xvfs_tclfs_channelType.blockModeProc = NULL;
38bed7cee0 2019-09-13 473: xvfs_tclfs_channelType.flushProc = NULL;
38bed7cee0 2019-09-13 474: xvfs_tclfs_channelType.handlerProc = NULL;
38bed7cee0 2019-09-13 475: xvfs_tclfs_channelType.wideSeekProc = NULL;
38bed7cee0 2019-09-13 476: xvfs_tclfs_channelType.threadActionProc = NULL;
38bed7cee0 2019-09-13 477: xvfs_tclfs_channelType.truncateProc = NULL;
d121970301 2019-05-02 478: }
d121970301 2019-05-02 479:
d121970301 2019-05-02 480: /*
d121970301 2019-05-02 481: * Internal Tcl_Filesystem functions, with the appropriate instance info
d121970301 2019-05-02 482: */
d121970301 2019-05-02 483: static int xvfs_tclfs_pathInFilesystem(Tcl_Obj *path, ClientData *dataPtr, struct xvfs_tclfs_instance_info *instanceInfo) {
d121970301 2019-05-02 484: const char *relativePath;
d80c88cee0 2019-09-16 485: int retval;
d80c88cee0 2019-09-16 486:
d80c88cee0 2019-09-16 487: XVFS_DEBUG_ENTER;
d80c88cee0 2019-09-16 488:
d80c88cee0 2019-09-16 489: XVFS_DEBUG_PRINTF("Checking to see if path \"%s\" is in the filesystem ...", Tcl_GetString(path));
d80c88cee0 2019-09-16 490:
d80c88cee0 2019-09-16 491: path = xvfs_absolutePath(path);
38bed7cee0 2019-09-13 492:
d121970301 2019-05-02 493: relativePath = xvfs_relativePath(path, instanceInfo);
d80c88cee0 2019-09-16 494:
d80c88cee0 2019-09-16 495: retval = TCL_OK;
d121970301 2019-05-02 496: if (!relativePath) {
d80c88cee0 2019-09-16 497: retval = -1;
d121970301 2019-05-02 498: }
38bed7cee0 2019-09-13 499:
d80c88cee0 2019-09-16 500: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 501:
d80c88cee0 2019-09-16 502: XVFS_DEBUG_PRINTF("... %s", retval == -1 ? "no" : "yes");
d80c88cee0 2019-09-16 503:
d80c88cee0 2019-09-16 504: XVFS_DEBUG_LEAVE;
d80c88cee0 2019-09-16 505: return(retval);
d121970301 2019-05-02 506: }
d121970301 2019-05-02 507:
d121970301 2019-05-02 508: static int xvfs_tclfs_stat(Tcl_Obj *path, Tcl_StatBuf *statBuf, struct xvfs_tclfs_instance_info *instanceInfo) {
d121970301 2019-05-02 509: const char *pathStr;
d121970301 2019-05-02 510: int retval;
d121970301 2019-05-02 511:
d80c88cee0 2019-09-16 512: XVFS_DEBUG_ENTER;
d80c88cee0 2019-09-16 513:
d80c88cee0 2019-09-16 514: XVFS_DEBUG_PRINTF("Getting stat() on \"%s\" ...", Tcl_GetString(path));
d80c88cee0 2019-09-16 515:
d80c88cee0 2019-09-16 516: path = xvfs_absolutePath(path);
d80c88cee0 2019-09-16 517:
d121970301 2019-05-02 518: pathStr = xvfs_relativePath(path, instanceInfo);
38bed7cee0 2019-09-13 519:
daf25f5222 2019-05-03 520: retval = instanceInfo->fsInfo->getStatProc(pathStr, statBuf);
149aa89b7d 2019-09-13 521: if (retval < 0) {
cf56967f97 2019-09-17 522: XVFS_DEBUG_PRINTF("... failed: %s", xvfs_strerror(retval));
f1d16a3958 2019-09-17 523:
f1d16a3958 2019-09-17 524: Tcl_SetErrno(xvfs_errorToErrno(retval));
f1d16a3958 2019-09-17 525:
149aa89b7d 2019-09-13 526: retval = -1;
d80c88cee0 2019-09-16 527: } else {
d80c88cee0 2019-09-16 528: XVFS_DEBUG_PUTS("... ok");
149aa89b7d 2019-09-13 529: }
38bed7cee0 2019-09-13 530:
d80c88cee0 2019-09-16 531: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 532:
d80c88cee0 2019-09-16 533: XVFS_DEBUG_LEAVE;
d121970301 2019-05-02 534: return(retval);
d121970301 2019-05-02 535: }
d121970301 2019-05-02 536:
12ff77016f 2019-09-14 537: static int xvfs_tclfs_access(Tcl_Obj *path, int mode, struct xvfs_tclfs_instance_info *instanceInfo) {
12ff77016f 2019-09-14 538: const char *pathStr;
12ff77016f 2019-09-14 539: Tcl_StatBuf fileInfo;
12ff77016f 2019-09-14 540: int statRetVal;
12ff77016f 2019-09-14 541:
d80c88cee0 2019-09-16 542: XVFS_DEBUG_ENTER;
d80c88cee0 2019-09-16 543:
d80c88cee0 2019-09-16 544: XVFS_DEBUG_PRINTF("Getting access(..., %i) on \"%s\" ...", mode, Tcl_GetString(path));
12ff77016f 2019-09-14 545:
12ff77016f 2019-09-14 546: if (mode & W_OK) {
d80c88cee0 2019-09-16 547: XVFS_DEBUG_PUTS("... no (not writable)");
d80c88cee0 2019-09-16 548:
d80c88cee0 2019-09-16 549: XVFS_DEBUG_LEAVE;
d80c88cee0 2019-09-16 550: return(-1);
d80c88cee0 2019-09-16 551: }
d80c88cee0 2019-09-16 552:
d80c88cee0 2019-09-16 553: path = xvfs_absolutePath(path);
d80c88cee0 2019-09-16 554:
d80c88cee0 2019-09-16 555: pathStr = xvfs_relativePath(path, instanceInfo);
d80c88cee0 2019-09-16 556: if (!pathStr) {
d80c88cee0 2019-09-16 557: XVFS_DEBUG_PUTS("... no (not in our path)");
d80c88cee0 2019-09-16 558:
d80c88cee0 2019-09-16 559: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 560:
d80c88cee0 2019-09-16 561: XVFS_DEBUG_LEAVE;
12ff77016f 2019-09-14 562: return(-1);
12ff77016f 2019-09-14 563: }
12ff77016f 2019-09-14 564:
12ff77016f 2019-09-14 565: statRetVal = instanceInfo->fsInfo->getStatProc(pathStr, &fileInfo);
12ff77016f 2019-09-14 566: if (statRetVal < 0) {
d80c88cee0 2019-09-16 567: XVFS_DEBUG_PUTS("... no (not statable)");
d80c88cee0 2019-09-16 568:
d80c88cee0 2019-09-16 569: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 570:
d80c88cee0 2019-09-16 571: XVFS_DEBUG_LEAVE;
12ff77016f 2019-09-14 572: return(-1);
12ff77016f 2019-09-14 573: }
12ff77016f 2019-09-14 574:
12ff77016f 2019-09-14 575: if (mode & X_OK) {
12ff77016f 2019-09-14 576: if (!(fileInfo.st_mode & 040000)) {
d80c88cee0 2019-09-16 577: XVFS_DEBUG_PUTS("... no (not a directory and X_OK specified)");
d80c88cee0 2019-09-16 578:
d80c88cee0 2019-09-16 579: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 580:
d80c88cee0 2019-09-16 581: XVFS_DEBUG_LEAVE;
12ff77016f 2019-09-14 582: return(-1);
12ff77016f 2019-09-14 583: }
12ff77016f 2019-09-14 584: }
12ff77016f 2019-09-14 585:
d80c88cee0 2019-09-16 586: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 587:
d80c88cee0 2019-09-16 588: XVFS_DEBUG_PUTS("... ok");
d80c88cee0 2019-09-16 589:
d80c88cee0 2019-09-16 590: XVFS_DEBUG_LEAVE;
12ff77016f 2019-09-14 591: return(0);
d121970301 2019-05-02 592: }
d121970301 2019-05-02 593:
d121970301 2019-05-02 594: 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 595: Tcl_Channel retval;
d80c88cee0 2019-09-16 596: Tcl_Obj *pathRel;
38bed7cee0 2019-09-13 597: const char *pathStr;
38bed7cee0 2019-09-13 598:
d80c88cee0 2019-09-16 599: XVFS_DEBUG_ENTER;
d80c88cee0 2019-09-16 600:
d80c88cee0 2019-09-16 601: XVFS_DEBUG_PRINTF("Asked to open(\"%s\", %x)...", Tcl_GetString(path), mode);
38bed7cee0 2019-09-13 602:
38bed7cee0 2019-09-13 603: if (mode & O_WRONLY) {
10f67b2ced 2019-09-16 604: XVFS_DEBUG_PUTS("... failed (asked to open for writing)");
10f67b2ced 2019-09-16 605:
e786b9e07b 2019-09-16 606: xvfs_setresults_error(interp, XVFS_RV_ERR_EROFS);
d80c88cee0 2019-09-16 607:
d80c88cee0 2019-09-16 608: XVFS_DEBUG_LEAVE;
38bed7cee0 2019-09-13 609: return(NULL);
38bed7cee0 2019-09-13 610: }
38bed7cee0 2019-09-13 611:
d80c88cee0 2019-09-16 612: path = xvfs_absolutePath(path);
d80c88cee0 2019-09-16 613:
d80c88cee0 2019-09-16 614: pathStr = xvfs_relativePath(path, instanceInfo);
d80c88cee0 2019-09-16 615:
d80c88cee0 2019-09-16 616: pathRel = Tcl_NewStringObj(pathStr, -1);
d80c88cee0 2019-09-16 617:
d80c88cee0 2019-09-16 618: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 619:
d80c88cee0 2019-09-16 620: XVFS_DEBUG_PUTS("... done, passing off to channel handler");
d80c88cee0 2019-09-16 621:
e786b9e07b 2019-09-16 622: retval = xvfs_tclfs_openChannel(interp, pathRel, instanceInfo);
d80c88cee0 2019-09-16 623:
d80c88cee0 2019-09-16 624: XVFS_DEBUG_LEAVE;
d80c88cee0 2019-09-16 625: return(retval);
5583d77f1c 2019-09-14 626: }
5583d77f1c 2019-09-14 627:
5583d77f1c 2019-09-14 628: static int xvfs_tclfs_verifyType(Tcl_Obj *path, Tcl_GlobTypeData *types, struct xvfs_tclfs_instance_info *instanceInfo) {
5583d77f1c 2019-09-14 629: const char *pathStr;
5583d77f1c 2019-09-14 630: Tcl_StatBuf fileInfo;
5583d77f1c 2019-09-14 631: int statRetVal;
5583d77f1c 2019-09-14 632:
d80c88cee0 2019-09-16 633: XVFS_DEBUG_ENTER;
d80c88cee0 2019-09-16 634:
d80c88cee0 2019-09-16 635: if (types) {
d80c88cee0 2019-09-16 636: 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 637: } else {
d80c88cee0 2019-09-16 638: XVFS_DEBUG_PRINTF("Asked to verify the existence \"%s\" ...", Tcl_GetString(path));
d80c88cee0 2019-09-16 639: }
d80c88cee0 2019-09-16 640:
5583d77f1c 2019-09-14 641: statRetVal = xvfs_tclfs_stat(path, &fileInfo, instanceInfo);
5583d77f1c 2019-09-14 642: if (statRetVal != 0) {
d80c88cee0 2019-09-16 643: XVFS_DEBUG_PUTS("... no (cannot stat)");
d80c88cee0 2019-09-16 644:
d80c88cee0 2019-09-16 645: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 646: return(0);
5583d77f1c 2019-09-14 647: }
5583d77f1c 2019-09-14 648:
5583d77f1c 2019-09-14 649: if (!types) {
d80c88cee0 2019-09-16 650: XVFS_DEBUG_PUTS("... yes");
d80c88cee0 2019-09-16 651:
d80c88cee0 2019-09-16 652: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 653: return(1);
5583d77f1c 2019-09-14 654: }
5583d77f1c 2019-09-14 655:
5583d77f1c 2019-09-14 656: if (types->perm != TCL_GLOB_PERM_RONLY) {
12ff77016f 2019-09-14 657: if (types->perm & (TCL_GLOB_PERM_W | TCL_GLOB_PERM_HIDDEN)) {
d80c88cee0 2019-09-16 658: XVFS_DEBUG_PUTS("... no (checked for writable or hidden, not supported)");
d80c88cee0 2019-09-16 659:
d80c88cee0 2019-09-16 660: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 661: return(0);
12ff77016f 2019-09-14 662: }
12ff77016f 2019-09-14 663:
12ff77016f 2019-09-14 664: if ((types->perm & TCL_GLOB_PERM_X) == TCL_GLOB_PERM_X) {
12ff77016f 2019-09-14 665: if (!(fileInfo.st_mode & 040000)) {
d80c88cee0 2019-09-16 666: XVFS_DEBUG_PUTS("... no (checked for executable but not a directory)");
d80c88cee0 2019-09-16 667:
d80c88cee0 2019-09-16 668: XVFS_DEBUG_LEAVE;
12ff77016f 2019-09-14 669: return(0);
12ff77016f 2019-09-14 670: }
5583d77f1c 2019-09-14 671: }
5583d77f1c 2019-09-14 672: }
5583d77f1c 2019-09-14 673:
5583d77f1c 2019-09-14 674: 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 675: XVFS_DEBUG_PUTS("... no (checked for block, char, pipe, sock, or link, not supported)");
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: if ((types->type & TCL_GLOB_TYPE_DIR) == TCL_GLOB_TYPE_DIR) {
5583d77f1c 2019-09-14 682: if (!(fileInfo.st_mode & 040000)) {
d80c88cee0 2019-09-16 683: XVFS_DEBUG_PUTS("... no (checked for directory but not a directory)");
d80c88cee0 2019-09-16 684:
d80c88cee0 2019-09-16 685: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 686: return(0);
5583d77f1c 2019-09-14 687: }
5583d77f1c 2019-09-14 688: }
5583d77f1c 2019-09-14 689:
5583d77f1c 2019-09-14 690: if ((types->type & TCL_GLOB_TYPE_FILE) == TCL_GLOB_TYPE_FILE) {
5583d77f1c 2019-09-14 691: if (!(fileInfo.st_mode & 0100000)) {
d80c88cee0 2019-09-16 692: XVFS_DEBUG_PUTS("... no (checked for file but not a file)");
d80c88cee0 2019-09-16 693:
d80c88cee0 2019-09-16 694: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 695: return(0);
5583d77f1c 2019-09-14 696: }
5583d77f1c 2019-09-14 697: }
5583d77f1c 2019-09-14 698:
5583d77f1c 2019-09-14 699: if ((types->type & TCL_GLOB_TYPE_MOUNT) == TCL_GLOB_TYPE_MOUNT) {
d80c88cee0 2019-09-16 700: path = xvfs_absolutePath(path);
5583d77f1c 2019-09-14 701: pathStr = xvfs_relativePath(path, instanceInfo);
5583d77f1c 2019-09-14 702: if (!pathStr) {
d80c88cee0 2019-09-16 703: XVFS_DEBUG_PUTS("... no (checked for mount but not able to resolve path)");
d80c88cee0 2019-09-16 704:
d80c88cee0 2019-09-16 705: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 706:
d80c88cee0 2019-09-16 707: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 708: return(0);
5583d77f1c 2019-09-14 709: }
5583d77f1c 2019-09-14 710:
5583d77f1c 2019-09-14 711: if (strlen(pathStr) != 0) {
d80c88cee0 2019-09-16 712: XVFS_DEBUG_PUTS("... no (checked for mount but not our top-level directory)");
d80c88cee0 2019-09-16 713:
d80c88cee0 2019-09-16 714: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 715:
d80c88cee0 2019-09-16 716: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 717: return(0);
5583d77f1c 2019-09-14 718: }
d80c88cee0 2019-09-16 719:
d80c88cee0 2019-09-16 720: Tcl_DecrRefCount(path);
5583d77f1c 2019-09-14 721: }
5583d77f1c 2019-09-14 722:
d80c88cee0 2019-09-16 723: XVFS_DEBUG_PUTS("... yes");
d80c88cee0 2019-09-16 724:
d80c88cee0 2019-09-16 725: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 726: return(1);
5583d77f1c 2019-09-14 727: }
5583d77f1c 2019-09-14 728:
5583d77f1c 2019-09-14 729: 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 730: const char *pathStr;
5583d77f1c 2019-09-14 731: const char **children, *child;
5583d77f1c 2019-09-14 732: Tcl_WideInt childrenCount, idx;
5583d77f1c 2019-09-14 733: Tcl_Obj *childObj;
5583d77f1c 2019-09-14 734: int tclRetVal;
5583d77f1c 2019-09-14 735:
5583d77f1c 2019-09-14 736: if (pattern == NULL) {
5583d77f1c 2019-09-14 737: if (xvfs_tclfs_verifyType(path, types, instanceInfo)) {
5583d77f1c 2019-09-14 738: return(TCL_OK);
5583d77f1c 2019-09-14 739: }
5583d77f1c 2019-09-14 740:
5583d77f1c 2019-09-14 741: return(TCL_ERROR);
5583d77f1c 2019-09-14 742: }
5583d77f1c 2019-09-14 743:
d80c88cee0 2019-09-16 744: XVFS_DEBUG_ENTER;
d80c88cee0 2019-09-16 745:
d80c88cee0 2019-09-16 746: path = xvfs_absolutePath(path);
d80c88cee0 2019-09-16 747:
d80c88cee0 2019-09-16 748: if (types) {
d80c88cee0 2019-09-16 749: 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 750: } else {
d80c88cee0 2019-09-16 751: XVFS_DEBUG_PRINTF("Checking for files matching %s in \"%s\" ...", pattern, Tcl_GetString(path));
d80c88cee0 2019-09-16 752: }
d80c88cee0 2019-09-16 753:
5583d77f1c 2019-09-14 754: pathStr = xvfs_relativePath(path, instanceInfo);
5583d77f1c 2019-09-14 755: if (!pathStr) {
d80c88cee0 2019-09-16 756: XVFS_DEBUG_PUTS("... error (not in our VFS)");
5583d77f1c 2019-09-14 757:
d80c88cee0 2019-09-16 758: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 759:
e786b9e07b 2019-09-16 760: xvfs_setresults_error(interp, XVFS_RV_ERR_ENOENT);
d80c88cee0 2019-09-16 761:
d80c88cee0 2019-09-16 762: XVFS_DEBUG_LEAVE;
d80c88cee0 2019-09-16 763: return(TCL_OK);
5583d77f1c 2019-09-14 764: }
5583d77f1c 2019-09-14 765:
5583d77f1c 2019-09-14 766: childrenCount = 0;
5583d77f1c 2019-09-14 767: children = instanceInfo->fsInfo->getChildrenProc(pathStr, &childrenCount);
5583d77f1c 2019-09-14 768: if (childrenCount < 0) {
cf56967f97 2019-09-17 769: XVFS_DEBUG_PRINTF("... error: %s", xvfs_strerror(childrenCount));
5583d77f1c 2019-09-14 770:
d80c88cee0 2019-09-16 771: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 772:
e786b9e07b 2019-09-16 773: xvfs_setresults_error(interp, childrenCount);
d80c88cee0 2019-09-16 774:
d80c88cee0 2019-09-16 775: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 776: return(TCL_ERROR);
5583d77f1c 2019-09-14 777: }
5583d77f1c 2019-09-14 778:
5583d77f1c 2019-09-14 779: for (idx = 0; idx < childrenCount; idx++) {
5583d77f1c 2019-09-14 780: child = children[idx];
5583d77f1c 2019-09-14 781:
5583d77f1c 2019-09-14 782: if (!Tcl_StringMatch(child, pattern)) {
5583d77f1c 2019-09-14 783: continue;
5583d77f1c 2019-09-14 784: }
5583d77f1c 2019-09-14 785:
5583d77f1c 2019-09-14 786: childObj = Tcl_DuplicateObj(path);
5583d77f1c 2019-09-14 787: Tcl_IncrRefCount(childObj);
5ae034e55e 2019-09-14 788: Tcl_AppendStringsToObj(childObj, "/", child, NULL);
5583d77f1c 2019-09-14 789:
5583d77f1c 2019-09-14 790: if (!xvfs_tclfs_verifyType(childObj, types, instanceInfo)) {
5583d77f1c 2019-09-14 791: Tcl_DecrRefCount(childObj);
5583d77f1c 2019-09-14 792:
5583d77f1c 2019-09-14 793: continue;
5583d77f1c 2019-09-14 794: }
5583d77f1c 2019-09-14 795:
5583d77f1c 2019-09-14 796: tclRetVal = Tcl_ListObjAppendElement(interp, resultPtr, childObj);
5583d77f1c 2019-09-14 797: Tcl_DecrRefCount(childObj);
5583d77f1c 2019-09-14 798:
5583d77f1c 2019-09-14 799: if (tclRetVal != TCL_OK) {
d80c88cee0 2019-09-16 800: XVFS_DEBUG_PUTS("... error (lappend)");
d80c88cee0 2019-09-16 801: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 802:
d80c88cee0 2019-09-16 803: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 804: return(tclRetVal);
5583d77f1c 2019-09-14 805: }
5583d77f1c 2019-09-14 806: }
5583d77f1c 2019-09-14 807:
d80c88cee0 2019-09-16 808: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 809:
d80c88cee0 2019-09-16 810: XVFS_DEBUG_PRINTF("... ok (returning items: %s)", Tcl_GetString(resultPtr));
d80c88cee0 2019-09-16 811:
d80c88cee0 2019-09-16 812: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 813: return(TCL_OK);
d121970301 2019-05-02 814: }
3e44e1def1 2019-05-02 815: #endif /* XVFS_MODE_SERVER || XVFS_MODE_STANDALONE || XVFS_MODE_FLEIXBLE */
d121970301 2019-05-02 816:
88f96696b7 2019-05-03 817: #if defined(XVFS_MODE_STANDALONE) || defined(XVFS_MODE_FLEXIBLE)
d121970301 2019-05-02 818: /*
d121970301 2019-05-02 819: * Tcl_Filesystem handlers for the standalone implementation
d121970301 2019-05-02 820: */
acfc5037c6 2019-05-02 821: static struct xvfs_tclfs_instance_info xvfs_tclfs_standalone_info;
d121970301 2019-05-02 822: static int xvfs_tclfs_standalone_pathInFilesystem(Tcl_Obj *path, ClientData *dataPtr) {
d121970301 2019-05-02 823: return(xvfs_tclfs_pathInFilesystem(path, dataPtr, &xvfs_tclfs_standalone_info));
d121970301 2019-05-02 824: }
d121970301 2019-05-02 825:
d121970301 2019-05-02 826: static int xvfs_tclfs_standalone_stat(Tcl_Obj *path, Tcl_StatBuf *statBuf) {
d121970301 2019-05-02 827: return(xvfs_tclfs_stat(path, statBuf, &xvfs_tclfs_standalone_info));
d121970301 2019-05-02 828: }
d121970301 2019-05-02 829:
12ff77016f 2019-09-14 830: static int xvfs_tclfs_standalone_access(Tcl_Obj *path, int mode) {
12ff77016f 2019-09-14 831: return(xvfs_tclfs_access(path, mode, &xvfs_tclfs_standalone_info));
e5b6962adf 2019-05-02 832: }
e5b6962adf 2019-05-02 833:
d121970301 2019-05-02 834: static Tcl_Channel xvfs_tclfs_standalone_openFileChannel(Tcl_Interp *interp, Tcl_Obj *path, int mode, int permissions) {
d121970301 2019-05-02 835: return(xvfs_tclfs_openFileChannel(interp, path, mode, permissions, &xvfs_tclfs_standalone_info));
5583d77f1c 2019-09-14 836: }
5583d77f1c 2019-09-14 837:
5583d77f1c 2019-09-14 838: 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 839: return(xvfs_tclfs_matchInDir(interp, resultPtr, pathPtr, pattern, types, &xvfs_tclfs_standalone_info));
e5b6962adf 2019-05-02 840: }
32b55a907b 2019-05-02 841:
32b55a907b 2019-05-02 842: /*
32b55a907b 2019-05-02 843: * There are three (3) modes of operation for Xvfs_Register:
32b55a907b 2019-05-02 844: * 1. standalone -- We register our own Tcl_Filesystem
32b55a907b 2019-05-02 845: * and handle requests under `//xvfs:/<fsName>`
32b55a907b 2019-05-02 846: * 2. client -- A single Tcl_Filesystem is registered for the
32b55a907b 2019-05-02 847: * interp to handle requests under `//xvfs:/` which
32b55a907b 2019-05-02 848: * then dispatches to the appropriate registered
32b55a907b 2019-05-02 849: * handler
32b55a907b 2019-05-02 850: * 3. flexible -- Attempts to find a core Xvfs instance for the
32b55a907b 2019-05-02 851: * process at runtime, if found do #2, otherwise
32b55a907b 2019-05-02 852: * fallback to #1
32b55a907b 2019-05-02 853: *
32b55a907b 2019-05-02 854: */
cb77ecfb24 2019-05-06 855: static Tcl_Filesystem xvfs_tclfs_standalone_fs;
b8cca3a6b4 2019-05-08 856: static int xvfs_standalone_register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
0e05b2a8c7 2019-09-16 857: int tclRet;
d121970301 2019-05-02 858: static int registered = 0;
38bed7cee0 2019-09-13 859:
d121970301 2019-05-02 860: /*
d121970301 2019-05-02 861: * Ensure this instance is not already registered
d121970301 2019-05-02 862: */
d121970301 2019-05-02 863: if (registered) {
d121970301 2019-05-02 864: return(TCL_OK);
d121970301 2019-05-02 865: }
d121970301 2019-05-02 866: registered = 1;
d121970301 2019-05-02 867:
e5b6962adf 2019-05-02 868: /*
e5b6962adf 2019-05-02 869: * In standalone mode, we only support the same protocol we are
e5b6962adf 2019-05-02 870: * compiling for.
e5b6962adf 2019-05-02 871: */
e5b6962adf 2019-05-02 872: if (fsInfo->protocolVersion != XVFS_PROTOCOL_VERSION) {
e5b6962adf 2019-05-02 873: if (interp) {
e5b6962adf 2019-05-02 874: Tcl_SetResult(interp, "Protocol mismatch", NULL);
e5b6962adf 2019-05-02 875: }
e5b6962adf 2019-05-02 876: return(TCL_ERROR);
e5b6962adf 2019-05-02 877: }
38bed7cee0 2019-09-13 878:
0e05b2a8c7 2019-09-16 879: xvfs_tclfs_standalone_fs.typeName = "xvfsInstance";
cb77ecfb24 2019-05-06 880: xvfs_tclfs_standalone_fs.structureLength = sizeof(xvfs_tclfs_standalone_fs);
cb77ecfb24 2019-05-06 881: xvfs_tclfs_standalone_fs.version = TCL_FILESYSTEM_VERSION_1;
cb77ecfb24 2019-05-06 882: xvfs_tclfs_standalone_fs.pathInFilesystemProc = xvfs_tclfs_standalone_pathInFilesystem;
cb77ecfb24 2019-05-06 883: xvfs_tclfs_standalone_fs.dupInternalRepProc = NULL;
cb77ecfb24 2019-05-06 884: xvfs_tclfs_standalone_fs.freeInternalRepProc = NULL;
cb77ecfb24 2019-05-06 885: xvfs_tclfs_standalone_fs.internalToNormalizedProc = NULL;
cb77ecfb24 2019-05-06 886: xvfs_tclfs_standalone_fs.createInternalRepProc = NULL;
cb77ecfb24 2019-05-06 887: xvfs_tclfs_standalone_fs.normalizePathProc = NULL;
cb77ecfb24 2019-05-06 888: xvfs_tclfs_standalone_fs.filesystemPathTypeProc = NULL;
cb77ecfb24 2019-05-06 889: xvfs_tclfs_standalone_fs.filesystemSeparatorProc = NULL;
cb77ecfb24 2019-05-06 890: xvfs_tclfs_standalone_fs.statProc = xvfs_tclfs_standalone_stat;
12ff77016f 2019-09-14 891: xvfs_tclfs_standalone_fs.accessProc = xvfs_tclfs_standalone_access;
cb77ecfb24 2019-05-06 892: xvfs_tclfs_standalone_fs.openFileChannelProc = xvfs_tclfs_standalone_openFileChannel;
5583d77f1c 2019-09-14 893: xvfs_tclfs_standalone_fs.matchInDirectoryProc = xvfs_tclfs_standalone_matchInDir;
cb77ecfb24 2019-05-06 894: xvfs_tclfs_standalone_fs.utimeProc = NULL;
cb77ecfb24 2019-05-06 895: xvfs_tclfs_standalone_fs.linkProc = NULL;
d80c88cee0 2019-09-16 896: xvfs_tclfs_standalone_fs.listVolumesProc = NULL;
cb77ecfb24 2019-05-06 897: xvfs_tclfs_standalone_fs.fileAttrStringsProc = NULL;
cb77ecfb24 2019-05-06 898: xvfs_tclfs_standalone_fs.fileAttrsGetProc = NULL;
cb77ecfb24 2019-05-06 899: xvfs_tclfs_standalone_fs.fileAttrsSetProc = NULL;
cb77ecfb24 2019-05-06 900: xvfs_tclfs_standalone_fs.createDirectoryProc = NULL;
cb77ecfb24 2019-05-06 901: xvfs_tclfs_standalone_fs.removeDirectoryProc = NULL;
cb77ecfb24 2019-05-06 902: xvfs_tclfs_standalone_fs.deleteFileProc = NULL;
cb77ecfb24 2019-05-06 903: xvfs_tclfs_standalone_fs.copyFileProc = NULL;
cb77ecfb24 2019-05-06 904: xvfs_tclfs_standalone_fs.renameFileProc = NULL;
cb77ecfb24 2019-05-06 905: xvfs_tclfs_standalone_fs.copyDirectoryProc = NULL;
cb77ecfb24 2019-05-06 906: xvfs_tclfs_standalone_fs.lstatProc = NULL;
cb77ecfb24 2019-05-06 907: xvfs_tclfs_standalone_fs.loadFileProc = NULL;
cb77ecfb24 2019-05-06 908: xvfs_tclfs_standalone_fs.getCwdProc = NULL;
cb77ecfb24 2019-05-06 909: xvfs_tclfs_standalone_fs.chdirProc = NULL;
d121970301 2019-05-02 910:
d121970301 2019-05-02 911: xvfs_tclfs_standalone_info.fsInfo = fsInfo;
d121970301 2019-05-02 912: xvfs_tclfs_standalone_info.mountpoint = Tcl_NewObj();
5ae034e55e 2019-09-14 913:
5ae034e55e 2019-09-14 914: Tcl_IncrRefCount(xvfs_tclfs_standalone_info.mountpoint);
d121970301 2019-05-02 915: Tcl_AppendStringsToObj(xvfs_tclfs_standalone_info.mountpoint, XVFS_ROOT_MOUNTPOINT, fsInfo->name, NULL);
5ae034e55e 2019-09-14 916:
0e05b2a8c7 2019-09-16 917: tclRet = Tcl_FSRegister(NULL, &xvfs_tclfs_standalone_fs);
0e05b2a8c7 2019-09-16 918: if (tclRet != TCL_OK) {
5ae034e55e 2019-09-14 919: Tcl_DecrRefCount(xvfs_tclfs_standalone_info.mountpoint);
38bed7cee0 2019-09-13 920:
9bcf758fef 2019-05-08 921: if (interp) {
9bcf758fef 2019-05-08 922: Tcl_SetResult(interp, "Tcl_FSRegister() failed", NULL);
9bcf758fef 2019-05-08 923: }
38bed7cee0 2019-09-13 924:
0e05b2a8c7 2019-09-16 925: return(tclRet);
9bcf758fef 2019-05-08 926: }
38bed7cee0 2019-09-13 927:
38bed7cee0 2019-09-13 928: xvfs_tclfs_prepareChannelType();
38bed7cee0 2019-09-13 929:
9bcf758fef 2019-05-08 930: return(TCL_OK);
9bcf758fef 2019-05-08 931: }
d92ba3d36d 2019-05-08 932: #endif /* XVFS_MODE_STANDALONE || XVFS_MODE_FLEXIBLE */
9bcf758fef 2019-05-08 933:
9bcf758fef 2019-05-08 934: #if defined(XVFS_MODE_FLEXIBLE)
b8cca3a6b4 2019-05-08 935: static int xvfs_flexible_register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
9bcf758fef 2019-05-08 936: ClientData fsHandlerDataRaw;
9bcf758fef 2019-05-08 937: struct xvfs_tclfs_server_info *fsHandlerData;
9bcf758fef 2019-05-08 938: const Tcl_Filesystem *fsHandler;
9bcf758fef 2019-05-08 939: int (*xvfs_register)(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo);
9bcf758fef 2019-05-08 940: Tcl_Obj *rootPathObj;
9bcf758fef 2019-05-08 941:
f1d16a3958 2019-09-17 942: XVFS_DEBUG_ENTER;
f1d16a3958 2019-09-17 943:
9bcf758fef 2019-05-08 944: xvfs_register = &xvfs_standalone_register;
9bcf758fef 2019-05-08 945:
9bcf758fef 2019-05-08 946: rootPathObj = Tcl_NewStringObj(XVFS_ROOT_MOUNTPOINT, -1);
9bcf758fef 2019-05-08 947: if (!rootPathObj) {
f1d16a3958 2019-09-17 948: XVFS_DEBUG_LEAVE;
f1d16a3958 2019-09-17 949:
9bcf758fef 2019-05-08 950: return(xvfs_register(interp, fsInfo));
9bcf758fef 2019-05-08 951: }
9bcf758fef 2019-05-08 952:
9bcf758fef 2019-05-08 953: Tcl_IncrRefCount(rootPathObj);
9bcf758fef 2019-05-08 954: fsHandler = Tcl_FSGetFileSystemForPath(rootPathObj);
9bcf758fef 2019-05-08 955: Tcl_DecrRefCount(rootPathObj);
9bcf758fef 2019-05-08 956:
9bcf758fef 2019-05-08 957: if (!fsHandler) {
f1d16a3958 2019-09-17 958: XVFS_DEBUG_LEAVE;
f1d16a3958 2019-09-17 959:
9bcf758fef 2019-05-08 960: return(xvfs_register(interp, fsInfo));
9bcf758fef 2019-05-08 961: }
9bcf758fef 2019-05-08 962:
9bcf758fef 2019-05-08 963: fsHandlerDataRaw = Tcl_FSData(fsHandler);
9bcf758fef 2019-05-08 964: if (!fsHandlerDataRaw) {
f1d16a3958 2019-09-17 965: XVFS_DEBUG_LEAVE;
f1d16a3958 2019-09-17 966:
9bcf758fef 2019-05-08 967: return(xvfs_register(interp, fsInfo));
9bcf758fef 2019-05-08 968: }
9bcf758fef 2019-05-08 969:
9bcf758fef 2019-05-08 970: fsHandlerData = (struct xvfs_tclfs_server_info *) fsHandlerDataRaw;
9bcf758fef 2019-05-08 971:
9bcf758fef 2019-05-08 972: /*
9bcf758fef 2019-05-08 973: * XXX:TODO: What is the chance that the handler for //xvfs:/ hold
b586d5b0a1 2019-05-08 974: * client data smaller than XVFS_INTERNAL_SERVER_MAGIC_LEN ?
9bcf758fef 2019-05-08 975: */
b586d5b0a1 2019-05-08 976: if (memcmp(fsHandlerData->magic, XVFS_INTERNAL_SERVER_MAGIC, sizeof(fsHandlerData->magic)) == 0) {
f1d16a3958 2019-09-17 977: XVFS_DEBUG_PUTS("Found a server handler");
9bcf758fef 2019-05-08 978: xvfs_register = fsHandlerData->registerProc;
9bcf758fef 2019-05-08 979: }
f1d16a3958 2019-09-17 980:
f1d16a3958 2019-09-17 981: XVFS_DEBUG_LEAVE;
d92ba3d36d 2019-05-08 982:
9bcf758fef 2019-05-08 983: return(xvfs_register(interp, fsInfo));
9bcf758fef 2019-05-08 984: }
d92ba3d36d 2019-05-08 985: #endif /* XVFS_MODE_FLEXIBLE */
9bcf758fef 2019-05-08 986:
9bcf758fef 2019-05-08 987: #if defined(XVFS_MODE_SERVER)
0e05b2a8c7 2019-09-16 988: static Tcl_Filesystem xvfs_tclfs_dispatch_fs;
0e05b2a8c7 2019-09-16 989: static Tcl_HashTable xvfs_tclfs_dispatch_map;
f1d16a3958 2019-09-17 990: static struct xvfs_tclfs_server_info xvfs_tclfs_dispatch_fsdata;
0e05b2a8c7 2019-09-16 991:
a101629cc6 2019-09-17 992: static int xvfs_tclfs_dispatch_pathInFS(Tcl_Obj *path, ClientData *dataPtr) {
0e05b2a8c7 2019-09-16 993: const char *pathStr, *rootStr;
0e05b2a8c7 2019-09-16 994: int pathLen, rootLen;
0e05b2a8c7 2019-09-16 995:
0e05b2a8c7 2019-09-16 996: XVFS_DEBUG_ENTER;
0e05b2a8c7 2019-09-16 997:
f1d16a3958 2019-09-17 998: XVFS_DEBUG_PRINTF("Verifying that \"%s\" belongs in XVFS ...", Tcl_GetString(path));
0e05b2a8c7 2019-09-16 999:
0e05b2a8c7 2019-09-16 1000: rootStr = XVFS_ROOT_MOUNTPOINT;
0e05b2a8c7 2019-09-16 1001: rootLen = strlen(XVFS_ROOT_MOUNTPOINT);
0e05b2a8c7 2019-09-16 1002:
0e05b2a8c7 2019-09-16 1003: pathStr = Tcl_GetStringFromObj(path, &pathLen);
0e05b2a8c7 2019-09-16 1004:
0e05b2a8c7 2019-09-16 1005: if (pathLen < rootLen) {
0e05b2a8c7 2019-09-16 1006: XVFS_DEBUG_PUTS("... failed (length too short)");
0e05b2a8c7 2019-09-16 1007: XVFS_DEBUG_LEAVE;
f1d16a3958 2019-09-17 1008: return(-1);
0e05b2a8c7 2019-09-16 1009: }
0e05b2a8c7 2019-09-16 1010:
0e05b2a8c7 2019-09-16 1011: if (memcmp(pathStr, rootStr, rootLen) != 0) {
0e05b2a8c7 2019-09-16 1012: XVFS_DEBUG_PUTS("... failed (incorrect prefix)");
0e05b2a8c7 2019-09-16 1013: XVFS_DEBUG_LEAVE;
f1d16a3958 2019-09-17 1014: return(-1);
f1d16a3958 2019-09-17 1015: }
f1d16a3958 2019-09-17 1016:
f1d16a3958 2019-09-17 1017: XVFS_DEBUG_PUTS("... yes");
f1d16a3958 2019-09-17 1018:
f1d16a3958 2019-09-17 1019: XVFS_DEBUG_LEAVE;
f1d16a3958 2019-09-17 1020:
f1d16a3958 2019-09-17 1021: return(TCL_OK);
f1d16a3958 2019-09-17 1022: }
f1d16a3958 2019-09-17 1023:
f1d16a3958 2019-09-17 1024: static struct xvfs_tclfs_instance_info *xvfs_tclfs_dispatch_pathToInfo(Tcl_Obj *path) {
f1d16a3958 2019-09-17 1025: Tcl_HashEntry *mapEntry;
f1d16a3958 2019-09-17 1026: struct xvfs_tclfs_instance_info *retval;
f1d16a3958 2019-09-17 1027: int rootLen;
f1d16a3958 2019-09-17 1028: char *pathStr, *fsName, *fsNameEnds, origSep;
f1d16a3958 2019-09-17 1029:
f1d16a3958 2019-09-17 1030: XVFS_DEBUG_ENTER;
f1d16a3958 2019-09-17 1031:
a101629cc6 2019-09-17 1032: if (xvfs_tclfs_dispatch_pathInFS(path, NULL) != TCL_OK) {
f1d16a3958 2019-09-17 1033: XVFS_DEBUG_LEAVE;
f1d16a3958 2019-09-17 1034:
0e05b2a8c7 2019-09-16 1035: return(NULL);
0e05b2a8c7 2019-09-16 1036: }
f1d16a3958 2019-09-17 1037:
f1d16a3958 2019-09-17 1038: rootLen = strlen(XVFS_ROOT_MOUNTPOINT);
f1d16a3958 2019-09-17 1039: pathStr = Tcl_GetString(path);
0e05b2a8c7 2019-09-16 1040:
0e05b2a8c7 2019-09-16 1041: fsName = ((char *) pathStr) + rootLen;
0e05b2a8c7 2019-09-16 1042:
0e05b2a8c7 2019-09-16 1043: fsNameEnds = strchr(fsName, '/');
0e05b2a8c7 2019-09-16 1044: if (fsNameEnds) {
0e05b2a8c7 2019-09-16 1045: origSep = *fsNameEnds;
0e05b2a8c7 2019-09-16 1046: *fsNameEnds = '\0';
0e05b2a8c7 2019-09-16 1047: }
0e05b2a8c7 2019-09-16 1048:
0e05b2a8c7 2019-09-16 1049: XVFS_DEBUG_PRINTF("... fsName = %s...", fsName);
0e05b2a8c7 2019-09-16 1050:
0e05b2a8c7 2019-09-16 1051: mapEntry = Tcl_FindHashEntry(&xvfs_tclfs_dispatch_map, fsName);
0e05b2a8c7 2019-09-16 1052:
0e05b2a8c7 2019-09-16 1053: if (fsNameEnds) {
0e05b2a8c7 2019-09-16 1054: *fsNameEnds = origSep;
0e05b2a8c7 2019-09-16 1055: }
0e05b2a8c7 2019-09-16 1056:
0e05b2a8c7 2019-09-16 1057: if (mapEntry) {
0e05b2a8c7 2019-09-16 1058: retval = (struct xvfs_tclfs_instance_info *) Tcl_GetHashValue(mapEntry);
0e05b2a8c7 2019-09-16 1059: XVFS_DEBUG_PRINTF("... found a registered filesystem: %p", retval);
0e05b2a8c7 2019-09-16 1060: } else {
0e05b2a8c7 2019-09-16 1061: retval = NULL;
0e05b2a8c7 2019-09-16 1062: XVFS_DEBUG_PUTS("... found no registered filesystem.");
0e05b2a8c7 2019-09-16 1063: }
0e05b2a8c7 2019-09-16 1064:
0e05b2a8c7 2019-09-16 1065: XVFS_DEBUG_LEAVE;
0e05b2a8c7 2019-09-16 1066: return(retval);
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;
f1d16a3958 2019-09-17 1118:
a101629cc6 2019-09-17 1119: #ifdef USE_TCL_STUBS
a101629cc6 2019-09-17 1120: const char *tclInitStubs_ret;
a101629cc6 2019-09-17 1121: /* Initialize Stubs */
a101629cc6 2019-09-17 1122: tclInitStubs_ret = Tcl_InitStubs(interp, TCL_PATCH_LEVEL, 0);
a101629cc6 2019-09-17 1123: if (!tclInitStubs_ret) {
a101629cc6 2019-09-17 1124: return(TCL_ERROR);
a101629cc6 2019-09-17 1125: }
a101629cc6 2019-09-17 1126: #endif
a101629cc6 2019-09-17 1127:
0e05b2a8c7 2019-09-16 1128: /* XXX:TODO: Make this thread-safe */
0e05b2a8c7 2019-09-16 1129: if (registered) {
0e05b2a8c7 2019-09-16 1130: return(TCL_OK);
0e05b2a8c7 2019-09-16 1131: }
0e05b2a8c7 2019-09-16 1132: registered = 1;
0e05b2a8c7 2019-09-16 1133:
0e05b2a8c7 2019-09-16 1134: xvfs_tclfs_dispatch_fs.typeName = "xvfsDispatch";
0e05b2a8c7 2019-09-16 1135: xvfs_tclfs_dispatch_fs.structureLength = sizeof(xvfs_tclfs_dispatch_fs);
0e05b2a8c7 2019-09-16 1136: xvfs_tclfs_dispatch_fs.version = TCL_FILESYSTEM_VERSION_1;
a101629cc6 2019-09-17 1137: xvfs_tclfs_dispatch_fs.pathInFilesystemProc = xvfs_tclfs_dispatch_pathInFS;
0e05b2a8c7 2019-09-16 1138: xvfs_tclfs_dispatch_fs.dupInternalRepProc = NULL;
0e05b2a8c7 2019-09-16 1139: xvfs_tclfs_dispatch_fs.freeInternalRepProc = NULL;
0e05b2a8c7 2019-09-16 1140: xvfs_tclfs_dispatch_fs.internalToNormalizedProc = NULL;
0e05b2a8c7 2019-09-16 1141: xvfs_tclfs_dispatch_fs.createInternalRepProc = NULL;
0e05b2a8c7 2019-09-16 1142: xvfs_tclfs_dispatch_fs.normalizePathProc = NULL;
0e05b2a8c7 2019-09-16 1143: xvfs_tclfs_dispatch_fs.filesystemPathTypeProc = NULL;
0e05b2a8c7 2019-09-16 1144: xvfs_tclfs_dispatch_fs.filesystemSeparatorProc = NULL;
0e05b2a8c7 2019-09-16 1145: xvfs_tclfs_dispatch_fs.statProc = xvfs_tclfs_dispatch_stat;
0e05b2a8c7 2019-09-16 1146: xvfs_tclfs_dispatch_fs.accessProc = xvfs_tclfs_dispatch_access;
0e05b2a8c7 2019-09-16 1147: xvfs_tclfs_dispatch_fs.openFileChannelProc = xvfs_tclfs_dispatch_openFileChannel;
0e05b2a8c7 2019-09-16 1148: xvfs_tclfs_dispatch_fs.matchInDirectoryProc = xvfs_tclfs_dispatch_matchInDir;
0e05b2a8c7 2019-09-16 1149: xvfs_tclfs_dispatch_fs.utimeProc = NULL;
0e05b2a8c7 2019-09-16 1150: xvfs_tclfs_dispatch_fs.linkProc = NULL;
0e05b2a8c7 2019-09-16 1151: xvfs_tclfs_dispatch_fs.listVolumesProc = NULL;
0e05b2a8c7 2019-09-16 1152: xvfs_tclfs_dispatch_fs.fileAttrStringsProc = NULL;
0e05b2a8c7 2019-09-16 1153: xvfs_tclfs_dispatch_fs.fileAttrsGetProc = NULL;
0e05b2a8c7 2019-09-16 1154: xvfs_tclfs_dispatch_fs.fileAttrsSetProc = NULL;
0e05b2a8c7 2019-09-16 1155: xvfs_tclfs_dispatch_fs.createDirectoryProc = NULL;
0e05b2a8c7 2019-09-16 1156: xvfs_tclfs_dispatch_fs.removeDirectoryProc = NULL;
0e05b2a8c7 2019-09-16 1157: xvfs_tclfs_dispatch_fs.deleteFileProc = NULL;
0e05b2a8c7 2019-09-16 1158: xvfs_tclfs_dispatch_fs.copyFileProc = NULL;
0e05b2a8c7 2019-09-16 1159: xvfs_tclfs_dispatch_fs.renameFileProc = NULL;
0e05b2a8c7 2019-09-16 1160: xvfs_tclfs_dispatch_fs.copyDirectoryProc = NULL;
0e05b2a8c7 2019-09-16 1161: xvfs_tclfs_dispatch_fs.lstatProc = NULL;
0e05b2a8c7 2019-09-16 1162: xvfs_tclfs_dispatch_fs.loadFileProc = NULL;
0e05b2a8c7 2019-09-16 1163: xvfs_tclfs_dispatch_fs.getCwdProc = NULL;
0e05b2a8c7 2019-09-16 1164: xvfs_tclfs_dispatch_fs.chdirProc = NULL;
0e05b2a8c7 2019-09-16 1165:
f1d16a3958 2019-09-17 1166: memcpy(xvfs_tclfs_dispatch_fsdata.magic, XVFS_INTERNAL_SERVER_MAGIC, XVFS_INTERNAL_SERVER_MAGIC_LEN);
f1d16a3958 2019-09-17 1167: xvfs_tclfs_dispatch_fsdata.registerProc = Xvfs_Register;
f1d16a3958 2019-09-17 1168:
f1d16a3958 2019-09-17 1169: tclRet = Tcl_FSRegister((ClientData) &xvfs_tclfs_dispatch_fsdata, &xvfs_tclfs_dispatch_fs);
0e05b2a8c7 2019-09-16 1170: if (tclRet != TCL_OK) {
0e05b2a8c7 2019-09-16 1171: if (interp) {
0e05b2a8c7 2019-09-16 1172: Tcl_SetResult(interp, "Tcl_FSRegister() failed", NULL);
0e05b2a8c7 2019-09-16 1173: }
0e05b2a8c7 2019-09-16 1174:
0e05b2a8c7 2019-09-16 1175: return(tclRet);
0e05b2a8c7 2019-09-16 1176: }
0e05b2a8c7 2019-09-16 1177:
0e05b2a8c7 2019-09-16 1178: /*
0e05b2a8c7 2019-09-16 1179: * Initialize the channel type we will use for I/O
0e05b2a8c7 2019-09-16 1180: */
0e05b2a8c7 2019-09-16 1181: xvfs_tclfs_prepareChannelType();
0e05b2a8c7 2019-09-16 1182:
0e05b2a8c7 2019-09-16 1183: /*
0e05b2a8c7 2019-09-16 1184: * Initialize the map to lookup paths to registered
0e05b2a8c7 2019-09-16 1185: * filesystems
0e05b2a8c7 2019-09-16 1186: */
0e05b2a8c7 2019-09-16 1187: Tcl_InitHashTable(&xvfs_tclfs_dispatch_map, TCL_STRING_KEYS);
0e05b2a8c7 2019-09-16 1188:
0e05b2a8c7 2019-09-16 1189: return(TCL_OK);
0e05b2a8c7 2019-09-16 1190: }
0e05b2a8c7 2019-09-16 1191:
d92ba3d36d 2019-05-08 1192: int Xvfs_Register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
0e05b2a8c7 2019-09-16 1193: Tcl_HashEntry *mapEntry;
0e05b2a8c7 2019-09-16 1194: struct xvfs_tclfs_instance_info *instanceInfo;
0e05b2a8c7 2019-09-16 1195: int dispatchInitRet;
0e05b2a8c7 2019-09-16 1196: int new;
0e05b2a8c7 2019-09-16 1197:
f1d16a3958 2019-09-17 1198: dispatchInitRet = Xvfs_Init(interp);
0e05b2a8c7 2019-09-16 1199: if (dispatchInitRet != TCL_OK) {
0e05b2a8c7 2019-09-16 1200: return(dispatchInitRet);
0e05b2a8c7 2019-09-16 1201: }
0e05b2a8c7 2019-09-16 1202:
0e05b2a8c7 2019-09-16 1203: /*
0e05b2a8c7 2019-09-16 1204: * Verify this is for a protocol we support
0e05b2a8c7 2019-09-16 1205: */
0e05b2a8c7 2019-09-16 1206: if (fsInfo->protocolVersion != XVFS_PROTOCOL_VERSION) {
0e05b2a8c7 2019-09-16 1207: if (interp) {
0e05b2a8c7 2019-09-16 1208: Tcl_SetResult(interp, "Protocol mismatch", NULL);
0e05b2a8c7 2019-09-16 1209: }
0e05b2a8c7 2019-09-16 1210: return(TCL_ERROR);
0e05b2a8c7 2019-09-16 1211: }
0e05b2a8c7 2019-09-16 1212:
0e05b2a8c7 2019-09-16 1213: /*
0e05b2a8c7 2019-09-16 1214: * Create the structure needed
0e05b2a8c7 2019-09-16 1215: */
0e05b2a8c7 2019-09-16 1216: instanceInfo = (struct xvfs_tclfs_instance_info *) Tcl_Alloc(sizeof(*instanceInfo));
0e05b2a8c7 2019-09-16 1217: instanceInfo->fsInfo = fsInfo;
0e05b2a8c7 2019-09-16 1218: instanceInfo->mountpoint = Tcl_ObjPrintf("%s%s", XVFS_ROOT_MOUNTPOINT, fsInfo->name);
0e05b2a8c7 2019-09-16 1219: Tcl_IncrRefCount(instanceInfo->mountpoint);
0e05b2a8c7 2019-09-16 1220:
0e05b2a8c7 2019-09-16 1221: /*
0e05b2a8c7 2019-09-16 1222: * Register a hash table entry for this name
0e05b2a8c7 2019-09-16 1223: */
0e05b2a8c7 2019-09-16 1224: new = 0;
0e05b2a8c7 2019-09-16 1225: mapEntry = Tcl_CreateHashEntry(&xvfs_tclfs_dispatch_map, fsInfo->name, &new);
0e05b2a8c7 2019-09-16 1226: Tcl_SetHashValue(mapEntry, instanceInfo);
0e05b2a8c7 2019-09-16 1227:
0e05b2a8c7 2019-09-16 1228: return(TCL_OK);
d92ba3d36d 2019-05-08 1229: }
d92ba3d36d 2019-05-08 1230: #endif /* XVFS_MODE_SERVER */
5f2895faba 2019-09-17 1231: #undef XVFS_DEBUG_PRINTF
5f2895faba 2019-09-17 1232: #undef XVFS_DEBUG_PUTS
5f2895faba 2019-09-17 1233: #undef XVFS_DEBUG_ENTER
5f2895faba 2019-09-17 1234: #undef XVFS_DEBUG_LEAVE
5f2895faba 2019-09-17 1235: #undef XVFS_INTERNAL_SERVER_MAGIC
5f2895faba 2019-09-17 1236: #undef XVFS_INTERNAL_SERVER_MAGIC_LEN
5f2895faba 2019-09-17 1237: #undef XVFS_ROOT_MOUNTPOINT