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