Annotation For lib/xvfs/xvfs.c.rvt

Lines of lib/xvfs/xvfs.c.rvt from check-in c57d8bead8 that are changed by the sequence of edits moving toward check-in 09e53d3c38:

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