09e53d3c38 2019-09-20 rkeene: <?
eebfe1f40f 2020-03-25 rkeene: # Care must be taken when editing this file as
eebfe1f40f 2020-03-25 rkeene: # it may be processed by either "xvfs-create"
eebfe1f40f 2020-03-25 rkeene: # which uses a full Tcl and Rivet parser
eebfe1f40f 2020-03-25 rkeene: # or by "xvfs-create-c" which uses a much
eebfe1f40f 2020-03-25 rkeene: # simpler one that only knows about printable
eebfe1f40f 2020-03-25 rkeene: # sections and ignores all Tcl sections
eebfe1f40f 2020-03-25 rkeene:
09e53d3c38 2019-09-20 rkeene: if {[info exists ::xvfs::xvfsCoreH]} {
eebfe1f40f 2020-03-25 rkeene: ::minirivet::_emitOutput $::xvfs::xvfsCoreH
09e53d3c38 2019-09-20 rkeene: } else {
09e53d3c38 2019-09-20 rkeene: ?>#include <xvfs-core.h><? } ?>
2b7fa3a8fa 2019-09-20 rkeene: #include <sys/stat.h>
2b7fa3a8fa 2019-09-20 rkeene: #include <unistd.h>
2b7fa3a8fa 2019-09-20 rkeene: #include <string.h>
2b7fa3a8fa 2019-09-20 rkeene: #include <tcl.h>
2b7fa3a8fa 2019-09-20 rkeene:
2b7fa3a8fa 2019-09-20 rkeene: #define XVFS_NAME_LOOKUP_ERROR (-1)
2b7fa3a8fa 2019-09-20 rkeene: #define XVFS_FILE_BLOCKSIZE 1024
2b7fa3a8fa 2019-09-20 rkeene:
2b7fa3a8fa 2019-09-20 rkeene: /*
2b7fa3a8fa 2019-09-20 rkeene: * XXX:TODO: Determine this automatically rather than
2b7fa3a8fa 2019-09-20 rkeene: * by heuristics
2b7fa3a8fa 2019-09-20 rkeene: */
2b7fa3a8fa 2019-09-20 rkeene: #define HAVE_STRUCT_STAT_ST_BLKSIZE 1
2b7fa3a8fa 2019-09-20 rkeene: #define HAVE_STRUCT_STAT_ST_BLOCKS 1
2b7fa3a8fa 2019-09-20 rkeene: #ifdef WIN32
2b7fa3a8fa 2019-09-20 rkeene: # undef HAVE_STRUCT_STAT_ST_BLKSIZE
2b7fa3a8fa 2019-09-20 rkeene: # undef HAVE_STRUCT_STAT_ST_BLOCKS
2b7fa3a8fa 2019-09-20 rkeene: #endif
2b7fa3a8fa 2019-09-20 rkeene:
c57d8bead8 2019-09-20 rkeene: #ifndef MIN
2b7fa3a8fa 2019-09-20 rkeene: #define MIN(a, b) (((a) < (b)) ? (a) : (b))
c57d8bead8 2019-09-20 rkeene: #endif
2b7fa3a8fa 2019-09-20 rkeene:
c57d8bead8 2019-09-20 rkeene: #ifndef HAVE_DEFINED_XVFS_FILE_TYPE_T
c57d8bead8 2019-09-20 rkeene: #define HAVE_DEFINED_XVFS_FILE_TYPE_T 1
2b7fa3a8fa 2019-09-20 rkeene: typedef enum {
2b7fa3a8fa 2019-09-20 rkeene: XVFS_FILE_TYPE_REG,
2b7fa3a8fa 2019-09-20 rkeene: XVFS_FILE_TYPE_DIR
2b7fa3a8fa 2019-09-20 rkeene: } xvfs_file_type_t;
c57d8bead8 2019-09-20 rkeene: #endif
2b7fa3a8fa 2019-09-20 rkeene:
c57d8bead8 2019-09-20 rkeene: #ifndef HAVE_DEFINED_XVFS_SIZE_T
c57d8bead8 2019-09-20 rkeene: #define HAVE_DEFINED_XVFS_SIZE_T 1
2b7fa3a8fa 2019-09-20 rkeene: typedef Tcl_WideInt xvfs_size_t;
c57d8bead8 2019-09-20 rkeene: #endif
2b7fa3a8fa 2019-09-20 rkeene:
c57d8bead8 2019-09-20 rkeene: #ifndef HAVE_DEFINED_XVFS_FILE_DATA
c57d8bead8 2019-09-20 rkeene: #define HAVE_DEFINED_XVFS_FILE_DATA 1
2b7fa3a8fa 2019-09-20 rkeene: struct xvfs_file_data {
a719156faf 2019-10-09 rkeene: const char * const name;
a719156faf 2019-10-09 rkeene: const xvfs_file_type_t type;
a719156faf 2019-10-09 rkeene: const xvfs_size_t size;
2b7fa3a8fa 2019-09-20 rkeene: union {
a719156faf 2019-10-09 rkeene: const unsigned char * const fileContents;
2b7fa3a8fa 2019-09-20 rkeene: const char **dirChildren;
2b7fa3a8fa 2019-09-20 rkeene: } data;
2b7fa3a8fa 2019-09-20 rkeene: };
c57d8bead8 2019-09-20 rkeene: #endif
2b7fa3a8fa 2019-09-20 rkeene:
2b7fa3a8fa 2019-09-20 rkeene: <?
2b7fa3a8fa 2019-09-20 rkeene: package require xvfs
2b7fa3a8fa 2019-09-20 rkeene:
eebfe1f40f 2020-03-25 rkeene: set ::xvfs::fileInfoStruct [xvfs::main $::xvfs::argv]
eebfe1f40f 2020-03-25 rkeene: ?><?= $::xvfs::fileInfoStruct ?>
2b7fa3a8fa 2019-09-20 rkeene: static long xvfs_<?= $::xvfs::fsName ?>_nameToIndex(const char *path) {
37d00c3cfb 2019-11-04 rkeene: <?
eebfe1f40f 2020-03-25 rkeene: 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 rkeene: set hashTableHeader [dict get $hashTable header]
eebfe1f40f 2020-03-25 rkeene: ?><?= $hashTableHeader ?>
eebfe1f40f 2020-03-25 rkeene: long pathIndex;
a719156faf 2019-10-09 rkeene: ssize_t pathLen;
a719156faf 2019-10-09 rkeene:
2b7fa3a8fa 2019-09-20 rkeene: if (path == NULL) {
2b7fa3a8fa 2019-09-20 rkeene: return(XVFS_NAME_LOOKUP_ERROR);
2b7fa3a8fa 2019-09-20 rkeene: }
2b7fa3a8fa 2019-09-20 rkeene:
2b7fa3a8fa 2019-09-20 rkeene: pathLen = strlen(path);
a719156faf 2019-10-09 rkeene:
eebfe1f40f 2020-03-25 rkeene: <?= [dict get $hashTable body] ?>
eebfe1f40f 2020-03-25 rkeene:
2b7fa3a8fa 2019-09-20 rkeene: return(XVFS_NAME_LOOKUP_ERROR);
2b7fa3a8fa 2019-09-20 rkeene: }
2b7fa3a8fa 2019-09-20 rkeene:
2b7fa3a8fa 2019-09-20 rkeene: static const char **xvfs_<?= $::xvfs::fsName ?>_getChildren(const char *path, Tcl_WideInt *count) {
2b7fa3a8fa 2019-09-20 rkeene: const struct xvfs_file_data *fileInfo;
2b7fa3a8fa 2019-09-20 rkeene: long inode;
2b7fa3a8fa 2019-09-20 rkeene:
2b7fa3a8fa 2019-09-20 rkeene: /*
2b7fa3a8fa 2019-09-20 rkeene: * Validate input parameters
2b7fa3a8fa 2019-09-20 rkeene: */
2b7fa3a8fa 2019-09-20 rkeene: if (count == NULL) {
2b7fa3a8fa 2019-09-20 rkeene: return(NULL);
2b7fa3a8fa 2019-09-20 rkeene: }
2b7fa3a8fa 2019-09-20 rkeene:
2b7fa3a8fa 2019-09-20 rkeene: /*
2b7fa3a8fa 2019-09-20 rkeene: * Get the inode from the lookup function
2b7fa3a8fa 2019-09-20 rkeene: */
2b7fa3a8fa 2019-09-20 rkeene: inode = xvfs_<?= $::xvfs::fsName ?>_nameToIndex(path);
2b7fa3a8fa 2019-09-20 rkeene: if (inode == XVFS_NAME_LOOKUP_ERROR) {
2b7fa3a8fa 2019-09-20 rkeene: *count = XVFS_RV_ERR_ENOENT;
2b7fa3a8fa 2019-09-20 rkeene: return(NULL);
2b7fa3a8fa 2019-09-20 rkeene: }
2b7fa3a8fa 2019-09-20 rkeene:
2b7fa3a8fa 2019-09-20 rkeene: fileInfo = &xvfs_<?= $::xvfs::fsName ?>_data[inode];
2b7fa3a8fa 2019-09-20 rkeene:
2b7fa3a8fa 2019-09-20 rkeene: /*
2b7fa3a8fa 2019-09-20 rkeene: * Ensure this is a directory
2b7fa3a8fa 2019-09-20 rkeene: */
2b7fa3a8fa 2019-09-20 rkeene: if (fileInfo->type != XVFS_FILE_TYPE_DIR) {
2b7fa3a8fa 2019-09-20 rkeene: *count = XVFS_RV_ERR_ENOTDIR;
2b7fa3a8fa 2019-09-20 rkeene: return(NULL);
2b7fa3a8fa 2019-09-20 rkeene: }
2b7fa3a8fa 2019-09-20 rkeene:
2b7fa3a8fa 2019-09-20 rkeene: *count = fileInfo->size;
2b7fa3a8fa 2019-09-20 rkeene: return(fileInfo->data.dirChildren);
2b7fa3a8fa 2019-09-20 rkeene: }
2b7fa3a8fa 2019-09-20 rkeene:
2b7fa3a8fa 2019-09-20 rkeene: static const unsigned char *xvfs_<?= $::xvfs::fsName ?>_getData(const char *path, Tcl_WideInt start, Tcl_WideInt *length) {
2b7fa3a8fa 2019-09-20 rkeene: const struct xvfs_file_data *fileInfo;
2b7fa3a8fa 2019-09-20 rkeene: Tcl_WideInt resultLength;
2b7fa3a8fa 2019-09-20 rkeene: long inode;
2b7fa3a8fa 2019-09-20 rkeene:
2b7fa3a8fa 2019-09-20 rkeene: /*
2b7fa3a8fa 2019-09-20 rkeene: * Validate input parameters
2b7fa3a8fa 2019-09-20 rkeene: */
2b7fa3a8fa 2019-09-20 rkeene: if (length == NULL) {
2b7fa3a8fa 2019-09-20 rkeene: return(NULL);
2b7fa3a8fa 2019-09-20 rkeene: }
2b7fa3a8fa 2019-09-20 rkeene:
2b7fa3a8fa 2019-09-20 rkeene: if (start < 0) {
2b7fa3a8fa 2019-09-20 rkeene: *length = XVFS_RV_ERR_EINVAL;
2b7fa3a8fa 2019-09-20 rkeene: return(NULL);
2b7fa3a8fa 2019-09-20 rkeene: }
2b7fa3a8fa 2019-09-20 rkeene:
2b7fa3a8fa 2019-09-20 rkeene: if (*length < 0) {
2b7fa3a8fa 2019-09-20 rkeene: *length = XVFS_RV_ERR_EINVAL;
2b7fa3a8fa 2019-09-20 rkeene: return(NULL);
2b7fa3a8fa 2019-09-20 rkeene: }
2b7fa3a8fa 2019-09-20 rkeene:
2b7fa3a8fa 2019-09-20 rkeene: /*
2b7fa3a8fa 2019-09-20 rkeene: * Get the inode from the lookup function
2b7fa3a8fa 2019-09-20 rkeene: */
2b7fa3a8fa 2019-09-20 rkeene: inode = xvfs_<?= $::xvfs::fsName ?>_nameToIndex(path);
2b7fa3a8fa 2019-09-20 rkeene: if (inode == XVFS_NAME_LOOKUP_ERROR) {
2b7fa3a8fa 2019-09-20 rkeene: *length = XVFS_RV_ERR_ENOENT;
2b7fa3a8fa 2019-09-20 rkeene: return(NULL);
2b7fa3a8fa 2019-09-20 rkeene: }
2b7fa3a8fa 2019-09-20 rkeene:
2b7fa3a8fa 2019-09-20 rkeene: fileInfo = &xvfs_<?= $::xvfs::fsName ?>_data[inode];
2b7fa3a8fa 2019-09-20 rkeene:
2b7fa3a8fa 2019-09-20 rkeene: /*
2b7fa3a8fa 2019-09-20 rkeene: * Ensure this is a file that can be read
2b7fa3a8fa 2019-09-20 rkeene: */
2b7fa3a8fa 2019-09-20 rkeene: if (fileInfo->type != XVFS_FILE_TYPE_REG) {
2b7fa3a8fa 2019-09-20 rkeene: *length = XVFS_RV_ERR_EISDIR;
2b7fa3a8fa 2019-09-20 rkeene: return(NULL);
2b7fa3a8fa 2019-09-20 rkeene: }
2b7fa3a8fa 2019-09-20 rkeene:
2b7fa3a8fa 2019-09-20 rkeene: /*
2b7fa3a8fa 2019-09-20 rkeene: * Validate the length
2b7fa3a8fa 2019-09-20 rkeene: */
2b7fa3a8fa 2019-09-20 rkeene: if (start > fileInfo->size) {
2b7fa3a8fa 2019-09-20 rkeene: *length = XVFS_RV_ERR_EFAULT;
2b7fa3a8fa 2019-09-20 rkeene: return(NULL);
2b7fa3a8fa 2019-09-20 rkeene: }
2b7fa3a8fa 2019-09-20 rkeene:
2b7fa3a8fa 2019-09-20 rkeene: if (*length == 0) {
2b7fa3a8fa 2019-09-20 rkeene: resultLength = fileInfo->size - start;
2b7fa3a8fa 2019-09-20 rkeene: } else {
2b7fa3a8fa 2019-09-20 rkeene: resultLength = MIN(fileInfo->size - start, *length);
2b7fa3a8fa 2019-09-20 rkeene: }
2b7fa3a8fa 2019-09-20 rkeene: *length = resultLength;
2b7fa3a8fa 2019-09-20 rkeene:
2b7fa3a8fa 2019-09-20 rkeene: /*
2b7fa3a8fa 2019-09-20 rkeene: * Return the data
2b7fa3a8fa 2019-09-20 rkeene: */
2b7fa3a8fa 2019-09-20 rkeene: return(fileInfo->data.fileContents + start);
2b7fa3a8fa 2019-09-20 rkeene: }
2b7fa3a8fa 2019-09-20 rkeene:
2b7fa3a8fa 2019-09-20 rkeene: static int xvfs_<?= $::xvfs::fsName ?>_getStat(const char *path, Tcl_StatBuf *statBuf) {
2b7fa3a8fa 2019-09-20 rkeene: const struct xvfs_file_data *fileInfo;
2b7fa3a8fa 2019-09-20 rkeene: long inode;
2b7fa3a8fa 2019-09-20 rkeene:
2b7fa3a8fa 2019-09-20 rkeene: /*
2b7fa3a8fa 2019-09-20 rkeene: * Validate input parameters
2b7fa3a8fa 2019-09-20 rkeene: */
2b7fa3a8fa 2019-09-20 rkeene: if (!statBuf) {
2b7fa3a8fa 2019-09-20 rkeene: return(XVFS_RV_ERR_EINVAL);
2b7fa3a8fa 2019-09-20 rkeene: }
2b7fa3a8fa 2019-09-20 rkeene:
2b7fa3a8fa 2019-09-20 rkeene: /*
2b7fa3a8fa 2019-09-20 rkeene: * Get the inode from the lookup function
2b7fa3a8fa 2019-09-20 rkeene: */
2b7fa3a8fa 2019-09-20 rkeene: inode = xvfs_<?= $::xvfs::fsName ?>_nameToIndex(path);
2b7fa3a8fa 2019-09-20 rkeene: if (inode == XVFS_NAME_LOOKUP_ERROR) {
2b7fa3a8fa 2019-09-20 rkeene: return(XVFS_RV_ERR_ENOENT);
2b7fa3a8fa 2019-09-20 rkeene: }
2b7fa3a8fa 2019-09-20 rkeene:
2b7fa3a8fa 2019-09-20 rkeene: fileInfo = &xvfs_<?= $::xvfs::fsName ?>_data[inode];
2b7fa3a8fa 2019-09-20 rkeene:
eebfe1f40f 2020-03-25 rkeene: statBuf->st_dev = <?= [zlib adler32 $::xvfs::fsName 0] ?>;
eebfe1f40f 2020-03-25 rkeene: statBuf->st_rdev = <?= [zlib adler32 $::xvfs::fsName 0] ?>;
2b7fa3a8fa 2019-09-20 rkeene: statBuf->st_ino = inode;
2b7fa3a8fa 2019-09-20 rkeene: statBuf->st_uid = 0;
2b7fa3a8fa 2019-09-20 rkeene: statBuf->st_gid = 0;
2b7fa3a8fa 2019-09-20 rkeene: statBuf->st_atime = 0;
2b7fa3a8fa 2019-09-20 rkeene: statBuf->st_ctime = 0;
2b7fa3a8fa 2019-09-20 rkeene: statBuf->st_mtime = 0;
2b7fa3a8fa 2019-09-20 rkeene: #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
2b7fa3a8fa 2019-09-20 rkeene: statBuf->st_blksize = XVFS_FILE_BLOCKSIZE;
2b7fa3a8fa 2019-09-20 rkeene: #endif
2b7fa3a8fa 2019-09-20 rkeene:
2b7fa3a8fa 2019-09-20 rkeene: if (fileInfo->type == XVFS_FILE_TYPE_REG) {
2b7fa3a8fa 2019-09-20 rkeene: statBuf->st_mode = 0100444;
2b7fa3a8fa 2019-09-20 rkeene: statBuf->st_nlink = 1;
2b7fa3a8fa 2019-09-20 rkeene: statBuf->st_size = fileInfo->size;
2b7fa3a8fa 2019-09-20 rkeene: #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
2b7fa3a8fa 2019-09-20 rkeene: statBuf->st_blocks = (fileInfo->size + statBuf->st_blksize - 1) / statBuf->st_blksize;
2b7fa3a8fa 2019-09-20 rkeene: #endif
2b7fa3a8fa 2019-09-20 rkeene: } else if (fileInfo->type == XVFS_FILE_TYPE_DIR) {
2b7fa3a8fa 2019-09-20 rkeene: statBuf->st_mode = 040555;
2b7fa3a8fa 2019-09-20 rkeene: statBuf->st_nlink = fileInfo->size;
2b7fa3a8fa 2019-09-20 rkeene: statBuf->st_size = fileInfo->size;
2b7fa3a8fa 2019-09-20 rkeene: #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
2b7fa3a8fa 2019-09-20 rkeene: statBuf->st_blocks = 1;
2b7fa3a8fa 2019-09-20 rkeene: #endif
2b7fa3a8fa 2019-09-20 rkeene: }
2b7fa3a8fa 2019-09-20 rkeene:
2b7fa3a8fa 2019-09-20 rkeene: return(0);
2b7fa3a8fa 2019-09-20 rkeene: }
2b7fa3a8fa 2019-09-20 rkeene:
2b7fa3a8fa 2019-09-20 rkeene: static struct Xvfs_FSInfo xvfs_<?= $::xvfs::fsName ?>_fsInfo = {
2b7fa3a8fa 2019-09-20 rkeene: .protocolVersion = XVFS_PROTOCOL_VERSION,
2b7fa3a8fa 2019-09-20 rkeene: .name = "<?= $::xvfs::fsName ?>",
2b7fa3a8fa 2019-09-20 rkeene: .getChildrenProc = xvfs_<?= $::xvfs::fsName ?>_getChildren,
2b7fa3a8fa 2019-09-20 rkeene: .getDataProc = xvfs_<?= $::xvfs::fsName ?>_getData,
2b7fa3a8fa 2019-09-20 rkeene: .getStatProc = xvfs_<?= $::xvfs::fsName ?>_getStat
2b7fa3a8fa 2019-09-20 rkeene: };
2b7fa3a8fa 2019-09-20 rkeene:
2b7fa3a8fa 2019-09-20 rkeene: int Xvfs_<?= $::xvfs::fsName ?>_Init(Tcl_Interp *interp) {
2b7fa3a8fa 2019-09-20 rkeene: int register_ret;
2b7fa3a8fa 2019-09-20 rkeene:
2b7fa3a8fa 2019-09-20 rkeene: #ifdef USE_TCL_STUBS
2b7fa3a8fa 2019-09-20 rkeene: const char *tclInitStubs_ret;
2b7fa3a8fa 2019-09-20 rkeene: /* Initialize Stubs */
2b7fa3a8fa 2019-09-20 rkeene: tclInitStubs_ret = Tcl_InitStubs(interp, TCL_PATCH_LEVEL, 0);
2b7fa3a8fa 2019-09-20 rkeene: if (!tclInitStubs_ret) {
2b7fa3a8fa 2019-09-20 rkeene: return(TCL_ERROR);
2b7fa3a8fa 2019-09-20 rkeene: }
2b7fa3a8fa 2019-09-20 rkeene: #endif
2b7fa3a8fa 2019-09-20 rkeene:
2b7fa3a8fa 2019-09-20 rkeene: register_ret = Xvfs_Register(interp, &xvfs_<?= $::xvfs::fsName ?>_fsInfo);
2b7fa3a8fa 2019-09-20 rkeene: if (register_ret != TCL_OK) {
2b7fa3a8fa 2019-09-20 rkeene: return(register_ret);
2b7fa3a8fa 2019-09-20 rkeene: }
2b7fa3a8fa 2019-09-20 rkeene:
2b7fa3a8fa 2019-09-20 rkeene: return(TCL_OK);
2b7fa3a8fa 2019-09-20 rkeene: }
2b7fa3a8fa 2019-09-20 rkeene: #undef XVFS_NAME_LOOKUP_ERROR
2b7fa3a8fa 2019-09-20 rkeene: #undef XVFS_FILE_BLOCKSIZE