Skip to contentSkip to conversation

hfsplus: emit proper file type from readdir

PATCH Superseded Sergei Antonov 4 participants 7 messages May 7, 2014 – May 10, 2014

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hfsplus: emit proper file type from readdir

hfsplus_readdir() incorrectly returned DT_REG for symbolic links and special files. Return DT_LNK, DT_FIFO, DT_CHR, DT_REG, DT_SOCK, or DT_UNKNOWN according to mode field in catalog record. Programs relying on information from readdir will now work correctly with HFS+.

CC Al Viro <[email protected]>
CC Christoph Hellwig <[email protected]>
CC Andrew Morton <[email protected]>
CC Vyacheslav Dubeyko <[email protected]>
CC Hin-Tak Leung <[email protected]>
---
 fs/hfsplus/dir.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
fs/hfsplus/dir.c +19 -1
index bdec665..8397d64 100644
@@ -212,13 +212,31 @@ static int hfsplus_readdir(struct file *file, struct dir_context *ctx)
 				    be32_to_cpu(entry.folder.id), DT_DIR))
 				break;
 		} else if (type == HFSPLUS_FILE) {
+			u16 mode;
+			unsigned type = DT_UNKNOWN;
+
 			if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
 				pr_err("small file entry\n");
 				err = -EIO;
 				goto out;
 			}
+
+			mode = be16_to_cpu(entry.file.permissions.mode);
+			if (S_ISFIFO(mode))
+				type = DT_FIFO;
+			if (S_ISCHR(mode))
+				type = DT_CHR;
+			if (S_ISBLK(mode))
+				type = DT_BLK;
+			if (S_ISREG(mode))
+				type = DT_REG;
+			if (S_ISLNK(mode))
+				type = DT_LNK;
+			if (S_ISSOCK(mode))
+				type = DT_SOCK;
+
 			if (!dir_emit(ctx, strbuf, len,
-				    be32_to_cpu(entry.file.id), DT_REG))
+				      be32_to_cpu(entry.file.id), type))
 				break;
 		} else {
 			pr_err("bad catalog entry type\n");
Signature · 1 line
1.9.0
#1 lore raw

Hi,

Quoted 36 lines · Sergei Antonov

hfsplus_readdir() incorrectly returned DT_REG for symbolic links and special files. Return DT_LNK, DT_FIFO, DT_CHR, DT_REG, DT_SOCK, or DT_UNKNOWN according to mode field in catalog record. Programs relying on information from readdir will now work correctly with HFS+.

CC Al Viro <[email protected]>
CC Christoph Hellwig <[email protected]>
CC Andrew Morton <[email protected]>
CC Vyacheslav Dubeyko <[email protected]>
CC Hin-Tak Leung <[email protected]>

---
fs/hfsplus/dir.c | 20 +++++++++++++++++++-

1 file changed, 19 insertions(+), 1 deletion(-)
fs/hfsplus/dir.c +8 -0
index bdec665..8397d64 100644
@@ -212,13 +212,31 @@ static int hfsplus_readdir(struct file *file, struct dir_context *ctx)
 				    be32_to_cpu(entry.folder.id), DT_DIR))
 				break;
 		} else if (type == HFSPLUS_FILE) {
+			u16 mode;
+			unsigned type = DT_UNKNOWN;
+
 			if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
 				pr_err("small file entry\n");
 				err = -EIO;
 				goto out;
 			}
+
+			mode = be16_to_cpu(entry.file.permissions.mode);
+			if (S_ISFIFO(mode))
+				type = DT_FIFO;
+			if (S_ISCHR(mode))

fs/hfsplus/dir.c static int hfsplus_readdir(struct file *file, struct dir_context *ctx) · line 237

Quoted 10 lines
fs/hfsplus/dir.c +10 -0
+				type = DT_CHR;
+			if (S_ISBLK(mode))
+				type = DT_BLK;
+			if (S_ISREG(mode))
+				type = DT_REG;
+			if (S_ISLNK(mode))
+				type = DT_LNK;
+			if (S_ISSOCK(mode))
+				type = DT_SOCK;
+

I would either use "switch"/"case" or "else if" instead of "if" otherwise you are doing all the checks for all the types even after you have found the right one - also place the DT_REG check first as that is the common case...

Best regards,

Anton

