Commit d6886116 authored by OGAWA Hirofumi's avatar OGAWA Hirofumi Committed by Linus Torvalds

fat: cleanup fs/fat/dir.c

This is no logic changes, just cleans fs/fat/dir.c up.
Signed-off-by: default avatarOGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 531f710f
...@@ -123,10 +123,11 @@ static inline int fat_get_entry(struct inode *dir, loff_t *pos, ...@@ -123,10 +123,11 @@ static inline int fat_get_entry(struct inode *dir, loff_t *pos,
* but ignore that right now. * but ignore that right now.
* Ahem... Stack smashing in ring 0 isn't fun. Fixed. * Ahem... Stack smashing in ring 0 isn't fun. Fixed.
*/ */
static int uni16_to_x8(unsigned char *ascii, wchar_t *uni, int len, static int uni16_to_x8(unsigned char *ascii, const wchar_t *uni, int len,
int uni_xlate, struct nls_table *nls) int uni_xlate, struct nls_table *nls)
{ {
wchar_t *ip, ec; const wchar_t *ip;
wchar_t ec;
unsigned char *op, nc; unsigned char *op, nc;
int charlen; int charlen;
int k; int k;
...@@ -166,6 +167,16 @@ static int uni16_to_x8(unsigned char *ascii, wchar_t *uni, int len, ...@@ -166,6 +167,16 @@ static int uni16_to_x8(unsigned char *ascii, wchar_t *uni, int len,
return (op - ascii); return (op - ascii);
} }
static inline int fat_uni_to_x8(struct msdos_sb_info *sbi, const wchar_t *uni,
unsigned char *buf, int size)
{
if (sbi->options.utf8)
return utf8_wcstombs(buf, uni, size);
else
return uni16_to_x8(buf, uni, size, sbi->options.unicode_xlate,
sbi->nls_io);
}
static inline int static inline int
fat_short2uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni) fat_short2uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
{ {
...@@ -226,6 +237,19 @@ fat_shortname2uni(struct nls_table *nls, unsigned char *buf, int buf_size, ...@@ -226,6 +237,19 @@ fat_shortname2uni(struct nls_table *nls, unsigned char *buf, int buf_size,
return len; return len;
} }
static inline int fat_name_match(struct msdos_sb_info *sbi,
const unsigned char *a, int a_len,
const unsigned char *b, int b_len)
{
if (a_len != b_len)
return 0;
if (sbi->options.name_check != 's')
return !nls_strnicmp(sbi->nls_io, a, b, a_len);
else
return !memcmp(a, b, a_len);
}
enum { PARSE_INVALID = 1, PARSE_NOT_LONGNAME, PARSE_EOF, }; enum { PARSE_INVALID = 1, PARSE_NOT_LONGNAME, PARSE_EOF, };
/** /**
...@@ -311,29 +335,24 @@ int fat_search_long(struct inode *inode, const unsigned char *name, ...@@ -311,29 +335,24 @@ int fat_search_long(struct inode *inode, const unsigned char *name,
struct msdos_sb_info *sbi = MSDOS_SB(sb); struct msdos_sb_info *sbi = MSDOS_SB(sb);
struct buffer_head *bh = NULL; struct buffer_head *bh = NULL;
struct msdos_dir_entry *de; struct msdos_dir_entry *de;
struct nls_table *nls_io = sbi->nls_io;
struct nls_table *nls_disk = sbi->nls_disk; struct nls_table *nls_disk = sbi->nls_disk;
wchar_t bufuname[14];
unsigned char nr_slots; unsigned char nr_slots;
int xlate_len; wchar_t bufuname[14];
wchar_t *unicode = NULL; wchar_t *unicode = NULL;
unsigned char work[MSDOS_NAME]; unsigned char work[MSDOS_NAME];
unsigned char *bufname = NULL; unsigned char *bufname = NULL;
int uni_xlate = sbi->options.unicode_xlate;
int utf8 = sbi->options.utf8;
int anycase = (sbi->options.name_check != 's');
unsigned short opt_shortname = sbi->options.shortname; unsigned short opt_shortname = sbi->options.shortname;
loff_t cpos = 0; loff_t cpos = 0;
int chl, i, j, last_u, err; int chl, i, j, last_u, err, len;
bufname = __getname(); bufname = __getname();
if (!bufname) if (!bufname)
return -ENOMEM; return -ENOMEM;
err = -ENOENT; err = -ENOENT;
while(1) { while (1) {
if (fat_get_entry(inode, &cpos, &bh, &de) == -1) if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
goto EODir; goto end_of_dir;
parse_record: parse_record:
nr_slots = 0; nr_slots = 0;
if (de->name[0] == DELETED_FLAG) if (de->name[0] == DELETED_FLAG)
...@@ -352,7 +371,7 @@ int fat_search_long(struct inode *inode, const unsigned char *name, ...@@ -352,7 +371,7 @@ int fat_search_long(struct inode *inode, const unsigned char *name,
else if (status == PARSE_NOT_LONGNAME) else if (status == PARSE_NOT_LONGNAME)
goto parse_record; goto parse_record;
else if (status == PARSE_EOF) else if (status == PARSE_EOF)
goto EODir; goto end_of_dir;
} }
memcpy(work, de->name, sizeof(de->name)); memcpy(work, de->name, sizeof(de->name));
...@@ -393,30 +412,21 @@ int fat_search_long(struct inode *inode, const unsigned char *name, ...@@ -393,30 +412,21 @@ int fat_search_long(struct inode *inode, const unsigned char *name,
if (!last_u) if (!last_u)
continue; continue;
/* Compare shortname */
bufuname[last_u] = 0x0000; bufuname[last_u] = 0x0000;
xlate_len = utf8 len = fat_uni_to_x8(sbi, bufuname, bufname, PATH_MAX);
?utf8_wcstombs(bufname, bufuname, PATH_MAX) if (fat_name_match(sbi, name, name_len, bufname, len))
:uni16_to_x8(bufname, bufuname, PATH_MAX, uni_xlate, nls_io); goto found;
if (xlate_len == name_len)
if ((!anycase && !memcmp(name, bufname, xlate_len)) ||
(anycase && !nls_strnicmp(nls_io, name, bufname,
xlate_len)))
goto Found;
if (nr_slots) { if (nr_slots) {
xlate_len = utf8 /* Compare longname */
?utf8_wcstombs(bufname, unicode, PATH_MAX) len = fat_uni_to_x8(sbi, unicode, bufname, PATH_MAX);
:uni16_to_x8(bufname, unicode, PATH_MAX, uni_xlate, nls_io); if (fat_name_match(sbi, name, name_len, bufname, len))
if (xlate_len != name_len) goto found;
continue;
if ((!anycase && !memcmp(name, bufname, xlate_len)) ||
(anycase && !nls_strnicmp(nls_io, name, bufname,
xlate_len)))
goto Found;
} }
} }
Found: found:
nr_slots++; /* include the de */ nr_slots++; /* include the de */
sinfo->slot_off = cpos - nr_slots * sizeof(*de); sinfo->slot_off = cpos - nr_slots * sizeof(*de);
sinfo->nr_slots = nr_slots; sinfo->nr_slots = nr_slots;
...@@ -424,7 +434,7 @@ int fat_search_long(struct inode *inode, const unsigned char *name, ...@@ -424,7 +434,7 @@ int fat_search_long(struct inode *inode, const unsigned char *name,
sinfo->bh = bh; sinfo->bh = bh;
sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de); sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
err = 0; err = 0;
EODir: end_of_dir:
if (bufname) if (bufname)
__putname(bufname); __putname(bufname);
if (unicode) if (unicode)
...@@ -452,23 +462,19 @@ static int __fat_readdir(struct inode *inode, struct file *filp, void *dirent, ...@@ -452,23 +462,19 @@ static int __fat_readdir(struct inode *inode, struct file *filp, void *dirent,
struct msdos_sb_info *sbi = MSDOS_SB(sb); struct msdos_sb_info *sbi = MSDOS_SB(sb);
struct buffer_head *bh; struct buffer_head *bh;
struct msdos_dir_entry *de; struct msdos_dir_entry *de;
struct nls_table *nls_io = sbi->nls_io;
struct nls_table *nls_disk = sbi->nls_disk; struct nls_table *nls_disk = sbi->nls_disk;
unsigned char long_slots; unsigned char nr_slots;
const char *fill_name;
int fill_len;
wchar_t bufuname[14]; wchar_t bufuname[14];
wchar_t *unicode = NULL; wchar_t *unicode = NULL;
unsigned char c, work[MSDOS_NAME], bufname[56], *ptname = bufname; unsigned char c, work[MSDOS_NAME], bufname[56], *ptname = bufname;
unsigned long lpos, dummy, *furrfu = &lpos; unsigned short opt_shortname = sbi->options.shortname;
int uni_xlate = sbi->options.unicode_xlate;
int isvfat = sbi->options.isvfat; int isvfat = sbi->options.isvfat;
int utf8 = sbi->options.utf8;
int nocase = sbi->options.nocase; int nocase = sbi->options.nocase;
unsigned short opt_shortname = sbi->options.shortname; const char *fill_name;
unsigned long inum; unsigned long inum;
int chi, chl, i, i2, j, last, last_u, dotoffset = 0; unsigned long lpos, dummy, *furrfu = &lpos;
loff_t cpos; loff_t cpos;
int chi, chl, i, i2, j, last, last_u, dotoffset = 0, fill_len;
int ret = 0; int ret = 0;
lock_super(sb); lock_super(sb);
...@@ -488,43 +494,43 @@ static int __fat_readdir(struct inode *inode, struct file *filp, void *dirent, ...@@ -488,43 +494,43 @@ static int __fat_readdir(struct inode *inode, struct file *filp, void *dirent,
cpos = 0; cpos = 0;
} }
} }
if (cpos & (sizeof(struct msdos_dir_entry)-1)) { if (cpos & (sizeof(struct msdos_dir_entry) - 1)) {
ret = -ENOENT; ret = -ENOENT;
goto out; goto out;
} }
bh = NULL; bh = NULL;
GetNew: get_new:
if (fat_get_entry(inode, &cpos, &bh, &de) == -1) if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
goto EODir; goto end_of_dir;
parse_record: parse_record:
long_slots = 0; nr_slots = 0;
/* Check for long filename entry */ /* Check for long filename entry */
if (isvfat) { if (isvfat) {
if (de->name[0] == DELETED_FLAG) if (de->name[0] == DELETED_FLAG)
goto RecEnd; goto record_end;
if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME)) if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
goto RecEnd; goto record_end;
if (de->attr != ATTR_EXT && IS_FREE(de->name)) if (de->attr != ATTR_EXT && IS_FREE(de->name))
goto RecEnd; goto record_end;
} else { } else {
if ((de->attr & ATTR_VOLUME) || IS_FREE(de->name)) if ((de->attr & ATTR_VOLUME) || IS_FREE(de->name))
goto RecEnd; goto record_end;
} }
if (isvfat && de->attr == ATTR_EXT) { if (isvfat && de->attr == ATTR_EXT) {
int status = fat_parse_long(inode, &cpos, &bh, &de, int status = fat_parse_long(inode, &cpos, &bh, &de,
&unicode, &long_slots); &unicode, &nr_slots);
if (status < 0) { if (status < 0) {
filp->f_pos = cpos; filp->f_pos = cpos;
ret = status; ret = status;
goto out; goto out;
} else if (status == PARSE_INVALID) } else if (status == PARSE_INVALID)
goto RecEnd; goto record_end;
else if (status == PARSE_NOT_LONGNAME) else if (status == PARSE_NOT_LONGNAME)
goto parse_record; goto parse_record;
else if (status == PARSE_EOF) else if (status == PARSE_EOF)
goto EODir; goto end_of_dir;
} }
if (sbi->options.dotsOK) { if (sbi->options.dotsOK) {
...@@ -586,12 +592,12 @@ static int __fat_readdir(struct inode *inode, struct file *filp, void *dirent, ...@@ -586,12 +592,12 @@ static int __fat_readdir(struct inode *inode, struct file *filp, void *dirent,
} }
} }
if (!last) if (!last)
goto RecEnd; goto record_end;
i = last + dotoffset; i = last + dotoffset;
j = last_u; j = last_u;
lpos = cpos - (long_slots+1)*sizeof(struct msdos_dir_entry); lpos = cpos - (nr_slots + 1) * sizeof(struct msdos_dir_entry);
if (!memcmp(de->name, MSDOS_DOT, MSDOS_NAME)) if (!memcmp(de->name, MSDOS_DOT, MSDOS_NAME))
inum = inode->i_ino; inum = inode->i_ino;
else if (!memcmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) { else if (!memcmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
...@@ -608,20 +614,17 @@ static int __fat_readdir(struct inode *inode, struct file *filp, void *dirent, ...@@ -608,20 +614,17 @@ static int __fat_readdir(struct inode *inode, struct file *filp, void *dirent,
if (isvfat) { if (isvfat) {
bufuname[j] = 0x0000; bufuname[j] = 0x0000;
i = utf8 ? utf8_wcstombs(bufname, bufuname, sizeof(bufname)) i = fat_uni_to_x8(sbi, bufuname, bufname, sizeof(bufname));
: uni16_to_x8(bufname, bufuname, sizeof(bufname), uni_xlate, nls_io);
} }
fill_name = bufname; fill_name = bufname;
fill_len = i; fill_len = i;
if (!short_only && long_slots) { if (!short_only && nr_slots) {
/* convert the unicode long name. 261 is maximum size /* convert the unicode long name. 261 is maximum size
* of unicode buffer. (13 * slots + nul) */ * of unicode buffer. (13 * slots + nul) */
void *longname = unicode + 261; void *longname = unicode + 261;
int buf_size = PATH_MAX - (261 * sizeof(unicode[0])); int buf_size = PATH_MAX - (261 * sizeof(unicode[0]));
int long_len = utf8 int long_len = fat_uni_to_x8(sbi, unicode, longname, buf_size);
? utf8_wcstombs(longname, unicode, buf_size)
: uni16_to_x8(longname, unicode, buf_size, uni_xlate, nls_io);
if (!both) { if (!both) {
fill_name = longname; fill_name = longname;
...@@ -640,15 +643,15 @@ static int __fat_readdir(struct inode *inode, struct file *filp, void *dirent, ...@@ -640,15 +643,15 @@ static int __fat_readdir(struct inode *inode, struct file *filp, void *dirent,
} }
if (filldir(dirent, fill_name, fill_len, *furrfu, inum, if (filldir(dirent, fill_name, fill_len, *furrfu, inum,
(de->attr & ATTR_DIR) ? DT_DIR : DT_REG) < 0) (de->attr & ATTR_DIR) ? DT_DIR : DT_REG) < 0)
goto FillFailed; goto fill_failed;
RecEnd: record_end:
furrfu = &lpos; furrfu = &lpos;
filp->f_pos = cpos; filp->f_pos = cpos;
goto GetNew; goto get_new;
EODir: end_of_dir:
filp->f_pos = cpos; filp->f_pos = cpos;
FillFailed: fill_failed:
brelse(bh); brelse(bh);
if (unicode) if (unicode)
__putname(unicode); __putname(unicode);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment