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