Lines of
xvfs.c.rvt
from check-in 3a68a54e44
that are changed by the sequence of edits moving toward
check-in f7a81bb10b:
1: #include <xvfs-core.h>
2: #include <sys/stat.h>
3: #include <unistd.h>
4: #include <string.h>
5: #include <tcl.h>
6:
7: #define XVFS_NAME_LOOKUP_ERROR (-1)
8: #define XVFS_FILE_BLOCKSIZE 1024
9:
10: /*
11: * XXX:TODO: Determine this automatically rather than
12: * by heuristics
13: */
14: #define HAVE_STRUCT_STAT_ST_BLKSIZE 1
15: #define HAVE_STRUCT_STAT_ST_BLOCKS 1
16: #ifdef WIN32
17: # undef HAVE_STRUCT_STAT_ST_BLKSIZE
18: # undef HAVE_STRUCT_STAT_ST_BLOCKS
19: #endif
20:
21: #define MIN(a, b) (((a) < (b)) ? (a) : (b))
22:
23: typedef enum {
24: XVFS_FILE_TYPE_REG,
25: XVFS_FILE_TYPE_DIR
26: } xvfs_file_type_t;
27:
28: typedef Tcl_WideInt xvfs_size_t;
29:
30: struct xvfs_file_data {
31: const char *name;
32: xvfs_file_type_t type;
33: xvfs_size_t size;
34: union {
35: const unsigned char *fileContents;
36: const char **dirChildren;
37: } data;
38: };
39:
40: <?
41: package require xvfs
42: xvfs::main $argv
43: ?>
44: static long xvfs_<?= $::xvfs::fsName ?>_nameToIndex(const char *path) {
45: unsigned int pathHash;
46: size_t pathLen;
47:
48: if (path == NULL) {
49: return(XVFS_NAME_LOOKUP_ERROR);
50: }
51:
52: pathLen = strlen(path);
53: pathHash = Tcl_ZlibAdler32(0, (const unsigned char *) path, pathLen);
54: switch (pathHash) {
55: <?
56: for {set index 0} {$index < [llength $::xvfs::outputFiles]} {incr index} {
57: set outputFile [lindex $::xvfs::outputFiles $index]
58: set outputFileHash [zlib adler32 $outputFile 0]
59: lappend outputFileHashToIndex($outputFileHash) $index
60: }
61:
62: foreach {outputFileHash outputFileIndexes} [lsort -stride 2 -dictionary [array get outputFileHashToIndex]] {
63: ?> case <?= $outputFileHash ?>:
64: <?
65: foreach outputFileIndex $outputFileIndexes {
66: set outputFileName [lindex $::xvfs::outputFiles $outputFileIndex]
67: set outputFileNameLen [string length $outputFileName]
68: ?> if (pathLen == <?= $outputFileNameLen ?> && memcmp(path, xvfs_<?= $::xvfs::fsName ?>_data[<?= $outputFileIndex ?>].name, pathLen) == 0) {
69: return(<?= $outputFileIndex ?>);
70: }
71: <?
72: }
73: ?> break;
74: <? } ?>
75: }
76:
77: return(XVFS_NAME_LOOKUP_ERROR);
78: }
79:
80: static const char **xvfs_<?= $::xvfs::fsName ?>_getChildren(const char *path, Tcl_WideInt *count) {
81: struct xvfs_file_data *fileInfo;
82: long inode;
83:
84: /*
85: * Validate input parameters
86: */
87: if (count == NULL) {
88: return(NULL);
89: }
90:
91: /*
92: * Get the inode from the lookup function
93: */
94: inode = xvfs_<?= $::xvfs::fsName ?>_nameToIndex(path);
95: if (inode == XVFS_NAME_LOOKUP_ERROR) {
96: *count = XVFS_RV_ERR_ENOENT;
97: return(NULL);
98: }
99:
100: fileInfo = &xvfs_<?= $::xvfs::fsName ?>_data[inode];
101:
102: /*
103: * Ensure this is a directory
104: */
105: if (fileInfo->type != XVFS_FILE_TYPE_DIR) {
106: *count = XVFS_RV_ERR_ENOTDIR;
107: return(NULL);
108: }
109:
110: *count = fileInfo->size;
111: return(fileInfo->data.dirChildren);
112: }
113:
114: static const unsigned char *xvfs_<?= $::xvfs::fsName ?>_getData(const char *path, Tcl_WideInt start, Tcl_WideInt *length) {
115: struct xvfs_file_data *fileInfo;
116: Tcl_WideInt resultLength;
117: long inode;
118:
119: /*
120: * Validate input parameters
121: */
122: if (length == NULL) {
123: return(NULL);
124: }
125:
126: if (start < 0) {
127: *length = XVFS_RV_ERR_EINVAL;
128: return(NULL);
129: }
130:
131: if (*length < 0) {
132: *length = XVFS_RV_ERR_EINVAL;
133: return(NULL);
134: }
135:
136: /*
137: * Get the inode from the lookup function
138: */
139: inode = xvfs_<?= $::xvfs::fsName ?>_nameToIndex(path);
140: if (inode == XVFS_NAME_LOOKUP_ERROR) {
141: *length = XVFS_RV_ERR_ENOENT;
142: return(NULL);
143: }
144:
145: fileInfo = &xvfs_<?= $::xvfs::fsName ?>_data[inode];
146:
147: /*
148: * Ensure this is a file that can be read
149: */
150: if (fileInfo->type != XVFS_FILE_TYPE_REG) {
151: *length = XVFS_RV_ERR_EISDIR;
152: return(NULL);
153: }
154:
155: /*
156: * Validate the length
157: */
158: if (start > fileInfo->size) {
159: *length = XVFS_RV_ERR_EFAULT;
160: return(NULL);
161: }
162:
163: if (*length == 0) {
164: resultLength = fileInfo->size - start;
165: } else {
166: resultLength = MIN(fileInfo->size - start, *length);
167: }
168: *length = resultLength;
169:
170: /*
171: * Return the data
172: */
173: return(fileInfo->data.fileContents + start);
174: }
175:
176: static int xvfs_<?= $::xvfs::fsName ?>_getStat(const char *path, Tcl_StatBuf *statBuf) {
177: struct xvfs_file_data *fileInfo;
178: long inode;
179:
180: /*
181: * Validate input parameters
182: */
183: if (!statBuf) {
184: return(XVFS_RV_ERR_EINVAL);
185: }
186:
187: /*
188: * Get the inode from the lookup function
189: */
190: inode = xvfs_<?= $::xvfs::fsName ?>_nameToIndex(path);
191: if (inode == XVFS_NAME_LOOKUP_ERROR) {
192: return(XVFS_RV_ERR_ENOENT);
193: }
194:
195: fileInfo = &xvfs_<?= $::xvfs::fsName ?>_data[inode];
196:
3a68a54e44 2019-09-17 197: statBuf->st_dev = 0;
3a68a54e44 2019-09-17 198: statBuf->st_rdev = 0;
199: statBuf->st_ino = inode;
3a68a54e44 2019-09-17 200: statBuf->st_uid = -1;
3a68a54e44 2019-09-17 201: statBuf->st_gid = -1;
202: statBuf->st_atime = 0;
203: statBuf->st_ctime = 0;
204: statBuf->st_mtime = 0;
205: #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
206: statBuf->st_blksize = XVFS_FILE_BLOCKSIZE;
207: #endif
208:
209: if (fileInfo->type == XVFS_FILE_TYPE_REG) {
210: statBuf->st_mode = 0100444;
211: statBuf->st_nlink = 1;
212: statBuf->st_size = fileInfo->size;
213: #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
214: statBuf->st_blocks = (fileInfo->size + statBuf->st_blksize - 1) / statBuf->st_blksize;
215: #endif
216: } else if (fileInfo->type == XVFS_FILE_TYPE_DIR) {
217: statBuf->st_mode = 040555;
218: statBuf->st_nlink = fileInfo->size;
219: statBuf->st_size = fileInfo->size;
220: #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
221: statBuf->st_blocks = 1;
222: #endif
223: }
224:
225: return(0);
226: }
227:
228: static struct Xvfs_FSInfo xvfs_<?= $::xvfs::fsName ?>_fsInfo = {
229: .protocolVersion = XVFS_PROTOCOL_VERSION,
230: .name = "<?= $::xvfs::fsName ?>",
231: .getChildrenProc = xvfs_<?= $::xvfs::fsName ?>_getChildren,
232: .getDataProc = xvfs_<?= $::xvfs::fsName ?>_getData,
233: .getStatProc = xvfs_<?= $::xvfs::fsName ?>_getStat
234: };
235:
236: int Xvfs_<?= $::xvfs::fsName ?>_Init(Tcl_Interp *interp) {
237: int register_ret;
238:
239: #ifdef USE_TCL_STUBS
240: const char *tclInitStubs_ret;
241: /* Initialize Stubs */
242: tclInitStubs_ret = Tcl_InitStubs(interp, TCL_PATCH_LEVEL, 0);
243: if (!tclInitStubs_ret) {
244: return(TCL_ERROR);
245: }
246: #endif
247:
248: register_ret = Xvfs_Register(interp, &xvfs_<?= $::xvfs::fsName ?>_fsInfo);
249: if (register_ret != TCL_OK) {
250: return(register_ret);
251: }
252:
253: return(TCL_OK);
254: }
255: #undef XVFS_NAME_LOOKUP_ERROR
256: #undef XVFS_FILE_BLOCKSIZE