Lines of
xvfs.c.rvt
from check-in e5b6962adf
that are changed by the sequence of edits moving toward
check-in d121970301:
1: #include <xvfs-core.h>
2: #include <unistd.h>
3: #include <string.h>
4: #include <tcl.h>
5:
6: #define XVFS_NAME_LOOKUP_ERROR (-1)
7: #define MIN(a, b) (((a) < (b)) ? (a) : (b))
8:
9: typedef enum {
10: XVFS_FILE_TYPE_REG,
11: XVFS_FILE_TYPE_DIR
12: } xvfs_file_type_t;
13:
14: typedef Tcl_WideInt xvfs_size_t;
15:
16: struct xvfs_file_data {
17: const char *name;
18: xvfs_file_type_t type;
19: xvfs_size_t size;
20: union {
21: const unsigned char *fileContents;
22: const char **dirChildren;
23: } data;
24: };
25:
26: <?
27: package require xvfs
28: xvfs::main $argv
29: ?>
30: static long xvfs_<?= $::xvfs::fsName ?>_nameToIndex(const char *path) {
31: if (path == NULL) {
32: return(XVFS_NAME_LOOKUP_ERROR);
33: }
34:
35: <? for {set index 0} {$index < [llength $::xvfs::outputFiles]} {incr index} {
36: set outputFile [lindex $::xvfs::outputFiles $index]
37: ?>
38: if (strcmp(path, "<?= [::xvfs::sanitizeCString $outputFile] ?>") == 0) {
39: return(<?= $index ?>);
40: }
41: <? } ?>
42: return(XVFS_NAME_LOOKUP_ERROR);
43: }
44:
45: static const char **xvfs_<?= $::xvfs::fsName ?>_getChildren(const char *path, Tcl_WideInt *count) {
46: struct xvfs_file_data *fileInfo;
47: long inode;
48:
49: /*
50: * Validate input parameters
51: */
52: if (count == NULL) {
53: return(NULL);
54: }
55:
56: /*
57: * Get the inode from the lookup function
58: */
59: inode = xvfs_<?= $::xvfs::fsName ?>_nameToIndex(path);
60: if (inode == XVFS_NAME_LOOKUP_ERROR) {
61: return(NULL);
62: }
63:
64: fileInfo = &xvfs_<?= $::xvfs::fsName ?>_data[inode];
65:
66: /*
67: * Ensure this is a directory
68: */
69: if (fileInfo->type != XVFS_FILE_TYPE_DIR) {
70: return(NULL);
71: }
72:
73: *count = fileInfo->size;
74: return(fileInfo->data.dirChildren);
75: }
76:
77: static const unsigned char *xvfs_<?= $::xvfs::fsName ?>_getData(const char *path, Tcl_WideInt start, Tcl_WideInt *length) {
78: struct xvfs_file_data *fileInfo;
79: Tcl_WideInt resultLength;
80: long inode;
81:
82: /*
83: * Validate input parameters
84: */
85: if (start < 0) {
86: return(NULL);
87: }
88:
89: if (length == NULL) {
90: return(NULL);
91: }
92:
93: if (*length < 0) {
94: return(NULL);
95: }
96:
97: /*
98: * Get the inode from the lookup function
99: */
100: inode = xvfs_<?= $::xvfs::fsName ?>_nameToIndex(path);
101: if (inode == XVFS_NAME_LOOKUP_ERROR) {
102: return(NULL);
103: }
104:
105: fileInfo = &xvfs_<?= $::xvfs::fsName ?>_data[inode];
106:
107: /*
108: * Ensure this is a file that can be read
109: */
110: if (fileInfo->type != XVFS_FILE_TYPE_REG) {
111: return(NULL);
112: }
113:
114: /*
115: * Validate the length
116: */
117: if (start > fileInfo->size) {
118: *length = -1;
119: return(NULL);
120: }
121:
122: if (*length == 0) {
123: resultLength = fileInfo->size - start;
124: } else {
125: resultLength = MIN(fileInfo->size - start, *length);
126: }
127: *length = resultLength;
128:
129: /*
130: * Return the data
131: */
132: return(fileInfo->data.fileContents + start);
133: }
134:
135: int Xvfs_<?= $::xvfs::fsName ?>_Init(Tcl_Interp *interp) {
e5b6962adf 2019-05-02 136: struct Xvfs_FSInfo fsInfo = {
e5b6962adf 2019-05-02 137: .protocolVersion = XVFS_PROTOCOL_VERSION,
e5b6962adf 2019-05-02 138: .fsName = "<?= $::xvfs::fsName ?>",
e5b6962adf 2019-05-02 139: .getChildrenProc = xvfs_<?= $::xvfs::fsName ?>_getChildren,
e5b6962adf 2019-05-02 140: .getDataProc = xvfs_<?= $::xvfs::fsName ?>_getData
e5b6962adf 2019-05-02 141: };
142: int register_ret;
143:
144: #ifdef USE_TCL_STUBS
145: const char *tclInitStubs_ret;
146: /* Initialize Stubs */
147: tclInitStubs_ret = Tcl_InitStubs(interp, TCL_PATCH_LEVEL, 0);
148: if (!tclInitStubs_ret) {
149: return(TCL_ERROR);
150: }
151: #endif
152:
e5b6962adf 2019-05-02 153: register_ret = Xvfs_Register(interp, &fsInfo);
154: if (register_ret != TCL_OK) {
155: return(register_ret);
156: }
157:
158: return(TCL_OK);
159: }