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:
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: static struct xvfs_tclfs_instance_info xvfs_tclfs_standalone_info;
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:
d121970301 2019-05-02 rkeene: retval = instanceInfo->fsInfo->getInfoProc(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: }
d121970301 2019-05-02 rkeene:
d121970301 2019-05-02 rkeene: /*
d121970301 2019-05-02 rkeene: * Tcl_Filesystem handlers for the standalone implementation
d121970301 2019-05-02 rkeene: */
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: */
e5b6962adf 2019-05-02 rkeene: static int xvfs_standalone_register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
d121970301 2019-05-02 rkeene: Tcl_Filesystem *xvfs_tclfs_Info;
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:
d121970301 2019-05-02 rkeene: xvfs_tclfs_Info = (Tcl_Filesystem *) Tcl_AttemptAlloc(sizeof(*xvfs_tclfs_Info));
d121970301 2019-05-02 rkeene: if (!xvfs_tclfs_Info) {
e5b6962adf 2019-05-02 rkeene: if (interp) {
e5b6962adf 2019-05-02 rkeene: Tcl_SetResult(interp, "Unable to allocate Tcl_Filesystem object", NULL);
e5b6962adf 2019-05-02 rkeene: }
e5b6962adf 2019-05-02 rkeene: return(TCL_ERROR);
e5b6962adf 2019-05-02 rkeene: }
e5b6962adf 2019-05-02 rkeene:
d121970301 2019-05-02 rkeene: xvfs_tclfs_Info->typeName = strdup("xvfs");
d121970301 2019-05-02 rkeene: xvfs_tclfs_Info->structureLength = sizeof(*xvfs_tclfs_Info);
d121970301 2019-05-02 rkeene: xvfs_tclfs_Info->version = TCL_FILESYSTEM_VERSION_1;
d121970301 2019-05-02 rkeene: xvfs_tclfs_Info->pathInFilesystemProc = xvfs_tclfs_standalone_pathInFilesystem;
d121970301 2019-05-02 rkeene: xvfs_tclfs_Info->dupInternalRepProc = NULL;
d121970301 2019-05-02 rkeene: xvfs_tclfs_Info->freeInternalRepProc = NULL;
d121970301 2019-05-02 rkeene: xvfs_tclfs_Info->internalToNormalizedProc = NULL;
d121970301 2019-05-02 rkeene: xvfs_tclfs_Info->createInternalRepProc = NULL;
d121970301 2019-05-02 rkeene: xvfs_tclfs_Info->normalizePathProc = NULL;
d121970301 2019-05-02 rkeene: xvfs_tclfs_Info->filesystemPathTypeProc = NULL;
d121970301 2019-05-02 rkeene: xvfs_tclfs_Info->filesystemSeparatorProc = NULL;
d121970301 2019-05-02 rkeene: xvfs_tclfs_Info->statProc = xvfs_tclfs_standalone_stat;
d121970301 2019-05-02 rkeene: xvfs_tclfs_Info->accessProc = NULL;
d121970301 2019-05-02 rkeene: xvfs_tclfs_Info->openFileChannelProc = xvfs_tclfs_standalone_openFileChannel;
d121970301 2019-05-02 rkeene: xvfs_tclfs_Info->matchInDirectoryProc = NULL;
d121970301 2019-05-02 rkeene: xvfs_tclfs_Info->utimeProc = NULL;
d121970301 2019-05-02 rkeene: xvfs_tclfs_Info->linkProc = NULL;
d121970301 2019-05-02 rkeene: xvfs_tclfs_Info->listVolumesProc = xvfs_tclfs_standalone_listVolumes;
d121970301 2019-05-02 rkeene: xvfs_tclfs_Info->fileAttrStringsProc = NULL;
d121970301 2019-05-02 rkeene: xvfs_tclfs_Info->fileAttrsGetProc = NULL;
d121970301 2019-05-02 rkeene: xvfs_tclfs_Info->fileAttrsSetProc = NULL;
d121970301 2019-05-02 rkeene: xvfs_tclfs_Info->createDirectoryProc = NULL;
d121970301 2019-05-02 rkeene: xvfs_tclfs_Info->removeDirectoryProc = NULL;
d121970301 2019-05-02 rkeene: xvfs_tclfs_Info->deleteFileProc = NULL;
d121970301 2019-05-02 rkeene: xvfs_tclfs_Info->copyFileProc = NULL;
d121970301 2019-05-02 rkeene: xvfs_tclfs_Info->renameFileProc = NULL;
d121970301 2019-05-02 rkeene: xvfs_tclfs_Info->copyDirectoryProc = NULL;
d121970301 2019-05-02 rkeene: xvfs_tclfs_Info->lstatProc = NULL;
d121970301 2019-05-02 rkeene: xvfs_tclfs_Info->loadFileProc = NULL;
d121970301 2019-05-02 rkeene: xvfs_tclfs_Info->getCwdProc = NULL;
d121970301 2019-05-02 rkeene: xvfs_tclfs_Info->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:
d121970301 2019-05-02 rkeene: tcl_ret = Tcl_FSRegister(NULL, xvfs_tclfs_Info);
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);
32b55a907b 2019-05-02 rkeene: }
32b55a907b 2019-05-02 rkeene:
e5b6962adf 2019-05-02 rkeene: int Xvfs_Register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
e5b6962adf 2019-05-02 rkeene: return(xvfs_standalone_register(interp, fsInfo));
69e476dcd5 2019-05-02 rkeene: }