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