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