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