09e53d3c38 2019-09-20 rkeene: <?
ae8437b96b 2019-12-01 rkeene: # Care must be taken when editing this file as
ae8437b96b 2019-12-01 rkeene: # it may be processed by either "xvfs-create"
ae8437b96b 2019-12-01 rkeene: # which uses a full Tcl and Rivet parser
ae8437b96b 2019-12-01 rkeene: # or by "xvfs-create-c" which uses a much
ae8437b96b 2019-12-01 rkeene: # simpler one that only knows about printable
ae8437b96b 2019-12-01 rkeene: # sections and ignores all Tcl sections
ae8437b96b 2019-12-01 rkeene:
09e53d3c38 2019-09-20 rkeene: if {[info exists ::xvfs::xvfsCoreH]} {
ae8437b96b 2019-12-01 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:
ae8437b96b 2019-12-01 rkeene: set ::xvfs::fileInfoStruct [xvfs::main $::xvfs::argv]
ae8437b96b 2019-12-01 rkeene: ?><?= $::xvfs::fileInfoStruct ?>
2b7fa3a8fa 2019-09-20 rkeene: static long xvfs_<?= $::xvfs::fsName ?>_nameToIndex(const char *path) {
37d00c3cfb 2019-11-04 rkeene: <?
0ffa676110 2019-12-02 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);"]
0ffa676110 2019-12-02 rkeene: set hashTableHeader [dict get $hashTable header]
0ffa676110 2019-12-02 rkeene: ?><?= $hashTableHeader ?>
0ffa676110 2019-12-02 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:
0ffa676110 2019-12-02 rkeene: <?= [dict get $hashTable body] ?>
0ffa676110 2019-12-02 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:
0ffa676110 2019-12-02 rkeene: statBuf->st_dev = <?= [zlib adler32 $::xvfs::fsName 0] ?>;
0ffa676110 2019-12-02 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