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