69e476dcd5 2019-05-02 1: #include <xvfs-core.h>
d121970301 2019-05-02 2: #include <string.h>
38bed7cee0 2019-09-13 3: #include <sys/stat.h>
38bed7cee0 2019-09-13 4: #include <errno.h>
38bed7cee0 2019-09-13 5: #include <fcntl.h>
69e476dcd5 2019-05-02 6: #include <tcl.h>
38bed7cee0 2019-09-13 7:
d80c88cee0 2019-09-16 8: #ifdef XVFS_DEBUG
d80c88cee0 2019-09-16 9: #include <stdio.h> /* Needed for XVFS_DEBUG_PRINTF */
d80c88cee0 2019-09-16 10: static int xvfs_debug_depth = 0;
d80c88cee0 2019-09-16 11: #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 12: #define XVFS_DEBUG_PUTS(str) XVFS_DEBUG_PRINTF("%s", str);
d80c88cee0 2019-09-16 13: #define XVFS_DEBUG_ENTER { xvfs_debug_depth++; XVFS_DEBUG_PUTS("Entered"); }
d80c88cee0 2019-09-16 14: #define XVFS_DEBUG_LEAVE { XVFS_DEBUG_PUTS("Returning"); xvfs_debug_depth--; }
d80c88cee0 2019-09-16 15: #else /* XVFS_DEBUG */
d80c88cee0 2019-09-16 16: #define XVFS_DEBUG_PRINTF(fmt, ...) /**/
d80c88cee0 2019-09-16 17: #define XVFS_DEBUG_PUTS(str) /**/
d80c88cee0 2019-09-16 18: #define XVFS_DEBUG_ENTER /**/
d80c88cee0 2019-09-16 19: #define XVFS_DEBUG_LEAVE /**/
d80c88cee0 2019-09-16 20: #endif /* XVFS_DEBUG */
d80c88cee0 2019-09-16 21:
d92ba3d36d 2019-05-08 22: #if defined(XVFS_MODE_FLEXIBLE) || defined(XVFS_MODE_SERVER)
d92ba3d36d 2019-05-08 23: #define XVFS_INTERNAL_SERVER_MAGIC "\xD4\xF3\x05\x96\x25\xCF\xAF\xFE"
d92ba3d36d 2019-05-08 24: #define XVFS_INTERNAL_SERVER_MAGIC_LEN 8
d92ba3d36d 2019-05-08 25:
d92ba3d36d 2019-05-08 26: struct xvfs_tclfs_server_info {
b586d5b0a1 2019-05-08 27: char magic[XVFS_INTERNAL_SERVER_MAGIC_LEN];
d92ba3d36d 2019-05-08 28: int (*registerProc)(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo);
d92ba3d36d 2019-05-08 29: };
d92ba3d36d 2019-05-08 30: #endif /* XVFS_MODE_FLEXIBLE || XVFS_MODE_SERVER */
3e44e1def1 2019-05-02 31:
3e44e1def1 2019-05-02 32: #if defined(XVFS_MODE_SERVER) || defined(XVFS_MODE_STANDALONE) || defined(XVFS_MODE_FLEXIBLE)
d121970301 2019-05-02 33: #define XVFS_ROOT_MOUNTPOINT "//xvfs:/"
d121970301 2019-05-02 34:
d121970301 2019-05-02 35: struct xvfs_tclfs_instance_info {
d121970301 2019-05-02 36: struct Xvfs_FSInfo *fsInfo;
d121970301 2019-05-02 37: Tcl_Obj *mountpoint;
d121970301 2019-05-02 38: };
d121970301 2019-05-02 39:
d121970301 2019-05-02 40: /*
d121970301 2019-05-02 41: * Internal Core Utilities
d121970301 2019-05-02 42: */
d80c88cee0 2019-09-16 43: static Tcl_Obj *xvfs_absolutePath(Tcl_Obj *path) {
5ae034e55e 2019-09-14 44: Tcl_Obj *currentDirectory;
d80c88cee0 2019-09-16 45: const char *pathStr;
5ae034e55e 2019-09-14 46:
d80c88cee0 2019-09-16 47: XVFS_DEBUG_ENTER;
d80c88cee0 2019-09-16 48:
d80c88cee0 2019-09-16 49: pathStr = Tcl_GetString(path);
d80c88cee0 2019-09-16 50:
5ae034e55e 2019-09-14 51: if (pathStr[0] != '/') {
5ae034e55e 2019-09-14 52: currentDirectory = Tcl_FSGetCwd(NULL);
5ae034e55e 2019-09-14 53: Tcl_IncrRefCount(currentDirectory);
5ae034e55e 2019-09-14 54:
5ae034e55e 2019-09-14 55: path = Tcl_ObjPrintf("%s/%s", Tcl_GetString(currentDirectory), pathStr);
5ae034e55e 2019-09-14 56: Tcl_IncrRefCount(path);
5ae034e55e 2019-09-14 57: Tcl_DecrRefCount(currentDirectory);
d80c88cee0 2019-09-16 58: } else {
d80c88cee0 2019-09-16 59: Tcl_IncrRefCount(path);
5ae034e55e 2019-09-14 60: }
d80c88cee0 2019-09-16 61:
d80c88cee0 2019-09-16 62: XVFS_DEBUG_PRINTF("Converted path \"%s\" to absolute path: \"%s\"", pathStr, Tcl_GetString(path));
d80c88cee0 2019-09-16 63:
d80c88cee0 2019-09-16 64: XVFS_DEBUG_LEAVE;
d80c88cee0 2019-09-16 65: return(path);
d80c88cee0 2019-09-16 66: }
d80c88cee0 2019-09-16 67:
d80c88cee0 2019-09-16 68: static const char *xvfs_relativePath(Tcl_Obj *path, struct xvfs_tclfs_instance_info *info) {
d80c88cee0 2019-09-16 69: const char *pathStr, *rootStr;
d80c88cee0 2019-09-16 70: const char *pathFinal;
d80c88cee0 2019-09-16 71: int pathLen, rootLen;
d80c88cee0 2019-09-16 72:
d80c88cee0 2019-09-16 73: XVFS_DEBUG_ENTER;
d80c88cee0 2019-09-16 74:
d80c88cee0 2019-09-16 75: rootStr = Tcl_GetStringFromObj(info->mountpoint, &rootLen);
d80c88cee0 2019-09-16 76: pathStr = Tcl_GetStringFromObj(path, &pathLen);
d80c88cee0 2019-09-16 77:
d80c88cee0 2019-09-16 78: XVFS_DEBUG_PRINTF("Finding relative path of \"%s\" from \"%s\" ...", pathStr, rootStr);
38bed7cee0 2019-09-13 79:
d121970301 2019-05-02 80: if (pathLen < rootLen) {
d80c88cee0 2019-09-16 81: XVFS_DEBUG_PUTS("... none possible (length)");
d80c88cee0 2019-09-16 82:
d80c88cee0 2019-09-16 83: XVFS_DEBUG_LEAVE;
d121970301 2019-05-02 84: return(NULL);
d121970301 2019-05-02 85: }
38bed7cee0 2019-09-13 86:
d121970301 2019-05-02 87: if (memcmp(pathStr, rootStr, rootLen) != 0) {
d80c88cee0 2019-09-16 88: XVFS_DEBUG_PUTS("... none possible (prefix differs)");
d80c88cee0 2019-09-16 89:
d80c88cee0 2019-09-16 90: XVFS_DEBUG_LEAVE;
d121970301 2019-05-02 91: return(NULL);
d121970301 2019-05-02 92: }
38bed7cee0 2019-09-13 93:
d121970301 2019-05-02 94: if (pathLen == rootLen) {
d80c88cee0 2019-09-16 95: XVFS_DEBUG_PUTS("... short circuit: \"\"");
d80c88cee0 2019-09-16 96:
d80c88cee0 2019-09-16 97: XVFS_DEBUG_LEAVE;
d121970301 2019-05-02 98: return("");
d121970301 2019-05-02 99: }
d121970301 2019-05-02 100:
d121970301 2019-05-02 101: /* XXX:TODO: Should this use the native OS path separator ? */
d121970301 2019-05-02 102: if (pathStr[rootLen] != '/') {
d80c88cee0 2019-09-16 103: XVFS_DEBUG_PUTS("... none possible (no seperator)");
d80c88cee0 2019-09-16 104:
d80c88cee0 2019-09-16 105: XVFS_DEBUG_LEAVE;
149aa89b7d 2019-09-13 106: return(NULL);
149aa89b7d 2019-09-13 107: }
38bed7cee0 2019-09-13 108:
5583d77f1c 2019-09-14 109: pathFinal = pathStr + rootLen + 1;
5583d77f1c 2019-09-14 110: pathLen -= rootLen + 1;
5583d77f1c 2019-09-14 111:
5583d77f1c 2019-09-14 112: if (pathLen == 1 && memcmp(pathFinal, ".", 1) == 0) {
d80c88cee0 2019-09-16 113: XVFS_DEBUG_PUTS("... short circuit: \".\" -> \"\"");
d80c88cee0 2019-09-16 114:
d80c88cee0 2019-09-16 115: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 116: return("");
5583d77f1c 2019-09-14 117: }
5583d77f1c 2019-09-14 118:
5583d77f1c 2019-09-14 119: while (pathLen >= 2 && memcmp(pathFinal, "./", 2) == 0) {
5583d77f1c 2019-09-14 120: pathFinal += 2;
5583d77f1c 2019-09-14 121: pathLen -= 2;
5583d77f1c 2019-09-14 122: }
5583d77f1c 2019-09-14 123:
d80c88cee0 2019-09-16 124: XVFS_DEBUG_PRINTF("... relative path: \"%s\"", pathFinal);
d80c88cee0 2019-09-16 125:
d80c88cee0 2019-09-16 126: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 127: return(pathFinal);
5583d77f1c 2019-09-14 128: }
38bed7cee0 2019-09-13 129:
38bed7cee0 2019-09-13 130: static int xvfs_errorToErrno(int xvfs_error) {
38bed7cee0 2019-09-13 131: if (xvfs_error >= 0) {
38bed7cee0 2019-09-13 132: return(0);
38bed7cee0 2019-09-13 133: }
38bed7cee0 2019-09-13 134:
38bed7cee0 2019-09-13 135: switch (xvfs_error) {
38bed7cee0 2019-09-13 136: case XVFS_RV_ERR_ENOENT:
38bed7cee0 2019-09-13 137: return(ENOENT);
38bed7cee0 2019-09-13 138: case XVFS_RV_ERR_EINVAL:
38bed7cee0 2019-09-13 139: return(EINVAL);
38bed7cee0 2019-09-13 140: case XVFS_RV_ERR_EISDIR:
38bed7cee0 2019-09-13 141: return(EISDIR);
38bed7cee0 2019-09-13 142: case XVFS_RV_ERR_ENOTDIR:
38bed7cee0 2019-09-13 143: return(ENOTDIR);
38bed7cee0 2019-09-13 144: case XVFS_RV_ERR_EFAULT:
38bed7cee0 2019-09-13 145: return(EFAULT);
10f67b2ced 2019-09-16 146: case XVFS_RV_ERR_EROFS:
10f67b2ced 2019-09-16 147: return(EROFS);
5583d77f1c 2019-09-14 148: case XVFS_RV_ERR_INTERNAL:
5583d77f1c 2019-09-14 149: return(EINVAL);
38bed7cee0 2019-09-13 150: default:
38bed7cee0 2019-09-13 151: return(ERANGE);
10f67b2ced 2019-09-16 152: }
10f67b2ced 2019-09-16 153: }
10f67b2ced 2019-09-16 154:
10f67b2ced 2019-09-16 155: static const char *xvfs_perror(int xvfs_error) {
10f67b2ced 2019-09-16 156: if (xvfs_error >= 0) {
10f67b2ced 2019-09-16 157: return("Not an error");
10f67b2ced 2019-09-16 158: }
10f67b2ced 2019-09-16 159:
10f67b2ced 2019-09-16 160: switch (xvfs_error) {
10f67b2ced 2019-09-16 161: case XVFS_RV_ERR_ENOENT:
10f67b2ced 2019-09-16 162: case XVFS_RV_ERR_EINVAL:
10f67b2ced 2019-09-16 163: case XVFS_RV_ERR_EISDIR:
10f67b2ced 2019-09-16 164: case XVFS_RV_ERR_ENOTDIR:
10f67b2ced 2019-09-16 165: case XVFS_RV_ERR_EFAULT:
10f67b2ced 2019-09-16 166: case XVFS_RV_ERR_EROFS:
10f67b2ced 2019-09-16 167: return(Tcl_ErrnoMsg(xvfs_errorToErrno(xvfs_error)));
10f67b2ced 2019-09-16 168: case XVFS_RV_ERR_INTERNAL:
10f67b2ced 2019-09-16 169: return("Internal error");
10f67b2ced 2019-09-16 170: default:
10f67b2ced 2019-09-16 171: return("Unknown error");
149aa89b7d 2019-09-13 172: }
38bed7cee0 2019-09-13 173: }
38bed7cee0 2019-09-13 174:
38bed7cee0 2019-09-13 175: /*
38bed7cee0 2019-09-13 176: * Xvfs Memory Channel
38bed7cee0 2019-09-13 177: */
38bed7cee0 2019-09-13 178: struct xvfs_tclfs_channel_id {
38bed7cee0 2019-09-13 179: Tcl_Channel channel;
38bed7cee0 2019-09-13 180: struct xvfs_tclfs_instance_info *fsInstanceInfo;
38bed7cee0 2019-09-13 181: Tcl_Obj *path;
38bed7cee0 2019-09-13 182: Tcl_WideInt currentOffset;
38bed7cee0 2019-09-13 183: Tcl_WideInt fileSize;
7d74392642 2019-09-13 184: int eofMarked;
aa08a4a749 2019-09-14 185: int queuedEvents;
aa08a4a749 2019-09-14 186: int closed;
7d74392642 2019-09-13 187: };
7d74392642 2019-09-13 188: struct xvfs_tclfs_channel_event {
7d74392642 2019-09-13 189: Tcl_Event tcl;
7d74392642 2019-09-13 190: struct xvfs_tclfs_channel_id *channelInstanceData;
38bed7cee0 2019-09-13 191: };
38bed7cee0 2019-09-13 192: static Tcl_ChannelType xvfs_tclfs_channelType;
38bed7cee0 2019-09-13 193:
38bed7cee0 2019-09-13 194: static Tcl_Channel xvfs_tclfs_openChannel(Tcl_Obj *path, struct xvfs_tclfs_instance_info *instanceInfo) {
38bed7cee0 2019-09-13 195: struct xvfs_tclfs_channel_id *channelInstanceData;
38bed7cee0 2019-09-13 196: Tcl_Channel channel;
38bed7cee0 2019-09-13 197: Tcl_StatBuf fileInfo;
7d74392642 2019-09-13 198: Tcl_Obj *channelName;
38bed7cee0 2019-09-13 199: int statRet;
7d74392642 2019-09-13 200:
d80c88cee0 2019-09-16 201: XVFS_DEBUG_ENTER;
d80c88cee0 2019-09-16 202: XVFS_DEBUG_PRINTF("Opening file \"%s\" ...", Tcl_GetString(path));
d80c88cee0 2019-09-16 203:
38bed7cee0 2019-09-13 204: statRet = instanceInfo->fsInfo->getStatProc(Tcl_GetString(path), &fileInfo);
38bed7cee0 2019-09-13 205: if (statRet < 0) {
d80c88cee0 2019-09-16 206: XVFS_DEBUG_PRINTF("... failed: %s", xvfs_perror(statRet));
d80c88cee0 2019-09-16 207:
d80c88cee0 2019-09-16 208: XVFS_DEBUG_LEAVE;
38bed7cee0 2019-09-13 209: return(NULL);
38bed7cee0 2019-09-13 210: }
38bed7cee0 2019-09-13 211:
38bed7cee0 2019-09-13 212: channelInstanceData = (struct xvfs_tclfs_channel_id *) Tcl_Alloc(sizeof(*channelInstanceData));
38bed7cee0 2019-09-13 213: channelInstanceData->currentOffset = 0;
7d74392642 2019-09-13 214: channelInstanceData->eofMarked = 0;
aa08a4a749 2019-09-14 215: channelInstanceData->queuedEvents = 0;
aa08a4a749 2019-09-14 216: channelInstanceData->closed = 0;
38bed7cee0 2019-09-13 217: channelInstanceData->channel = NULL;
38bed7cee0 2019-09-13 218:
7d74392642 2019-09-13 219: channelName = Tcl_ObjPrintf("xvfs0x%llx", (unsigned long long) channelInstanceData);
7d74392642 2019-09-13 220: if (!channelName) {
d80c88cee0 2019-09-16 221: XVFS_DEBUG_PUTS("... failed");
d80c88cee0 2019-09-16 222:
7d74392642 2019-09-13 223: Tcl_Free((char *) channelInstanceData);
7d74392642 2019-09-13 224:
d80c88cee0 2019-09-16 225: XVFS_DEBUG_LEAVE;
7d74392642 2019-09-13 226: return(NULL);
7d74392642 2019-09-13 227: }
7d74392642 2019-09-13 228: Tcl_IncrRefCount(channelName);
7d74392642 2019-09-13 229:
38bed7cee0 2019-09-13 230: channelInstanceData->fsInstanceInfo = instanceInfo;
38bed7cee0 2019-09-13 231: channelInstanceData->fileSize = fileInfo.st_size;
7d74392642 2019-09-13 232: channelInstanceData->path = path;
38bed7cee0 2019-09-13 233: Tcl_IncrRefCount(path);
38bed7cee0 2019-09-13 234:
7d74392642 2019-09-13 235: channel = Tcl_CreateChannel(&xvfs_tclfs_channelType, Tcl_GetString(channelName), channelInstanceData, TCL_READABLE);
7d74392642 2019-09-13 236: Tcl_DecrRefCount(channelName);
38bed7cee0 2019-09-13 237: if (!channel) {
d80c88cee0 2019-09-16 238: XVFS_DEBUG_PUTS("... failed");
d80c88cee0 2019-09-16 239:
38bed7cee0 2019-09-13 240: Tcl_DecrRefCount(path);
38bed7cee0 2019-09-13 241: Tcl_Free((char *) channelInstanceData);
38bed7cee0 2019-09-13 242:
d80c88cee0 2019-09-16 243: XVFS_DEBUG_LEAVE;
38bed7cee0 2019-09-13 244: return(NULL);
38bed7cee0 2019-09-13 245: }
38bed7cee0 2019-09-13 246:
38bed7cee0 2019-09-13 247: channelInstanceData->channel = channel;
38bed7cee0 2019-09-13 248:
d80c88cee0 2019-09-16 249: XVFS_DEBUG_PRINTF("... ok (%p)", channelInstanceData);
d80c88cee0 2019-09-16 250:
d80c88cee0 2019-09-16 251: XVFS_DEBUG_LEAVE;
38bed7cee0 2019-09-13 252: return(channel);
aa08a4a749 2019-09-14 253: }
aa08a4a749 2019-09-14 254:
aa08a4a749 2019-09-14 255: static int xvfs_tclfs_closeChannel(ClientData channelInstanceData_p, Tcl_Interp *interp);
aa08a4a749 2019-09-14 256: static int xvfs_tclfs_closeChannelEvent(Tcl_Event *event_p, int flags) {
aa08a4a749 2019-09-14 257: struct xvfs_tclfs_channel_id *channelInstanceData;
aa08a4a749 2019-09-14 258: struct xvfs_tclfs_channel_event *event;
aa08a4a749 2019-09-14 259:
aa08a4a749 2019-09-14 260: event = (struct xvfs_tclfs_channel_event *) event_p;
aa08a4a749 2019-09-14 261: channelInstanceData = event->channelInstanceData;
aa08a4a749 2019-09-14 262:
aa08a4a749 2019-09-14 263: channelInstanceData->queuedEvents--;
aa08a4a749 2019-09-14 264:
aa08a4a749 2019-09-14 265: xvfs_tclfs_closeChannel((ClientData) channelInstanceData, NULL);
aa08a4a749 2019-09-14 266:
aa08a4a749 2019-09-14 267: return(1);
38bed7cee0 2019-09-13 268: }
38bed7cee0 2019-09-13 269:
38bed7cee0 2019-09-13 270: static int xvfs_tclfs_closeChannel(ClientData channelInstanceData_p, Tcl_Interp *interp) {
38bed7cee0 2019-09-13 271: struct xvfs_tclfs_channel_id *channelInstanceData;
aa08a4a749 2019-09-14 272: struct xvfs_tclfs_channel_event *event;
aa08a4a749 2019-09-14 273:
d80c88cee0 2019-09-16 274: XVFS_DEBUG_ENTER;
d80c88cee0 2019-09-16 275: XVFS_DEBUG_PRINTF("Closing channel %p ...", channelInstanceData_p);
d80c88cee0 2019-09-16 276:
38bed7cee0 2019-09-13 277: channelInstanceData = (struct xvfs_tclfs_channel_id *) channelInstanceData_p;
aa08a4a749 2019-09-14 278:
aa08a4a749 2019-09-14 279: channelInstanceData->closed = 1;
aa08a4a749 2019-09-14 280:
aa08a4a749 2019-09-14 281: if (channelInstanceData->queuedEvents != 0) {
d80c88cee0 2019-09-16 282: XVFS_DEBUG_PUTS("... queued");
d80c88cee0 2019-09-16 283:
aa08a4a749 2019-09-14 284: event = (struct xvfs_tclfs_channel_event *) Tcl_Alloc(sizeof(*event));
aa08a4a749 2019-09-14 285: event->tcl.proc = xvfs_tclfs_closeChannelEvent;
aa08a4a749 2019-09-14 286: event->tcl.nextPtr = NULL;
aa08a4a749 2019-09-14 287: event->channelInstanceData = channelInstanceData;
aa08a4a749 2019-09-14 288:
aa08a4a749 2019-09-14 289: channelInstanceData->queuedEvents++;
aa08a4a749 2019-09-14 290:
aa08a4a749 2019-09-14 291: Tcl_QueueEvent((Tcl_Event *) event, TCL_QUEUE_TAIL);
aa08a4a749 2019-09-14 292:
d80c88cee0 2019-09-16 293: XVFS_DEBUG_LEAVE;
aa08a4a749 2019-09-14 294: return(0);
aa08a4a749 2019-09-14 295: }
38bed7cee0 2019-09-13 296:
38bed7cee0 2019-09-13 297: Tcl_DecrRefCount(channelInstanceData->path);
38bed7cee0 2019-09-13 298: Tcl_Free((char *) channelInstanceData);
38bed7cee0 2019-09-13 299:
d80c88cee0 2019-09-16 300: XVFS_DEBUG_PUTS("... ok");
d80c88cee0 2019-09-16 301:
d80c88cee0 2019-09-16 302: XVFS_DEBUG_LEAVE;
38bed7cee0 2019-09-13 303: return(0);
38bed7cee0 2019-09-13 304: }
38bed7cee0 2019-09-13 305:
38bed7cee0 2019-09-13 306: static int xvfs_tclfs_readChannel(ClientData channelInstanceData_p, char *buf, int bufSize, int *errorCodePtr) {
38bed7cee0 2019-09-13 307: struct xvfs_tclfs_channel_id *channelInstanceData;
38bed7cee0 2019-09-13 308: const unsigned char *data;
38bed7cee0 2019-09-13 309: Tcl_WideInt offset, length;
38bed7cee0 2019-09-13 310: char *path;
38bed7cee0 2019-09-13 311:
38bed7cee0 2019-09-13 312: channelInstanceData = (struct xvfs_tclfs_channel_id *) channelInstanceData_p;
7d74392642 2019-09-13 313:
7d74392642 2019-09-13 314: /*
7d74392642 2019-09-13 315: * If we are already at the end of the file we can skip
7d74392642 2019-09-13 316: * attempting to read it
7d74392642 2019-09-13 317: */
7d74392642 2019-09-13 318: if (channelInstanceData->eofMarked) {
7d74392642 2019-09-13 319: return(0);
7d74392642 2019-09-13 320: }
38bed7cee0 2019-09-13 321:
38bed7cee0 2019-09-13 322: path = Tcl_GetString(channelInstanceData->path);
38bed7cee0 2019-09-13 323: offset = channelInstanceData->currentOffset;
38bed7cee0 2019-09-13 324: length = bufSize;
38bed7cee0 2019-09-13 325:
38bed7cee0 2019-09-13 326: data = channelInstanceData->fsInstanceInfo->fsInfo->getDataProc(path, offset, &length);
38bed7cee0 2019-09-13 327:
38bed7cee0 2019-09-13 328: if (length < 0) {
38bed7cee0 2019-09-13 329: *errorCodePtr = xvfs_errorToErrno(length);
38bed7cee0 2019-09-13 330:
38bed7cee0 2019-09-13 331: return(-1);
38bed7cee0 2019-09-13 332: }
38bed7cee0 2019-09-13 333:
7d74392642 2019-09-13 334: if (length == 0) {
7d74392642 2019-09-13 335: channelInstanceData->eofMarked = 1;
7d74392642 2019-09-13 336: } else {
7d74392642 2019-09-13 337: memcpy(buf, data, length);
38bed7cee0 2019-09-13 338:
7d74392642 2019-09-13 339: channelInstanceData->currentOffset += length;
7d74392642 2019-09-13 340: }
38bed7cee0 2019-09-13 341:
38bed7cee0 2019-09-13 342: return(length);
38bed7cee0 2019-09-13 343: }
38bed7cee0 2019-09-13 344:
7d74392642 2019-09-13 345: static int xvfs_tclfs_watchChannelEvent(Tcl_Event *event_p, int flags) {
7d74392642 2019-09-13 346: struct xvfs_tclfs_channel_id *channelInstanceData;
7d74392642 2019-09-13 347: struct xvfs_tclfs_channel_event *event;
7d74392642 2019-09-13 348:
7d74392642 2019-09-13 349: event = (struct xvfs_tclfs_channel_event *) event_p;
7d74392642 2019-09-13 350: channelInstanceData = event->channelInstanceData;
7d74392642 2019-09-13 351:
aa08a4a749 2019-09-14 352: channelInstanceData->queuedEvents--;
aa08a4a749 2019-09-14 353:
aa08a4a749 2019-09-14 354: if (channelInstanceData->closed) {
aa08a4a749 2019-09-14 355: return(1);
aa08a4a749 2019-09-14 356: }
aa08a4a749 2019-09-14 357:
7d74392642 2019-09-13 358: Tcl_NotifyChannel(channelInstanceData->channel, TCL_READABLE);
7d74392642 2019-09-13 359:
aa08a4a749 2019-09-14 360: return(1);
7d74392642 2019-09-13 361: }
7d74392642 2019-09-13 362:
38bed7cee0 2019-09-13 363: static void xvfs_tclfs_watchChannel(ClientData channelInstanceData_p, int mask) {
7d74392642 2019-09-13 364: struct xvfs_tclfs_channel_id *channelInstanceData;
7d74392642 2019-09-13 365: struct xvfs_tclfs_channel_event *event;
7d74392642 2019-09-13 366:
38bed7cee0 2019-09-13 367: if ((mask & TCL_READABLE) != TCL_READABLE) {
38bed7cee0 2019-09-13 368: return;
38bed7cee0 2019-09-13 369: }
38bed7cee0 2019-09-13 370:
7d74392642 2019-09-13 371: channelInstanceData = (struct xvfs_tclfs_channel_id *) channelInstanceData_p;
7d74392642 2019-09-13 372:
7d74392642 2019-09-13 373: /*
7d74392642 2019-09-13 374: * If the read call has marked that we have reached EOF,
7d74392642 2019-09-13 375: * do not signal any further
7d74392642 2019-09-13 376: */
7d74392642 2019-09-13 377: if (channelInstanceData->eofMarked) {
7d74392642 2019-09-13 378: return;
7d74392642 2019-09-13 379: }
7d74392642 2019-09-13 380:
7d74392642 2019-09-13 381: event = (struct xvfs_tclfs_channel_event *) Tcl_Alloc(sizeof(*event));
7d74392642 2019-09-13 382: event->tcl.proc = xvfs_tclfs_watchChannelEvent;
7d74392642 2019-09-13 383: event->tcl.nextPtr = NULL;
7d74392642 2019-09-13 384: event->channelInstanceData = channelInstanceData;
aa08a4a749 2019-09-14 385:
aa08a4a749 2019-09-14 386: channelInstanceData->queuedEvents++;
7d74392642 2019-09-13 387:
7d74392642 2019-09-13 388: Tcl_QueueEvent((Tcl_Event *) event, TCL_QUEUE_TAIL);
7d74392642 2019-09-13 389:
38bed7cee0 2019-09-13 390: return;
38bed7cee0 2019-09-13 391: }
38bed7cee0 2019-09-13 392:
38bed7cee0 2019-09-13 393: static int xvfs_tclfs_seekChannel(ClientData channelInstanceData_p, long offset, int mode, int *errorCodePtr) {
38bed7cee0 2019-09-13 394: struct xvfs_tclfs_channel_id *channelInstanceData;
38bed7cee0 2019-09-13 395: Tcl_WideInt newOffset, fileSize;
38bed7cee0 2019-09-13 396:
38bed7cee0 2019-09-13 397: channelInstanceData = (struct xvfs_tclfs_channel_id *) channelInstanceData_p;
38bed7cee0 2019-09-13 398:
38bed7cee0 2019-09-13 399: newOffset = channelInstanceData->currentOffset;
38bed7cee0 2019-09-13 400: fileSize = channelInstanceData->fileSize;
38bed7cee0 2019-09-13 401:
38bed7cee0 2019-09-13 402: switch (mode) {
38bed7cee0 2019-09-13 403: case SEEK_CUR:
38bed7cee0 2019-09-13 404: newOffset += offset;
38bed7cee0 2019-09-13 405: break;
38bed7cee0 2019-09-13 406: case SEEK_SET:
38bed7cee0 2019-09-13 407: newOffset = offset;
38bed7cee0 2019-09-13 408: break;
38bed7cee0 2019-09-13 409: case SEEK_END:
38bed7cee0 2019-09-13 410: newOffset = fileSize + offset;
38bed7cee0 2019-09-13 411: break;
38bed7cee0 2019-09-13 412: default:
38bed7cee0 2019-09-13 413: *errorCodePtr = xvfs_errorToErrno(XVFS_RV_ERR_EINVAL);
38bed7cee0 2019-09-13 414:
38bed7cee0 2019-09-13 415: return(-1);
38bed7cee0 2019-09-13 416: }
38bed7cee0 2019-09-13 417:
38bed7cee0 2019-09-13 418: /*
38bed7cee0 2019-09-13 419: * We allow users to seek right up to the end of the buffer, but
38bed7cee0 2019-09-13 420: * no further, this way if they want to seek backwards from there
38bed7cee0 2019-09-13 421: * it is possible to do so.
38bed7cee0 2019-09-13 422: */
38bed7cee0 2019-09-13 423: if (newOffset < 0 || newOffset > fileSize) {
38bed7cee0 2019-09-13 424: *errorCodePtr = xvfs_errorToErrno(XVFS_RV_ERR_EINVAL);
38bed7cee0 2019-09-13 425:
38bed7cee0 2019-09-13 426: return(-1);
38bed7cee0 2019-09-13 427: }
38bed7cee0 2019-09-13 428:
7d74392642 2019-09-13 429: if (newOffset != channelInstanceData->currentOffset) {
7d74392642 2019-09-13 430: channelInstanceData->eofMarked = 0;
7d74392642 2019-09-13 431: channelInstanceData->currentOffset = newOffset;
7d74392642 2019-09-13 432: }
38bed7cee0 2019-09-13 433:
38bed7cee0 2019-09-13 434: return(channelInstanceData->currentOffset);
38bed7cee0 2019-09-13 435: }
38bed7cee0 2019-09-13 436:
38bed7cee0 2019-09-13 437: static void xvfs_tclfs_prepareChannelType(void) {
38bed7cee0 2019-09-13 438: xvfs_tclfs_channelType.typeName = "xvfs";
38bed7cee0 2019-09-13 439: xvfs_tclfs_channelType.version = TCL_CHANNEL_VERSION_2;
38bed7cee0 2019-09-13 440: xvfs_tclfs_channelType.closeProc = xvfs_tclfs_closeChannel;
38bed7cee0 2019-09-13 441: xvfs_tclfs_channelType.inputProc = xvfs_tclfs_readChannel;
38bed7cee0 2019-09-13 442: xvfs_tclfs_channelType.outputProc = NULL;
38bed7cee0 2019-09-13 443: xvfs_tclfs_channelType.watchProc = xvfs_tclfs_watchChannel;
38bed7cee0 2019-09-13 444: xvfs_tclfs_channelType.getHandleProc = NULL;
38bed7cee0 2019-09-13 445: xvfs_tclfs_channelType.seekProc = xvfs_tclfs_seekChannel;
38bed7cee0 2019-09-13 446: xvfs_tclfs_channelType.setOptionProc = NULL;
38bed7cee0 2019-09-13 447: xvfs_tclfs_channelType.getOptionProc = NULL;
38bed7cee0 2019-09-13 448: xvfs_tclfs_channelType.close2Proc = NULL;
38bed7cee0 2019-09-13 449: xvfs_tclfs_channelType.blockModeProc = NULL;
38bed7cee0 2019-09-13 450: xvfs_tclfs_channelType.flushProc = NULL;
38bed7cee0 2019-09-13 451: xvfs_tclfs_channelType.handlerProc = NULL;
38bed7cee0 2019-09-13 452: xvfs_tclfs_channelType.wideSeekProc = NULL;
38bed7cee0 2019-09-13 453: xvfs_tclfs_channelType.threadActionProc = NULL;
38bed7cee0 2019-09-13 454: xvfs_tclfs_channelType.truncateProc = NULL;
d121970301 2019-05-02 455: }
d121970301 2019-05-02 456:
d121970301 2019-05-02 457: /*
d121970301 2019-05-02 458: * Internal Tcl_Filesystem functions, with the appropriate instance info
d121970301 2019-05-02 459: */
d121970301 2019-05-02 460: static int xvfs_tclfs_pathInFilesystem(Tcl_Obj *path, ClientData *dataPtr, struct xvfs_tclfs_instance_info *instanceInfo) {
d121970301 2019-05-02 461: const char *relativePath;
d80c88cee0 2019-09-16 462: int retval;
d80c88cee0 2019-09-16 463:
d80c88cee0 2019-09-16 464: XVFS_DEBUG_ENTER;
d80c88cee0 2019-09-16 465:
d80c88cee0 2019-09-16 466: XVFS_DEBUG_PRINTF("Checking to see if path \"%s\" is in the filesystem ...", Tcl_GetString(path));
d80c88cee0 2019-09-16 467:
d80c88cee0 2019-09-16 468: path = xvfs_absolutePath(path);
38bed7cee0 2019-09-13 469:
d121970301 2019-05-02 470: relativePath = xvfs_relativePath(path, instanceInfo);
d80c88cee0 2019-09-16 471:
d80c88cee0 2019-09-16 472: retval = TCL_OK;
d121970301 2019-05-02 473: if (!relativePath) {
d80c88cee0 2019-09-16 474: retval = -1;
d121970301 2019-05-02 475: }
38bed7cee0 2019-09-13 476:
d80c88cee0 2019-09-16 477: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 478:
d80c88cee0 2019-09-16 479: XVFS_DEBUG_PRINTF("... %s", retval == -1 ? "no" : "yes");
d80c88cee0 2019-09-16 480:
d80c88cee0 2019-09-16 481: XVFS_DEBUG_LEAVE;
d80c88cee0 2019-09-16 482: return(retval);
d121970301 2019-05-02 483: }
d121970301 2019-05-02 484:
d121970301 2019-05-02 485: static int xvfs_tclfs_stat(Tcl_Obj *path, Tcl_StatBuf *statBuf, struct xvfs_tclfs_instance_info *instanceInfo) {
d121970301 2019-05-02 486: const char *pathStr;
d121970301 2019-05-02 487: int retval;
d121970301 2019-05-02 488:
d80c88cee0 2019-09-16 489: XVFS_DEBUG_ENTER;
d80c88cee0 2019-09-16 490:
d80c88cee0 2019-09-16 491: XVFS_DEBUG_PRINTF("Getting stat() on \"%s\" ...", Tcl_GetString(path));
d80c88cee0 2019-09-16 492:
d80c88cee0 2019-09-16 493: path = xvfs_absolutePath(path);
d80c88cee0 2019-09-16 494:
d121970301 2019-05-02 495: pathStr = xvfs_relativePath(path, instanceInfo);
38bed7cee0 2019-09-13 496:
daf25f5222 2019-05-03 497: retval = instanceInfo->fsInfo->getStatProc(pathStr, statBuf);
149aa89b7d 2019-09-13 498: if (retval < 0) {
d80c88cee0 2019-09-16 499: XVFS_DEBUG_PRINTF("... failed: %s", xvfs_perror(retval));
149aa89b7d 2019-09-13 500: retval = -1;
d80c88cee0 2019-09-16 501: } else {
d80c88cee0 2019-09-16 502: XVFS_DEBUG_PUTS("... ok");
149aa89b7d 2019-09-13 503: }
38bed7cee0 2019-09-13 504:
d80c88cee0 2019-09-16 505: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 506:
d80c88cee0 2019-09-16 507: XVFS_DEBUG_LEAVE;
d121970301 2019-05-02 508: return(retval);
d121970301 2019-05-02 509: }
d121970301 2019-05-02 510:
12ff77016f 2019-09-14 511: static int xvfs_tclfs_access(Tcl_Obj *path, int mode, struct xvfs_tclfs_instance_info *instanceInfo) {
12ff77016f 2019-09-14 512: const char *pathStr;
12ff77016f 2019-09-14 513: Tcl_StatBuf fileInfo;
12ff77016f 2019-09-14 514: int statRetVal;
12ff77016f 2019-09-14 515:
d80c88cee0 2019-09-16 516: XVFS_DEBUG_ENTER;
d80c88cee0 2019-09-16 517:
d80c88cee0 2019-09-16 518: XVFS_DEBUG_PRINTF("Getting access(..., %i) on \"%s\" ...", mode, Tcl_GetString(path));
12ff77016f 2019-09-14 519:
12ff77016f 2019-09-14 520: if (mode & W_OK) {
d80c88cee0 2019-09-16 521: XVFS_DEBUG_PUTS("... no (not writable)");
d80c88cee0 2019-09-16 522:
d80c88cee0 2019-09-16 523: XVFS_DEBUG_LEAVE;
d80c88cee0 2019-09-16 524: return(-1);
d80c88cee0 2019-09-16 525: }
d80c88cee0 2019-09-16 526:
d80c88cee0 2019-09-16 527: path = xvfs_absolutePath(path);
d80c88cee0 2019-09-16 528:
d80c88cee0 2019-09-16 529: pathStr = xvfs_relativePath(path, instanceInfo);
d80c88cee0 2019-09-16 530: if (!pathStr) {
d80c88cee0 2019-09-16 531: XVFS_DEBUG_PUTS("... no (not in our path)");
d80c88cee0 2019-09-16 532:
d80c88cee0 2019-09-16 533: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 534:
d80c88cee0 2019-09-16 535: XVFS_DEBUG_LEAVE;
12ff77016f 2019-09-14 536: return(-1);
12ff77016f 2019-09-14 537: }
12ff77016f 2019-09-14 538:
12ff77016f 2019-09-14 539: statRetVal = instanceInfo->fsInfo->getStatProc(pathStr, &fileInfo);
12ff77016f 2019-09-14 540: if (statRetVal < 0) {
d80c88cee0 2019-09-16 541: XVFS_DEBUG_PUTS("... no (not statable)");
d80c88cee0 2019-09-16 542:
d80c88cee0 2019-09-16 543: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 544:
d80c88cee0 2019-09-16 545: XVFS_DEBUG_LEAVE;
12ff77016f 2019-09-14 546: return(-1);
12ff77016f 2019-09-14 547: }
12ff77016f 2019-09-14 548:
12ff77016f 2019-09-14 549: if (mode & X_OK) {
12ff77016f 2019-09-14 550: if (!(fileInfo.st_mode & 040000)) {
d80c88cee0 2019-09-16 551: XVFS_DEBUG_PUTS("... no (not a directory and X_OK specified)");
d80c88cee0 2019-09-16 552:
d80c88cee0 2019-09-16 553: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 554:
d80c88cee0 2019-09-16 555: XVFS_DEBUG_LEAVE;
12ff77016f 2019-09-14 556: return(-1);
12ff77016f 2019-09-14 557: }
12ff77016f 2019-09-14 558: }
12ff77016f 2019-09-14 559:
d80c88cee0 2019-09-16 560: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 561:
d80c88cee0 2019-09-16 562: XVFS_DEBUG_PUTS("... ok");
d80c88cee0 2019-09-16 563:
d80c88cee0 2019-09-16 564: XVFS_DEBUG_LEAVE;
12ff77016f 2019-09-14 565: return(0);
d121970301 2019-05-02 566: }
d121970301 2019-05-02 567:
d121970301 2019-05-02 568: 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 569: Tcl_Channel retval;
d80c88cee0 2019-09-16 570: Tcl_Obj *pathRel;
38bed7cee0 2019-09-13 571: const char *pathStr;
38bed7cee0 2019-09-13 572:
d80c88cee0 2019-09-16 573: XVFS_DEBUG_ENTER;
d80c88cee0 2019-09-16 574:
d80c88cee0 2019-09-16 575: XVFS_DEBUG_PRINTF("Asked to open(\"%s\", %x)...", Tcl_GetString(path), mode);
38bed7cee0 2019-09-13 576:
38bed7cee0 2019-09-13 577: if (mode & O_WRONLY) {
10f67b2ced 2019-09-16 578: XVFS_DEBUG_PUTS("... failed (asked to open for writing)");
10f67b2ced 2019-09-16 579:
10f67b2ced 2019-09-16 580: if (interp) {
10f67b2ced 2019-09-16 581: Tcl_SetErrno(xvfs_errorToErrno(XVFS_RV_ERR_EROFS));
10f67b2ced 2019-09-16 582: Tcl_SetResult(interp, (char *) Tcl_PosixError(interp), NULL);
10f67b2ced 2019-09-16 583: }
d80c88cee0 2019-09-16 584:
d80c88cee0 2019-09-16 585: XVFS_DEBUG_LEAVE;
38bed7cee0 2019-09-13 586: return(NULL);
38bed7cee0 2019-09-13 587: }
38bed7cee0 2019-09-13 588:
d80c88cee0 2019-09-16 589: path = xvfs_absolutePath(path);
d80c88cee0 2019-09-16 590:
d80c88cee0 2019-09-16 591: pathStr = xvfs_relativePath(path, instanceInfo);
d80c88cee0 2019-09-16 592:
d80c88cee0 2019-09-16 593: pathRel = Tcl_NewStringObj(pathStr, -1);
d80c88cee0 2019-09-16 594:
d80c88cee0 2019-09-16 595: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 596:
d80c88cee0 2019-09-16 597: XVFS_DEBUG_PUTS("... done, passing off to channel handler");
d80c88cee0 2019-09-16 598:
d80c88cee0 2019-09-16 599: retval = xvfs_tclfs_openChannel(pathRel, instanceInfo);
d80c88cee0 2019-09-16 600:
d80c88cee0 2019-09-16 601: XVFS_DEBUG_LEAVE;
d80c88cee0 2019-09-16 602: return(retval);
5583d77f1c 2019-09-14 603: }
5583d77f1c 2019-09-14 604:
5583d77f1c 2019-09-14 605: static int xvfs_tclfs_verifyType(Tcl_Obj *path, Tcl_GlobTypeData *types, struct xvfs_tclfs_instance_info *instanceInfo) {
5583d77f1c 2019-09-14 606: const char *pathStr;
5583d77f1c 2019-09-14 607: Tcl_StatBuf fileInfo;
5583d77f1c 2019-09-14 608: int statRetVal;
5583d77f1c 2019-09-14 609:
d80c88cee0 2019-09-16 610: XVFS_DEBUG_ENTER;
d80c88cee0 2019-09-16 611:
d80c88cee0 2019-09-16 612: if (types) {
d80c88cee0 2019-09-16 613: 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 614: } else {
d80c88cee0 2019-09-16 615: XVFS_DEBUG_PRINTF("Asked to verify the existence \"%s\" ...", Tcl_GetString(path));
d80c88cee0 2019-09-16 616: }
d80c88cee0 2019-09-16 617:
5583d77f1c 2019-09-14 618: statRetVal = xvfs_tclfs_stat(path, &fileInfo, instanceInfo);
5583d77f1c 2019-09-14 619: if (statRetVal != 0) {
d80c88cee0 2019-09-16 620: XVFS_DEBUG_PUTS("... no (cannot stat)");
d80c88cee0 2019-09-16 621:
d80c88cee0 2019-09-16 622: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 623: return(0);
5583d77f1c 2019-09-14 624: }
5583d77f1c 2019-09-14 625:
5583d77f1c 2019-09-14 626: if (!types) {
d80c88cee0 2019-09-16 627: XVFS_DEBUG_PUTS("... yes");
d80c88cee0 2019-09-16 628:
d80c88cee0 2019-09-16 629: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 630: return(1);
5583d77f1c 2019-09-14 631: }
5583d77f1c 2019-09-14 632:
5583d77f1c 2019-09-14 633: if (types->perm != TCL_GLOB_PERM_RONLY) {
12ff77016f 2019-09-14 634: if (types->perm & (TCL_GLOB_PERM_W | TCL_GLOB_PERM_HIDDEN)) {
d80c88cee0 2019-09-16 635: XVFS_DEBUG_PUTS("... no (checked for writable or hidden, not supported)");
d80c88cee0 2019-09-16 636:
d80c88cee0 2019-09-16 637: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 638: return(0);
12ff77016f 2019-09-14 639: }
12ff77016f 2019-09-14 640:
12ff77016f 2019-09-14 641: if ((types->perm & TCL_GLOB_PERM_X) == TCL_GLOB_PERM_X) {
12ff77016f 2019-09-14 642: if (!(fileInfo.st_mode & 040000)) {
d80c88cee0 2019-09-16 643: XVFS_DEBUG_PUTS("... no (checked for executable but not a directory)");
d80c88cee0 2019-09-16 644:
d80c88cee0 2019-09-16 645: XVFS_DEBUG_LEAVE;
12ff77016f 2019-09-14 646: return(0);
12ff77016f 2019-09-14 647: }
5583d77f1c 2019-09-14 648: }
5583d77f1c 2019-09-14 649: }
5583d77f1c 2019-09-14 650:
5583d77f1c 2019-09-14 651: 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 652: XVFS_DEBUG_PUTS("... no (checked for block, char, pipe, sock, or link, not supported)");
d80c88cee0 2019-09-16 653:
d80c88cee0 2019-09-16 654: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 655: return(0);
5583d77f1c 2019-09-14 656: }
5583d77f1c 2019-09-14 657:
5583d77f1c 2019-09-14 658: if ((types->type & TCL_GLOB_TYPE_DIR) == TCL_GLOB_TYPE_DIR) {
5583d77f1c 2019-09-14 659: if (!(fileInfo.st_mode & 040000)) {
d80c88cee0 2019-09-16 660: XVFS_DEBUG_PUTS("... no (checked for directory but not a directory)");
d80c88cee0 2019-09-16 661:
d80c88cee0 2019-09-16 662: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 663: return(0);
5583d77f1c 2019-09-14 664: }
5583d77f1c 2019-09-14 665: }
5583d77f1c 2019-09-14 666:
5583d77f1c 2019-09-14 667: if ((types->type & TCL_GLOB_TYPE_FILE) == TCL_GLOB_TYPE_FILE) {
5583d77f1c 2019-09-14 668: if (!(fileInfo.st_mode & 0100000)) {
d80c88cee0 2019-09-16 669: XVFS_DEBUG_PUTS("... no (checked for file but not a file)");
d80c88cee0 2019-09-16 670:
d80c88cee0 2019-09-16 671: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 672: return(0);
5583d77f1c 2019-09-14 673: }
5583d77f1c 2019-09-14 674: }
5583d77f1c 2019-09-14 675:
5583d77f1c 2019-09-14 676: if ((types->type & TCL_GLOB_TYPE_MOUNT) == TCL_GLOB_TYPE_MOUNT) {
d80c88cee0 2019-09-16 677: path = xvfs_absolutePath(path);
5583d77f1c 2019-09-14 678: pathStr = xvfs_relativePath(path, instanceInfo);
5583d77f1c 2019-09-14 679: if (!pathStr) {
d80c88cee0 2019-09-16 680: XVFS_DEBUG_PUTS("... no (checked for mount but not able to resolve path)");
d80c88cee0 2019-09-16 681:
d80c88cee0 2019-09-16 682: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 683:
d80c88cee0 2019-09-16 684: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 685: return(0);
5583d77f1c 2019-09-14 686: }
5583d77f1c 2019-09-14 687:
5583d77f1c 2019-09-14 688: if (strlen(pathStr) != 0) {
d80c88cee0 2019-09-16 689: XVFS_DEBUG_PUTS("... no (checked for mount but not our top-level directory)");
d80c88cee0 2019-09-16 690:
d80c88cee0 2019-09-16 691: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 692:
d80c88cee0 2019-09-16 693: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 694: return(0);
5583d77f1c 2019-09-14 695: }
d80c88cee0 2019-09-16 696:
d80c88cee0 2019-09-16 697: Tcl_DecrRefCount(path);
5583d77f1c 2019-09-14 698: }
5583d77f1c 2019-09-14 699:
d80c88cee0 2019-09-16 700: XVFS_DEBUG_PUTS("... yes");
d80c88cee0 2019-09-16 701:
d80c88cee0 2019-09-16 702: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 703: return(1);
5583d77f1c 2019-09-14 704: }
5583d77f1c 2019-09-14 705:
5583d77f1c 2019-09-14 706: 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 707: const char *pathStr;
5583d77f1c 2019-09-14 708: const char **children, *child;
5583d77f1c 2019-09-14 709: Tcl_WideInt childrenCount, idx;
5583d77f1c 2019-09-14 710: Tcl_Obj *childObj;
5583d77f1c 2019-09-14 711: int tclRetVal;
5583d77f1c 2019-09-14 712:
5583d77f1c 2019-09-14 713: if (pattern == NULL) {
5583d77f1c 2019-09-14 714: if (xvfs_tclfs_verifyType(path, types, instanceInfo)) {
5583d77f1c 2019-09-14 715: return(TCL_OK);
5583d77f1c 2019-09-14 716: }
5583d77f1c 2019-09-14 717:
5583d77f1c 2019-09-14 718: return(TCL_ERROR);
5583d77f1c 2019-09-14 719: }
5583d77f1c 2019-09-14 720:
d80c88cee0 2019-09-16 721: XVFS_DEBUG_ENTER;
d80c88cee0 2019-09-16 722:
d80c88cee0 2019-09-16 723: path = xvfs_absolutePath(path);
d80c88cee0 2019-09-16 724:
d80c88cee0 2019-09-16 725: if (types) {
d80c88cee0 2019-09-16 726: 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 727: } else {
d80c88cee0 2019-09-16 728: XVFS_DEBUG_PRINTF("Checking for files matching %s in \"%s\" ...", pattern, Tcl_GetString(path));
d80c88cee0 2019-09-16 729: }
d80c88cee0 2019-09-16 730:
5583d77f1c 2019-09-14 731: pathStr = xvfs_relativePath(path, instanceInfo);
5583d77f1c 2019-09-14 732: if (!pathStr) {
d80c88cee0 2019-09-16 733: XVFS_DEBUG_PUTS("... error (not in our VFS)");
d80c88cee0 2019-09-16 734:
d80c88cee0 2019-09-16 735: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 736:
5583d77f1c 2019-09-14 737: if (interp) {
5583d77f1c 2019-09-14 738: Tcl_SetResult(interp, (char *) xvfs_perror(XVFS_RV_ERR_ENOENT), NULL);
5583d77f1c 2019-09-14 739: }
5583d77f1c 2019-09-14 740:
d80c88cee0 2019-09-16 741: XVFS_DEBUG_LEAVE;
d80c88cee0 2019-09-16 742: return(TCL_OK);
5583d77f1c 2019-09-14 743: }
5583d77f1c 2019-09-14 744:
5583d77f1c 2019-09-14 745: childrenCount = 0;
5583d77f1c 2019-09-14 746: children = instanceInfo->fsInfo->getChildrenProc(pathStr, &childrenCount);
5583d77f1c 2019-09-14 747: if (childrenCount < 0) {
d80c88cee0 2019-09-16 748: XVFS_DEBUG_PRINTF("... error: %s", xvfs_perror(childrenCount));
d80c88cee0 2019-09-16 749:
d80c88cee0 2019-09-16 750: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 751:
5583d77f1c 2019-09-14 752: if (interp) {
5583d77f1c 2019-09-14 753: Tcl_SetResult(interp, (char *) xvfs_perror(childrenCount), NULL);
5583d77f1c 2019-09-14 754: }
5583d77f1c 2019-09-14 755:
d80c88cee0 2019-09-16 756: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 757: return(TCL_ERROR);
5583d77f1c 2019-09-14 758: }
5583d77f1c 2019-09-14 759:
5583d77f1c 2019-09-14 760: for (idx = 0; idx < childrenCount; idx++) {
5583d77f1c 2019-09-14 761: child = children[idx];
5583d77f1c 2019-09-14 762:
5583d77f1c 2019-09-14 763: if (!Tcl_StringMatch(child, pattern)) {
5583d77f1c 2019-09-14 764: continue;
5583d77f1c 2019-09-14 765: }
5583d77f1c 2019-09-14 766:
5583d77f1c 2019-09-14 767: childObj = Tcl_DuplicateObj(path);
5583d77f1c 2019-09-14 768: Tcl_IncrRefCount(childObj);
5ae034e55e 2019-09-14 769: Tcl_AppendStringsToObj(childObj, "/", child, NULL);
5583d77f1c 2019-09-14 770:
5583d77f1c 2019-09-14 771: if (!xvfs_tclfs_verifyType(childObj, types, instanceInfo)) {
5583d77f1c 2019-09-14 772: Tcl_DecrRefCount(childObj);
5583d77f1c 2019-09-14 773:
5583d77f1c 2019-09-14 774: continue;
5583d77f1c 2019-09-14 775: }
5583d77f1c 2019-09-14 776:
5583d77f1c 2019-09-14 777: tclRetVal = Tcl_ListObjAppendElement(interp, resultPtr, childObj);
5583d77f1c 2019-09-14 778: Tcl_DecrRefCount(childObj);
5583d77f1c 2019-09-14 779:
5583d77f1c 2019-09-14 780: if (tclRetVal != TCL_OK) {
d80c88cee0 2019-09-16 781: XVFS_DEBUG_PUTS("... error (lappend)");
d80c88cee0 2019-09-16 782: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 783:
d80c88cee0 2019-09-16 784: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 785: return(tclRetVal);
5583d77f1c 2019-09-14 786: }
5583d77f1c 2019-09-14 787: }
5583d77f1c 2019-09-14 788:
d80c88cee0 2019-09-16 789: Tcl_DecrRefCount(path);
d80c88cee0 2019-09-16 790:
d80c88cee0 2019-09-16 791: XVFS_DEBUG_PRINTF("... ok (returning items: %s)", Tcl_GetString(resultPtr));
d80c88cee0 2019-09-16 792:
d80c88cee0 2019-09-16 793: XVFS_DEBUG_LEAVE;
5583d77f1c 2019-09-14 794: return(TCL_OK);
d121970301 2019-05-02 795: }
3e44e1def1 2019-05-02 796: #endif /* XVFS_MODE_SERVER || XVFS_MODE_STANDALONE || XVFS_MODE_FLEIXBLE */
d121970301 2019-05-02 797:
88f96696b7 2019-05-03 798: #if defined(XVFS_MODE_STANDALONE) || defined(XVFS_MODE_FLEXIBLE)
d121970301 2019-05-02 799: /*
d121970301 2019-05-02 800: * Tcl_Filesystem handlers for the standalone implementation
d121970301 2019-05-02 801: */
acfc5037c6 2019-05-02 802: static struct xvfs_tclfs_instance_info xvfs_tclfs_standalone_info;
d121970301 2019-05-02 803: static int xvfs_tclfs_standalone_pathInFilesystem(Tcl_Obj *path, ClientData *dataPtr) {
d121970301 2019-05-02 804: return(xvfs_tclfs_pathInFilesystem(path, dataPtr, &xvfs_tclfs_standalone_info));
d121970301 2019-05-02 805: }
d121970301 2019-05-02 806:
d121970301 2019-05-02 807: static int xvfs_tclfs_standalone_stat(Tcl_Obj *path, Tcl_StatBuf *statBuf) {
d121970301 2019-05-02 808: return(xvfs_tclfs_stat(path, statBuf, &xvfs_tclfs_standalone_info));
d121970301 2019-05-02 809: }
d121970301 2019-05-02 810:
12ff77016f 2019-09-14 811: static int xvfs_tclfs_standalone_access(Tcl_Obj *path, int mode) {
12ff77016f 2019-09-14 812: return(xvfs_tclfs_access(path, mode, &xvfs_tclfs_standalone_info));
e5b6962adf 2019-05-02 813: }
e5b6962adf 2019-05-02 814:
d121970301 2019-05-02 815: static Tcl_Channel xvfs_tclfs_standalone_openFileChannel(Tcl_Interp *interp, Tcl_Obj *path, int mode, int permissions) {
d121970301 2019-05-02 816: return(xvfs_tclfs_openFileChannel(interp, path, mode, permissions, &xvfs_tclfs_standalone_info));
5583d77f1c 2019-09-14 817: }
5583d77f1c 2019-09-14 818:
5583d77f1c 2019-09-14 819: 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 820: return(xvfs_tclfs_matchInDir(interp, resultPtr, pathPtr, pattern, types, &xvfs_tclfs_standalone_info));
e5b6962adf 2019-05-02 821: }
32b55a907b 2019-05-02 822:
32b55a907b 2019-05-02 823: /*
32b55a907b 2019-05-02 824: * There are three (3) modes of operation for Xvfs_Register:
32b55a907b 2019-05-02 825: * 1. standalone -- We register our own Tcl_Filesystem
32b55a907b 2019-05-02 826: * and handle requests under `//xvfs:/<fsName>`
32b55a907b 2019-05-02 827: * 2. client -- A single Tcl_Filesystem is registered for the
32b55a907b 2019-05-02 828: * interp to handle requests under `//xvfs:/` which
32b55a907b 2019-05-02 829: * then dispatches to the appropriate registered
32b55a907b 2019-05-02 830: * handler
32b55a907b 2019-05-02 831: * 3. flexible -- Attempts to find a core Xvfs instance for the
32b55a907b 2019-05-02 832: * process at runtime, if found do #2, otherwise
32b55a907b 2019-05-02 833: * fallback to #1
32b55a907b 2019-05-02 834: *
32b55a907b 2019-05-02 835: */
cb77ecfb24 2019-05-06 836: static Tcl_Filesystem xvfs_tclfs_standalone_fs;
b8cca3a6b4 2019-05-08 837: static int xvfs_standalone_register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
e5b6962adf 2019-05-02 838: int tcl_ret;
d121970301 2019-05-02 839: static int registered = 0;
38bed7cee0 2019-09-13 840:
d121970301 2019-05-02 841: /*
d121970301 2019-05-02 842: * Ensure this instance is not already registered
d121970301 2019-05-02 843: */
d121970301 2019-05-02 844: if (registered) {
d121970301 2019-05-02 845: return(TCL_OK);
d121970301 2019-05-02 846: }
d121970301 2019-05-02 847: registered = 1;
d121970301 2019-05-02 848:
e5b6962adf 2019-05-02 849: /*
e5b6962adf 2019-05-02 850: * In standalone mode, we only support the same protocol we are
e5b6962adf 2019-05-02 851: * compiling for.
e5b6962adf 2019-05-02 852: */
e5b6962adf 2019-05-02 853: if (fsInfo->protocolVersion != XVFS_PROTOCOL_VERSION) {
e5b6962adf 2019-05-02 854: if (interp) {
e5b6962adf 2019-05-02 855: Tcl_SetResult(interp, "Protocol mismatch", NULL);
e5b6962adf 2019-05-02 856: }
e5b6962adf 2019-05-02 857: return(TCL_ERROR);
e5b6962adf 2019-05-02 858: }
38bed7cee0 2019-09-13 859:
cb77ecfb24 2019-05-06 860: xvfs_tclfs_standalone_fs.typeName = "xvfs";
cb77ecfb24 2019-05-06 861: xvfs_tclfs_standalone_fs.structureLength = sizeof(xvfs_tclfs_standalone_fs);
cb77ecfb24 2019-05-06 862: xvfs_tclfs_standalone_fs.version = TCL_FILESYSTEM_VERSION_1;
cb77ecfb24 2019-05-06 863: xvfs_tclfs_standalone_fs.pathInFilesystemProc = xvfs_tclfs_standalone_pathInFilesystem;
cb77ecfb24 2019-05-06 864: xvfs_tclfs_standalone_fs.dupInternalRepProc = NULL;
cb77ecfb24 2019-05-06 865: xvfs_tclfs_standalone_fs.freeInternalRepProc = NULL;
cb77ecfb24 2019-05-06 866: xvfs_tclfs_standalone_fs.internalToNormalizedProc = NULL;
cb77ecfb24 2019-05-06 867: xvfs_tclfs_standalone_fs.createInternalRepProc = NULL;
cb77ecfb24 2019-05-06 868: xvfs_tclfs_standalone_fs.normalizePathProc = NULL;
cb77ecfb24 2019-05-06 869: xvfs_tclfs_standalone_fs.filesystemPathTypeProc = NULL;
cb77ecfb24 2019-05-06 870: xvfs_tclfs_standalone_fs.filesystemSeparatorProc = NULL;
cb77ecfb24 2019-05-06 871: xvfs_tclfs_standalone_fs.statProc = xvfs_tclfs_standalone_stat;
12ff77016f 2019-09-14 872: xvfs_tclfs_standalone_fs.accessProc = xvfs_tclfs_standalone_access;
cb77ecfb24 2019-05-06 873: xvfs_tclfs_standalone_fs.openFileChannelProc = xvfs_tclfs_standalone_openFileChannel;
5583d77f1c 2019-09-14 874: xvfs_tclfs_standalone_fs.matchInDirectoryProc = xvfs_tclfs_standalone_matchInDir;
cb77ecfb24 2019-05-06 875: xvfs_tclfs_standalone_fs.utimeProc = NULL;
cb77ecfb24 2019-05-06 876: xvfs_tclfs_standalone_fs.linkProc = NULL;
d80c88cee0 2019-09-16 877: xvfs_tclfs_standalone_fs.listVolumesProc = NULL;
cb77ecfb24 2019-05-06 878: xvfs_tclfs_standalone_fs.fileAttrStringsProc = NULL;
cb77ecfb24 2019-05-06 879: xvfs_tclfs_standalone_fs.fileAttrsGetProc = NULL;
cb77ecfb24 2019-05-06 880: xvfs_tclfs_standalone_fs.fileAttrsSetProc = NULL;
cb77ecfb24 2019-05-06 881: xvfs_tclfs_standalone_fs.createDirectoryProc = NULL;
cb77ecfb24 2019-05-06 882: xvfs_tclfs_standalone_fs.removeDirectoryProc = NULL;
cb77ecfb24 2019-05-06 883: xvfs_tclfs_standalone_fs.deleteFileProc = NULL;
cb77ecfb24 2019-05-06 884: xvfs_tclfs_standalone_fs.copyFileProc = NULL;
cb77ecfb24 2019-05-06 885: xvfs_tclfs_standalone_fs.renameFileProc = NULL;
cb77ecfb24 2019-05-06 886: xvfs_tclfs_standalone_fs.copyDirectoryProc = NULL;
cb77ecfb24 2019-05-06 887: xvfs_tclfs_standalone_fs.lstatProc = NULL;
cb77ecfb24 2019-05-06 888: xvfs_tclfs_standalone_fs.loadFileProc = NULL;
cb77ecfb24 2019-05-06 889: xvfs_tclfs_standalone_fs.getCwdProc = NULL;
cb77ecfb24 2019-05-06 890: xvfs_tclfs_standalone_fs.chdirProc = NULL;
d121970301 2019-05-02 891:
d121970301 2019-05-02 892: xvfs_tclfs_standalone_info.fsInfo = fsInfo;
d121970301 2019-05-02 893: xvfs_tclfs_standalone_info.mountpoint = Tcl_NewObj();
5ae034e55e 2019-09-14 894:
5ae034e55e 2019-09-14 895: Tcl_IncrRefCount(xvfs_tclfs_standalone_info.mountpoint);
d121970301 2019-05-02 896: Tcl_AppendStringsToObj(xvfs_tclfs_standalone_info.mountpoint, XVFS_ROOT_MOUNTPOINT, fsInfo->name, NULL);
5ae034e55e 2019-09-14 897:
cb77ecfb24 2019-05-06 898: tcl_ret = Tcl_FSRegister(NULL, &xvfs_tclfs_standalone_fs);
e5b6962adf 2019-05-02 899: if (tcl_ret != TCL_OK) {
5ae034e55e 2019-09-14 900: Tcl_DecrRefCount(xvfs_tclfs_standalone_info.mountpoint);
5ae034e55e 2019-09-14 901:
e5b6962adf 2019-05-02 902: if (interp) {
e5b6962adf 2019-05-02 903: Tcl_SetResult(interp, "Tcl_FSRegister() failed", NULL);
e5b6962adf 2019-05-02 904: }
38bed7cee0 2019-09-13 905:
e5b6962adf 2019-05-02 906: return(tcl_ret);
e5b6962adf 2019-05-02 907: }
38bed7cee0 2019-09-13 908:
38bed7cee0 2019-09-13 909: xvfs_tclfs_prepareChannelType();
38bed7cee0 2019-09-13 910:
e5b6962adf 2019-05-02 911: return(TCL_OK);
e5b6962adf 2019-05-02 912: }
d92ba3d36d 2019-05-08 913: #endif /* XVFS_MODE_STANDALONE || XVFS_MODE_FLEXIBLE */
88f96696b7 2019-05-03 914:
88f96696b7 2019-05-03 915: #if defined(XVFS_MODE_FLEXIBLE)
b8cca3a6b4 2019-05-08 916: static int xvfs_flexible_register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
9bcf758fef 2019-05-08 917: ClientData fsHandlerDataRaw;
9bcf758fef 2019-05-08 918: struct xvfs_tclfs_server_info *fsHandlerData;
9bcf758fef 2019-05-08 919: const Tcl_Filesystem *fsHandler;
9bcf758fef 2019-05-08 920: int (*xvfs_register)(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo);
9bcf758fef 2019-05-08 921: Tcl_Obj *rootPathObj;
9bcf758fef 2019-05-08 922:
9bcf758fef 2019-05-08 923: xvfs_register = &xvfs_standalone_register;
9bcf758fef 2019-05-08 924:
9bcf758fef 2019-05-08 925: rootPathObj = Tcl_NewStringObj(XVFS_ROOT_MOUNTPOINT, -1);
9bcf758fef 2019-05-08 926: if (!rootPathObj) {
9bcf758fef 2019-05-08 927: return(xvfs_register(interp, fsInfo));
9bcf758fef 2019-05-08 928: }
9bcf758fef 2019-05-08 929:
9bcf758fef 2019-05-08 930: Tcl_IncrRefCount(rootPathObj);
9bcf758fef 2019-05-08 931: fsHandler = Tcl_FSGetFileSystemForPath(rootPathObj);
9bcf758fef 2019-05-08 932: Tcl_DecrRefCount(rootPathObj);
9bcf758fef 2019-05-08 933:
9bcf758fef 2019-05-08 934: if (!fsHandler) {
9bcf758fef 2019-05-08 935: return(xvfs_register(interp, fsInfo));
9bcf758fef 2019-05-08 936: }
9bcf758fef 2019-05-08 937:
9bcf758fef 2019-05-08 938: fsHandlerDataRaw = Tcl_FSData(fsHandler);
9bcf758fef 2019-05-08 939: if (!fsHandlerDataRaw) {
9bcf758fef 2019-05-08 940: return(xvfs_register(interp, fsInfo));
9bcf758fef 2019-05-08 941: }
9bcf758fef 2019-05-08 942:
9bcf758fef 2019-05-08 943: fsHandlerData = (struct xvfs_tclfs_server_info *) fsHandlerDataRaw;
88f96696b7 2019-05-03 944:
88f96696b7 2019-05-03 945: /*
9bcf758fef 2019-05-08 946: * XXX:TODO: What is the chance that the handler for //xvfs:/ hold
b586d5b0a1 2019-05-08 947: * client data smaller than XVFS_INTERNAL_SERVER_MAGIC_LEN ?
88f96696b7 2019-05-03 948: */
b586d5b0a1 2019-05-08 949: if (memcmp(fsHandlerData->magic, XVFS_INTERNAL_SERVER_MAGIC, sizeof(fsHandlerData->magic)) == 0) {
9bcf758fef 2019-05-08 950: xvfs_register = fsHandlerData->registerProc;
88f96696b7 2019-05-03 951: }
9bcf758fef 2019-05-08 952:
88f96696b7 2019-05-03 953: return(xvfs_register(interp, fsInfo));
88f96696b7 2019-05-03 954: }
d92ba3d36d 2019-05-08 955: #endif /* XVFS_MODE_FLEXIBLE */
3e44e1def1 2019-05-02 956:
3e44e1def1 2019-05-02 957: #if defined(XVFS_MODE_SERVER)
e5b6962adf 2019-05-02 958: int Xvfs_Register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
acfc5037c6 2019-05-02 959: return(TCL_ERROR);
69e476dcd5 2019-05-02 960: }
d92ba3d36d 2019-05-08 961: #endif /* XVFS_MODE_SERVER */