bug-findutils
[Top][All Lists]
Advanced

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

Added blocks-on-disk predicate. Patch included.


From: Rogier Wolff
Subject: Added blocks-on-disk predicate. Patch included.
Date: Wed, 14 Sep 2011 16:39:07 +0200
User-agent: Mutt/1.5.13 (2006-08-11)


Hi,

I sometimes need to find files that have a specific size on-disk as
opposed to number-of-bytes file-length.

Using the find-utility for this purpose is appropriate, but the
required predicate does not exist.

I've patched findutils-4.4.2. The patch works here.

I then followed http://www.gnu.org/s/findutils/ under the heading 
"how to help":

> If posting patches they should be in unified diff format against the
> latest cvs testing version available from Savannah if possible

and got the "latest cvs" version. 

After patching that I found that findutils currently lives in "git" so
I should have gotten the git version. 

So I got the git version, and patched that. 

Compiling the git version however doesn't work. I get a
version-dependency explosion. Apparently I need to import "gnulib",
but that needs "gettext 0.18" while I have 0.17.


Upgrading "gettext" would:
- put me out-of-sync with my distribution which leads to numerous problems. 
- likely require upgrading of even more tools. 

So I have not persued that path. 

I have copied over the gnulib from 4.4.2, which didn't work. 

I've tried a few other things, but long story short: I can't get the
git version of findutils to compile so I haven't tested my patch in
the latest version.

I called the predicate "dsize", as in "onDisk-size". Native english
speakers will probably be able to come up with something better.

If you would want to integrate this into findutils, you can prod me to
clean up the patch a bit. If you don't like "dsize" as the predicate,
feel free to suggest a different one. The current version I think also
isn't nice in what units it uses for the dsize argument. This too
could be cleaned up.

I'm willing to do this, provided that you can indicate that this is
going to be used in the standard version. Otherwise my version here is
in a "it works for me" state: good enough for me.

        Roger. 


diff --git a/find/defs.h b/find/defs.h
index 6e11174..0635ccb 100644
--- a/find/defs.h
+++ b/find/defs.h
@@ -451,6 +451,7 @@ PREDICATEFUNCTION pred_readable;
 PREDICATEFUNCTION pred_regex;
 PREDICATEFUNCTION pred_samefile;
 PREDICATEFUNCTION pred_size;
+PREDICATEFUNCTION pred_dsize;
 PREDICATEFUNCTION pred_true;
 PREDICATEFUNCTION pred_type;
 PREDICATEFUNCTION pred_uid;
diff --git a/find/parser.c b/find/parser.c
index aa01253..913c860 100644
--- a/find/parser.c
+++ b/find/parser.c
@@ -298,6 +298,8 @@ static struct parser_table const parse_table[] =
   PARSE_OPTION     ("show-control-chars",    show_control_chars), /* GNU, 
4.3.0+ */
 #endif
   PARSE_TEST       ("size",                  size), /* POSIX */
+#define parse_dsize parse_size
+  PARSE_TEST       ("dsize",                 dsize), /* -- REW */
   PARSE_TEST       ("type",                  type), /* POSIX */
   PARSE_TEST       ("uid",                   uid),          /* GNU */
   PARSE_TEST       ("used",                  used),         /* GNU */
@@ -1239,7 +1241,7 @@ tests (N can be +N or -N or N): -amin N -anewer FILE 
-atime N -cmin N\n\
   puts (_("\
       -nouser -nogroup -path PATTERN -perm [+-]MODE -regex PATTERN\n\
       -readable -writable -executable\n\
-      -wholename PATTERN -size N[bcwkMG] -true -type [bcdpflsD] -uid N\n\
+      -wholename PATTERN -size N[bcwkMG] -dsize N[bcwkMG] -true -type 
[bcdpflsD] -uid N\n\
       -used N -user NAME -xtype [bcdpfls]"));
   puts (_("\
       -context CONTEXT\n"));
diff --git a/find/pred.c b/find/pred.c
index 88dacd9..ffe9c4b 100644
--- a/find/pred.c
+++ b/find/pred.c
@@ -135,6 +135,7 @@ struct pred_assoc pred_table[] =
   {pred_regex, "regex   "},
   {pred_samefile,"samefile "},
   {pred_size, "size    "},
+  {pred_dsize, "dsize    "},
   {pred_true, "true    "},
   {pred_type, "type    "},
   {pred_uid, "uid     "},
@@ -973,26 +974,20 @@ pred_regex (const char *pathname, struct stat *stat_buf, 
struct predicate *pred_
   return (false);
 }
 
-bool
-pred_size (const char *pathname, struct stat *stat_buf, struct predicate 
*pred_ptr)
+bool find_compare (int kind, uintmax_t val, uintmax_t limit)
 {
-  uintmax_t f_val;
-
-  (void) pathname;
-  f_val = ((stat_buf->st_size / pred_ptr->args.size.blocksize)
-          + (stat_buf->st_size % pred_ptr->args.size.blocksize != 0));
-  switch (pred_ptr->args.size.kind)
+  switch (kind)
     {
     case COMP_GT:
-      if (f_val > pred_ptr->args.size.size)
+      if (val > limit)
        return (true);
       break;
     case COMP_LT:
-      if (f_val < pred_ptr->args.size.size)
+      if (val < limit)
        return (true);
       break;
     case COMP_EQ:
-      if (f_val == pred_ptr->args.size.size)
+      if (val == limit)
        return (true);
       break;
     }
@@ -1000,6 +995,26 @@ pred_size (const char *pathname, struct stat *stat_buf, 
struct predicate *pred_p
 }
 
 bool
+pred_size (const char *pathname, struct stat *stat_buf, struct predicate 
*pred_ptr)
+{
+  uintmax_t f_val;
+
+  (void) pathname;
+  f_val = ((stat_buf->st_size / pred_ptr->args.size.blocksize)
+          + (stat_buf->st_size % pred_ptr->args.size.blocksize != 0));
+  return find_compare (pred_ptr->args.size.kind, 
+                      f_val, pred_ptr->args.size.size);
+}
+
+bool
+pred_size (const char *pathname, struct stat *stat_buf, struct predicate 
*pred_ptr)
+{
+  (void) pathname;
+  return find_compare (pred_ptr->args.size.kind, 
+                      stat_buf->st_blocks, pred_ptr->args.size.size);
+}
+
+bool
 pred_samefile (const char *pathname, struct stat *stat_buf, struct predicate 
*pred_ptr)
 {
   /* Potential optimisation: because of the loop protection, we always
diff --git a/find/tree.c b/find/tree.c
index 7502fd5..a62a500 100644
--- a/find/tree.c
+++ b/find/tree.c
@@ -995,6 +995,7 @@ static struct pred_cost_lookup costlookup[] =
     { pred_regex     ,  NeedsNothing         },
     { pred_samefile  ,  NeedsStatInfo        },
     { pred_size      ,  NeedsStatInfo        },
+    { pred_dsize     ,  NeedsStatInfo        },
     { pred_true             ,  NeedsNothing         },
     { pred_type      ,  NeedsType            },
     { pred_uid       ,  NeedsStatInfo        },

-- 
** address@hidden ** http://www.BitWizard.nl/ ** +31-15-2600998 **
**    Delftechpark 26 2628 XH  Delft, The Netherlands. KVK: 27239233    **
*-- BitWizard writes Linux device drivers for any device you may have! --*
The plan was simple, like my brother-in-law Phil. But unlike
Phil, this plan just might work.



reply via email to

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