Lines of
xvfs-core.c
from check-in d121970301
that are changed by the sequence of edits moving toward
check-in acfc5037c6:
1: #include <xvfs-core.h>
2: #include <string.h>
3: #include <tcl.h>
4:
5: #define XVFS_ROOT_MOUNTPOINT "//xvfs:/"
6:
7: struct xvfs_tclfs_instance_info {
8: struct Xvfs_FSInfo *fsInfo;
9: Tcl_Obj *mountpoint;
10: };
d121970301 2019-05-02 11: static struct xvfs_tclfs_instance_info xvfs_tclfs_standalone_info;
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->getInfoProc(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:
81: /*
82: * Tcl_Filesystem handlers for the standalone implementation
83: */
84: static int xvfs_tclfs_standalone_pathInFilesystem(Tcl_Obj *path, ClientData *dataPtr) {
85: return(xvfs_tclfs_pathInFilesystem(path, dataPtr, &xvfs_tclfs_standalone_info));
86: }
87:
88: static int xvfs_tclfs_standalone_stat(Tcl_Obj *path, Tcl_StatBuf *statBuf) {
89: return(xvfs_tclfs_stat(path, statBuf, &xvfs_tclfs_standalone_info));
90: }
91:
92: static Tcl_Obj *xvfs_tclfs_standalone_listVolumes(void) {
93: return(xvfs_tclfs_listVolumes(&xvfs_tclfs_standalone_info));
94: }
95:
96: static Tcl_Channel xvfs_tclfs_standalone_openFileChannel(Tcl_Interp *interp, Tcl_Obj *path, int mode, int permissions) {
97: return(xvfs_tclfs_openFileChannel(interp, path, mode, permissions, &xvfs_tclfs_standalone_info));
98: }
99:
100: /*
101: * There are three (3) modes of operation for Xvfs_Register:
102: * 1. standalone -- We register our own Tcl_Filesystem
103: * and handle requests under `//xvfs:/<fsName>`
104: * 2. client -- A single Tcl_Filesystem is registered for the
105: * interp to handle requests under `//xvfs:/` which
106: * then dispatches to the appropriate registered
107: * handler
108: * 3. flexible -- Attempts to find a core Xvfs instance for the
109: * process at runtime, if found do #2, otherwise
110: * fallback to #1
111: *
112: */
d121970301 2019-05-02 113: static int xvfs_standalone_register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
114: Tcl_Filesystem *xvfs_tclfs_Info;
115: int tcl_ret;
116: static int registered = 0;
117:
118: /*
119: * Ensure this instance is not already registered
120: */
121: if (registered) {
122: return(TCL_OK);
123: }
124: registered = 1;
125:
126: /*
127: * In standalone mode, we only support the same protocol we are
128: * compiling for.
129: */
130: if (fsInfo->protocolVersion != XVFS_PROTOCOL_VERSION) {
131: if (interp) {
132: Tcl_SetResult(interp, "Protocol mismatch", NULL);
133: }
134: return(TCL_ERROR);
135: }
136:
137: xvfs_tclfs_Info = (Tcl_Filesystem *) Tcl_AttemptAlloc(sizeof(*xvfs_tclfs_Info));
138: if (!xvfs_tclfs_Info) {
139: if (interp) {
140: Tcl_SetResult(interp, "Unable to allocate Tcl_Filesystem object", NULL);
141: }
142: return(TCL_ERROR);
143: }
144:
145: xvfs_tclfs_Info->typeName = strdup("xvfs");
146: xvfs_tclfs_Info->structureLength = sizeof(*xvfs_tclfs_Info);
147: xvfs_tclfs_Info->version = TCL_FILESYSTEM_VERSION_1;
148: xvfs_tclfs_Info->pathInFilesystemProc = xvfs_tclfs_standalone_pathInFilesystem;
149: xvfs_tclfs_Info->dupInternalRepProc = NULL;
150: xvfs_tclfs_Info->freeInternalRepProc = NULL;
151: xvfs_tclfs_Info->internalToNormalizedProc = NULL;
152: xvfs_tclfs_Info->createInternalRepProc = NULL;
153: xvfs_tclfs_Info->normalizePathProc = NULL;
154: xvfs_tclfs_Info->filesystemPathTypeProc = NULL;
155: xvfs_tclfs_Info->filesystemSeparatorProc = NULL;
156: xvfs_tclfs_Info->statProc = xvfs_tclfs_standalone_stat;
157: xvfs_tclfs_Info->accessProc = NULL;
158: xvfs_tclfs_Info->openFileChannelProc = xvfs_tclfs_standalone_openFileChannel;
159: xvfs_tclfs_Info->matchInDirectoryProc = NULL;
160: xvfs_tclfs_Info->utimeProc = NULL;
161: xvfs_tclfs_Info->linkProc = NULL;
162: xvfs_tclfs_Info->listVolumesProc = xvfs_tclfs_standalone_listVolumes;
163: xvfs_tclfs_Info->fileAttrStringsProc = NULL;
164: xvfs_tclfs_Info->fileAttrsGetProc = NULL;
165: xvfs_tclfs_Info->fileAttrsSetProc = NULL;
166: xvfs_tclfs_Info->createDirectoryProc = NULL;
167: xvfs_tclfs_Info->removeDirectoryProc = NULL;
168: xvfs_tclfs_Info->deleteFileProc = NULL;
169: xvfs_tclfs_Info->copyFileProc = NULL;
170: xvfs_tclfs_Info->renameFileProc = NULL;
171: xvfs_tclfs_Info->copyDirectoryProc = NULL;
172: xvfs_tclfs_Info->lstatProc = NULL;
173: xvfs_tclfs_Info->loadFileProc = NULL;
174: xvfs_tclfs_Info->getCwdProc = NULL;
175: xvfs_tclfs_Info->chdirProc = NULL;
176:
177: xvfs_tclfs_standalone_info.fsInfo = fsInfo;
178: xvfs_tclfs_standalone_info.mountpoint = Tcl_NewObj();
179: Tcl_AppendStringsToObj(xvfs_tclfs_standalone_info.mountpoint, XVFS_ROOT_MOUNTPOINT, fsInfo->name, NULL);
180:
181: tcl_ret = Tcl_FSRegister(NULL, xvfs_tclfs_Info);
182: if (tcl_ret != TCL_OK) {
183: if (interp) {
184: Tcl_SetResult(interp, "Tcl_FSRegister() failed", NULL);
185: }
186:
187: return(tcl_ret);
188: }
189:
190: return(TCL_OK);
191: }
d121970301 2019-05-02 192:
193: int Xvfs_Register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
d121970301 2019-05-02 194: return(xvfs_standalone_register(interp, fsInfo));
195: }