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