Quoted 6 lines
fs/hfsplus/dir.c +1 -1
 			if (!dir_emit(ctx, strbuf, len,
-				    be32_to_cpu(entry.file.id), DT_REG))
+				      be32_to_cpu(entry.file.id), type))
 				break;
 		} else {
 			pr_err("bad catalog entry type\n");
Signature · 3 lines
Anton Altaparmakov <aia21 at cam.ac.uk> (replace at with @)
University of Cambridge Information Services, Roger Needham Building
7 JJ Thomson Avenue, Cambridge, CB3 0RB, UK
#2 lore raw

Hi,

On Wed, 2014-05-07 at 16:55 +0100, Anton Altaparmakov wrote:

[snip]

Quoted 9 lines
Quoted 9 lines
fs/hfsplus/dir.c +9 -0
+
+			mode = be16_to_cpu(entry.file.permissions.mode);
+			if (S_ISFIFO(mode))
+				type = DT_FIFO;
+			if (S_ISCHR(mode))
+				type = DT_CHR;
+			if (S_ISBLK(mode))
+				type = DT_BLK;
+			if (S_ISREG(mode))
Quoted 9 lines

fs/hfsplus/dir.c static int hfsplus_readdir(struct file *file, struct dir_context *ctx) · line 237

Quoted 6 lines
fs/hfsplus/dir.c +6 -0
+				type = DT_REG;
+			if (S_ISLNK(mode))
+				type = DT_LNK;
+			if (S_ISSOCK(mode))
+				type = DT_SOCK;
+

I would either use "switch"/"case" or "else if" instead of "if" otherwise you are doing all the checks for all the types even after you have found the right one - also place the DT_REG check first as that is the common case...

I believe that such example is good solution too:

static unsigned char
nilfs_filetype_table[NILFS_FT_MAX] = {

        [NILFS_FT_UNKNOWN]      = DT_UNKNOWN,
        [NILFS_FT_REG_FILE]     = DT_REG,
        [NILFS_FT_DIR]          = DT_DIR,
        [NILFS_FT_CHRDEV]       = DT_CHR,
        [NILFS_FT_BLKDEV]       = DT_BLK,
        [NILFS_FT_FIFO]         = DT_FIFO,
        [NILFS_FT_SOCK]         = DT_SOCK,
        [NILFS_FT_SYMLINK]      = DT_LNK,

};

What about likewise way?

Thanks,
Vyacheslav Dubeyko.

#3 lore raw

Hi,

Quoted 34 lines · Vyacheslav Dubeyko

On Wed, 2014-05-07 at 16:55 +0100, Anton Altaparmakov wrote:

[snip]

Quoted 9 lines
Quoted 9 lines
fs/hfsplus/dir.c +9 -0
+
+			mode = be16_to_cpu(entry.file.permissions.mode);
+			if (S_ISFIFO(mode))
+				type = DT_FIFO;
+			if (S_ISCHR(mode))
+				type = DT_CHR;
+			if (S_ISBLK(mode))
+				type = DT_BLK;
+			if (S_ISREG(mode))
Quoted 9 lines

fs/hfsplus/dir.c static int hfsplus_readdir(struct file *file, struct dir_context *ctx) · line 237

Quoted 6 lines
fs/hfsplus/dir.c +6 -0
+				type = DT_REG;
+			if (S_ISLNK(mode))
+				type = DT_LNK;
+			if (S_ISSOCK(mode))
+				type = DT_SOCK;
+

I would either use "switch"/"case" or "else if" instead of "if" otherwise you are doing all the checks for all the types even after you have found the right one - also place the DT_REG check first as that is the common case...

I believe that such example is good solution too:

static unsigned char
nilfs_filetype_table[NILFS_FT_MAX] = {

       [NILFS_FT_UNKNOWN]      = DT_UNKNOWN,
       [NILFS_FT_REG_FILE]     = DT_REG,
       [NILFS_FT_DIR]          = DT_DIR,
       [NILFS_FT_CHRDEV]       = DT_CHR,
       [NILFS_FT_BLKDEV]       = DT_BLK,
       [NILFS_FT_FIFO]         = DT_FIFO,
       [NILFS_FT_SOCK]         = DT_SOCK,
       [NILFS_FT_SYMLINK]      = DT_LNK,
Quoted 3 lines

};

What about likewise way?

Sure, that also works. My objection was purely because all the "if"s were being evaluated unnecessarily.

Best regards,

Anton

Quoted 2 lines

Thanks,
Vyacheslav Dubeyko.

Signature · 3 lines
Anton Altaparmakov <aia21 at cam.ac.uk> (replace at with @)
University of Cambridge Information Services, Roger Needham Building
7 JJ Thomson Avenue, Cambridge, CB3 0RB, UK
#4 lore raw
In reply to Vyacheslav Dubeyko
Quoted 36 lines · Vyacheslav Dubeyko

Hi,

On Wed, 2014-05-07 at 16:55 +0100, Anton Altaparmakov wrote:

[snip]

Quoted 9 lines
Quoted 9 lines
fs/hfsplus/dir.c +9 -0
+
+                   mode = be16_to_cpu(entry.file.permissions.mode);
+                   if (S_ISFIFO(mode))
+                           type = DT_FIFO;
+                   if (S_ISCHR(mode))
+                           type = DT_CHR;
+                   if (S_ISBLK(mode))
+                           type = DT_BLK;
+                   if (S_ISREG(mode))
Quoted 9 lines

fs/hfsplus/dir.c static int hfsplus_readdir(struct file *file, struct dir_context *ctx) · line 237

Quoted 6 lines
fs/hfsplus/dir.c +6 -0
+                           type = DT_REG;
+                   if (S_ISLNK(mode))
+                           type = DT_LNK;
+                   if (S_ISSOCK(mode))
+                           type = DT_SOCK;
+

I would either use "switch"/"case" or "else if" instead of "if" otherwise you are doing all the checks for all the types even after you have found the right one - also place the DT_REG check first as that is the common case...

I believe that such example is good solution too:

static unsigned char
nilfs_filetype_table[NILFS_FT_MAX] = {

        [NILFS_FT_UNKNOWN]      = DT_UNKNOWN,
        [NILFS_FT_REG_FILE]     = DT_REG,
        [NILFS_FT_DIR]          = DT_DIR,
        [NILFS_FT_CHRDEV]       = DT_CHR,
        [NILFS_FT_BLKDEV]       = DT_BLK,
        [NILFS_FT_FIFO]         = DT_FIFO,
        [NILFS_FT_SOCK]         = DT_SOCK,
        [NILFS_FT_SYMLINK]      = DT_LNK,
Quoted 3 lines

};

What about likewise way?

Using an array means calculating an index to it using such expression:
(mode & S_IFMT) >> 12.
I prefer a more straightforward "if/else if".

#5 lore raw
Quoted 15 lines · Sergei Antonov
Quoted 14 lines · Vyacheslav Dubeyko

I believe that such example is good solution too:

static unsigned char
nilfs_filetype_table[NILFS_FT_MAX] = {

        [NILFS_FT_UNKNOWN]      = DT_UNKNOWN,
        [NILFS_FT_REG_FILE]     = DT_REG,
        [NILFS_FT_DIR]          = DT_DIR,
        [NILFS_FT_CHRDEV]       = DT_CHR,
        [NILFS_FT_BLKDEV]       = DT_BLK,
        [NILFS_FT_FIFO]         = DT_FIFO,
        [NILFS_FT_SOCK]         = DT_SOCK,
        [NILFS_FT_SYMLINK]      = DT_LNK,

};

Quoted 5 lines
Quoted 1 line

What about likewise way?

Using an array means calculating an index to it using such expression:
(mode & S_IFMT) >> 12.
I prefer a more straightforward "if/else if".

This should all be wrapped up neatly like so:

} else if (type == HFSPLUS_FILE) {
+ unsigned type;
+

			if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
				pr_err("small file entry\n");
				err = -EIO;
				goto out;
			}

