09e53d3c38 2019-09-20 1: <?
eebfe1f40f 2020-03-25 2: # Care must be taken when editing this file as
eebfe1f40f 2020-03-25 3: # it may be processed by either "xvfs-create"
eebfe1f40f 2020-03-25 4: # which uses a full Tcl and Rivet parser
eebfe1f40f 2020-03-25 5: # or by "xvfs-create-c" which uses a much
eebfe1f40f 2020-03-25 6: # simpler one that only knows about printable
eebfe1f40f 2020-03-25 7: # sections and ignores all Tcl sections
eebfe1f40f 2020-03-25 8:
09e53d3c38 2019-09-20 9: if {[info exists ::xvfs::xvfsCoreH]} {
eebfe1f40f 2020-03-25 10: ::minirivet::_emitOutput $::xvfs::xvfsCoreH
09e53d3c38 2019-09-20 11: } else {
09e53d3c38 2019-09-20 12: ?>#include <xvfs-core.h><? } ?>
2b7fa3a8fa 2019-09-20 13: #include <sys/stat.h>
2b7fa3a8fa 2019-09-20 14: #include <string.h>
2b7fa3a8fa 2019-09-20 15: #include <tcl.h>
2b7fa3a8fa 2019-09-20 16:
2b7fa3a8fa 2019-09-20 17: #define XVFS_NAME_LOOKUP_ERROR (-1)
2b7fa3a8fa 2019-09-20 18: #define XVFS_FILE_BLOCKSIZE 1024
2b7fa3a8fa 2019-09-20 19:
2b7fa3a8fa 2019-09-20 20: /*
2b7fa3a8fa 2019-09-20 21: * XXX:TODO: Determine this automatically rather than
2b7fa3a8fa 2019-09-20 22: * by heuristics
2b7fa3a8fa 2019-09-20 23: */
2b7fa3a8fa 2019-09-20 24: #define HAVE_STRUCT_STAT_ST_BLKSIZE 1
2b7fa3a8fa 2019-09-20 25: #define HAVE_STRUCT_STAT_ST_BLOCKS 1
2b7fa3a8fa 2019-09-20 26: #ifdef WIN32
2b7fa3a8fa 2019-09-20 27: # undef HAVE_STRUCT_STAT_ST_BLKSIZE
2b7fa3a8fa 2019-09-20 28: # undef HAVE_STRUCT_STAT_ST_BLOCKS
2b7fa3a8fa 2019-09-20 29: #endif
2b7fa3a8fa 2019-09-20 30:
c57d8bead8 2019-09-20 31: #ifndef MIN
2b7fa3a8fa 2019-09-20 32: #define MIN(a, b) (((a) < (b)) ? (a) : (b))
c57d8bead8 2019-09-20 33: #endif
2b7fa3a8fa 2019-09-20 34:
c57d8bead8 2019-09-20 35: #ifndef HAVE_DEFINED_XVFS_FILE_TYPE_T
c57d8bead8 2019-09-20 36: #define HAVE_DEFINED_XVFS_FILE_TYPE_T 1
2b7fa3a8fa 2019-09-20 37: typedef enum {
2b7fa3a8fa 2019-09-20 38: XVFS_FILE_TYPE_REG,
2b7fa3a8fa 2019-09-20 39: XVFS_FILE_TYPE_DIR
2b7fa3a8fa 2019-09-20 40: } xvfs_file_type_t;
c57d8bead8 2019-09-20 41: #endif
2b7fa3a8fa 2019-09-20 42:
c57d8bead8 2019-09-20 43: #ifndef HAVE_DEFINED_XVFS_SIZE_T
c57d8bead8 2019-09-20 44: #define HAVE_DEFINED_XVFS_SIZE_T 1
2b7fa3a8fa 2019-09-20 45: typedef Tcl_WideInt xvfs_size_t;
c57d8bead8 2019-09-20 46: #endif
2b7fa3a8fa 2019-09-20 47:
c57d8bead8 2019-09-20 48: #ifndef HAVE_DEFINED_XVFS_FILE_DATA
c57d8bead8 2019-09-20 49: #define HAVE_DEFINED_XVFS_FILE_DATA 1
2b7fa3a8fa 2019-09-20 50: struct xvfs_file_data {
a719156faf 2019-10-09 51: const char * const name;
a719156faf 2019-10-09 52: const xvfs_file_type_t type;
a719156faf 2019-10-09 53: const xvfs_size_t size;
2b7fa3a8fa 2019-09-20 54: union {
a719156faf 2019-10-09 55: const unsigned char * const fileContents;
2b7fa3a8fa 2019-09-20 56: const char **dirChildren;
2b7fa3a8fa 2019-09-20 57: } data;
2b7fa3a8fa 2019-09-20 58: };
c57d8bead8 2019-09-20 59: #endif
2b7fa3a8fa 2019-09-20 60:
2b7fa3a8fa 2019-09-20 61: <?
2b7fa3a8fa 2019-09-20 62: package require xvfs
2b7fa3a8fa 2019-09-20 63:
eebfe1f40f 2020-03-25 64: set ::xvfs::fileInfoStruct [xvfs::main $::xvfs::argv]
eebfe1f40f 2020-03-25 65: ?><?= $::xvfs::fileInfoStruct ?>
2b7fa3a8fa 2019-09-20 66: static long xvfs_<?= $::xvfs::fsName ?>_nameToIndex(const char *path) {
37d00c3cfb 2019-11-04 67: <?
eebfe1f40f 2020-03-25 68: set hashTable [::xvfs::generateHashTable pathIndex path pathLen XVFS_NAME_LOOKUP_ERROR $::xvfs::outputFiles prefix "\t" hashTableSize 30 validate "strcmp(path, xvfs_${::xvfs::fsName}_data\[pathIndex\].name) == 0" onValidated "return(pathIndex);"]
eebfe1f40f 2020-03-25 69: set hashTableHeader [dict get $hashTable header]
eebfe1f40f 2020-03-25 70: ?><?= $hashTableHeader ?>
eebfe1f40f 2020-03-25 71: long pathIndex;
f01f82c2d8 2020-04-01 72: size_t pathLen;
a719156faf 2019-10-09 73:
2b7fa3a8fa 2019-09-20 74: if (path == NULL) {
2b7fa3a8fa 2019-09-20 75: return(XVFS_NAME_LOOKUP_ERROR);
2b7fa3a8fa 2019-09-20 76: }
2b7fa3a8fa 2019-09-20 77:
2b7fa3a8fa 2019-09-20 78: pathLen = strlen(path);
a719156faf 2019-10-09 79:
eebfe1f40f 2020-03-25 80: <?= [dict get $hashTable body] ?>
eebfe1f40f 2020-03-25 81:
2b7fa3a8fa 2019-09-20 82: return(XVFS_NAME_LOOKUP_ERROR);
2b7fa3a8fa 2019-09-20 83: }
2b7fa3a8fa 2019-09-20 84:
142a373444 2020-04-13 85: static const char **xvfs_<?= $::xvfs::fsName ?>_getChildren(const char *path, long inode, Tcl_WideInt *count) {
2b7fa3a8fa 2019-09-20 86: const struct xvfs_file_data *fileInfo;
2b7fa3a8fa 2019-09-20 87:
2b7fa3a8fa 2019-09-20 88: /*
2b7fa3a8fa 2019-09-20 89: * Validate input parameters
2b7fa3a8fa 2019-09-20 90: */
2b7fa3a8fa 2019-09-20 91: if (count == NULL) {
2b7fa3a8fa 2019-09-20 92: return(NULL);
2b7fa3a8fa 2019-09-20 93: }
2b7fa3a8fa 2019-09-20 94:
2b7fa3a8fa 2019-09-20 95: /*
142a373444 2020-04-13 96: * Use user-supplied inode, or look up the path
2b7fa3a8fa 2019-09-20 97: */
5aadfc2b05 2020-04-13 98: if (inode != XVFS_INODE_NULL) {
5aadfc2b05 2020-04-13 99: if (inode >= <?= [llength $::xvfs::outputFiles] ?> || inode < 0) {
5aadfc2b05 2020-04-13 100: inode = XVFS_INODE_NULL;
5aadfc2b05 2020-04-13 101: path = NULL;
5aadfc2b05 2020-04-13 102: }
5aadfc2b05 2020-04-13 103: }
142a373444 2020-04-13 104: if (inode == XVFS_INODE_NULL) {
142a373444 2020-04-13 105: /*
142a373444 2020-04-13 106: * Get the inode from the lookup function
142a373444 2020-04-13 107: */
142a373444 2020-04-13 108: inode = xvfs_<?= $::xvfs::fsName ?>_nameToIndex(path);
142a373444 2020-04-13 109: if (inode == XVFS_NAME_LOOKUP_ERROR) {
142a373444 2020-04-13 110: *count = XVFS_RV_ERR_ENOENT;
142a373444 2020-04-13 111: return(NULL);
142a373444 2020-04-13 112: }
2b7fa3a8fa 2019-09-20 113: }
2b7fa3a8fa 2019-09-20 114:
2b7fa3a8fa 2019-09-20 115: fileInfo = &xvfs_<?= $::xvfs::fsName ?>_data[inode];
2b7fa3a8fa 2019-09-20 116:
2b7fa3a8fa 2019-09-20 117: /*
2b7fa3a8fa 2019-09-20 118: * Ensure this is a directory
2b7fa3a8fa 2019-09-20 119: */
2b7fa3a8fa 2019-09-20 120: if (fileInfo->type != XVFS_FILE_TYPE_DIR) {
2b7fa3a8fa 2019-09-20 121: *count = XVFS_RV_ERR_ENOTDIR;
2b7fa3a8fa 2019-09-20 122: return(NULL);
2b7fa3a8fa 2019-09-20 123: }
2b7fa3a8fa 2019-09-20 124:
2b7fa3a8fa 2019-09-20 125: *count = fileInfo->size;
2b7fa3a8fa 2019-09-20 126: return(fileInfo->data.dirChildren);
2b7fa3a8fa 2019-09-20 127: }
2b7fa3a8fa 2019-09-20 128:
142a373444 2020-04-13 129: static const unsigned char *xvfs_<?= $::xvfs::fsName ?>_getData(const char *path, long inode, Tcl_WideInt start, Tcl_WideInt *length) {
2b7fa3a8fa 2019-09-20 130: const struct xvfs_file_data *fileInfo;
2b7fa3a8fa 2019-09-20 131: Tcl_WideInt resultLength;
2b7fa3a8fa 2019-09-20 132:
2b7fa3a8fa 2019-09-20 133: /*
2b7fa3a8fa 2019-09-20 134: * Validate input parameters
2b7fa3a8fa 2019-09-20 135: */
2b7fa3a8fa 2019-09-20 136: if (length == NULL) {
2b7fa3a8fa 2019-09-20 137: return(NULL);
2b7fa3a8fa 2019-09-20 138: }
2b7fa3a8fa 2019-09-20 139:
2b7fa3a8fa 2019-09-20 140: if (start < 0) {
2b7fa3a8fa 2019-09-20 141: *length = XVFS_RV_ERR_EINVAL;
2b7fa3a8fa 2019-09-20 142: return(NULL);
2b7fa3a8fa 2019-09-20 143: }
2b7fa3a8fa 2019-09-20 144:
2b7fa3a8fa 2019-09-20 145: if (*length < 0) {
2b7fa3a8fa 2019-09-20 146: *length = XVFS_RV_ERR_EINVAL;
2b7fa3a8fa 2019-09-20 147: return(NULL);
2b7fa3a8fa 2019-09-20 148: }
142a373444 2020-04-13 149:
142a373444 2020-04-13 150:
2b7fa3a8fa 2019-09-20 151: /*
142a373444 2020-04-13 152: * Use user-supplied inode, or look up the path
2b7fa3a8fa 2019-09-20 153: */
5aadfc2b05 2020-04-13 154: if (inode != XVFS_INODE_NULL) {
5aadfc2b05 2020-04-13 155: if (inode >= <?= [llength $::xvfs::outputFiles] ?> || inode < 0) {
5aadfc2b05 2020-04-13 156: inode = XVFS_INODE_NULL;
5aadfc2b05 2020-04-13 157: path = NULL;
5aadfc2b05 2020-04-13 158: }
5aadfc2b05 2020-04-13 159: }
142a373444 2020-04-13 160: if (inode == XVFS_INODE_NULL) {
142a373444 2020-04-13 161: /*
142a373444 2020-04-13 162: * Get the inode from the lookup function
142a373444 2020-04-13 163: */
142a373444 2020-04-13 164: inode = xvfs_<?= $::xvfs::fsName ?>_nameToIndex(path);
142a373444 2020-04-13 165: if (inode == XVFS_NAME_LOOKUP_ERROR) {
142a373444 2020-04-13 166: *length = XVFS_RV_ERR_ENOENT;
142a373444 2020-04-13 167: return(NULL);
142a373444 2020-04-13 168: }
2b7fa3a8fa 2019-09-20 169: }
2b7fa3a8fa 2019-09-20 170:
2b7fa3a8fa 2019-09-20 171: fileInfo = &xvfs_<?= $::xvfs::fsName ?>_data[inode];
2b7fa3a8fa 2019-09-20 172:
2b7fa3a8fa 2019-09-20 173: /*
2b7fa3a8fa 2019-09-20 174: * Ensure this is a file that can be read
2b7fa3a8fa 2019-09-20 175: */
2b7fa3a8fa 2019-09-20 176: if (fileInfo->type != XVFS_FILE_TYPE_REG) {
2b7fa3a8fa 2019-09-20 177: *length = XVFS_RV_ERR_EISDIR;
2b7fa3a8fa 2019-09-20 178: return(NULL);
2b7fa3a8fa 2019-09-20 179: }
2b7fa3a8fa 2019-09-20 180:
2b7fa3a8fa 2019-09-20 181: /*
2b7fa3a8fa 2019-09-20 182: * Validate the length
2b7fa3a8fa 2019-09-20 183: */
2b7fa3a8fa 2019-09-20 184: if (start > fileInfo->size) {
2b7fa3a8fa 2019-09-20 185: *length = XVFS_RV_ERR_EFAULT;
2b7fa3a8fa 2019-09-20 186: return(NULL);
2b7fa3a8fa 2019-09-20 187: }
2b7fa3a8fa 2019-09-20 188:
2b7fa3a8fa 2019-09-20 189: if (*length == 0) {
2b7fa3a8fa 2019-09-20 190: resultLength = fileInfo->size - start;
2b7fa3a8fa 2019-09-20 191: } else {
2b7fa3a8fa 2019-09-20 192: resultLength = MIN(fileInfo->size - start, *length);
2b7fa3a8fa 2019-09-20 193: }
2b7fa3a8fa 2019-09-20 194: *length = resultLength;
2b7fa3a8fa 2019-09-20 195:
2b7fa3a8fa 2019-09-20 196: /*
2b7fa3a8fa 2019-09-20 197: * Return the data
2b7fa3a8fa 2019-09-20 198: */
2b7fa3a8fa 2019-09-20 199: return(fileInfo->data.fileContents + start);
2b7fa3a8fa 2019-09-20 200: }
2b7fa3a8fa 2019-09-20 201:
142a373444 2020-04-13 202: static int xvfs_<?= $::xvfs::fsName ?>_getStat(const char *path, long inode, Tcl_StatBuf *statBuf) {
2b7fa3a8fa 2019-09-20 203: const struct xvfs_file_data *fileInfo;
2b7fa3a8fa 2019-09-20 204:
2b7fa3a8fa 2019-09-20 205: /*
2b7fa3a8fa 2019-09-20 206: * Validate input parameters
2b7fa3a8fa 2019-09-20 207: */
2b7fa3a8fa 2019-09-20 208: if (!statBuf) {
2b7fa3a8fa 2019-09-20 209: return(XVFS_RV_ERR_EINVAL);
2b7fa3a8fa 2019-09-20 210: }
2b7fa3a8fa 2019-09-20 211:
2b7fa3a8fa 2019-09-20 212: /*
142a373444 2020-04-13 213: * Use user-supplied inode, or look up the path
2b7fa3a8fa 2019-09-20 214: */
5aadfc2b05 2020-04-13 215: if (inode != XVFS_INODE_NULL) {
5aadfc2b05 2020-04-13 216: if (inode >= <?= [llength $::xvfs::outputFiles] ?> || inode < 0) {
5aadfc2b05 2020-04-13 217: inode = XVFS_INODE_NULL;
5aadfc2b05 2020-04-13 218: path = NULL;
5aadfc2b05 2020-04-13 219: }
5aadfc2b05 2020-04-13 220: }
142a373444 2020-04-13 221: if (inode == XVFS_INODE_NULL) {
142a373444 2020-04-13 222: /*
142a373444 2020-04-13 223: * Get the inode from the lookup function
142a373444 2020-04-13 224: */
142a373444 2020-04-13 225: inode = xvfs_<?= $::xvfs::fsName ?>_nameToIndex(path);
142a373444 2020-04-13 226: if (inode == XVFS_NAME_LOOKUP_ERROR) {
142a373444 2020-04-13 227: return(XVFS_RV_ERR_ENOENT);
142a373444 2020-04-13 228: }
2b7fa3a8fa 2019-09-20 229: }
2b7fa3a8fa 2019-09-20 230:
2b7fa3a8fa 2019-09-20 231: fileInfo = &xvfs_<?= $::xvfs::fsName ?>_data[inode];
2b7fa3a8fa 2019-09-20 232:
eebfe1f40f 2020-03-25 233: statBuf->st_dev = <?= [zlib adler32 $::xvfs::fsName 0] ?>;
eebfe1f40f 2020-03-25 234: statBuf->st_rdev = <?= [zlib adler32 $::xvfs::fsName 0] ?>;
2b7fa3a8fa 2019-09-20 235: statBuf->st_ino = inode;
2b7fa3a8fa 2019-09-20 236: statBuf->st_uid = 0;
2b7fa3a8fa 2019-09-20 237: statBuf->st_gid = 0;
2b7fa3a8fa 2019-09-20 238: statBuf->st_atime = 0;
2b7fa3a8fa 2019-09-20 239: statBuf->st_ctime = 0;
2b7fa3a8fa 2019-09-20 240: statBuf->st_mtime = 0;
2b7fa3a8fa 2019-09-20 241: #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
2b7fa3a8fa 2019-09-20 242: statBuf->st_blksize = XVFS_FILE_BLOCKSIZE;
2b7fa3a8fa 2019-09-20 243: #endif
2b7fa3a8fa 2019-09-20 244:
2b7fa3a8fa 2019-09-20 245: if (fileInfo->type == XVFS_FILE_TYPE_REG) {
2b7fa3a8fa 2019-09-20 246: statBuf->st_mode = 0100444;
2b7fa3a8fa 2019-09-20 247: statBuf->st_nlink = 1;
2b7fa3a8fa 2019-09-20 248: statBuf->st_size = fileInfo->size;
2b7fa3a8fa 2019-09-20 249: #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
2b7fa3a8fa 2019-09-20 250: statBuf->st_blocks = (fileInfo->size + statBuf->st_blksize - 1) / statBuf->st_blksize;
2b7fa3a8fa 2019-09-20 251: #endif
2b7fa3a8fa 2019-09-20 252: } else if (fileInfo->type == XVFS_FILE_TYPE_DIR) {
2b7fa3a8fa 2019-09-20 253: statBuf->st_mode = 040555;
2b7fa3a8fa 2019-09-20 254: statBuf->st_nlink = fileInfo->size;
2b7fa3a8fa 2019-09-20 255: statBuf->st_size = fileInfo->size;
2b7fa3a8fa 2019-09-20 256: #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
2b7fa3a8fa 2019-09-20 257: statBuf->st_blocks = 1;
2b7fa3a8fa 2019-09-20 258: #endif
2b7fa3a8fa 2019-09-20 259: }
2b7fa3a8fa 2019-09-20 260:
2b7fa3a8fa 2019-09-20 261: return(0);
2b7fa3a8fa 2019-09-20 262: }
2b7fa3a8fa 2019-09-20 263:
2b7fa3a8fa 2019-09-20 264: static struct Xvfs_FSInfo xvfs_<?= $::xvfs::fsName ?>_fsInfo = {
2b7fa3a8fa 2019-09-20 265: .protocolVersion = XVFS_PROTOCOL_VERSION,
2b7fa3a8fa 2019-09-20 266: .name = "<?= $::xvfs::fsName ?>",
2b7fa3a8fa 2019-09-20 267: .getChildrenProc = xvfs_<?= $::xvfs::fsName ?>_getChildren,
2b7fa3a8fa 2019-09-20 268: .getDataProc = xvfs_<?= $::xvfs::fsName ?>_getData,
2b7fa3a8fa 2019-09-20 269: .getStatProc = xvfs_<?= $::xvfs::fsName ?>_getStat
2b7fa3a8fa 2019-09-20 270: };
2b7fa3a8fa 2019-09-20 271:
717062426a 2020-04-13 272: #ifdef XVFS_<?= $::xvfs::fsName ?>_INIT_STATIC
717062426a 2020-04-13 273: static
717062426a 2020-04-13 274: #endif
2b7fa3a8fa 2019-09-20 275: int Xvfs_<?= $::xvfs::fsName ?>_Init(Tcl_Interp *interp) {
2b7fa3a8fa 2019-09-20 276: int register_ret;
2b7fa3a8fa 2019-09-20 277:
2b7fa3a8fa 2019-09-20 278: #ifdef USE_TCL_STUBS
2b7fa3a8fa 2019-09-20 279: const char *tclInitStubs_ret;
2b7fa3a8fa 2019-09-20 280: /* Initialize Stubs */
2b7fa3a8fa 2019-09-20 281: tclInitStubs_ret = Tcl_InitStubs(interp, TCL_PATCH_LEVEL, 0);
2b7fa3a8fa 2019-09-20 282: if (!tclInitStubs_ret) {
2b7fa3a8fa 2019-09-20 283: return(TCL_ERROR);
2b7fa3a8fa 2019-09-20 284: }
2b7fa3a8fa 2019-09-20 285: #endif
2b7fa3a8fa 2019-09-20 286:
2b7fa3a8fa 2019-09-20 287: register_ret = Xvfs_Register(interp, &xvfs_<?= $::xvfs::fsName ?>_fsInfo);
2b7fa3a8fa 2019-09-20 288: if (register_ret != TCL_OK) {
2b7fa3a8fa 2019-09-20 289: return(register_ret);
2b7fa3a8fa 2019-09-20 290: }
2b7fa3a8fa 2019-09-20 291:
2b7fa3a8fa 2019-09-20 292: return(TCL_OK);
2b7fa3a8fa 2019-09-20 293: }
2b7fa3a8fa 2019-09-20 294: #undef XVFS_NAME_LOOKUP_ERROR
2b7fa3a8fa 2019-09-20 295: #undef XVFS_FILE_BLOCKSIZE
717062426a 2020-04-13 296: #undef XVFS_<?= $::xvfs::fsName ?>_INIT_STATIC