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:
d92ba3d36d 2019-05-08 rkeene: #if defined(XVFS_MODE_FLEXIBLE) || defined(XVFS_MODE_SERVER)
d92ba3d36d 2019-05-08 rkeene: #define XVFS_INTERNAL_SERVER_MAGIC "\xD4\xF3\x05\x96\x25\xCF\xAF\xFE"
d92ba3d36d 2019-05-08 rkeene: #define XVFS_INTERNAL_SERVER_MAGIC_LEN 8
d92ba3d36d 2019-05-08 rkeene:
d92ba3d36d 2019-05-08 rkeene: struct xvfs_tclfs_server_info {
b586d5b0a1 2019-05-08 rkeene: char magic[XVFS_INTERNAL_SERVER_MAGIC_LEN];
d92ba3d36d 2019-05-08 rkeene: int (*registerProc)(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo);
d92ba3d36d 2019-05-08 rkeene: };
d92ba3d36d 2019-05-08 rkeene: #endif /* XVFS_MODE_FLEXIBLE || XVFS_MODE_SERVER */
d92ba3d36d 2019-05-08 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:
149aa89b7d 2019-09-13 rkeene: static const char *xvfs_perror(int xvfs_error) {
149aa89b7d 2019-09-13 rkeene: if (xvfs_error >= 0) {
149aa89b7d 2019-09-13 rkeene: return("Not an error");
149aa89b7d 2019-09-13 rkeene: }
149aa89b7d 2019-09-13 rkeene:
149aa89b7d 2019-09-13 rkeene: switch (xvfs_error) {
149aa89b7d 2019-09-13 rkeene: case XVFS_RV_ERR_ENOENT:
149aa89b7d 2019-09-13 rkeene: return("No such file or directory");
149aa89b7d 2019-09-13 rkeene: case XVFS_RV_ERR_EINVAL:
149aa89b7d 2019-09-13 rkeene: return("Invalid argument");
149aa89b7d 2019-09-13 rkeene: case XVFS_RV_ERR_EISDIR:
149aa89b7d 2019-09-13 rkeene: return("Is a directory");
149aa89b7d 2019-09-13 rkeene: case XVFS_RV_ERR_ENOTDIR:
149aa89b7d 2019-09-13 rkeene: return("Not a directory");
149aa89b7d 2019-09-13 rkeene: case XVFS_RV_ERR_EFAULT:
149aa89b7d 2019-09-13 rkeene: return("Bad address");
149aa89b7d 2019-09-13 rkeene: default:
149aa89b7d 2019-09-13 rkeene: return("Unknown error");
149aa89b7d 2019-09-13 rkeene: }
149aa89b7d 2019-09-13 rkeene: }
149aa89b7d 2019-09-13 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);
149aa89b7d 2019-09-13 rkeene: if (retval < 0) {
149aa89b7d 2019-09-13 rkeene: retval = -1;
149aa89b7d 2019-09-13 rkeene: }
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;
149aa89b7d 2019-09-13 rkeene: Tcl_WideInt length;
149aa89b7d 2019-09-13 rkeene: const char *data;
d121970301 2019-05-02 rkeene:
d121970301 2019-05-02 rkeene: pathStr = xvfs_relativePath(path, instanceInfo);
149aa89b7d 2019-09-13 rkeene:
9d3052c6f1 2019-05-08 rkeene: /*
9d3052c6f1 2019-05-08 rkeene: * XXX:TODO: Do something to create the Tcl_Channel we
9d3052c6f1 2019-05-08 rkeene: * need to return here
9d3052c6f1 2019-05-08 rkeene: */
9d3052c6f1 2019-05-08 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;
b8cca3a6b4 2019-05-08 rkeene: static 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);
e5b6962adf 2019-05-02 rkeene: }
d92ba3d36d 2019-05-08 rkeene: #endif /* XVFS_MODE_STANDALONE || XVFS_MODE_FLEXIBLE */
88f96696b7 2019-05-03 rkeene:
88f96696b7 2019-05-03 rkeene: #if defined(XVFS_MODE_FLEXIBLE)
b8cca3a6b4 2019-05-08 rkeene: static int xvfs_flexible_register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
9bcf758fef 2019-05-08 rkeene: ClientData fsHandlerDataRaw;
9bcf758fef 2019-05-08 rkeene: struct xvfs_tclfs_server_info *fsHandlerData;
9bcf758fef 2019-05-08 rkeene: const Tcl_Filesystem *fsHandler;
9bcf758fef 2019-05-08 rkeene: int (*xvfs_register)(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo);
9bcf758fef 2019-05-08 rkeene: Tcl_Obj *rootPathObj;
9bcf758fef 2019-05-08 rkeene:
9bcf758fef 2019-05-08 rkeene: xvfs_register = &xvfs_standalone_register;
9bcf758fef 2019-05-08 rkeene:
9bcf758fef 2019-05-08 rkeene: rootPathObj = Tcl_NewStringObj(XVFS_ROOT_MOUNTPOINT, -1);
9bcf758fef 2019-05-08 rkeene: if (!rootPathObj) {
9bcf758fef 2019-05-08 rkeene: return(xvfs_register(interp, fsInfo));
9bcf758fef 2019-05-08 rkeene: }
9bcf758fef 2019-05-08 rkeene:
9bcf758fef 2019-05-08 rkeene: Tcl_IncrRefCount(rootPathObj);
9bcf758fef 2019-05-08 rkeene: fsHandler = Tcl_FSGetFileSystemForPath(rootPathObj);
9bcf758fef 2019-05-08 rkeene: Tcl_DecrRefCount(rootPathObj);
9bcf758fef 2019-05-08 rkeene:
9bcf758fef 2019-05-08 rkeene: if (!fsHandler) {
9bcf758fef 2019-05-08 rkeene: return(xvfs_register(interp, fsInfo));
9bcf758fef 2019-05-08 rkeene: }
9bcf758fef 2019-05-08 rkeene:
9bcf758fef 2019-05-08 rkeene: fsHandlerDataRaw = Tcl_FSData(fsHandler);
9bcf758fef 2019-05-08 rkeene: if (!fsHandlerDataRaw) {
9bcf758fef 2019-05-08 rkeene: return(xvfs_register(interp, fsInfo));
9bcf758fef 2019-05-08 rkeene: }
9bcf758fef 2019-05-08 rkeene:
9bcf758fef 2019-05-08 rkeene: fsHandlerData = (struct xvfs_tclfs_server_info *) fsHandlerDataRaw;
88f96696b7 2019-05-03 rkeene:
88f96696b7 2019-05-03 rkeene: /*
9bcf758fef 2019-05-08 rkeene: * XXX:TODO: What is the chance that the handler for //xvfs:/ hold
b586d5b0a1 2019-05-08 rkeene: * client data smaller than XVFS_INTERNAL_SERVER_MAGIC_LEN ?
88f96696b7 2019-05-03 rkeene: */
b586d5b0a1 2019-05-08 rkeene: if (memcmp(fsHandlerData->magic, XVFS_INTERNAL_SERVER_MAGIC, sizeof(fsHandlerData->magic)) == 0) {
9bcf758fef 2019-05-08 rkeene: xvfs_register = fsHandlerData->registerProc;
88f96696b7 2019-05-03 rkeene: }
9bcf758fef 2019-05-08 rkeene:
88f96696b7 2019-05-03 rkeene: return(xvfs_register(interp, fsInfo));
88f96696b7 2019-05-03 rkeene: }
d92ba3d36d 2019-05-08 rkeene: #endif /* XVFS_MODE_FLEXIBLE */
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: }
d92ba3d36d 2019-05-08 rkeene: #endif /* XVFS_MODE_SERVER */