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