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