Skip to contentSkip to conversation

Don't use DT_DIR: It doesn't work on non-ext* filesystems

BUGFIX Vladimir 'phcoder' Serbinenko 4 participants 6 messages Jul 23, 2009 – Jul 25, 2009

lore
Share & tools
lore
#0 lore raw
Signature · 4 lines
Regards
Vladimir 'phcoder' Serbinenko

Personal git repository: http://repo.or.cz/w/grub2/phcoder.git
ChangeLog +5 -0
index a0780ab..16aab92 100644
@@ -1,3 +1,8 @@
+2009-07-23  Vladimir Serbinenko  <[email protected]>
+
+	* util/hostfs.c (grub_hostfs_dir): Don't use DT_DIR: It doesn't work
+	on XFS or ReiserFS.
+
 2009-07-21  Vladimir Serbinenko  <[email protected]>
 
 	UUID support for UFS
util/hostfs.c +0 -6
index 1b963bb..83db2f1 100644
@@ -28,7 +28,6 @@
 #include <stdio.h>
 
 
-#ifndef DT_DIR
 /* dirent.d_type is a BSD extension, not part of POSIX */
 #include <sys/stat.h>
 #include <string.h>
@@ -53,7 +52,6 @@ is_dir (const char *path, const char *name)
     return 0;
   return S_ISDIR (st.st_mode);
 }
-#endif
 
 static grub_err_t
 grub_hostfs_dir (grub_device_t device, const char *path,
@@ -81,11 +79,7 @@ grub_hostfs_dir (grub_device_t device, const char *path,
       if (! de)
 	break;
 
-#ifdef DT_DIR
-      info.dir = (de->d_type == DT_DIR);
-#else
       info.dir = !! is_dir (path, de->d_name);
-#endif
       hook (de->d_name, &info);
 
     }
#1 lore raw

util/hostfs.c grub_hostfs_dir (grub_device_t device, const char *path, · line 88

Quoted 5 lines · Vladimir 'phcoder' Serbinenko
util/hostfs.c +0 -4
-#ifdef DT_DIR
-      info.dir = (de->d_type == DT_DIR);
-#else
       info.dir = !! is_dir (path, de->d_name);
-#endif

Fine with me. Finally a patch that reduces the number of preprocessor directives :-)

Signature · 2 lines
Regards,
Pavel Roskin
#2 lore raw
Quoted 14 lines · Pavel Roskin

util/hostfs.c grub_hostfs_dir (grub_device_t device, const char *path, · line 88

Quoted 6 lines · Vladimir 'phcoder' Serbinenko
util/hostfs.c +0 -4
-#ifdef DT_DIR
-      info.dir = (de->d_type == DT_DIR);
-#else
       info.dir = !! is_dir (path, de->d_name);
-#endif

Fine with me. Finally a patch that reduces the number of preprocessor directives :-)

:-)

Grub hostfs has no performance requirement like e.g. find. The optimization with d_type can simply be ignored - patch is OK.

The idea behind d_type is that it should only be set != DT_UNKNOWN if the type info is available for free or at low cost. It is safe for a filesystem implementation to return DT_UNKNOWN always.

A correct performance-aware solution would look like:

#ifdef DT_DIR
if (de->d_type == DT_DIR)
info.dir = 1;
else if (de->type == DT_FILE)
info.dir = 0;
else
#endif
info.dir = !! is_dir (path, de->d_name);

This was actually wrong in my original patch (svn 1353) that was necessary because of missing d_type on Cygwin - Sorry! Meantime I added d_type support to Cygwin itself and therefore learned how this is supposed to work :-)

Signature · 2 lines
Regards,
Christian Franke
#3 lore raw
Quoted 6 lines · Christian Franke

A correct performance-aware solution would look like:

#ifdef DT_DIR
if (de->d_type == DT_DIR)

    info.dir = 1;

else if (de->type == DT_FILE)

There in no DT_FILE in glibc, but there is DT_REG. DT_UNKNOWN is present. Perhaps the above line should be

else if (de->type != DT_UNKNOWN)

We only care if it's a directory or not. All other objects can be treated like files.

I'm fine either way, whether we fix the "high-performance" code or remove it, as long as we don't have to add more checks.

Signature · 2 lines
Regards,
Pavel Roskin
#4 lore raw
Quoted 5 lines · Pavel Roskin
Quoted 2 lines · Christian Franke

A correct performance-aware solution would look like:

Quoted 7 lines
Quoted 5 lines

#ifdef DT_DIR
if (de->d_type == DT_DIR)

    info.dir = 1;

else if (de->type == DT_FILE)

There in no DT_FILE in glibc, but there is DT_REG.

Yes correct.

Quoted 9 lines

DT_UNKNOWN is
present. Perhaps the above line should be

else if (de->type != DT_UNKNOWN)

We only care if it's a directory or not. All other objects can be treated like files.

It depends: If DT_UNKNOWN is used, you have to replace stat() by lstat() in is_dir(). Otherwise symlinked directories would be appear as dir only if d_type == DT_UNKNOWN and not if d_type == DT_LNK.

Quoted 4 lines

I'm fine either way, whether we fix the "high-performance" code or remove it, as long as we don't have to add more checks.

I would suggest to remove it.

Signature · 2 lines
Regards,
Christian Franke
#5 lore raw
In reply to Pavel Roskin
Quoted 12 lines · Pavel Roskin
Quoted 6 lines · Christian Franke

A correct performance-aware solution would look like:

#ifdef DT_DIR
if (de->d_type == DT_DIR)

    info.dir = 1;

else if (de->type == DT_FILE)

There in no DT_FILE in glibc, but there is DT_REG. DT_UNKNOWN is present. Perhaps the above line should be

Quoted 7 lines

else if (de->type != DT_UNKNOWN)

We only care if it's a directory or not. All other objects can be treated like files.

I'm fine either way, whether we fix the "high-performance" code or remove it, as long as we don't have to add more checks.

d_type doesn't always exist. E.g. on OpenSolaris.

Signature · 5 lines
Robert Millan

  The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and
  how) you may access your data; but nobody's threatening your freedom: we
  still allow you to remove your data and not access it at all."