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