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