Annotation For xvfs.c.rvt

Origin for each line in xvfs.c.rvt from check-in 745e0c0839:

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