⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 namei.c

📁 《嵌入式系统设计与实例开发实验教材二源码》Linux内核移植与编译实验
💻 C
📖 第 1 页 / 共 3 页
字号:
/* *  linux/fs/vfat/namei.c * *  Written 1992,1993 by Werner Almesberger * *  Windows95/Windows NT compatible extended MSDOS filesystem *    by Gordon Chaffee Copyright (C) 1995.  Send bug reports for the *    VFAT filesystem to <chaffee@cs.berkeley.edu>.  Specify *    what file operation caused you trouble and if you can duplicate *    the problem, send a script that demonstrates it. * *  Short name translation 1999, 2001 by Wolfram Pienkoss <wp@bszh.de> * *  Support Multibyte character and cleanup by *  				OGAWA Hirofumi <hirofumi@mail.parknet.co.jp> */#include <linux/module.h>#include <linux/sched.h>#include <linux/msdos_fs.h>#include <linux/nls.h>#include <linux/kernel.h>#include <linux/errno.h>#include <linux/string.h>#include <linux/ctype.h>#include <linux/stat.h>#include <linux/mm.h>#include <linux/slab.h>#define DEBUG_LEVEL 0#if (DEBUG_LEVEL >= 1)#  define PRINTK1(x) printk x#else#  define PRINTK1(x)#endif#if (DEBUG_LEVEL >= 2)#  define PRINTK2(x) printk x#else#  define PRINTK2(x)#endif#if (DEBUG_LEVEL >= 3)#  define PRINTK3(x) printk x#else#  define PRINTK3(x)#endifstatic int vfat_hashi(struct dentry *parent, struct qstr *qstr);static int vfat_hash(struct dentry *parent, struct qstr *qstr);static int vfat_cmpi(struct dentry *dentry, struct qstr *a, struct qstr *b);static int vfat_cmp(struct dentry *dentry, struct qstr *a, struct qstr *b);static int vfat_revalidate(struct dentry *dentry, int);static struct dentry_operations vfat_dentry_ops[4] = {	{		d_hash:		vfat_hashi,		d_compare:	vfat_cmpi,	},	{		d_revalidate:	vfat_revalidate,		d_hash:		vfat_hashi,		d_compare:	vfat_cmpi,	},	{		d_hash:		vfat_hash,		d_compare:	vfat_cmp,	},	{		d_revalidate:	vfat_revalidate,		d_hash:		vfat_hash,		d_compare:	vfat_cmp,	}};static int vfat_revalidate(struct dentry *dentry, int flags){	PRINTK1(("vfat_revalidate: %s\n", dentry->d_name.name));	spin_lock(&dcache_lock);	if (dentry->d_time == dentry->d_parent->d_inode->i_version) {		spin_unlock(&dcache_lock);		return 1;	}	spin_unlock(&dcache_lock);	return 0;}static int simple_getbool(char *s, int *setval){	if (s) {		if (!strcmp(s,"1") || !strcmp(s,"yes") || !strcmp(s,"true")) {			*setval = 1;		} else if (!strcmp(s,"0") || !strcmp(s,"no") || !strcmp(s,"false")) {			*setval = 0;		} else {			return 0;		}	} else {		*setval = 1;	}	return 1;}static int parse_options(char *options,	struct fat_mount_options *opts){	char *this_char,*value,save,*savep;	int ret, val;	opts->unicode_xlate = opts->posixfs = 0;	opts->numtail = 1;	opts->utf8 = 0;	opts->shortname = VFAT_SFN_DISPLAY_LOWER | VFAT_SFN_CREATE_WIN95;	/* for backward compatible */	if (opts->nocase) {		opts->nocase = 0;		opts->shortname = VFAT_SFN_DISPLAY_WIN95		  		| VFAT_SFN_CREATE_WIN95;	}	if (!options) return 1;	save = 0;	savep = NULL;	ret = 1;	for (this_char = strtok(options,","); this_char; this_char = strtok(NULL,",")) {		if ((value = strchr(this_char,'=')) != NULL) {			save = *value;			savep = value;			*value++ = 0;		}		if (!strcmp(this_char,"utf8")) {			ret = simple_getbool(value, &val);			if (ret) opts->utf8 = val;		} else if (!strcmp(this_char,"uni_xlate")) {			ret = simple_getbool(value, &val);			if (ret) opts->unicode_xlate = val;		} else if (!strcmp(this_char,"posix")) {			ret = simple_getbool(value, &val);			if (ret) opts->posixfs = val;		} else if (!strcmp(this_char,"nonumtail")) {			ret = simple_getbool(value, &val);			if (ret) {				opts->numtail = !val;			}		} else if (!strcmp(this_char, "shortname")) {			if (!strcmp(value, "lower"))				opts->shortname = VFAT_SFN_DISPLAY_LOWER						| VFAT_SFN_CREATE_WIN95;			else if (!strcmp(value, "win95"))				opts->shortname = VFAT_SFN_DISPLAY_WIN95						| VFAT_SFN_CREATE_WIN95;			else if (!strcmp(value, "winnt"))				opts->shortname = VFAT_SFN_DISPLAY_WINNT						| VFAT_SFN_CREATE_WINNT;			else if (!strcmp(value, "mixed"))				opts->shortname = VFAT_SFN_DISPLAY_WINNT						| VFAT_SFN_CREATE_WIN95;			else				ret = 0;		}		if (this_char != options)			*(this_char-1) = ',';		if (value) {			*savep = save;		}		if (ret == 0) {			return 0;		}	}	if (opts->unicode_xlate) {		opts->utf8 = 0;	}	return 1;}static inline unsigned charvfat_tolower(struct nls_table *t, unsigned char c){	unsigned char nc = t->charset2lower[c];	return nc ? nc : c;}static inline unsigned charvfat_toupper(struct nls_table *t, unsigned char c){	unsigned char nc = t->charset2upper[c];	return nc ? nc : c;}static intvfat_strnicmp(struct nls_table *t, const unsigned char *s1,					const unsigned char *s2, int len){	while(len--)		if (vfat_tolower(t, *s1++) != vfat_tolower(t, *s2++))			return 1;	return 0;}/* * Compute the hash for the vfat name corresponding to the dentry. * Note: if the name is invalid, we leave the hash code unchanged so * that the existing dentry can be used. The vfat fs routines will * return ENOENT or EINVAL as appropriate. */static int vfat_hash(struct dentry *dentry, struct qstr *qstr){	const char *name;	int len;	len = qstr->len;	name = qstr->name;	while (len && name[len-1] == '.')		len--;	qstr->hash = full_name_hash(name, len);	return 0;}/* * Compute the hash for the vfat name corresponding to the dentry. * Note: if the name is invalid, we leave the hash code unchanged so * that the existing dentry can be used. The vfat fs routines will * return ENOENT or EINVAL as appropriate. */static int vfat_hashi(struct dentry *dentry, struct qstr *qstr){	struct nls_table *t = MSDOS_SB(dentry->d_inode->i_sb)->nls_io;	const char *name;	int len;	unsigned long hash;	len = qstr->len;	name = qstr->name;	while (len && name[len-1] == '.')		len--;	hash = init_name_hash();	while (len--)		hash = partial_name_hash(vfat_tolower(t, *name++), hash);	qstr->hash = end_name_hash(hash);	return 0;}/* * Case insensitive compare of two vfat names. */static int vfat_cmpi(struct dentry *dentry, struct qstr *a, struct qstr *b){	struct nls_table *t = MSDOS_SB(dentry->d_inode->i_sb)->nls_io;	int alen, blen;	/* A filename cannot end in '.' or we treat it like it has none */	alen = a->len;	blen = b->len;	while (alen && a->name[alen-1] == '.')		alen--;	while (blen && b->name[blen-1] == '.')		blen--;	if (alen == blen) {		if (vfat_strnicmp(t, a->name, b->name, alen) == 0)			return 0;	}	return 1;}/* * Case sensitive compare of two vfat names. */static int vfat_cmp(struct dentry *dentry, struct qstr *a, struct qstr *b){	int alen, blen;	/* A filename cannot end in '.' or we treat it like it has none */	alen = a->len;	blen = b->len;	while (alen && a->name[alen-1] == '.')		alen--;	while (blen && b->name[blen-1] == '.')		blen--;	if (alen == blen) {		if (strncmp(a->name, b->name, alen) == 0)			return 0;	}	return 1;}#ifdef DEBUGstatic void dump_fat(struct super_block *sb,int start){	printk("[");	while (start) {		printk("%d ",start);		start = fat_access(sb,start,-1);		if (!start) {			printk("ERROR");			break;		}		if (start == -1) break;	}	printk("]\n");}static void dump_de(struct msdos_dir_entry *de){	int i;	unsigned char *p = (unsigned char *) de;	printk("[");	for (i = 0; i < 32; i++, p++) {		printk("%02x ", *p);	}	printk("]\n");}#endif/* MS-DOS "device special files" */static const char *reserved3_names[] = {	"con     ", "prn     ", "nul     ", "aux     ", NULL};static const char *reserved4_names[] = {	"com1    ", "com2    ", "com3    ", "com4    ", "com5    ",	"com6    ", "com7    ", "com8    ", "com9    ",	"lpt1    ", "lpt2    ", "lpt3    ", "lpt4    ", "lpt5    ",	"lpt6    ", "lpt7    ", "lpt8    ", "lpt9    ",	NULL };/* Characters that are undesirable in an MS-DOS file name */static wchar_t bad_chars[] = {	/*  `*'     `?'     `<'    `>'      `|'     `"'     `:'     `/' */	0x002A, 0x003F, 0x003C, 0x003E, 0x007C, 0x0022, 0x003A, 0x002F,	/*  `\' */	0x005C, 0,};#define IS_BADCHAR(uni)	(vfat_unistrchr(bad_chars, (uni)) != NULL)static wchar_t replace_chars[] = {	/*  `['     `]'    `;'     `,'     `+'      `=' */	0x005B, 0x005D, 0x003B, 0x002C, 0x002B, 0x003D, 0,};#define IS_REPLACECHAR(uni)	(vfat_unistrchr(replace_chars, (uni)) != NULL)static wchar_t skip_chars[] = {	/*  `.'     ` ' */	0x002E, 0x0020, 0,};#define IS_SKIPCHAR(uni) \	((wchar_t)(uni) == skip_chars[0] || (wchar_t)(uni) == skip_chars[1])static inline wchar_t *vfat_unistrchr(const wchar_t *s, const wchar_t c){	for(; *s != c; ++s)		if (*s == 0)			return NULL;	return (wchar_t *) s;}static inline int vfat_is_used_badchars(const wchar_t *s, int len){	int i;		for (i = 0; i < len; i++)		if (s[i] < 0x0020 || IS_BADCHAR(s[i]))			return -EINVAL;	return 0;}/* Checks the validity of a long MS-DOS filename *//* Returns negative number on error, 0 for a normal * return, and 1 for . or .. */static int vfat_valid_longname(const char *name, int len, int xlate){	const char **reserved, *walk;	int baselen;	if (len && name[len-1] == ' ') return -EINVAL;	if (len >= 256) return -EINVAL; 	if (len < 3) return 0;	for (walk = name; *walk != 0 && *walk != '.'; walk++);	baselen = walk - name;	if (baselen == 3) {		for (reserved = reserved3_names; *reserved; reserved++) {			if (!strnicmp(name,*reserved,baselen))				return -EINVAL;		}	} else if (baselen == 4) {		for (reserved = reserved4_names; *reserved; reserved++) {			if (!strnicmp(name,*reserved,baselen))				return -EINVAL;		}	}	return 0;}static int vfat_find_form(struct inode *dir,char *name){	struct msdos_dir_entry *de;	struct buffer_head *bh = NULL;	int ino,res;	res=fat_scan(dir,name,&bh,&de,&ino);	fat_brelse(dir->i_sb, bh);	if (res<0)		return -ENOENT;	return 0;}/*  * 1) Valid characters for the 8.3 format alias are any combination of * letters, uppercase alphabets, digits, any of the * following special characters: *     $ % ' ` - @ { } ~ ! # ( ) & _ ^ * In this case Longfilename is not stored in disk. * * WinNT's Extension: * File name and extension name is contain uppercase/lowercase * only. And it is expressed by CASE_LOWER_BASE and CASE_LOWER_EXT. *     

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -