Lines of
xvfs.c.rvt
from check-in 32b55a907b
that are changed by the sequence of edits moving toward
check-in e5b6962adf:
1: #include <xvfs-core.h>
2: #include <unistd.h>
3: #include <string.h>
4: #include <tcl.h>
5:
6: #define XVFS_NAME_LOOKUP_ERROR (-1)
7: #define MIN(a, b) (((a) < (b)) ? (a) : (b))
8:
9: typedef enum {
10: XVFS_FILE_TYPE_REG,
11: XVFS_FILE_TYPE_DIR
12: } xvfs_file_type_t;
13:
14: typedef Tcl_WideInt xvfs_size_t;
15:
16: struct xvfs_file_data {
17: const char *name;
18: xvfs_file_type_t type;
19: xvfs_size_t size;
20: union {
21: const unsigned char *fileContents;
22: const char **dirChildren;
23: } data;
24: };
25:
26: <?
27: package require xvfs
28: xvfs::main $argv
29: ?>
30: static long xvfs_<?= $::xvfs::fsName ?>_nameToIndex(const char *path) {
31: if (path == NULL) {
32: return(XVFS_NAME_LOOKUP_ERROR);
33: }
34:
35: <? for {set index 0} {$index < [llength $::xvfs::outputFiles]} {incr index} {
36: set outputFile [lindex $::xvfs::outputFiles $index]
37: ?>
38: if (strcmp(path, "<?= [::xvfs::sanitizeCString $outputFile] ?>") == 0) {
39: return(<?= $index ?>);
40: }
41: <? } ?>
42: return(XVFS_NAME_LOOKUP_ERROR);
43: }
44:
45: static const char **xvfs_<?= $::xvfs::fsName ?>_getChildren(const char *path, Tcl_WideInt *count) {
46: struct xvfs_file_data *fileInfo;
47: long inode;
48:
49: /*
50: * Validate input parameters
51: */
52: if (count == NULL) {
53: return(NULL);
54: }
55:
56: /*
57: * Get the inode from the lookup function
58: */
59: inode = xvfs_<?= $::xvfs::fsName ?>_nameToIndex(path);
60: if (inode == XVFS_NAME_LOOKUP_ERROR) {
61: return(NULL);
62: }
63:
64: fileInfo = &xvfs_<?= $::xvfs::fsName ?>_data[inode];
65:
66: /*
67: * Ensure this is a directory
68: */
69: if (fileInfo->type != XVFS_FILE_TYPE_DIR) {
70: return(NULL);
71: }
72:
73: *count = fileInfo->size;
74: return(fileInfo->data.dirChildren);
75: }
76:
77: static const unsigned char *xvfs_<?= $::xvfs::fsName ?>_getData(const char *path, Tcl_WideInt start, Tcl_WideInt *length) {
78: struct xvfs_file_data *fileInfo;
79: Tcl_WideInt resultLength;
80: long inode;
81:
82: /*
83: * Validate input parameters
84: */
85: if (start < 0) {
86: return(NULL);
87: }
88:
89: if (length == NULL) {
90: return(NULL);
91: }
92:
93: if (*length < 0) {
94: return(NULL);
95: }
96:
97: /*
98: * Get the inode from the lookup function
99: */
100: inode = xvfs_<?= $::xvfs::fsName ?>_nameToIndex(path);
101: if (inode == XVFS_NAME_LOOKUP_ERROR) {
102: return(NULL);
103: }
104:
105: fileInfo = &xvfs_<?= $::xvfs::fsName ?>_data[inode];
106:
107: /*
108: * Ensure this is a file that can be read
109: */
110: if (fileInfo->type != XVFS_FILE_TYPE_REG) {
111: return(NULL);
112: }
113:
114: /*
115: * Validate the length
116: */
117: if (start > fileInfo->size) {
118: *length = -1;
119: return(NULL);
120: }
121:
122: if (*length == 0) {
123: resultLength = fileInfo->size - start;
124: } else {
125: resultLength = MIN(fileInfo->size - start, *length);
126: }
127: *length = resultLength;
128:
129: /*
130: * Return the data
131: */
132: return(fileInfo->data.fileContents + start);
133: }
134:
135: int Xvfs_<?= $::xvfs::fsName ?>_Init(Tcl_Interp *interp) {
136: int register_ret;
32b55a907b 2019-05-02 137:
32b55a907b 2019-05-02 138: /* XXX:TODO: Stubs */
32b55a907b 2019-05-02 139:
32b55a907b 2019-05-02 140: register_ret = Xvfs_Register(interp, "<?= $::xvfs::fsName ?>", XVFS_PROTOCOL_VERSION, xvfs_<?= $::xvfs::fsName ?>_getChildren, xvfs_<?= $::xvfs::fsName ?>_getData);
141: if (register_ret != TCL_OK) {
142: return(register_ret);
143: }
144:
145: return(TCL_OK);
146: }