Hi coreutils people!

On a recent SUSE Linux df became unreliable for e.g. USB-drives.
This is because hald automatically mounts and unmounts such drives
as they are accessed.

Usually I get something like:

$ df /media/USB_DISK
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda1                    0         0         0   -  /media/USB_DISK

only if the USB_DISK is being accessed, I get the expected output.

$ ls /media/USB_DISK > /dev/null; df /media/USB_DISK
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda1               252522    238718     13804  95% /media/USB_DISK

A simple enhancement for df is to actively access the USB_DISK while running
statfs(). I've added an opendir() call in the attached patch. This can be
suppressed with a new commandline option -n. 

Please keep me in CC, I am not subscribed.

thanks,
        Jw.

-- 
 o \  Juergen Weigert  paint it green! __/ _=======.=======_
<V> | [EMAIL PROTECTED]       wide open suse_/        _---|____________\/
 \  | 0911 74053-508         (tm)__/          (____/            /\
(/) | __________________________/             _/ \_ vim:set sw=2 wm=8
--- ./doc/coreutils.texi.orig   2005-09-17 09:44:42.000000000 +0200
+++ ./doc/coreutils.texi        2005-09-19 19:08:54.528587971 +0200
@@ -8763,6 +8763,16 @@
 Limit the listing to local file systems.  By default, remote file systems
 are also listed.
 
[EMAIL PROTECTED] -n
[EMAIL PROTECTED] --no-mount
[EMAIL PROTECTED] -n
[EMAIL PROTECTED] --no-mount
[EMAIL PROTECTED] file system space, retriving output from removable media
+Per default, a mountpoint that reports 0 blocks, will be opended via 
@code{opendir}
+and a second attempt will be made to report the stats. This trick usually 
monts the 
+device, if it is a removable media.
+This option prevents this mount attempt and reports 0 blocks in that case.
+
 @item --no-sync
 @opindex --no-sync
 @cindex file system space, retrieving old data more quickly
--- ./src/df.c.orig     2005-08-16 22:33:40.000000000 +0200
+++ ./src/df.c  2005-09-19 19:08:05.492105049 +0200
@@ -24,6 +24,7 @@
 #include <sys/types.h>
 #include <getopt.h>
 #include <assert.h>
+#include <dirent.h>
 
 #include "system.h"
 #include "canonicalize.h"
@@ -69,6 +70,9 @@
 /* If true, use the POSIX output format.  */
 static bool posix_format;
 
+/* If true, use opendir to attempt a mount if blocks would be 0.  */
+static bool no_mount_attempt;
+
 /* If true, invoke the `sync' system call before getting any usage data.
    Using this option can make df very slow, especially with many or very
    busy disks.  Note that this may make a difference on some systems --
@@ -128,6 +132,7 @@
   {"kilobytes", no_argument, NULL, 'k'}, /* long form is obsolescent */
   {"local", no_argument, NULL, 'l'},
   {"megabytes", no_argument, NULL, 'm'}, /* obsolescent */
+  {"no-mount", no_argument, NULL, 'n'},
   {"portability", no_argument, NULL, 'P'},
   {"print-type", no_argument, NULL, 'T'},
   {"sync", no_argument, NULL, SYNC_OPTION},
@@ -305,6 +310,20 @@
       return;
     }
 
+  if (!no_mount_attempt && fsu.fsu_blocks == 0)
+    {
+      DIR *d = NULL;
+      d = opendir(stat_file);
+      if (get_fs_usage (stat_file, disk, &fsu))
+        {
+          if (d) closedir(d);
+          error (0, errno, "%s", quote (stat_file));
+          exit_status = EXIT_FAILURE;
+          return;
+        }
+      if (d) closedir(d);
+    }
+
   if (fsu.fsu_blocks == 0 && !show_all_fs && !show_listed_fs)
     return;
 
@@ -732,6 +751,7 @@
   -k                    like --block-size=1K\n\
   -l, --local           limit listing to local file systems\n\
       --no-sync         do not invoke sync before getting usage info 
(default)\n\
+  -n, --no-mount        do not attempt to mount removable media\n\
 "), stdout);
       fputs (_("\
   -P, --portability     use the POSIX output format\n\
@@ -772,6 +792,7 @@
   inode_format = false;
   show_all_fs = false;
   show_listed_fs = false;
+  no_mount_attempt = false;
 
   human_output_opts = human_options (getenv ("DF_BLOCK_SIZE"), false,
                                     &output_block_size);
@@ -780,7 +801,7 @@
   posix_format = false;
   exit_status = EXIT_SUCCESS;
 
-  while ((c = getopt_long (argc, argv, "aB:iF:hHklmPTt:vx:", long_options, 
NULL))
+  while ((c = getopt_long (argc, argv, "aB:iF:hHklmnPTt:vx:", long_options, 
NULL))
         != -1)
     {
       switch (c)
@@ -813,6 +834,9 @@
          human_output_opts = 0;
          output_block_size = 1024 * 1024;
          break;
+       case 'n':
+         no_mount_attempt = true;
+         break;
        case 'T':
          print_type = true;
          break;
_______________________________________________
Bug-coreutils mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/bug-coreutils

Reply via email to