69e476dcd5 2019-05-02 rkeene: #include <xvfs-core.h>
d121970301 2019-05-02 rkeene: #include <string.h>
69e476dcd5 2019-05-02 rkeene: #include <tcl.h>
e5b6962adf 2019-05-02 rkeene:
3e44e1def1 2019-05-02 rkeene: #if defined(XVFS_MODE_SERVER) || defined(XVFS_MODE_STANDALONE) || defined(XVFS_MODE_FLEXIBLE)
d121970301 2019-05-02 rkeene: #define XVFS_ROOT_MOUNTPOINT "//xvfs:/"
d121970301 2019-05-02 rkeene:
d121970301 2019-05-02 rkeene: struct xvfs_tclfs_instance_info {
d121970301 2019-05-02 rkeene: struct Xvfs_FSInfo *fsInfo;
d121970301 2019-05-02 rkeene: Tcl_Obj *mountpoint;
d121970301 2019-05-02 rkeene: };
d121970301 2019-05-02 rkeene:
d121970301 2019-05-02 rkeene: /*
d121970301 2019-05-02 rkeene: * Internal Core Utilities
d121970301 2019-05-02 rkeene: */
d121970301 2019-05-02 rkeene: static const char *xvfs_relativePath(Tcl_Obj *path, struct xvfs_tclfs_instance_info *info) {
d121970301 2019-05-02 rkeene: const char *pathStr, *rootStr;
d121970301 2019-05-02 rkeene: int pathLen, rootLen;
d121970301 2019-05-02 rkeene:
d121970301 2019-05-02 rkeene: pathStr = Tcl_GetStringFromObj(path, &pathLen);
d121970301 2019-05-02 rkeene: rootStr = Tcl_GetStringFromObj(info->mountpoint, &rootLen);
d121970301 2019-05-02 rkeene:
d121970301 2019-05-02 rkeene: if (pathLen < rootLen) {
d121970301 2019-05-02 rkeene: return(NULL);
d121970301 2019-05-02 rkeene: }
d121970301 2019-05-02 rkeene:
d121970301 2019-05-02 rkeene: if (memcmp(pathStr, rootStr, rootLen) != 0) {
d121970301 2019-05-02 rkeene: return(NULL);
d121970301 2019-05-02 rkeene: }
d121970301 2019-05-02 rkeene:
d121970301 2019-05-02 rkeene: if (pathLen == rootLen) {
d121970301 2019-05-02 rkeene: return("");
d121970301 2019-05-02 rkeene: }
d121970301 2019-05-02 rkeene:
d121970301 2019-05-02 rkeene: /* XXX:TODO: Should this use the native OS path separator ? */
d121970301 2019-05-02 rkeene: if (pathStr[rootLen] != '/') {
d121970301 2019-05-02 rkeene: return(NULL);
d121970301 2019-05-02 rkeene: }
d121970301 2019-05-02 rkeene:
d121970301 2019-05-02 rkeene: return(pathStr + rootLen + 1);
d121970301 2019-05-02 rkeene: }
d121970301 2019-05-02 rkeene:
d121970301 2019-05-02 rkeene: /*
d121970301 2019-05-02 rkeene: * Internal Tcl_Filesystem functions, with the appropriate instance info
d121970301 2019-05-02 rkeene: */
d121970301 2019-05-02 rkeene: static int xvfs_tclfs_pathInFilesystem(Tcl_Obj *path, ClientData *dataPtr, struct xvfs_tclfs_instance_info *instanceInfo) {
d121970301 2019-05-02 rkeene: const char *relativePath;
d121970301 2019-05-02 rkeene:
d121970301 2019-05-02 rkeene: relativePath = xvfs_relativePath(path, instanceInfo);
d121970301 2019-05-02 rkeene: if (!relativePath) {
d121970301 2019-05-02 rkeene: return(-1);
d121970301 2019-05-02 rkeene: }
d121970301 2019-05-02 rkeene:
d121970301 2019-05-02 rkeene: return(TCL_OK);
d121970301 2019-05-02 rkeene: }
d121970301 2019-05-02 rkeene:
d121970301 2019-05-02 rkeene: static int xvfs_tclfs_stat(Tcl_Obj *path, Tcl_StatBuf *statBuf, struct xvfs_tclfs_instance_info *instanceInfo) {
d121970301 2019-05-02 rkeene: const char *pathStr;
d121970301 2019-05-02 rkeene: int retval;
d121970301 2019-05-02 rkeene:
d121970301 2019-05-02 rkeene: pathStr = xvfs_relativePath(path, instanceInfo);
d121970301 2019-05-02 rkeene:
daf25f5222 2019-05-03 rkeene: retval = instanceInfo->fsInfo->getStatProc(pathStr, statBuf);
d121970301 2019-05-02 rkeene:
d121970301 2019-05-02 rkeene: return(retval);
d121970301 2019-05-02 rkeene: }
d121970301 2019-05-02 rkeene:
d121970301 2019-05-02 rkeene: static Tcl_Obj *xvfs_tclfs_listVolumes(struct xvfs_tclfs_instance_info *instanceInfo) {
d121970301 2019-05-02 rkeene: return(NULL);
d121970301 2019-05-02 rkeene: }
d121970301 2019-05-02 rkeene:
d121970301 2019-05-02 rkeene: 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 rkeene: const char *pathStr;
d121970301 2019-05-02 rkeene:
d121970301 2019-05-02 rkeene: pathStr = xvfs_relativePath(path, instanceInfo);
d121970301 2019-05-02 rkeene: fprintf(stderr, "Called open(%s)!\n", pathStr);
d121970301 2019-05-02 rkeene:
d121970301 2019-05-02 rkeene: return(NULL);
d121970301 2019-05-02 rkeene: }
3e44e1def1 2019-05-02 rkeene: #endif /* XVFS_MODE_SERVER || XVFS_MODE_STANDALONE || XVFS_MODE_FLEIXBLE */
d121970301 2019-05-02 rkeene:
88f96696b7 2019-05-03 rkeene: #if defined(XVFS_MODE_STANDALONE) || defined(XVFS_MODE_FLEXIBLE)
d121970301 2019-05-02 rkeene: /*
d121970301 2019-05-02 rkeene: * Tcl_Filesystem handlers for the standalone implementation
d121970301 2019-05-02 rkeene: */
acfc5037c6 2019-05-02 rkeene: static struct xvfs_tclfs_instance_info xvfs_tclfs_standalone_info;
d121970301 2019-05-02 rkeene: static int xvfs_tclfs_standalone_pathInFilesystem(Tcl_Obj *path, ClientData *dataPtr) {
d121970301 2019-05-02 rkeene: return(xvfs_tclfs_pathInFilesystem(path, dataPtr, &xvfs_tclfs_standalone_info));
d121970301 2019-05-02 rkeene: }
d121970301 2019-05-02 rkeene:
d121970301 2019-05-02 rkeene: static int xvfs_tclfs_standalone_stat(Tcl_Obj *path, Tcl_StatBuf *statBuf) {
d121970301 2019-05-02 rkeene: return(xvfs_tclfs_stat(path, statBuf, &xvfs_tclfs_standalone_info));
e5b6962adf 2019-05-02 rkeene: }
e5b6962adf 2019-05-02 rkeene:
d121970301 2019-05-02 rkeene: static Tcl_Obj *xvfs_tclfs_standalone_listVolumes(void) {
d121970301 2019-05-02 rkeene: return(xvfs_tclfs_listVolumes(&xvfs_tclfs_standalone_info));
e5b6962adf 2019-05-02 rkeene: }
e5b6962adf 2019-05-02 rkeene:
d121970301 2019-05-02 rkeene: static Tcl_Channel xvfs_tclfs_standalone_openFileChannel(Tcl_Interp *interp, Tcl_Obj *path, int mode, int permissions) {
d121970301 2019-05-02 rkeene: return(xvfs_tclfs_openFileChannel(interp, path, mode, permissions, &xvfs_tclfs_standalone_info));
e5b6962adf 2019-05-02 rkeene: }
32b55a907b 2019-05-02 rkeene:
32b55a907b 2019-05-02 rkeene: /*
32b55a907b 2019-05-02 rkeene: * There are three (3) modes of operation for Xvfs_Register:
32b55a907b 2019-05-02 rkeene: * 1. standalone -- We register our own Tcl_Filesystem
32b55a907b 2019-05-02 rkeene: * and handle requests under `//xvfs:/<fsName>`
32b55a907b 2019-05-02 rkeene: * 2. client -- A single Tcl_Filesystem is registered for the
32b55a907b 2019-05-02 rkeene: * interp to handle requests under `//xvfs:/` which
32b55a907b 2019-05-02 rkeene: * then dispatches to the appropriate registered
32b55a907b 2019-05-02 rkeene: * handler
32b55a907b 2019-05-02 rkeene: * 3. flexible -- Attempts to find a core Xvfs instance for the
32b55a907b 2019-05-02 rkeene: * process at runtime, if found do #2, otherwise
32b55a907b 2019-05-02 rkeene: * fallback to #1
32b55a907b 2019-05-02 rkeene: *
32b55a907b 2019-05-02 rkeene: */
cb77ecfb24 2019-05-06 rkeene: static Tcl_Filesystem xvfs_tclfs_standalone_fs;
acfc5037c6 2019-05-02 rkeene: int xvfs_standalone_register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
e5b6962adf 2019-05-02 rkeene: int tcl_ret;
d121970301 2019-05-02 rkeene: static int registered = 0;
e5b6962adf 2019-05-02 rkeene:
d121970301 2019-05-02 rkeene: /*
d121970301 2019-05-02 rkeene: * Ensure this instance is not already registered
d121970301 2019-05-02 rkeene: */
d121970301 2019-05-02 rkeene: if (registered) {
d121970301 2019-05-02 rkeene: return(TCL_OK);
d121970301 2019-05-02 rkeene: }
d121970301 2019-05-02 rkeene: registered = 1;
d121970301 2019-05-02 rkeene:
e5b6962adf 2019-05-02 rkeene: /*
e5b6962adf 2019-05-02 rkeene: * In standalone mode, we only support the same protocol we are
e5b6962adf 2019-05-02 rkeene: * compiling for.
e5b6962adf 2019-05-02 rkeene: */
e5b6962adf 2019-05-02 rkeene: if (fsInfo->protocolVersion != XVFS_PROTOCOL_VERSION) {
e5b6962adf 2019-05-02 rkeene: if (interp) {
e5b6962adf 2019-05-02 rkeene: Tcl_SetResult(interp, "Protocol mismatch", NULL);
e5b6962adf 2019-05-02 rkeene: }
e5b6962adf 2019-05-02 rkeene: return(TCL_ERROR);
e5b6962adf 2019-05-02 rkeene: }
e5b6962adf 2019-05-02 rkeene:
cb77ecfb24 2019-05-06 rkeene: xvfs_tclfs_standalone_fs.typeName = "xvfs";
cb77ecfb24 2019-05-06 rkeene: xvfs_tclfs_standalone_fs.structureLength = sizeof(xvfs_tclfs_standalone_fs);
cb77ecfb24 2019-05-06 rkeene: xvfs_tclfs_standalone_fs.version = TCL_FILESYSTEM_VERSION_1;
cb77ecfb24 2019-05-06 rkeene: xvfs_tclfs_standalone_fs.pathInFilesystemProc = xvfs_tclfs_standalone_pathInFilesystem;
cb77ecfb24 2019-05-06 rkeene: xvfs_tclfs_standalone_fs.dupInternalRepProc = NULL;
cb77ecfb24 2019-05-06 rkeene: xvfs_tclfs_standalone_fs.freeInternalRepProc = NULL;
cb77ecfb24 2019-05-06 rkeene: xvfs_tclfs_standalone_fs.internalToNormalizedProc = NULL;
cb77ecfb24 2019-05-06 rkeene: xvfs_tclfs_standalone_fs.createInternalRepProc = NULL;
cb77ecfb24 2019-05-06 rkeene: xvfs_tclfs_standalone_fs.normalizePathProc = NULL;
cb77ecfb24 2019-05-06 rkeene: xvfs_tclfs_standalone_fs.filesystemPathTypeProc = NULL;
cb77ecfb24 2019-05-06 rkeene: xvfs_tclfs_standalone_fs.filesystemSeparatorProc = NULL;
cb77ecfb24 2019-05-06 rkeene: xvfs_tclfs_standalone_fs.statProc = xvfs_tclfs_standalone_stat;
cb77ecfb24 2019-05-06 rkeene: xvfs_tclfs_standalone_fs.accessProc = NULL;
cb77ecfb24 2019-05-06 rkeene: xvfs_tclfs_standalone_fs.openFileChannelProc = xvfs_tclfs_standalone_openFileChannel;
cb77ecfb24 2019-05-06 rkeene: xvfs_tclfs_standalone_fs.matchInDirectoryProc = NULL;
cb77ecfb24 2019-05-06 rkeene: xvfs_tclfs_standalone_fs.utimeProc = NULL;
cb77ecfb24 2019-05-06 rkeene: xvfs_tclfs_standalone_fs.linkProc = NULL;
cb77ecfb24 2019-05-06 rkeene: xvfs_tclfs_standalone_fs.listVolumesProc = xvfs_tclfs_standalone_listVolumes;
cb77ecfb24 2019-05-06 rkeene: xvfs_tclfs_standalone_fs.fileAttrStringsProc = NULL;
cb77ecfb24 2019-05-06 rkeene: xvfs_tclfs_standalone_fs.fileAttrsGetProc = NULL;
cb77ecfb24 2019-05-06 rkeene: xvfs_tclfs_standalone_fs.fileAttrsSetProc = NULL;
cb77ecfb24 2019-05-06 rkeene: xvfs_tclfs_standalone_fs.createDirectoryProc = NULL;
cb77ecfb24 2019-05-06 rkeene: xvfs_tclfs_standalone_fs.removeDirectoryProc = NULL;
cb77ecfb24 2019-05-06 rkeene: xvfs_tclfs_standalone_fs.deleteFileProc = NULL;
cb77ecfb24 2019-05-06 rkeene: xvfs_tclfs_standalone_fs.copyFileProc = NULL;
cb77ecfb24 2019-05-06 rkeene: xvfs_tclfs_standalone_fs.renameFileProc = NULL;
cb77ecfb24 2019-05-06 rkeene: xvfs_tclfs_standalone_fs.copyDirectoryProc = NULL;
cb77ecfb24 2019-05-06 rkeene: xvfs_tclfs_standalone_fs.lstatProc = NULL;
cb77ecfb24 2019-05-06 rkeene: xvfs_tclfs_standalone_fs.loadFileProc = NULL;
cb77ecfb24 2019-05-06 rkeene: xvfs_tclfs_standalone_fs.getCwdProc = NULL;
cb77ecfb24 2019-05-06 rkeene: xvfs_tclfs_standalone_fs.chdirProc = NULL;
d121970301 2019-05-02 rkeene:
d121970301 2019-05-02 rkeene: xvfs_tclfs_standalone_info.fsInfo = fsInfo;
d121970301 2019-05-02 rkeene: xvfs_tclfs_standalone_info.mountpoint = Tcl_NewObj();
d121970301 2019-05-02 rkeene: Tcl_AppendStringsToObj(xvfs_tclfs_standalone_info.mountpoint, XVFS_ROOT_MOUNTPOINT, fsInfo->name, NULL);
d121970301 2019-05-02 rkeene:
cb77ecfb24 2019-05-06 rkeene: tcl_ret = Tcl_FSRegister(NULL, &xvfs_tclfs_standalone_fs);
e5b6962adf 2019-05-02 rkeene: if (tcl_ret != TCL_OK) {
e5b6962adf 2019-05-02 rkeene: if (interp) {
e5b6962adf 2019-05-02 rkeene: Tcl_SetResult(interp, "Tcl_FSRegister() failed", NULL);
e5b6962adf 2019-05-02 rkeene: }
e5b6962adf 2019-05-02 rkeene:
e5b6962adf 2019-05-02 rkeene: return(tcl_ret);
e5b6962adf 2019-05-02 rkeene: }
e5b6962adf 2019-05-02 rkeene:
e5b6962adf 2019-05-02 rkeene: return(TCL_OK);
88f96696b7 2019-05-03 rkeene: }
88f96696b7 2019-05-03 rkeene: #endif
88f96696b7 2019-05-03 rkeene:
88f96696b7 2019-05-03 rkeene: #if defined(XVFS_MODE_FLEXIBLE)
88f96696b7 2019-05-03 rkeene: #include <dlfcn.h>
88f96696b7 2019-05-03 rkeene: int xvfs_flexible_register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
88f96696b7 2019-05-03 rkeene: int (*xvfs_register)(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) = NULL;
88f96696b7 2019-05-03 rkeene:
88f96696b7 2019-05-03 rkeene: /*
88f96696b7 2019-05-03 rkeene: * XXX:TODO: Find some way to use Tcl_FindSymbol() to do this
88f96696b7 2019-05-03 rkeene: */
88f96696b7 2019-05-03 rkeene: xvfs_register = dlsym(NULL, "Xvfs_Register");
88f96696b7 2019-05-03 rkeene: if (!xvfs_register) {
88f96696b7 2019-05-03 rkeene: xvfs_register = &xvfs_standalone_register;
88f96696b7 2019-05-03 rkeene: }
88f96696b7 2019-05-03 rkeene:
88f96696b7 2019-05-03 rkeene: return(xvfs_register(interp, fsInfo));
3e44e1def1 2019-05-02 rkeene: }
3e44e1def1 2019-05-02 rkeene: #endif
3e44e1def1 2019-05-02 rkeene:
3e44e1def1 2019-05-02 rkeene: #if defined(XVFS_MODE_SERVER)
e5b6962adf 2019-05-02 rkeene: int Xvfs_Register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
acfc5037c6 2019-05-02 rkeene: return(TCL_ERROR);
69e476dcd5 2019-05-02 rkeene: }
acfc5037c6 2019-05-02 rkeene: #endif