bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] Support in du for showing both apparent and on-disk size and the


From: Luca Barbieri
Subject: [PATCH] Support in du for showing both apparent and on-disk size and the ratio between them
Date: Wed, 31 Jan 2007 21:56:10 +0100

This patch allows to display simultaneously both the disk usage and
apparent size and also allows to display the "completeness" (or
"nonsparsity") of file, which is the ratio of the disk usage to the
apparent size.

For instance, this is useful when dealing with software that download
files by creating sparse files and filling them: the "completeness" then
is approximately the progress of the download.

--- du-ratio.orig/src/du.c      2005-11-04 11:58:30.000000000 +0100
+++ du-ratio/src/du.c   2007-01-30 01:39:50.000000000 +0100
@@ -84,6 +84,9 @@
   /* Size of files in directory.  */
   uintmax_t size;
 
+  /* Apparent size of files in directory.  */
+  uintmax_t apparent_size;
+
   /* Latest time stamp found.  If tmax.tv_sec == TYPE_MINIMUM (time_t)
      && tmax.tv_nsec < 0, no time stamp has been found.  */
   struct timespec tmax;
@@ -94,15 +97,17 @@
 duinfo_init (struct duinfo *a)
 {
   a->size = 0;
+  a->apparent_size = 0;
   a->tmax.tv_sec = TYPE_MINIMUM (time_t);
   a->tmax.tv_nsec = -1;
 }
 
 /* Set directory data.  */
 static inline void
-duinfo_set (struct duinfo *a, uintmax_t size, struct timespec tmax)
+duinfo_set (struct duinfo *a, uintmax_t size, uintmax_t apparent_size, struct 
timespec tmax)
 {
   a->size = size;
+  a->apparent_size = apparent_size;
   a->tmax = tmax;
 }
 
@@ -111,6 +116,7 @@
 duinfo_add (struct duinfo *a, struct duinfo const *b)
 {
   a->size += b->size;
+  a->apparent_size += b->apparent_size;
   if (timespec_cmp (a->tmax, b->tmax) < 0)
     a->tmax = b->tmax;
 }
@@ -135,6 +141,12 @@
    use the apparent size (a la stat.st_size).  */
 static bool apparent_size = false;
 
+/* Print size / apparent size ratio.  */
+static bool print_completeness = false;
+
+/* Print the disk usage.  */
+static bool print_disk_usage = false;
+
 /* If true, count each hard link of files with multiple links.  */
 static bool opt_count_all = false;
 
@@ -206,7 +218,9 @@
   MEGABYTES_LONG_OPTION,
 
   TIME_OPTION,
-  TIME_STYLE_OPTION
+  TIME_STYLE_OPTION,
+  SIZE_OPTION,
+  COMPLETENESS_OPTION,
 };
 
 static struct option const long_options[] =
@@ -215,6 +229,7 @@
   {"apparent-size", no_argument, NULL, APPARENT_SIZE_OPTION},
   {"block-size", required_argument, NULL, 'B'},
   {"bytes", no_argument, NULL, 'b'},
+  {"completeness", no_argument, NULL, COMPLETENESS_OPTION},
   {"count-links", no_argument, NULL, 'l'},
   {"dereference", no_argument, NULL, 'L'},
   {"dereference-args", no_argument, NULL, 'D'},
@@ -230,6 +245,7 @@
   {"no-dereference", no_argument, NULL, 'P'},
   {"one-file-system", no_argument, NULL, 'x'},
   {"separate-dirs", no_argument, NULL, 'S'},
+  {"size", no_argument, NULL, SIZE_OPTION},
   {"summarize", no_argument, NULL, 's'},
   {"total", no_argument, NULL, 'c'},
   {"time", optional_argument, NULL, TIME_OPTION},
@@ -297,6 +313,7 @@
   -B, --block-size=SIZE use SIZE-byte blocks\n\
   -b, --bytes           equivalent to `--apparent-size --block-size=1'\n\
   -c, --total           produce a grand total\n\
+      --completeness    print ratios between disk usage and apparent size\n\
   -D, --dereference-args  dereference FILEs that are symbolic links\n\
 "), stdout);
       fputs (_("\
@@ -315,6 +332,7 @@
   -P, --no-dereference  don't follow any symbolic links (this is the 
default)\n\
   -0, --null            end each output line with 0 byte rather than newline\n\
   -S, --separate-dirs   do not include size of subdirectories\n\
+      --size            print disk usage\n\
   -s, --summarize       display only a total for each argument\n\
 "), stdout);
       fputs (_("\
@@ -445,7 +463,29 @@
 static void
 print_size (const struct duinfo *pdui, const char *string)
 {
-  print_only_size (pdui->size);
+  bool printed = false;
+
+  if(print_disk_usage)
+  {
+    if(printed) fputc('\t', stdout);
+    printed = true;
+    print_only_size (pdui->size);
+  }
+  
+  if(apparent_size)
+  {
+    if(printed) fputc('\t', stdout);
+    printed = true;
+    print_only_size (pdui->apparent_size);
+  }
+    
+  if(print_completeness)
+  {
+    if(printed) fputc('\t', stdout);
+    printed = true;
+    fprintf(stdout, "%f", (double)pdui->size / (double)pdui->apparent_size);
+  }
+    
   if (opt_time)
     {
       putchar ('\t');
@@ -535,9 +575,8 @@
   else
     {
       duinfo_set (&dui,
-                 (apparent_size
-                  ? sb->st_size
-                  : (uintmax_t) ST_NBLOCKS (*sb) * ST_NBLOCKSIZE),
+                  (uintmax_t) ST_NBLOCKS (*sb) * ST_NBLOCKSIZE,
+                  sb->st_size,
                  (time_type == time_mtime ? get_stat_mtime (sb)
                   : time_type == time_atime ? get_stat_atime (sb)
                   : get_stat_ctime (sb)));
@@ -725,6 +764,14 @@
          apparent_size = true;
          break;
 
+       case SIZE_OPTION:
+         print_disk_usage = true;
+         break;
+
+       case COMPLETENESS_OPTION:
+         print_completeness = true;
+         break;
+
        case 'b':
          apparent_size = true;
          human_output_opts = 0;
@@ -859,6 +906,9 @@
   if (!ok)
     usage (EXIT_FAILURE);
 
+  if(!apparent_size && !print_completeness)
+    print_disk_usage = true;
+
   if (opt_all & opt_summarize_only)
     {
       error (0, 0, _("cannot both summarize and show all entries"));






reply via email to

[Prev in Thread] Current Thread [Next in Thread]