Lines of
xvfs-core.c
from check-in 7d74392642
that are changed by the sequence of edits moving toward
check-in aa08a4a749:
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: int eofMarked;
113: };
114: struct xvfs_tclfs_channel_event {
115: Tcl_Event tcl;
116: struct xvfs_tclfs_channel_id *channelInstanceData;
117: };
118: static Tcl_ChannelType xvfs_tclfs_channelType;
119:
120: static Tcl_Channel xvfs_tclfs_openChannel(Tcl_Obj *path, struct xvfs_tclfs_instance_info *instanceInfo) {
121: struct xvfs_tclfs_channel_id *channelInstanceData;
122: Tcl_Channel channel;
123: Tcl_StatBuf fileInfo;
124: Tcl_Obj *channelName;
125: int statRet;
126:
127: statRet = instanceInfo->fsInfo->getStatProc(Tcl_GetString(path), &fileInfo);
128: if (statRet < 0) {
129: return(NULL);
130: }
131:
132: channelInstanceData = (struct xvfs_tclfs_channel_id *) Tcl_Alloc(sizeof(*channelInstanceData));
133: channelInstanceData->currentOffset = 0;
134: channelInstanceData->eofMarked = 0;
135: channelInstanceData->channel = NULL;
136:
137: channelName = Tcl_ObjPrintf("xvfs0x%llx", (unsigned long long) channelInstanceData);
138: if (!channelName) {
139: Tcl_Free((char *) channelInstanceData);
140:
141: return(NULL);
142: }
143: Tcl_IncrRefCount(channelName);
144:
145: channelInstanceData->fsInstanceInfo = instanceInfo;
146: channelInstanceData->fileSize = fileInfo.st_size;
147: channelInstanceData->path = path;
148: Tcl_IncrRefCount(path);
149:
150: channel = Tcl_CreateChannel(&xvfs_tclfs_channelType, Tcl_GetString(channelName), channelInstanceData, TCL_READABLE);
151: Tcl_DecrRefCount(channelName);
152: if (!channel) {
153: Tcl_DecrRefCount(path);
154: Tcl_Free((char *) channelInstanceData);
155:
156: return(NULL);
157: }
158:
159: channelInstanceData->channel = channel;
160:
161: return(channel);
162: }
163:
164: static int xvfs_tclfs_closeChannel(ClientData channelInstanceData_p, Tcl_Interp *interp) {
165: struct xvfs_tclfs_channel_id *channelInstanceData;
166:
167: channelInstanceData = (struct xvfs_tclfs_channel_id *) channelInstanceData_p;
168:
169: Tcl_DecrRefCount(channelInstanceData->path);
170: Tcl_Free((char *) channelInstanceData);
171:
172: return(0);
173: }
174:
175: static int xvfs_tclfs_readChannel(ClientData channelInstanceData_p, char *buf, int bufSize, int *errorCodePtr) {
176: struct xvfs_tclfs_channel_id *channelInstanceData;
177: const unsigned char *data;
178: Tcl_WideInt offset, length;
179: char *path;
180:
181: channelInstanceData = (struct xvfs_tclfs_channel_id *) channelInstanceData_p;
182:
183: /*
184: * If we are already at the end of the file we can skip
185: * attempting to read it
186: */
187: if (channelInstanceData->eofMarked) {
188: return(0);
189: }
190:
191: path = Tcl_GetString(channelInstanceData->path);
192: offset = channelInstanceData->currentOffset;
193: length = bufSize;
194:
195: data = channelInstanceData->fsInstanceInfo->fsInfo->getDataProc(path, offset, &length);
196:
197: if (length < 0) {
198: *errorCodePtr = xvfs_errorToErrno(length);
199:
200: return(-1);
201: }
202:
203: if (length == 0) {
204: channelInstanceData->eofMarked = 1;
205: } else {
206: memcpy(buf, data, length);
207:
208: channelInstanceData->currentOffset += length;
209: }
210:
211: return(length);
212: }
213:
214: static int xvfs_tclfs_watchChannelEvent(Tcl_Event *event_p, int flags) {
215: struct xvfs_tclfs_channel_id *channelInstanceData;
216: struct xvfs_tclfs_channel_event *event;
217:
218: event = (struct xvfs_tclfs_channel_event *) event_p;
219: channelInstanceData = event->channelInstanceData;
220:
221: Tcl_NotifyChannel(channelInstanceData->channel, TCL_READABLE);
222:
7d74392642 2019-09-13 223: return(0);
224: }
225:
226: static void xvfs_tclfs_watchChannel(ClientData channelInstanceData_p, int mask) {
227: struct xvfs_tclfs_channel_id *channelInstanceData;
228: struct xvfs_tclfs_channel_event *event;
229:
230: if ((mask & TCL_READABLE) != TCL_READABLE) {
231: return;
232: }
233:
234: channelInstanceData = (struct xvfs_tclfs_channel_id *) channelInstanceData_p;
235:
236: /*
237: * If the read call has marked that we have reached EOF,
238: * do not signal any further
239: */
240: if (channelInstanceData->eofMarked) {
241: return;
242: }
243:
244: event = (struct xvfs_tclfs_channel_event *) Tcl_Alloc(sizeof(*event));
245: event->tcl.proc = xvfs_tclfs_watchChannelEvent;
246: event->tcl.nextPtr = NULL;
247: event->channelInstanceData = channelInstanceData;
248:
249: Tcl_QueueEvent((Tcl_Event *) event, TCL_QUEUE_TAIL);
250:
251: return;
252: }
253:
254: static int xvfs_tclfs_seekChannel(ClientData channelInstanceData_p, long offset, int mode, int *errorCodePtr) {
255: struct xvfs_tclfs_channel_id *channelInstanceData;
256: Tcl_WideInt newOffset, fileSize;
257:
258: channelInstanceData = (struct xvfs_tclfs_channel_id *) channelInstanceData_p;
259:
260: newOffset = channelInstanceData->currentOffset;
261: fileSize = channelInstanceData->fileSize;
262:
263: switch (mode) {
264: case SEEK_CUR:
265: newOffset += offset;
266: break;
267: case SEEK_SET:
268: newOffset = offset;
269: break;
270: case SEEK_END:
271: newOffset = fileSize + offset;
272: break;
273: default:
274: *errorCodePtr = xvfs_errorToErrno(XVFS_RV_ERR_EINVAL);
275:
276: return(-1);
277: }
278:
279: /*
280: * We allow users to seek right up to the end of the buffer, but
281: * no further, this way if they want to seek backwards from there
282: * it is possible to do so.
283: */
284: if (newOffset < 0 || newOffset > fileSize) {
285: *errorCodePtr = xvfs_errorToErrno(XVFS_RV_ERR_EINVAL);
286:
287: return(-1);
288: }
289:
290: if (newOffset != channelInstanceData->currentOffset) {
291: channelInstanceData->eofMarked = 0;
292: channelInstanceData->currentOffset = newOffset;
293: }
294:
295: return(channelInstanceData->currentOffset);
296: }
297:
298: static void xvfs_tclfs_prepareChannelType(void) {
299: xvfs_tclfs_channelType.typeName = "xvfs";
300: xvfs_tclfs_channelType.version = TCL_CHANNEL_VERSION_2;
301: xvfs_tclfs_channelType.closeProc = xvfs_tclfs_closeChannel;
302: xvfs_tclfs_channelType.inputProc = xvfs_tclfs_readChannel;
303: xvfs_tclfs_channelType.outputProc = NULL;
304: xvfs_tclfs_channelType.watchProc = xvfs_tclfs_watchChannel;
305: xvfs_tclfs_channelType.getHandleProc = NULL;
306: xvfs_tclfs_channelType.seekProc = xvfs_tclfs_seekChannel;
307: xvfs_tclfs_channelType.setOptionProc = NULL;
308: xvfs_tclfs_channelType.getOptionProc = NULL;
309: xvfs_tclfs_channelType.close2Proc = NULL;
310: xvfs_tclfs_channelType.blockModeProc = NULL;
311: xvfs_tclfs_channelType.flushProc = NULL;
312: xvfs_tclfs_channelType.handlerProc = NULL;
313: xvfs_tclfs_channelType.wideSeekProc = NULL;
314: xvfs_tclfs_channelType.threadActionProc = NULL;
315: xvfs_tclfs_channelType.truncateProc = NULL;
316: }
317:
318: /*
319: * Internal Tcl_Filesystem functions, with the appropriate instance info
320: */
321: static int xvfs_tclfs_pathInFilesystem(Tcl_Obj *path, ClientData *dataPtr, struct xvfs_tclfs_instance_info *instanceInfo) {
322: const char *relativePath;
323:
324: relativePath = xvfs_relativePath(path, instanceInfo);
325: if (!relativePath) {
326: return(-1);
327: }
328:
329: return(TCL_OK);
330: }
331:
332: static int xvfs_tclfs_stat(Tcl_Obj *path, Tcl_StatBuf *statBuf, struct xvfs_tclfs_instance_info *instanceInfo) {
333: const char *pathStr;
334: int retval;
335:
336: pathStr = xvfs_relativePath(path, instanceInfo);
337:
338: retval = instanceInfo->fsInfo->getStatProc(pathStr, statBuf);
339: if (retval < 0) {
340: retval = -1;
341: }
342:
343: return(retval);
344: }
345:
346: static Tcl_Obj *xvfs_tclfs_listVolumes(struct xvfs_tclfs_instance_info *instanceInfo) {
347: return(NULL);
348: }
349:
350: static Tcl_Channel xvfs_tclfs_openFileChannel(Tcl_Interp *interp, Tcl_Obj *path, int mode, int permissions, struct xvfs_tclfs_instance_info *instanceInfo) {
351: const char *pathStr;
352:
353: pathStr = xvfs_relativePath(path, instanceInfo);
354:
355: if (mode & O_WRONLY) {
356: return(NULL);
357: }
358:
359: return(xvfs_tclfs_openChannel(Tcl_NewStringObj(pathStr, -1), instanceInfo));
360: }
361: #endif /* XVFS_MODE_SERVER || XVFS_MODE_STANDALONE || XVFS_MODE_FLEIXBLE */
362:
363: #if defined(XVFS_MODE_STANDALONE) || defined(XVFS_MODE_FLEXIBLE)
364: /*
365: * Tcl_Filesystem handlers for the standalone implementation
366: */
367: static struct xvfs_tclfs_instance_info xvfs_tclfs_standalone_info;
368: static int xvfs_tclfs_standalone_pathInFilesystem(Tcl_Obj *path, ClientData *dataPtr) {
369: return(xvfs_tclfs_pathInFilesystem(path, dataPtr, &xvfs_tclfs_standalone_info));
370: }
371:
372: static int xvfs_tclfs_standalone_stat(Tcl_Obj *path, Tcl_StatBuf *statBuf) {
373: return(xvfs_tclfs_stat(path, statBuf, &xvfs_tclfs_standalone_info));
374: }
375:
376: static Tcl_Obj *xvfs_tclfs_standalone_listVolumes(void) {
377: return(xvfs_tclfs_listVolumes(&xvfs_tclfs_standalone_info));
378: }
379:
380: static Tcl_Channel xvfs_tclfs_standalone_openFileChannel(Tcl_Interp *interp, Tcl_Obj *path, int mode, int permissions) {
381: return(xvfs_tclfs_openFileChannel(interp, path, mode, permissions, &xvfs_tclfs_standalone_info));
382: }
383:
384: /*
385: * There are three (3) modes of operation for Xvfs_Register:
386: * 1. standalone -- We register our own Tcl_Filesystem
387: * and handle requests under `//xvfs:/<fsName>`
388: * 2. client -- A single Tcl_Filesystem is registered for the
389: * interp to handle requests under `//xvfs:/` which
390: * then dispatches to the appropriate registered
391: * handler
392: * 3. flexible -- Attempts to find a core Xvfs instance for the
393: * process at runtime, if found do #2, otherwise
394: * fallback to #1
395: *
396: */
397: static Tcl_Filesystem xvfs_tclfs_standalone_fs;
398: static int xvfs_standalone_register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
399: int tcl_ret;
400: static int registered = 0;
401:
402: /*
403: * Ensure this instance is not already registered
404: */
405: if (registered) {
406: return(TCL_OK);
407: }
408: registered = 1;
409:
410: /*
411: * In standalone mode, we only support the same protocol we are
412: * compiling for.
413: */
414: if (fsInfo->protocolVersion != XVFS_PROTOCOL_VERSION) {
415: if (interp) {
416: Tcl_SetResult(interp, "Protocol mismatch", NULL);
417: }
418: return(TCL_ERROR);
419: }
420:
421: xvfs_tclfs_standalone_fs.typeName = "xvfs";
422: xvfs_tclfs_standalone_fs.structureLength = sizeof(xvfs_tclfs_standalone_fs);
423: xvfs_tclfs_standalone_fs.version = TCL_FILESYSTEM_VERSION_1;
424: xvfs_tclfs_standalone_fs.pathInFilesystemProc = xvfs_tclfs_standalone_pathInFilesystem;
425: xvfs_tclfs_standalone_fs.dupInternalRepProc = NULL;
426: xvfs_tclfs_standalone_fs.freeInternalRepProc = NULL;
427: xvfs_tclfs_standalone_fs.internalToNormalizedProc = NULL;
428: xvfs_tclfs_standalone_fs.createInternalRepProc = NULL;
429: xvfs_tclfs_standalone_fs.normalizePathProc = NULL;
430: xvfs_tclfs_standalone_fs.filesystemPathTypeProc = NULL;
431: xvfs_tclfs_standalone_fs.filesystemSeparatorProc = NULL;
432: xvfs_tclfs_standalone_fs.statProc = xvfs_tclfs_standalone_stat;
433: xvfs_tclfs_standalone_fs.accessProc = NULL;
434: xvfs_tclfs_standalone_fs.openFileChannelProc = xvfs_tclfs_standalone_openFileChannel;
435: xvfs_tclfs_standalone_fs.matchInDirectoryProc = NULL; /* XXX:TODO */
436: xvfs_tclfs_standalone_fs.utimeProc = NULL;
437: xvfs_tclfs_standalone_fs.linkProc = NULL;
438: xvfs_tclfs_standalone_fs.listVolumesProc = xvfs_tclfs_standalone_listVolumes;
439: xvfs_tclfs_standalone_fs.fileAttrStringsProc = NULL;
440: xvfs_tclfs_standalone_fs.fileAttrsGetProc = NULL;
441: xvfs_tclfs_standalone_fs.fileAttrsSetProc = NULL;
442: xvfs_tclfs_standalone_fs.createDirectoryProc = NULL;
443: xvfs_tclfs_standalone_fs.removeDirectoryProc = NULL;
444: xvfs_tclfs_standalone_fs.deleteFileProc = NULL;
445: xvfs_tclfs_standalone_fs.copyFileProc = NULL;
446: xvfs_tclfs_standalone_fs.renameFileProc = NULL;
447: xvfs_tclfs_standalone_fs.copyDirectoryProc = NULL;
448: xvfs_tclfs_standalone_fs.lstatProc = NULL;
449: xvfs_tclfs_standalone_fs.loadFileProc = NULL;
450: xvfs_tclfs_standalone_fs.getCwdProc = NULL;
451: xvfs_tclfs_standalone_fs.chdirProc = NULL;
452:
453: xvfs_tclfs_standalone_info.fsInfo = fsInfo;
454: xvfs_tclfs_standalone_info.mountpoint = Tcl_NewObj();
455: Tcl_AppendStringsToObj(xvfs_tclfs_standalone_info.mountpoint, XVFS_ROOT_MOUNTPOINT, fsInfo->name, NULL);
456:
457: tcl_ret = Tcl_FSRegister(NULL, &xvfs_tclfs_standalone_fs);
458: if (tcl_ret != TCL_OK) {
459: if (interp) {
460: Tcl_SetResult(interp, "Tcl_FSRegister() failed", NULL);
461: }
462:
463: return(tcl_ret);
464: }
465:
466: xvfs_tclfs_prepareChannelType();
467:
468: return(TCL_OK);
469: }
470: #endif /* XVFS_MODE_STANDALONE || XVFS_MODE_FLEXIBLE */
471:
472: #if defined(XVFS_MODE_FLEXIBLE)
473: static int xvfs_flexible_register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
474: ClientData fsHandlerDataRaw;
475: struct xvfs_tclfs_server_info *fsHandlerData;
476: const Tcl_Filesystem *fsHandler;
477: int (*xvfs_register)(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo);
478: Tcl_Obj *rootPathObj;
479:
480: xvfs_register = &xvfs_standalone_register;
481:
482: rootPathObj = Tcl_NewStringObj(XVFS_ROOT_MOUNTPOINT, -1);
483: if (!rootPathObj) {
484: return(xvfs_register(interp, fsInfo));
485: }
486:
487: Tcl_IncrRefCount(rootPathObj);
488: fsHandler = Tcl_FSGetFileSystemForPath(rootPathObj);
489: Tcl_DecrRefCount(rootPathObj);
490:
491: if (!fsHandler) {
492: return(xvfs_register(interp, fsInfo));
493: }
494:
495: fsHandlerDataRaw = Tcl_FSData(fsHandler);
496: if (!fsHandlerDataRaw) {
497: return(xvfs_register(interp, fsInfo));
498: }
499:
500: fsHandlerData = (struct xvfs_tclfs_server_info *) fsHandlerDataRaw;
501:
502: /*
503: * XXX:TODO: What is the chance that the handler for //xvfs:/ hold
504: * client data smaller than XVFS_INTERNAL_SERVER_MAGIC_LEN ?
505: */
506: if (memcmp(fsHandlerData->magic, XVFS_INTERNAL_SERVER_MAGIC, sizeof(fsHandlerData->magic)) == 0) {
507: xvfs_register = fsHandlerData->registerProc;
508: }
509:
510: return(xvfs_register(interp, fsInfo));
511: }
512: #endif /* XVFS_MODE_FLEXIBLE */
513:
514: #if defined(XVFS_MODE_SERVER)
515: int Xvfs_Register(Tcl_Interp *interp, struct Xvfs_FSInfo *fsInfo) {
516: return(TCL_ERROR);
517: }
518: #endif /* XVFS_MODE_SERVER */