69e476dcd5 2019-05-02 1: #include <xvfs-core.h>
d121970301 2019-05-02 2: #include <string.h>
38bed7cee0 2019-09-13 3: #include <sys/stat.h>
38bed7cee0 2019-09-13 4: #include <errno.h>
38bed7cee0 2019-09-13 5: #include <fcntl.h>
69e476dcd5 2019-05-02 6: #include <tcl.h>
d92ba3d36d 2019-05-08 7:
d92ba3d36d 2019-05-08 8: #if defined(XVFS_MODE_FLEXIBLE) || defined(XVFS_MODE_SERVER)
d92ba3d36d 2019-05-08 9: #define XVFS_INTERNAL_SERVER_MAGIC "\xD4\xF3\x05\x96\x25\xCF\xAF\xFE"
d92ba3d36d 2019-05-08 10: #define XVFS_INTERNAL_SERVER_MAGIC_LEN 8
d92ba3d36d 2019-05-08 11:
d92ba3d36d 2019-05-08 12: struct xvfs_tclfs_server_info {
b586d5b0a1 2019-05-08 13: char magic[XVFS_INTERNAL_SERVER_MAGIC_LEN];
d92ba3d36d 2019-05-08 14: int (*registerProc)(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo);
d92ba3d36d 2019-05-08 15: };
d92ba3d36d 2019-05-08 16: #endif /* XVFS_MODE_FLEXIBLE || XVFS_MODE_SERVER */
3e44e1def1 2019-05-02 17:
3e44e1def1 2019-05-02 18: #if defined(XVFS_MODE_SERVER) || defined(XVFS_MODE_STANDALONE) || defined(XVFS_MODE_FLEXIBLE)
d121970301 2019-05-02 19: #define XVFS_ROOT_MOUNTPOINT "//xvfs:/"
d121970301 2019-05-02 20:
d121970301 2019-05-02 21: struct xvfs_tclfs_instance_info {
d121970301 2019-05-02 22: struct Xvfs_FSInfo *fsInfo;
d121970301 2019-05-02 23: Tcl_Obj *mountpoint;
d121970301 2019-05-02 24: };
d121970301 2019-05-02 25:
d121970301 2019-05-02 26: /*
d121970301 2019-05-02 27: * Internal Core Utilities
d121970301 2019-05-02 28: */
d121970301 2019-05-02 29: static const char *xvfs_relativePath(Tcl_Obj *path, struct xvfs_tclfs_instance_info *info) {
d121970301 2019-05-02 30: const char *pathStr, *rootStr;
d121970301 2019-05-02 31: int pathLen, rootLen;
38bed7cee0 2019-09-13 32:
d121970301 2019-05-02 33: pathStr = Tcl_GetStringFromObj(path, &pathLen);
d121970301 2019-05-02 34: rootStr = Tcl_GetStringFromObj(info->mountpoint, &rootLen);
38bed7cee0 2019-09-13 35:
d121970301 2019-05-02 36: if (pathLen < rootLen) {
d121970301 2019-05-02 37: return(NULL);
d121970301 2019-05-02 38: }
38bed7cee0 2019-09-13 39:
d121970301 2019-05-02 40: if (memcmp(pathStr, rootStr, rootLen) != 0) {
d121970301 2019-05-02 41: return(NULL);
d121970301 2019-05-02 42: }
38bed7cee0 2019-09-13 43:
d121970301 2019-05-02 44: if (pathLen == rootLen) {
d121970301 2019-05-02 45: return("");
d121970301 2019-05-02 46: }
d121970301 2019-05-02 47:
d121970301 2019-05-02 48: /* XXX:TODO: Should this use the native OS path separator ? */
d121970301 2019-05-02 49: if (pathStr[rootLen] != '/') {
d121970301 2019-05-02 50: return(NULL);
d121970301 2019-05-02 51: }
38bed7cee0 2019-09-13 52:
d121970301 2019-05-02 53: return(pathStr + rootLen + 1);
149aa89b7d 2019-09-13 54: }
149aa89b7d 2019-09-13 55:
38bed7cee0 2019-09-13 56: #if 0
38bed7cee0 2019-09-13 57: /*
38bed7cee0 2019-09-13 58: * Currently unused
38bed7cee0 2019-09-13 59: */
149aa89b7d 2019-09-13 60: static const char *xvfs_perror(int xvfs_error) {
149aa89b7d 2019-09-13 61: if (xvfs_error >= 0) {
149aa89b7d 2019-09-13 62: return("Not an error");
149aa89b7d 2019-09-13 63: }
149aa89b7d 2019-09-13 64:
149aa89b7d 2019-09-13 65: switch (xvfs_error) {
149aa89b7d 2019-09-13 66: case XVFS_RV_ERR_ENOENT:
149aa89b7d 2019-09-13 67: return("No such file or directory");
149aa89b7d 2019-09-13 68: case XVFS_RV_ERR_EINVAL:
149aa89b7d 2019-09-13 69: return("Invalid argument");
149aa89b7d 2019-09-13 70: case XVFS_RV_ERR_EISDIR:
149aa89b7d 2019-09-13 71: return("Is a directory");
149aa89b7d 2019-09-13 72: case XVFS_RV_ERR_ENOTDIR:
149aa89b7d 2019-09-13 73: return("Not a directory");
149aa89b7d 2019-09-13 74: case XVFS_RV_ERR_EFAULT:
149aa89b7d 2019-09-13 75: return("Bad address");
149aa89b7d 2019-09-13 76: default:
149aa89b7d 2019-09-13 77: return("Unknown error");
149aa89b7d 2019-09-13 78: }
149aa89b7d 2019-09-13 79: }
38bed7cee0 2019-09-13 80: #endif
38bed7cee0 2019-09-13 81:
38bed7cee0 2019-09-13 82: static int xvfs_errorToErrno(int xvfs_error) {
38bed7cee0 2019-09-13 83: if (xvfs_error >= 0) {
38bed7cee0 2019-09-13 84: return(0);
38bed7cee0 2019-09-13 85: }
38bed7cee0 2019-09-13 86:
38bed7cee0 2019-09-13 87: switch (xvfs_error) {
38bed7cee0 2019-09-13 88: case XVFS_RV_ERR_ENOENT:
38bed7cee0 2019-09-13 89: return(ENOENT);
38bed7cee0 2019-09-13 90: case XVFS_RV_ERR_EINVAL:
38bed7cee0 2019-09-13 91: return(EINVAL);
38bed7cee0 2019-09-13 92: case XVFS_RV_ERR_EISDIR:
38bed7cee0 2019-09-13 93: return(EISDIR);
38bed7cee0 2019-09-13 94: case XVFS_RV_ERR_ENOTDIR:
38bed7cee0 2019-09-13 95: return(ENOTDIR);
38bed7cee0 2019-09-13 96: case XVFS_RV_ERR_EFAULT:
38bed7cee0 2019-09-13 97: return(EFAULT);
38bed7cee0 2019-09-13 98: default:
38bed7cee0 2019-09-13 99: return(ERANGE);
38bed7cee0 2019-09-13 100: }
38bed7cee0 2019-09-13 101: }
38bed7cee0 2019-09-13 102:
38bed7cee0 2019-09-13 103: /*
38bed7cee0 2019-09-13 104: * Xvfs Memory Channel
38bed7cee0 2019-09-13 105: */
38bed7cee0 2019-09-13 106: struct xvfs_tclfs_channel_id {
38bed7cee0 2019-09-13 107: Tcl_Channel channel;
38bed7cee0 2019-09-13 108: struct xvfs_tclfs_instance_info *fsInstanceInfo;
38bed7cee0 2019-09-13 109: Tcl_Obj *path;
38bed7cee0 2019-09-13 110: Tcl_WideInt currentOffset;
38bed7cee0 2019-09-13 111: Tcl_WideInt fileSize;
7d74392642 2019-09-13 112: int eofMarked;
7d74392642 2019-09-13 113: };
7d74392642 2019-09-13 114: struct xvfs_tclfs_channel_event {
7d74392642 2019-09-13 115: Tcl_Event tcl;
7d74392642 2019-09-13 116: struct xvfs_tclfs_channel_id *channelInstanceData;
38bed7cee0 2019-09-13 117: };
38bed7cee0 2019-09-13 118: static Tcl_ChannelType xvfs_tclfs_channelType;
38bed7cee0 2019-09-13 119:
38bed7cee0 2019-09-13 120: static Tcl_Channel xvfs_tclfs_openChannel(Tcl_Obj *path, struct xvfs_tclfs_instance_info *instanceInfo) {
38bed7cee0 2019-09-13 121: struct xvfs_tclfs_channel_id *channelInstanceData;
38bed7cee0 2019-09-13 122: Tcl_Channel channel;
38bed7cee0 2019-09-13 123: Tcl_StatBuf fileInfo;
7d74392642 2019-09-13 124: Tcl_Obj *channelName;
38bed7cee0 2019-09-13 125: int statRet;
38bed7cee0 2019-09-13 126:
38bed7cee0 2019-09-13 127: statRet = instanceInfo->fsInfo->getStatProc(Tcl_GetString(path), &fileInfo);
38bed7cee0 2019-09-13 128: if (statRet < 0) {
38bed7cee0 2019-09-13 129: return(NULL);
38bed7cee0 2019-09-13 130: }
38bed7cee0 2019-09-13 131:
38bed7cee0 2019-09-13 132: channelInstanceData = (struct xvfs_tclfs_channel_id *) Tcl_Alloc(sizeof(*channelInstanceData));
38bed7cee0 2019-09-13 133: channelInstanceData->currentOffset = 0;
7d74392642 2019-09-13 134: channelInstanceData->eofMarked = 0;
38bed7cee0 2019-09-13 135: channelInstanceData->channel = NULL;
38bed7cee0 2019-09-13 136:
7d74392642 2019-09-13 137: channelName = Tcl_ObjPrintf("xvfs0x%llx", (unsigned long long) channelInstanceData);
7d74392642 2019-09-13 138: if (!channelName) {
7d74392642 2019-09-13 139: Tcl_Free((char *) channelInstanceData);
7d74392642 2019-09-13 140:
7d74392642 2019-09-13 141: return(NULL);
7d74392642 2019-09-13 142: }
7d74392642 2019-09-13 143: Tcl_IncrRefCount(channelName);
7d74392642 2019-09-13 144:
38bed7cee0 2019-09-13 145: channelInstanceData->fsInstanceInfo = instanceInfo;
38bed7cee0 2019-09-13 146: channelInstanceData->fileSize = fileInfo.st_size;
7d74392642 2019-09-13 147: channelInstanceData->path = path;
38bed7cee0 2019-09-13 148: Tcl_IncrRefCount(path);
38bed7cee0 2019-09-13 149:
7d74392642 2019-09-13 150: channel = Tcl_CreateChannel(&xvfs_tclfs_channelType, Tcl_GetString(channelName), channelInstanceData, TCL_READABLE);
7d74392642 2019-09-13 151: Tcl_DecrRefCount(channelName);
38bed7cee0 2019-09-13 152: if (!channel) {
38bed7cee0 2019-09-13 153: Tcl_DecrRefCount(path);
38bed7cee0 2019-09-13 154: Tcl_Free((char *) channelInstanceData);
38bed7cee0 2019-09-13 155:
38bed7cee0 2019-09-13 156: return(NULL);
38bed7cee0 2019-09-13 157: }
38bed7cee0 2019-09-13 158:
38bed7cee0 2019-09-13 159: channelInstanceData->channel = channel;
38bed7cee0 2019-09-13 160:
38bed7cee0 2019-09-13 161: return(channel);
38bed7cee0 2019-09-13 162: }
38bed7cee0 2019-09-13 163:
38bed7cee0 2019-09-13 164: static int xvfs_tclfs_closeChannel(ClientData channelInstanceData_p, Tcl_Interp *interp) {
38bed7cee0 2019-09-13 165: struct xvfs_tclfs_channel_id *channelInstanceData;
38bed7cee0 2019-09-13 166:
38bed7cee0 2019-09-13 167: channelInstanceData = (struct xvfs_tclfs_channel_id *) channelInstanceData_p;
38bed7cee0 2019-09-13 168:
38bed7cee0 2019-09-13 169: Tcl_DecrRefCount(channelInstanceData->path);
38bed7cee0 2019-09-13 170: Tcl_Free((char *) channelInstanceData);
38bed7cee0 2019-09-13 171:
38bed7cee0 2019-09-13 172: return(0);
38bed7cee0 2019-09-13 173: }
38bed7cee0 2019-09-13 174:
38bed7cee0 2019-09-13 175: static int xvfs_tclfs_readChannel(ClientData channelInstanceData_p, char *buf, int bufSize, int *errorCodePtr) {
38bed7cee0 2019-09-13 176: struct xvfs_tclfs_channel_id *channelInstanceData;
38bed7cee0 2019-09-13 177: const unsigned char *data;
38bed7cee0 2019-09-13 178: Tcl_WideInt offset, length;
38bed7cee0 2019-09-13 179: char *path;
38bed7cee0 2019-09-13 180:
38bed7cee0 2019-09-13 181: channelInstanceData = (struct xvfs_tclfs_channel_id *) channelInstanceData_p;
38bed7cee0 2019-09-13 182:
7d74392642 2019-09-13 183: /*
7d74392642 2019-09-13 184: * If we are already at the end of the file we can skip
7d74392642 2019-09-13 185: * attempting to read it
7d74392642 2019-09-13 186: */
7d74392642 2019-09-13 187: if (channelInstanceData->eofMarked) {
7d74392642 2019-09-13 188: return(0);
7d74392642 2019-09-13 189: }
7d74392642 2019-09-13 190:
38bed7cee0 2019-09-13 191: path = Tcl_GetString(channelInstanceData->path);
38bed7cee0 2019-09-13 192: offset = channelInstanceData->currentOffset;
38bed7cee0 2019-09-13 193: length = bufSize;
38bed7cee0 2019-09-13 194:
38bed7cee0 2019-09-13 195: data = channelInstanceData->fsInstanceInfo->fsInfo->getDataProc(path, offset, &length);
38bed7cee0 2019-09-13 196:
38bed7cee0 2019-09-13 197: if (length < 0) {
38bed7cee0 2019-09-13 198: *errorCodePtr = xvfs_errorToErrno(length);
38bed7cee0 2019-09-13 199:
38bed7cee0 2019-09-13 200: return(-1);
38bed7cee0 2019-09-13 201: }
38bed7cee0 2019-09-13 202:
7d74392642 2019-09-13 203: if (length == 0) {
7d74392642 2019-09-13 204: channelInstanceData->eofMarked = 1;
7d74392642 2019-09-13 205: } else {
7d74392642 2019-09-13 206: memcpy(buf, data, length);
38bed7cee0 2019-09-13 207:
7d74392642 2019-09-13 208: channelInstanceData->currentOffset += length;
7d74392642 2019-09-13 209: }
38bed7cee0 2019-09-13 210:
38bed7cee0 2019-09-13 211: return(length);
38bed7cee0 2019-09-13 212: }
38bed7cee0 2019-09-13 213:
7d74392642 2019-09-13 214: static int xvfs_tclfs_watchChannelEvent(Tcl_Event *event_p, int flags) {
7d74392642 2019-09-13 215: struct xvfs_tclfs_channel_id *channelInstanceData;
7d74392642 2019-09-13 216: struct xvfs_tclfs_channel_event *event;
7d74392642 2019-09-13 217:
7d74392642 2019-09-13 218: event = (struct xvfs_tclfs_channel_event *) event_p;
7d74392642 2019-09-13 219: channelInstanceData = event->channelInstanceData;
7d74392642 2019-09-13 220:
7d74392642 2019-09-13 221: Tcl_NotifyChannel(channelInstanceData->channel, TCL_READABLE);
7d74392642 2019-09-13 222:
7d74392642 2019-09-13 223: return(0);
7d74392642 2019-09-13 224: }
7d74392642 2019-09-13 225:
38bed7cee0 2019-09-13 226: static void xvfs_tclfs_watchChannel(ClientData channelInstanceData_p, int mask) {
7d74392642 2019-09-13 227: struct xvfs_tclfs_channel_id *channelInstanceData;
7d74392642 2019-09-13 228: struct xvfs_tclfs_channel_event *event;
7d74392642 2019-09-13 229:
38bed7cee0 2019-09-13 230: if ((mask & TCL_READABLE) != TCL_READABLE) {
38bed7cee0 2019-09-13 231: return;
38bed7cee0 2019-09-13 232: }
38bed7cee0 2019-09-13 233:
7d74392642 2019-09-13 234: channelInstanceData = (struct xvfs_tclfs_channel_id *) channelInstanceData_p;
7d74392642 2019-09-13 235:
7d74392642 2019-09-13 236: /*
7d74392642 2019-09-13 237: * If the read call has marked that we have reached EOF,
7d74392642 2019-09-13 238: * do not signal any further
7d74392642 2019-09-13 239: */
7d74392642 2019-09-13 240: if (channelInstanceData->eofMarked) {
7d74392642 2019-09-13 241: return;
7d74392642 2019-09-13 242: }
7d74392642 2019-09-13 243:
7d74392642 2019-09-13 244: event = (struct xvfs_tclfs_channel_event *) Tcl_Alloc(sizeof(*event));
7d74392642 2019-09-13 245: event->tcl.proc = xvfs_tclfs_watchChannelEvent;
7d74392642 2019-09-13 246: event->tcl.nextPtr = NULL;
7d74392642 2019-09-13 247: event->channelInstanceData = channelInstanceData;
7d74392642 2019-09-13 248:
7d74392642 2019-09-13 249: Tcl_QueueEvent((Tcl_Event *) event, TCL_QUEUE_TAIL);
7d74392642 2019-09-13 250:
38bed7cee0 2019-09-13 251: return;
38bed7cee0 2019-09-13 252: }
38bed7cee0 2019-09-13 253:
38bed7cee0 2019-09-13 254: static int xvfs_tclfs_seekChannel(ClientData channelInstanceData_p, long offset, int mode, int *errorCodePtr) {
38bed7cee0 2019-09-13 255: struct xvfs_tclfs_channel_id *channelInstanceData;
38bed7cee0 2019-09-13 256: Tcl_WideInt newOffset, fileSize;
38bed7cee0 2019-09-13 257:
38bed7cee0 2019-09-13 258: channelInstanceData = (struct xvfs_tclfs_channel_id *) channelInstanceData_p;
38bed7cee0 2019-09-13 259:
38bed7cee0 2019-09-13 260: newOffset = channelInstanceData->currentOffset;
38bed7cee0 2019-09-13 261: fileSize = channelInstanceData->fileSize;
38bed7cee0 2019-09-13 262:
38bed7cee0 2019-09-13 263: switch (mode) {
38bed7cee0 2019-09-13 264: case SEEK_CUR:
38bed7cee0 2019-09-13 265: newOffset += offset;
38bed7cee0 2019-09-13 266: break;
38bed7cee0 2019-09-13 267: case SEEK_SET:
38bed7cee0 2019-09-13 268: newOffset = offset;
38bed7cee0 2019-09-13 269: break;
38bed7cee0 2019-09-13 270: case SEEK_END:
38bed7cee0 2019-09-13 271: newOffset = fileSize + offset;
38bed7cee0 2019-09-13 272: break;
38bed7cee0 2019-09-13 273: default:
38bed7cee0 2019-09-13 274: *errorCodePtr = xvfs_errorToErrno(XVFS_RV_ERR_EINVAL);
38bed7cee0 2019-09-13 275:
38bed7cee0 2019-09-13 276: return(-1);
38bed7cee0 2019-09-13 277: }
38bed7cee0 2019-09-13 278:
38bed7cee0 2019-09-13 279: /*
38bed7cee0 2019-09-13 280: * We allow users to seek right up to the end of the buffer, but
38bed7cee0 2019-09-13 281: * no further, this way if they want to seek backwards from there
38bed7cee0 2019-09-13 282: * it is possible to do so.
38bed7cee0 2019-09-13 283: */
38bed7cee0 2019-09-13 284: if (newOffset < 0 || newOffset > fileSize) {
38bed7cee0 2019-09-13 285: *errorCodePtr = xvfs_errorToErrno(XVFS_RV_ERR_EINVAL);
38bed7cee0 2019-09-13 286:
38bed7cee0 2019-09-13 287: return(-1);
38bed7cee0 2019-09-13 288: }
38bed7cee0 2019-09-13 289:
7d74392642 2019-09-13 290: if (newOffset != channelInstanceData->currentOffset) {
7d74392642 2019-09-13 291: channelInstanceData->eofMarked = 0;
7d74392642 2019-09-13 292: channelInstanceData->currentOffset = newOffset;
7d74392642 2019-09-13 293: }
38bed7cee0 2019-09-13 294:
38bed7cee0 2019-09-13 295: return(channelInstanceData->currentOffset);
38bed7cee0 2019-09-13 296: }
38bed7cee0 2019-09-13 297:
38bed7cee0 2019-09-13 298: static void xvfs_tclfs_prepareChannelType(void) {
38bed7cee0 2019-09-13 299: xvfs_tclfs_channelType.typeName = "xvfs";
38bed7cee0 2019-09-13 300: xvfs_tclfs_channelType.version = TCL_CHANNEL_VERSION_2;
38bed7cee0 2019-09-13 301: xvfs_tclfs_channelType.closeProc = xvfs_tclfs_closeChannel;
38bed7cee0 2019-09-13 302: xvfs_tclfs_channelType.inputProc = xvfs_tclfs_readChannel;
38bed7cee0 2019-09-13 303: xvfs_tclfs_channelType.outputProc = NULL;
38bed7cee0 2019-09-13 304: xvfs_tclfs_channelType.watchProc = xvfs_tclfs_watchChannel;
38bed7cee0 2019-09-13 305: xvfs_tclfs_channelType.getHandleProc = NULL;
38bed7cee0 2019-09-13 306: xvfs_tclfs_channelType.seekProc = xvfs_tclfs_seekChannel;
38bed7cee0 2019-09-13 307: xvfs_tclfs_channelType.setOptionProc = NULL;
38bed7cee0 2019-09-13 308: xvfs_tclfs_channelType.getOptionProc = NULL;
38bed7cee0 2019-09-13 309: xvfs_tclfs_channelType.close2Proc = NULL;
38bed7cee0 2019-09-13 310: xvfs_tclfs_channelType.blockModeProc = NULL;
38bed7cee0 2019-09-13 311: xvfs_tclfs_channelType.flushProc = NULL;
38bed7cee0 2019-09-13 312: xvfs_tclfs_channelType.handlerProc = NULL;
38bed7cee0 2019-09-13 313: xvfs_tclfs_channelType.wideSeekProc = NULL;
38bed7cee0 2019-09-13 314: xvfs_tclfs_channelType.threadActionProc = NULL;
38bed7cee0 2019-09-13 315: xvfs_tclfs_channelType.truncateProc = NULL;
38bed7cee0 2019-09-13 316: }
d121970301 2019-05-02 317:
d121970301 2019-05-02 318: /*
d121970301 2019-05-02 319: * Internal Tcl_Filesystem functions, with the appropriate instance info
d121970301 2019-05-02 320: */
d121970301 2019-05-02 321: static int xvfs_tclfs_pathInFilesystem(Tcl_Obj *path, ClientData *dataPtr, struct xvfs_tclfs_instance_info *instanceInfo) {
d121970301 2019-05-02 322: const char *relativePath;
38bed7cee0 2019-09-13 323:
d121970301 2019-05-02 324: relativePath = xvfs_relativePath(path, instanceInfo);
d121970301 2019-05-02 325: if (!relativePath) {
d121970301 2019-05-02 326: return(-1);
d121970301 2019-05-02 327: }
38bed7cee0 2019-09-13 328:
d121970301 2019-05-02 329: return(TCL_OK);
d121970301 2019-05-02 330: }
d121970301 2019-05-02 331:
d121970301 2019-05-02 332: static int xvfs_tclfs_stat(Tcl_Obj *path, Tcl_StatBuf *statBuf, struct xvfs_tclfs_instance_info *instanceInfo) {
d121970301 2019-05-02 333: const char *pathStr;
d121970301 2019-05-02 334: int retval;
d121970301 2019-05-02 335:
d121970301 2019-05-02 336: pathStr = xvfs_relativePath(path, instanceInfo);
38bed7cee0 2019-09-13 337:
daf25f5222 2019-05-03 338: retval = instanceInfo->fsInfo->getStatProc(pathStr, statBuf);
149aa89b7d 2019-09-13 339: if (retval < 0) {
149aa89b7d 2019-09-13 340: retval = -1;
149aa89b7d 2019-09-13 341: }
38bed7cee0 2019-09-13 342:
d121970301 2019-05-02 343: return(retval);
d121970301 2019-05-02 344: }
d121970301 2019-05-02 345:
d121970301 2019-05-02 346: static Tcl_Obj *xvfs_tclfs_listVolumes(struct xvfs_tclfs_instance_info *instanceInfo) {
d121970301 2019-05-02 347: return(NULL);
d121970301 2019-05-02 348: }
d121970301 2019-05-02 349:
d121970301 2019-05-02 350: 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 351: const char *pathStr;
d121970301 2019-05-02 352:
d121970301 2019-05-02 353: pathStr = xvfs_relativePath(path, instanceInfo);
149aa89b7d 2019-09-13 354:
38bed7cee0 2019-09-13 355: if (mode & O_WRONLY) {
38bed7cee0 2019-09-13 356: return(NULL);
38bed7cee0 2019-09-13 357: }
9d3052c6f1 2019-05-08 358:
38bed7cee0 2019-09-13 359: return(xvfs_tclfs_openChannel(Tcl_NewStringObj(pathStr, -1), instanceInfo));
d121970301 2019-05-02 360: }
3e44e1def1 2019-05-02 361: #endif /* XVFS_MODE_SERVER || XVFS_MODE_STANDALONE || XVFS_MODE_FLEIXBLE */
d121970301 2019-05-02 362:
88f96696b7 2019-05-03 363: #if defined(XVFS_MODE_STANDALONE) || defined(XVFS_MODE_FLEXIBLE)
d121970301 2019-05-02 364: /*
d121970301 2019-05-02 365: * Tcl_Filesystem handlers for the standalone implementation
d121970301 2019-05-02 366: */
acfc5037c6 2019-05-02 367: static struct xvfs_tclfs_instance_info xvfs_tclfs_standalone_info;
d121970301 2019-05-02 368: static int xvfs_tclfs_standalone_pathInFilesystem(Tcl_Obj *path, ClientData *dataPtr) {
d121970301 2019-05-02 369: return(xvfs_tclfs_pathInFilesystem(path, dataPtr, &xvfs_tclfs_standalone_info));
d121970301 2019-05-02 370: }
d121970301 2019-05-02 371:
d121970301 2019-05-02 372: static int xvfs_tclfs_standalone_stat(Tcl_Obj *path, Tcl_StatBuf *statBuf) {
d121970301 2019-05-02 373: return(xvfs_tclfs_stat(path, statBuf, &xvfs_tclfs_standalone_info));
e5b6962adf 2019-05-02 374: }
e5b6962adf 2019-05-02 375:
d121970301 2019-05-02 376: static Tcl_Obj *xvfs_tclfs_standalone_listVolumes(void) {
d121970301 2019-05-02 377: return(xvfs_tclfs_listVolumes(&xvfs_tclfs_standalone_info));
e5b6962adf 2019-05-02 378: }
e5b6962adf 2019-05-02 379:
d121970301 2019-05-02 380: static Tcl_Channel xvfs_tclfs_standalone_openFileChannel(Tcl_Interp *interp, Tcl_Obj *path, int mode, int permissions) {
d121970301 2019-05-02 381: return(xvfs_tclfs_openFileChannel(interp, path, mode, permissions, &xvfs_tclfs_standalone_info));
e5b6962adf 2019-05-02 382: }
32b55a907b 2019-05-02 383:
32b55a907b 2019-05-02 384: /*
32b55a907b 2019-05-02 385: * There are three (3) modes of operation for Xvfs_Register:
32b55a907b 2019-05-02 386: * 1. standalone -- We register our own Tcl_Filesystem
32b55a907b 2019-05-02 387: * and handle requests under `//xvfs:/<fsName>`
32b55a907b 2019-05-02 388: * 2. client -- A single Tcl_Filesystem is registered for the
32b55a907b 2019-05-02 389: * interp to handle requests under `//xvfs:/` which
32b55a907b 2019-05-02 390: * then dispatches to the appropriate registered
32b55a907b 2019-05-02 391: * handler
32b55a907b 2019-05-02 392: * 3. flexible -- Attempts to find a core Xvfs instance for the
32b55a907b 2019-05-02 393: * process at runtime, if found do #2, otherwise
32b55a907b 2019-05-02 394: * fallback to #1
32b55a907b 2019-05-02 395: *
32b55a907b 2019-05-02 396: */
cb77ecfb24 2019-05-06 397: static Tcl_Filesystem xvfs_tclfs_standalone_fs;
b8cca3a6b4 2019-05-08 398: static int xvfs_standalone_register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
e5b6962adf 2019-05-02 399: int tcl_ret;
d121970301 2019-05-02 400: static int registered = 0;
38bed7cee0 2019-09-13 401:
d121970301 2019-05-02 402: /*
d121970301 2019-05-02 403: * Ensure this instance is not already registered
d121970301 2019-05-02 404: */
d121970301 2019-05-02 405: if (registered) {
d121970301 2019-05-02 406: return(TCL_OK);
d121970301 2019-05-02 407: }
d121970301 2019-05-02 408: registered = 1;
d121970301 2019-05-02 409:
e5b6962adf 2019-05-02 410: /*
e5b6962adf 2019-05-02 411: * In standalone mode, we only support the same protocol we are
e5b6962adf 2019-05-02 412: * compiling for.
e5b6962adf 2019-05-02 413: */
e5b6962adf 2019-05-02 414: if (fsInfo->protocolVersion != XVFS_PROTOCOL_VERSION) {
e5b6962adf 2019-05-02 415: if (interp) {
e5b6962adf 2019-05-02 416: Tcl_SetResult(interp, "Protocol mismatch", NULL);
e5b6962adf 2019-05-02 417: }
e5b6962adf 2019-05-02 418: return(TCL_ERROR);
e5b6962adf 2019-05-02 419: }
38bed7cee0 2019-09-13 420:
cb77ecfb24 2019-05-06 421: xvfs_tclfs_standalone_fs.typeName = "xvfs";
cb77ecfb24 2019-05-06 422: xvfs_tclfs_standalone_fs.structureLength = sizeof(xvfs_tclfs_standalone_fs);
cb77ecfb24 2019-05-06 423: xvfs_tclfs_standalone_fs.version = TCL_FILESYSTEM_VERSION_1;
cb77ecfb24 2019-05-06 424: xvfs_tclfs_standalone_fs.pathInFilesystemProc = xvfs_tclfs_standalone_pathInFilesystem;
cb77ecfb24 2019-05-06 425: xvfs_tclfs_standalone_fs.dupInternalRepProc = NULL;
cb77ecfb24 2019-05-06 426: xvfs_tclfs_standalone_fs.freeInternalRepProc = NULL;
cb77ecfb24 2019-05-06 427: xvfs_tclfs_standalone_fs.internalToNormalizedProc = NULL;
cb77ecfb24 2019-05-06 428: xvfs_tclfs_standalone_fs.createInternalRepProc = NULL;
cb77ecfb24 2019-05-06 429: xvfs_tclfs_standalone_fs.normalizePathProc = NULL;
cb77ecfb24 2019-05-06 430: xvfs_tclfs_standalone_fs.filesystemPathTypeProc = NULL;
cb77ecfb24 2019-05-06 431: xvfs_tclfs_standalone_fs.filesystemSeparatorProc = NULL;
cb77ecfb24 2019-05-06 432: xvfs_tclfs_standalone_fs.statProc = xvfs_tclfs_standalone_stat;
cb77ecfb24 2019-05-06 433: xvfs_tclfs_standalone_fs.accessProc = NULL;
cb77ecfb24 2019-05-06 434: xvfs_tclfs_standalone_fs.openFileChannelProc = xvfs_tclfs_standalone_openFileChannel;
38bed7cee0 2019-09-13 435: xvfs_tclfs_standalone_fs.matchInDirectoryProc = NULL; /* XXX:TODO */
cb77ecfb24 2019-05-06 436: xvfs_tclfs_standalone_fs.utimeProc = NULL;
cb77ecfb24 2019-05-06 437: xvfs_tclfs_standalone_fs.linkProc = NULL;
cb77ecfb24 2019-05-06 438: xvfs_tclfs_standalone_fs.listVolumesProc = xvfs_tclfs_standalone_listVolumes;
cb77ecfb24 2019-05-06 439: xvfs_tclfs_standalone_fs.fileAttrStringsProc = NULL;
cb77ecfb24 2019-05-06 440: xvfs_tclfs_standalone_fs.fileAttrsGetProc = NULL;
cb77ecfb24 2019-05-06 441: xvfs_tclfs_standalone_fs.fileAttrsSetProc = NULL;
cb77ecfb24 2019-05-06 442: xvfs_tclfs_standalone_fs.createDirectoryProc = NULL;
cb77ecfb24 2019-05-06 443: xvfs_tclfs_standalone_fs.removeDirectoryProc = NULL;
cb77ecfb24 2019-05-06 444: xvfs_tclfs_standalone_fs.deleteFileProc = NULL;
cb77ecfb24 2019-05-06 445: xvfs_tclfs_standalone_fs.copyFileProc = NULL;
cb77ecfb24 2019-05-06 446: xvfs_tclfs_standalone_fs.renameFileProc = NULL;
cb77ecfb24 2019-05-06 447: xvfs_tclfs_standalone_fs.copyDirectoryProc = NULL;
cb77ecfb24 2019-05-06 448: xvfs_tclfs_standalone_fs.lstatProc = NULL;
cb77ecfb24 2019-05-06 449: xvfs_tclfs_standalone_fs.loadFileProc = NULL;
cb77ecfb24 2019-05-06 450: xvfs_tclfs_standalone_fs.getCwdProc = NULL;
cb77ecfb24 2019-05-06 451: xvfs_tclfs_standalone_fs.chdirProc = NULL;
d121970301 2019-05-02 452:
d121970301 2019-05-02 453: xvfs_tclfs_standalone_info.fsInfo = fsInfo;
d121970301 2019-05-02 454: xvfs_tclfs_standalone_info.mountpoint = Tcl_NewObj();
d121970301 2019-05-02 455: Tcl_AppendStringsToObj(xvfs_tclfs_standalone_info.mountpoint, XVFS_ROOT_MOUNTPOINT, fsInfo->name, NULL);
38bed7cee0 2019-09-13 456:
cb77ecfb24 2019-05-06 457: tcl_ret = Tcl_FSRegister(NULL, &xvfs_tclfs_standalone_fs);
e5b6962adf 2019-05-02 458: if (tcl_ret != TCL_OK) {
e5b6962adf 2019-05-02 459: if (interp) {
e5b6962adf 2019-05-02 460: Tcl_SetResult(interp, "Tcl_FSRegister() failed", NULL);
e5b6962adf 2019-05-02 461: }
38bed7cee0 2019-09-13 462:
e5b6962adf 2019-05-02 463: return(tcl_ret);
e5b6962adf 2019-05-02 464: }
38bed7cee0 2019-09-13 465:
38bed7cee0 2019-09-13 466: xvfs_tclfs_prepareChannelType();
38bed7cee0 2019-09-13 467:
e5b6962adf 2019-05-02 468: return(TCL_OK);
e5b6962adf 2019-05-02 469: }
d92ba3d36d 2019-05-08 470: #endif /* XVFS_MODE_STANDALONE || XVFS_MODE_FLEXIBLE */
88f96696b7 2019-05-03 471:
88f96696b7 2019-05-03 472: #if defined(XVFS_MODE_FLEXIBLE)
b8cca3a6b4 2019-05-08 473: static int xvfs_flexible_register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
9bcf758fef 2019-05-08 474: ClientData fsHandlerDataRaw;
9bcf758fef 2019-05-08 475: struct xvfs_tclfs_server_info *fsHandlerData;
9bcf758fef 2019-05-08 476: const Tcl_Filesystem *fsHandler;
9bcf758fef 2019-05-08 477: int (*xvfs_register)(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo);
9bcf758fef 2019-05-08 478: Tcl_Obj *rootPathObj;
9bcf758fef 2019-05-08 479:
9bcf758fef 2019-05-08 480: xvfs_register = &xvfs_standalone_register;
9bcf758fef 2019-05-08 481:
9bcf758fef 2019-05-08 482: rootPathObj = Tcl_NewStringObj(XVFS_ROOT_MOUNTPOINT, -1);
9bcf758fef 2019-05-08 483: if (!rootPathObj) {
9bcf758fef 2019-05-08 484: return(xvfs_register(interp, fsInfo));
9bcf758fef 2019-05-08 485: }
9bcf758fef 2019-05-08 486:
9bcf758fef 2019-05-08 487: Tcl_IncrRefCount(rootPathObj);
9bcf758fef 2019-05-08 488: fsHandler = Tcl_FSGetFileSystemForPath(rootPathObj);
9bcf758fef 2019-05-08 489: Tcl_DecrRefCount(rootPathObj);
9bcf758fef 2019-05-08 490:
9bcf758fef 2019-05-08 491: if (!fsHandler) {
9bcf758fef 2019-05-08 492: return(xvfs_register(interp, fsInfo));
9bcf758fef 2019-05-08 493: }
9bcf758fef 2019-05-08 494:
9bcf758fef 2019-05-08 495: fsHandlerDataRaw = Tcl_FSData(fsHandler);
9bcf758fef 2019-05-08 496: if (!fsHandlerDataRaw) {
9bcf758fef 2019-05-08 497: return(xvfs_register(interp, fsInfo));
9bcf758fef 2019-05-08 498: }
9bcf758fef 2019-05-08 499:
9bcf758fef 2019-05-08 500: fsHandlerData = (struct xvfs_tclfs_server_info *) fsHandlerDataRaw;
88f96696b7 2019-05-03 501:
88f96696b7 2019-05-03 502: /*
9bcf758fef 2019-05-08 503: * XXX:TODO: What is the chance that the handler for //xvfs:/ hold
b586d5b0a1 2019-05-08 504: * client data smaller than XVFS_INTERNAL_SERVER_MAGIC_LEN ?
88f96696b7 2019-05-03 505: */
b586d5b0a1 2019-05-08 506: if (memcmp(fsHandlerData->magic, XVFS_INTERNAL_SERVER_MAGIC, sizeof(fsHandlerData->magic)) == 0) {
9bcf758fef 2019-05-08 507: xvfs_register = fsHandlerData->registerProc;
88f96696b7 2019-05-03 508: }
9bcf758fef 2019-05-08 509:
88f96696b7 2019-05-03 510: return(xvfs_register(interp, fsInfo));
88f96696b7 2019-05-03 511: }
d92ba3d36d 2019-05-08 512: #endif /* XVFS_MODE_FLEXIBLE */
3e44e1def1 2019-05-02 513:
3e44e1def1 2019-05-02 514: #if defined(XVFS_MODE_SERVER)
e5b6962adf 2019-05-02 515: int Xvfs_Register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
acfc5037c6 2019-05-02 516: return(TCL_ERROR);
69e476dcd5 2019-05-02 517: }
d92ba3d36d 2019-05-08 518: #endif /* XVFS_MODE_SERVER */