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