69e476dcd5 2019-05-02 1: #include <xvfs-core.h>
d121970301 2019-05-02 2: #include <string.h>
69e476dcd5 2019-05-02 3: #include <tcl.h>
e5b6962adf 2019-05-02 4:
3e44e1def1 2019-05-02 5: #if defined(XVFS_MODE_SERVER) || defined(XVFS_MODE_STANDALONE) || defined(XVFS_MODE_FLEXIBLE)
d121970301 2019-05-02 6: #define XVFS_ROOT_MOUNTPOINT "//xvfs:/"
d121970301 2019-05-02 7:
d121970301 2019-05-02 8: struct xvfs_tclfs_instance_info {
d121970301 2019-05-02 9: struct Xvfs_FSInfo *fsInfo;
d121970301 2019-05-02 10: Tcl_Obj *mountpoint;
d121970301 2019-05-02 11: };
d121970301 2019-05-02 12:
d121970301 2019-05-02 13: /*
d121970301 2019-05-02 14: * Internal Core Utilities
d121970301 2019-05-02 15: */
d121970301 2019-05-02 16: static const char *xvfs_relativePath(Tcl_Obj *path, struct xvfs_tclfs_instance_info *info) {
d121970301 2019-05-02 17: const char *pathStr, *rootStr;
d121970301 2019-05-02 18: int pathLen, rootLen;
d121970301 2019-05-02 19:
d121970301 2019-05-02 20: pathStr = Tcl_GetStringFromObj(path, &pathLen);
d121970301 2019-05-02 21: rootStr = Tcl_GetStringFromObj(info->mountpoint, &rootLen);
d121970301 2019-05-02 22:
d121970301 2019-05-02 23: if (pathLen < rootLen) {
d121970301 2019-05-02 24: return(NULL);
d121970301 2019-05-02 25: }
d121970301 2019-05-02 26:
d121970301 2019-05-02 27: if (memcmp(pathStr, rootStr, rootLen) != 0) {
d121970301 2019-05-02 28: return(NULL);
d121970301 2019-05-02 29: }
d121970301 2019-05-02 30:
d121970301 2019-05-02 31: if (pathLen == rootLen) {
d121970301 2019-05-02 32: return("");
d121970301 2019-05-02 33: }
d121970301 2019-05-02 34:
d121970301 2019-05-02 35: /* XXX:TODO: Should this use the native OS path separator ? */
d121970301 2019-05-02 36: if (pathStr[rootLen] != '/') {
d121970301 2019-05-02 37: return(NULL);
d121970301 2019-05-02 38: }
d121970301 2019-05-02 39:
d121970301 2019-05-02 40: return(pathStr + rootLen + 1);
d121970301 2019-05-02 41: }
d121970301 2019-05-02 42:
d121970301 2019-05-02 43: /*
d121970301 2019-05-02 44: * Internal Tcl_Filesystem functions, with the appropriate instance info
d121970301 2019-05-02 45: */
d121970301 2019-05-02 46: static int xvfs_tclfs_pathInFilesystem(Tcl_Obj *path, ClientData *dataPtr, struct xvfs_tclfs_instance_info *instanceInfo) {
d121970301 2019-05-02 47: const char *relativePath;
d121970301 2019-05-02 48:
d121970301 2019-05-02 49: relativePath = xvfs_relativePath(path, instanceInfo);
d121970301 2019-05-02 50: if (!relativePath) {
d121970301 2019-05-02 51: return(-1);
d121970301 2019-05-02 52: }
d121970301 2019-05-02 53:
d121970301 2019-05-02 54: return(TCL_OK);
d121970301 2019-05-02 55: }
d121970301 2019-05-02 56:
d121970301 2019-05-02 57: static int xvfs_tclfs_stat(Tcl_Obj *path, Tcl_StatBuf *statBuf, struct xvfs_tclfs_instance_info *instanceInfo) {
d121970301 2019-05-02 58: const char *pathStr;
d121970301 2019-05-02 59: int retval;
d121970301 2019-05-02 60:
d121970301 2019-05-02 61: pathStr = xvfs_relativePath(path, instanceInfo);
d121970301 2019-05-02 62:
daf25f5222 2019-05-03 63: retval = instanceInfo->fsInfo->getStatProc(pathStr, statBuf);
d121970301 2019-05-02 64:
d121970301 2019-05-02 65: return(retval);
d121970301 2019-05-02 66: }
d121970301 2019-05-02 67:
d121970301 2019-05-02 68: static Tcl_Obj *xvfs_tclfs_listVolumes(struct xvfs_tclfs_instance_info *instanceInfo) {
d121970301 2019-05-02 69: return(NULL);
d121970301 2019-05-02 70: }
d121970301 2019-05-02 71:
d121970301 2019-05-02 72: static Tcl_Channel xvfs_tclfs_openFileChannel(Tcl_Interp *interp, Tcl_Obj *path, int mode, int permissions, struct xvfs_tclfs_instance_info *instanceInfo) {
d121970301 2019-05-02 73: const char *pathStr;
d121970301 2019-05-02 74:
d121970301 2019-05-02 75: pathStr = xvfs_relativePath(path, instanceInfo);
d121970301 2019-05-02 76: fprintf(stderr, "Called open(%s)!\n", pathStr);
d121970301 2019-05-02 77:
d121970301 2019-05-02 78: return(NULL);
d121970301 2019-05-02 79: }
3e44e1def1 2019-05-02 80: #endif /* XVFS_MODE_SERVER || XVFS_MODE_STANDALONE || XVFS_MODE_FLEIXBLE */
d121970301 2019-05-02 81:
88f96696b7 2019-05-03 82: #if defined(XVFS_MODE_STANDALONE) || defined(XVFS_MODE_FLEXIBLE)
d121970301 2019-05-02 83: /*
d121970301 2019-05-02 84: * Tcl_Filesystem handlers for the standalone implementation
d121970301 2019-05-02 85: */
acfc5037c6 2019-05-02 86: static struct xvfs_tclfs_instance_info xvfs_tclfs_standalone_info;
d121970301 2019-05-02 87: static int xvfs_tclfs_standalone_pathInFilesystem(Tcl_Obj *path, ClientData *dataPtr) {
d121970301 2019-05-02 88: return(xvfs_tclfs_pathInFilesystem(path, dataPtr, &xvfs_tclfs_standalone_info));
d121970301 2019-05-02 89: }
d121970301 2019-05-02 90:
d121970301 2019-05-02 91: static int xvfs_tclfs_standalone_stat(Tcl_Obj *path, Tcl_StatBuf *statBuf) {
d121970301 2019-05-02 92: return(xvfs_tclfs_stat(path, statBuf, &xvfs_tclfs_standalone_info));
e5b6962adf 2019-05-02 93: }
e5b6962adf 2019-05-02 94:
d121970301 2019-05-02 95: static Tcl_Obj *xvfs_tclfs_standalone_listVolumes(void) {
d121970301 2019-05-02 96: return(xvfs_tclfs_listVolumes(&xvfs_tclfs_standalone_info));
e5b6962adf 2019-05-02 97: }
e5b6962adf 2019-05-02 98:
d121970301 2019-05-02 99: static Tcl_Channel xvfs_tclfs_standalone_openFileChannel(Tcl_Interp *interp, Tcl_Obj *path, int mode, int permissions) {
d121970301 2019-05-02 100: return(xvfs_tclfs_openFileChannel(interp, path, mode, permissions, &xvfs_tclfs_standalone_info));
e5b6962adf 2019-05-02 101: }
32b55a907b 2019-05-02 102:
32b55a907b 2019-05-02 103: /*
32b55a907b 2019-05-02 104: * There are three (3) modes of operation for Xvfs_Register:
32b55a907b 2019-05-02 105: * 1. standalone -- We register our own Tcl_Filesystem
32b55a907b 2019-05-02 106: * and handle requests under `//xvfs:/<fsName>`
32b55a907b 2019-05-02 107: * 2. client -- A single Tcl_Filesystem is registered for the
32b55a907b 2019-05-02 108: * interp to handle requests under `//xvfs:/` which
32b55a907b 2019-05-02 109: * then dispatches to the appropriate registered
32b55a907b 2019-05-02 110: * handler
32b55a907b 2019-05-02 111: * 3. flexible -- Attempts to find a core Xvfs instance for the
32b55a907b 2019-05-02 112: * process at runtime, if found do #2, otherwise
32b55a907b 2019-05-02 113: * fallback to #1
32b55a907b 2019-05-02 114: *
32b55a907b 2019-05-02 115: */
cb77ecfb24 2019-05-06 116: static Tcl_Filesystem xvfs_tclfs_standalone_fs;
acfc5037c6 2019-05-02 117: int xvfs_standalone_register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
e5b6962adf 2019-05-02 118: int tcl_ret;
d121970301 2019-05-02 119: static int registered = 0;
e5b6962adf 2019-05-02 120:
d121970301 2019-05-02 121: /*
d121970301 2019-05-02 122: * Ensure this instance is not already registered
d121970301 2019-05-02 123: */
d121970301 2019-05-02 124: if (registered) {
d121970301 2019-05-02 125: return(TCL_OK);
d121970301 2019-05-02 126: }
d121970301 2019-05-02 127: registered = 1;
d121970301 2019-05-02 128:
e5b6962adf 2019-05-02 129: /*
e5b6962adf 2019-05-02 130: * In standalone mode, we only support the same protocol we are
e5b6962adf 2019-05-02 131: * compiling for.
e5b6962adf 2019-05-02 132: */
e5b6962adf 2019-05-02 133: if (fsInfo->protocolVersion != XVFS_PROTOCOL_VERSION) {
e5b6962adf 2019-05-02 134: if (interp) {
e5b6962adf 2019-05-02 135: Tcl_SetResult(interp, "Protocol mismatch", NULL);
e5b6962adf 2019-05-02 136: }
e5b6962adf 2019-05-02 137: return(TCL_ERROR);
e5b6962adf 2019-05-02 138: }
e5b6962adf 2019-05-02 139:
cb77ecfb24 2019-05-06 140: xvfs_tclfs_standalone_fs.typeName = "xvfs";
cb77ecfb24 2019-05-06 141: xvfs_tclfs_standalone_fs.structureLength = sizeof(xvfs_tclfs_standalone_fs);
cb77ecfb24 2019-05-06 142: xvfs_tclfs_standalone_fs.version = TCL_FILESYSTEM_VERSION_1;
cb77ecfb24 2019-05-06 143: xvfs_tclfs_standalone_fs.pathInFilesystemProc = xvfs_tclfs_standalone_pathInFilesystem;
cb77ecfb24 2019-05-06 144: xvfs_tclfs_standalone_fs.dupInternalRepProc = NULL;
cb77ecfb24 2019-05-06 145: xvfs_tclfs_standalone_fs.freeInternalRepProc = NULL;
cb77ecfb24 2019-05-06 146: xvfs_tclfs_standalone_fs.internalToNormalizedProc = NULL;
cb77ecfb24 2019-05-06 147: xvfs_tclfs_standalone_fs.createInternalRepProc = NULL;
cb77ecfb24 2019-05-06 148: xvfs_tclfs_standalone_fs.normalizePathProc = NULL;
cb77ecfb24 2019-05-06 149: xvfs_tclfs_standalone_fs.filesystemPathTypeProc = NULL;
cb77ecfb24 2019-05-06 150: xvfs_tclfs_standalone_fs.filesystemSeparatorProc = NULL;
cb77ecfb24 2019-05-06 151: xvfs_tclfs_standalone_fs.statProc = xvfs_tclfs_standalone_stat;
cb77ecfb24 2019-05-06 152: xvfs_tclfs_standalone_fs.accessProc = NULL;
cb77ecfb24 2019-05-06 153: xvfs_tclfs_standalone_fs.openFileChannelProc = xvfs_tclfs_standalone_openFileChannel;
cb77ecfb24 2019-05-06 154: xvfs_tclfs_standalone_fs.matchInDirectoryProc = NULL;
cb77ecfb24 2019-05-06 155: xvfs_tclfs_standalone_fs.utimeProc = NULL;
cb77ecfb24 2019-05-06 156: xvfs_tclfs_standalone_fs.linkProc = NULL;
cb77ecfb24 2019-05-06 157: xvfs_tclfs_standalone_fs.listVolumesProc = xvfs_tclfs_standalone_listVolumes;
cb77ecfb24 2019-05-06 158: xvfs_tclfs_standalone_fs.fileAttrStringsProc = NULL;
cb77ecfb24 2019-05-06 159: xvfs_tclfs_standalone_fs.fileAttrsGetProc = NULL;
cb77ecfb24 2019-05-06 160: xvfs_tclfs_standalone_fs.fileAttrsSetProc = NULL;
cb77ecfb24 2019-05-06 161: xvfs_tclfs_standalone_fs.createDirectoryProc = NULL;
cb77ecfb24 2019-05-06 162: xvfs_tclfs_standalone_fs.removeDirectoryProc = NULL;
cb77ecfb24 2019-05-06 163: xvfs_tclfs_standalone_fs.deleteFileProc = NULL;
cb77ecfb24 2019-05-06 164: xvfs_tclfs_standalone_fs.copyFileProc = NULL;
cb77ecfb24 2019-05-06 165: xvfs_tclfs_standalone_fs.renameFileProc = NULL;
cb77ecfb24 2019-05-06 166: xvfs_tclfs_standalone_fs.copyDirectoryProc = NULL;
cb77ecfb24 2019-05-06 167: xvfs_tclfs_standalone_fs.lstatProc = NULL;
cb77ecfb24 2019-05-06 168: xvfs_tclfs_standalone_fs.loadFileProc = NULL;
cb77ecfb24 2019-05-06 169: xvfs_tclfs_standalone_fs.getCwdProc = NULL;
cb77ecfb24 2019-05-06 170: xvfs_tclfs_standalone_fs.chdirProc = NULL;
d121970301 2019-05-02 171:
d121970301 2019-05-02 172: xvfs_tclfs_standalone_info.fsInfo = fsInfo;
d121970301 2019-05-02 173: xvfs_tclfs_standalone_info.mountpoint = Tcl_NewObj();
d121970301 2019-05-02 174: Tcl_AppendStringsToObj(xvfs_tclfs_standalone_info.mountpoint, XVFS_ROOT_MOUNTPOINT, fsInfo->name, NULL);
d121970301 2019-05-02 175:
cb77ecfb24 2019-05-06 176: tcl_ret = Tcl_FSRegister(NULL, &xvfs_tclfs_standalone_fs);
e5b6962adf 2019-05-02 177: if (tcl_ret != TCL_OK) {
e5b6962adf 2019-05-02 178: if (interp) {
e5b6962adf 2019-05-02 179: Tcl_SetResult(interp, "Tcl_FSRegister() failed", NULL);
e5b6962adf 2019-05-02 180: }
e5b6962adf 2019-05-02 181:
e5b6962adf 2019-05-02 182: return(tcl_ret);
e5b6962adf 2019-05-02 183: }
e5b6962adf 2019-05-02 184:
e5b6962adf 2019-05-02 185: return(TCL_OK);
88f96696b7 2019-05-03 186: }
88f96696b7 2019-05-03 187: #endif
88f96696b7 2019-05-03 188:
9bcf758fef 2019-05-08 189: #if defined(XVFS_MODE_FLEXIBLE) || defined(XVFS_MODE_SERVER)
9bcf758fef 2019-05-08 190: struct xvfs_tclfs_server_info {
9bcf758fef 2019-05-08 191: char magic[XVFS_PROTOCOL_SERVER_MAGIC_LEN];
9bcf758fef 2019-05-08 192: int (*registerProc)(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo);
9bcf758fef 2019-05-08 193: };
9bcf758fef 2019-05-08 194: #endif
9bcf758fef 2019-05-08 195:
88f96696b7 2019-05-03 196: #if defined(XVFS_MODE_FLEXIBLE)
88f96696b7 2019-05-03 197: int xvfs_flexible_register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
9bcf758fef 2019-05-08 198: ClientData fsHandlerDataRaw;
9bcf758fef 2019-05-08 199: struct xvfs_tclfs_server_info *fsHandlerData;
9bcf758fef 2019-05-08 200: const Tcl_Filesystem *fsHandler;
9bcf758fef 2019-05-08 201: int (*xvfs_register)(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo);
9bcf758fef 2019-05-08 202: Tcl_Obj *rootPathObj;
9bcf758fef 2019-05-08 203:
9bcf758fef 2019-05-08 204: xvfs_register = &xvfs_standalone_register;
9bcf758fef 2019-05-08 205:
9bcf758fef 2019-05-08 206: rootPathObj = Tcl_NewStringObj(XVFS_ROOT_MOUNTPOINT, -1);
9bcf758fef 2019-05-08 207: if (!rootPathObj) {
9bcf758fef 2019-05-08 208: return(xvfs_register(interp, fsInfo));
9bcf758fef 2019-05-08 209: }
9bcf758fef 2019-05-08 210:
9bcf758fef 2019-05-08 211: Tcl_IncrRefCount(rootPathObj);
9bcf758fef 2019-05-08 212: fsHandler = Tcl_FSGetFileSystemForPath(rootPathObj);
9bcf758fef 2019-05-08 213: Tcl_DecrRefCount(rootPathObj);
9bcf758fef 2019-05-08 214:
9bcf758fef 2019-05-08 215: if (!fsHandler) {
9bcf758fef 2019-05-08 216: return(xvfs_register(interp, fsInfo));
9bcf758fef 2019-05-08 217: }
9bcf758fef 2019-05-08 218:
9bcf758fef 2019-05-08 219: fsHandlerDataRaw = Tcl_FSData(fsHandler);
9bcf758fef 2019-05-08 220: if (!fsHandlerDataRaw) {
9bcf758fef 2019-05-08 221: return(xvfs_register(interp, fsInfo));
9bcf758fef 2019-05-08 222: }
9bcf758fef 2019-05-08 223:
9bcf758fef 2019-05-08 224: fsHandlerData = (struct xvfs_tclfs_server_info *) fsHandlerDataRaw;
88f96696b7 2019-05-03 225:
88f96696b7 2019-05-03 226: /*
9bcf758fef 2019-05-08 227: * XXX:TODO: What is the chance that the handler for //xvfs:/ hold
9bcf758fef 2019-05-08 228: * client data smaller than XVFS_PROTOCOL_SERVER_MAGIC_LEN ?
88f96696b7 2019-05-03 229: */
9bcf758fef 2019-05-08 230: if (memcmp(fsHandlerData->magic, XVFS_PROTOCOL_SERVER_MAGIC, sizeof(fsHandlerData->magic)) == 0) {
9bcf758fef 2019-05-08 231: xvfs_register = fsHandlerData->registerProc;
88f96696b7 2019-05-03 232: }
9bcf758fef 2019-05-08 233:
88f96696b7 2019-05-03 234: return(xvfs_register(interp, fsInfo));
3e44e1def1 2019-05-02 235: }
3e44e1def1 2019-05-02 236: #endif
3e44e1def1 2019-05-02 237:
3e44e1def1 2019-05-02 238: #if defined(XVFS_MODE_SERVER)
e5b6962adf 2019-05-02 239: int Xvfs_Register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
acfc5037c6 2019-05-02 240: return(TCL_ERROR);
69e476dcd5 2019-05-02 241: }
acfc5037c6 2019-05-02 242: #endif