Annotation For xvfs.c.rvt

Lines of xvfs.c.rvt from check-in 48adf54b98 that are changed by the sequence of edits moving toward check-in 5583d77f1c:

                         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: #define MIN(a, b) (((a) < (b)) ? (a) : (b))
                        11: 
                        12: typedef enum {
                        13: 	XVFS_FILE_TYPE_REG,
                        14: 	XVFS_FILE_TYPE_DIR
                        15: } xvfs_file_type_t;
                        16: 
                        17: typedef Tcl_WideInt xvfs_size_t;
                        18: 
                        19: struct xvfs_file_data {
                        20: 	const char          *name;
                        21: 	xvfs_file_type_t    type;
                        22: 	xvfs_size_t         size;
                        23: 	union {
                        24: 		const unsigned char *fileContents;
                        25: 		const char          **dirChildren;
                        26: 	} data;
                        27: };
                        28: 
                        29: <?
                        30: 	package require xvfs
                        31: 	xvfs::main $argv
                        32: ?>
                        33: static long xvfs_<?= $::xvfs::fsName ?>_nameToIndex(const char *path) {
                        34: 	unsigned int pathHash;
                        35: 	size_t pathLen;
                        36: 	
                        37: 	if (path == NULL) {
                        38: 		return(XVFS_NAME_LOOKUP_ERROR);
                        39: 	}
                        40: 
                        41: 	pathLen = strlen(path);
                        42: 	pathHash = Tcl_ZlibAdler32(0, (const unsigned char *) path, pathLen);
                        43: 	switch (pathHash) {
                        44: <?
                        45: 	for {set index 0} {$index < [llength $::xvfs::outputFiles]} {incr index} {
                        46: 		set outputFile [lindex $::xvfs::outputFiles $index]
                        47: 		set outputFileHash [zlib adler32 $outputFile 0]
                        48: 		lappend outputFileHashToIndex($outputFileHash) $index
                        49: 	}
                        50: 	
                        51: 	foreach {outputFileHash outputFileIndexes} [lsort -stride 2 -dictionary [array get outputFileHashToIndex]] {
                        52: ?>		case <?= $outputFileHash ?>:
                        53: <?
                        54: 			foreach outputFileIndex $outputFileIndexes {
                        55: 				set outputFileName [lindex $::xvfs::outputFiles $outputFileIndex]
                        56: 				set outputFileNameLen [string length $outputFileName]
                        57: ?>			if (pathLen == <?= $outputFileNameLen ?> && memcmp(path, xvfs_<?= $::xvfs::fsName ?>_data[<?= $outputFileIndex ?>].name, pathLen) == 0) {
                        58: 				return(<?= $outputFileIndex ?>);
                        59: 			}
                        60: <?
                        61: 			}
                        62: ?>			break;
                        63: <?	} ?>
                        64: 	}
                        65: 	
                        66: 	return(XVFS_NAME_LOOKUP_ERROR);
                        67: }
                        68: 
                        69: static const char **xvfs_<?= $::xvfs::fsName ?>_getChildren(const char *path, Tcl_WideInt *count) {
                        70: 	struct xvfs_file_data *fileInfo;
                        71: 	long inode;
                        72: 
                        73: 	/*
                        74: 	 * Validate input parameters
                        75: 	 */
                        76: 	if (count == NULL) {
                        77: 		return(NULL);
                        78: 	}
                        79: 	
                        80: 	/*
                        81: 	 * Get the inode from the lookup function
                        82: 	 */
                        83: 	inode = xvfs_<?= $::xvfs::fsName ?>_nameToIndex(path);
                        84: 	if (inode == XVFS_NAME_LOOKUP_ERROR) {
                        85: 		*count = XVFS_RV_ERR_ENOENT;
                        86: 		return(NULL);
                        87: 	}
                        88: 	
                        89: 	fileInfo = &xvfs_<?= $::xvfs::fsName ?>_data[inode];
                        90: 
                        91: 	/*
                        92: 	 * Ensure this is a directory
                        93: 	 */
                        94: 	if (fileInfo->type != XVFS_FILE_TYPE_DIR) {
                        95: 		*count = XVFS_RV_ERR_ENOTDIR;
                        96: 		return(NULL);
                        97: 	}
                        98: 	
                        99: 	*count = fileInfo->size;
                       100: 	return(fileInfo->data.dirChildren);
                       101: }
                       102: 
                       103: static const unsigned char *xvfs_<?= $::xvfs::fsName ?>_getData(const char *path, Tcl_WideInt start, Tcl_WideInt *length) {
                       104: 	struct xvfs_file_data *fileInfo;
                       105: 	Tcl_WideInt resultLength;
                       106: 	long inode;
                       107: 
                       108: 	/*
                       109: 	 * Validate input parameters
                       110: 	 */
                       111: 	if (length == NULL) {
                       112: 		return(NULL);
                       113: 	}
                       114: 	
                       115: 	if (start < 0) {
                       116: 		*length = XVFS_RV_ERR_EINVAL;
                       117: 		return(NULL);
                       118: 	}
                       119: 	
                       120: 	if (*length < 0) {
                       121: 		*length = XVFS_RV_ERR_EINVAL;
                       122: 		return(NULL);
                       123: 	}
                       124: 	
                       125: 	/*
                       126: 	 * Get the inode from the lookup function
                       127: 	 */
                       128: 	inode = xvfs_<?= $::xvfs::fsName ?>_nameToIndex(path);
                       129: 	if (inode == XVFS_NAME_LOOKUP_ERROR) {
                       130: 		*length = XVFS_RV_ERR_ENOENT;
                       131: 		return(NULL);
                       132: 	}
                       133: 	
                       134: 	fileInfo = &xvfs_<?= $::xvfs::fsName ?>_data[inode];
                       135: 
                       136: 	/*
                       137: 	 * Ensure this is a file that can be read
                       138: 	 */
                       139: 	if (fileInfo->type != XVFS_FILE_TYPE_REG) {
                       140: 		*length = XVFS_RV_ERR_EISDIR;
                       141: 		return(NULL);
                       142: 	}
                       143: 
                       144: 	/*
                       145: 	 * Validate the length
                       146: 	 */
                       147: 	if (start > fileInfo->size) {
                       148: 		*length = XVFS_RV_ERR_EFAULT;
                       149: 		return(NULL);
                       150: 	}
                       151: 
                       152: 	if (*length == 0) {
                       153: 		resultLength = fileInfo->size - start;
                       154: 	} else {
                       155: 		resultLength = MIN(fileInfo->size - start, *length);
                       156: 	}
                       157: 	*length = resultLength;
                       158: 
                       159: 	/*
                       160: 	 * Return the data
                       161: 	 */
                       162: 	return(fileInfo->data.fileContents + start);
                       163: }
                       164: 
                       165: static int xvfs_<?= $::xvfs::fsName ?>_getStat(const char *path, Tcl_StatBuf *statBuf) {
                       166: 	struct xvfs_file_data *fileInfo;
                       167: 	long inode;
                       168: 
                       169: 	/*
                       170: 	 * Validate input parameters
                       171: 	 */
                       172: 	if (!statBuf) {
                       173: 		return(XVFS_RV_ERR_EINVAL);
                       174: 	}
                       175: 	
                       176: 	/*
                       177: 	 * Get the inode from the lookup function
                       178: 	 */
                       179: 	inode = xvfs_<?= $::xvfs::fsName ?>_nameToIndex(path);
                       180: 	if (inode == XVFS_NAME_LOOKUP_ERROR) {
                       181: 		return(XVFS_RV_ERR_ENOENT);
                       182: 	}
                       183: 	
                       184: 	fileInfo = &xvfs_<?= $::xvfs::fsName ?>_data[inode];
                       185: 	
                       186: 	statBuf->st_dev   = 0;
                       187: 	statBuf->st_rdev  = 0;
                       188: 	statBuf->st_ino   = inode;
                       189: 	statBuf->st_uid   = -1;
                       190: 	statBuf->st_gid   = -1;
                       191: 	statBuf->st_atime = 0;
                       192: 	statBuf->st_ctime = 0;
                       193: 	statBuf->st_mtime = 0;
                       194: 	statBuf->st_blksize = XVFS_FILE_BLOCKSIZE;
                       195: 	
                       196: 	if (fileInfo->type == XVFS_FILE_TYPE_REG) {
48adf54b98 2019-09-13  197: 		statBuf->st_mode   = 0400;
                       198: 		statBuf->st_nlink  = 1;
                       199: 		statBuf->st_size   = fileInfo->size;
                       200: 		statBuf->st_blocks = (fileInfo->size + statBuf->st_blksize - 1) / statBuf->st_blksize;
                       201: 	} else if (fileInfo->type == XVFS_FILE_TYPE_DIR) {
48adf54b98 2019-09-13  202: 		statBuf->st_mode   = 0500;
                       203: 		statBuf->st_nlink  = fileInfo->size;
                       204: 		statBuf->st_size   = fileInfo->size;
                       205: 		statBuf->st_blocks = 1;
                       206: 	}
                       207: 	
                       208: 	return(0);
                       209: }
                       210: 
                       211: 
                       212: static struct Xvfs_FSInfo xvfs_<?= $::xvfs::fsName ?>_fsInfo = {
                       213: 	.protocolVersion = XVFS_PROTOCOL_VERSION,
                       214: 	.name            = "<?= $::xvfs::fsName ?>",
                       215: 	.getChildrenProc = xvfs_<?= $::xvfs::fsName ?>_getChildren,
                       216: 	.getDataProc     = xvfs_<?= $::xvfs::fsName ?>_getData,
                       217: 	.getStatProc     = xvfs_<?= $::xvfs::fsName ?>_getStat
                       218: };
                       219: 
                       220: int Xvfs_<?= $::xvfs::fsName ?>_Init(Tcl_Interp *interp) {
                       221: 	int register_ret;
                       222: 
                       223: #ifdef USE_TCL_STUBS
                       224: 	const char *tclInitStubs_ret;
                       225: 	/* Initialize Stubs */
                       226: 	tclInitStubs_ret = Tcl_InitStubs(interp, TCL_PATCH_LEVEL, 0);
                       227: 	if (!tclInitStubs_ret) {
                       228: 		return(TCL_ERROR);
                       229: 	}
                       230: #endif
                       231: 	
                       232: 	register_ret = Xvfs_Register(interp, &xvfs_<?= $::xvfs::fsName ?>_fsInfo);
                       233: 	if (register_ret != TCL_OK) {
                       234: 		return(register_ret);
                       235: 	}
                       236: 	
                       237: 	return(TCL_OK);
                       238: }