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