+
+ type = hfsplus_de_type(&entry);
+
if (!dir_emit(ctx, strbuf, len,
- be32_to_cpu(entry.file.id), DT_REG))
+ be32_to_cpu(entry.file.id), type))
break;

and then the array, case, series of else ifs, whatever can all be confined to a single function.

#6 lore raw
Quoted 25 lines · Matthew Wilcox
Quoted 15 lines · Sergei Antonov
Quoted 14 lines · Vyacheslav Dubeyko

I believe that such example is good solution too:

static unsigned char
nilfs_filetype_table[NILFS_FT_MAX] = {

        [NILFS_FT_UNKNOWN]      = DT_UNKNOWN,
        [NILFS_FT_REG_FILE]     = DT_REG,
        [NILFS_FT_DIR]          = DT_DIR,
        [NILFS_FT_CHRDEV]       = DT_CHR,
        [NILFS_FT_BLKDEV]       = DT_BLK,
        [NILFS_FT_FIFO]         = DT_FIFO,
        [NILFS_FT_SOCK]         = DT_SOCK,
        [NILFS_FT_SYMLINK]      = DT_LNK,

};

Quoted 5 lines
Quoted 1 line

What about likewise way?

Using an array means calculating an index to it using such expression:
(mode & S_IFMT) >> 12.
I prefer a more straightforward "if/else if".

This should all be wrapped up neatly like so:

                } else if (type == HFSPLUS_FILE) {

fs/hfsplus/dir.c static int hfsplus_readdir(struct file *file, struct dir_context *ctx) · line 237

Quoted 10 lines
fs/hfsplus/dir.c +5 -0
+                       unsigned type;
+
                        if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
                                pr_err("small file entry\n");
                                err = -EIO;
                                goto out;
                        }
+
+                       type = hfsplus_de_type(&entry);
+

A dedicated function is a good idea. I regret it didn't occur to me at the moment of writing the patch.

Furthermore, hfsplus_readdir() is 125 lines long which exceeds Documentation/CodingStyle recommended limit of two screenfuls of text (i.e. around 50 lines). It makes sense to submit a patch breaking hfsplus_readdir() into several (not only hfsplus_de_type()) smaller functions.

Quoted 7 lines

if (!dir_emit(ctx, strbuf, len,

fs/hfsplus/dir.c +1 -1
-                                       be32_to_cpu(entry.file.id), DT_REG))
+                                       be32_to_cpu(entry.file.id), type))
                                break;

and then the array, case, series of else ifs, whatever can all be confined to a single function.