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