aboutsummaryrefslogtreecommitdiff
path: root/usr.bin/find
diff options
context:
space:
mode:
authorDavid Malone <dwmalone@FreeBSD.org>2013-03-03 20:10:56 +0000
committerDavid Malone <dwmalone@FreeBSD.org>2013-03-03 20:10:56 +0000
commit9ed0c92c91e9a8459462774b33c368b56e2c7ced (patch)
tree0b46db9b2f431142315be4b5f8be443ed56f2e40 /usr.bin/find
parentb6bc5f51faf75e6433e51ba31d47015eefd8d8de (diff)
downloadsrc-9ed0c92c91e9a8459462774b33c368b56e2c7ced.tar.gz
src-9ed0c92c91e9a8459462774b33c368b56e2c7ced.zip
Add an option for finding sparse files.
Reviewed by: iedowse MFC after: 3 weeks
Notes
Notes: svn path=/head/; revision=247730
Diffstat (limited to 'usr.bin/find')
-rw-r--r--usr.bin/find/extern.h2
-rw-r--r--usr.bin/find/find.16
-rw-r--r--usr.bin/find/function.c23
-rw-r--r--usr.bin/find/option.c1
4 files changed, 31 insertions, 1 deletions
diff --git a/usr.bin/find/extern.h b/usr.bin/find/extern.h
index 6fa25d2debef..90b9b29d265e 100644
--- a/usr.bin/find/extern.h
+++ b/usr.bin/find/extern.h
@@ -73,6 +73,7 @@ creat_f c_regex;
creat_f c_samefile;
creat_f c_simple;
creat_f c_size;
+creat_f c_sparse;
creat_f c_type;
creat_f c_user;
creat_f c_xdev;
@@ -109,6 +110,7 @@ exec_f f_prune;
exec_f f_quit;
exec_f f_regex;
exec_f f_size;
+exec_f f_sparse;
exec_f f_type;
exec_f f_user;
diff --git a/usr.bin/find/find.1 b/usr.bin/find/find.1
index 47c40d81842b..cffeacf7ef51 100644
--- a/usr.bin/find/find.1
+++ b/usr.bin/find/find.1
@@ -816,6 +816,10 @@ terabytes (1024 gigabytes)
.It Cm P
petabytes (1024 terabytes)
.El
+.It Ic -sparse
+True if the current file is sparse,
+i.e. has fewer blocks allocated than expected based on its size in bytes.
+This might also match files that have been compressed by the filesystem.
.It Ic -type Ar t
True if the file is of the specified type.
Possible file types are as follows:
@@ -997,7 +1001,7 @@ and
as well as
.Ic -amin , -anewer , -cmin , -cnewer , -delete , -empty , -fstype ,
.Ic -iname , -inum , -iregex , -ls , -maxdepth , -mindepth , -mmin ,
-.Ic -path , -print0 , -regex
+.Ic -path , -print0 , -regex, -sparse
and all of the
.Ic -B*
birthtime related primaries are extensions to
diff --git a/usr.bin/find/function.c b/usr.bin/find/function.c
index cad6eb15fc87..61d61cb49077 100644
--- a/usr.bin/find/function.c
+++ b/usr.bin/find/function.c
@@ -1497,6 +1497,29 @@ c_size(OPTION *option, char ***argvp)
}
/*
+ * -sparse functions --
+ *
+ * Check if a file is sparse by finding if it occupies fewer blocks
+ * than we expect based on its size.
+ */
+int
+f_sparse(PLAN *plan __unused, FTSENT *entry)
+{
+ off_t expected_blocks;
+
+ expected_blocks = (entry->fts_statp->st_size + 511) / 512;
+ return entry->fts_statp->st_blocks < expected_blocks;
+}
+
+PLAN *
+c_sparse(OPTION *option, char ***argvp __unused)
+{
+ ftsoptions &= ~FTS_NOSTAT;
+
+ return palloc(option);
+}
+
+/*
* -type c functions --
*
* True if the type of the file is c, where c is b, c, d, p, f or w
diff --git a/usr.bin/find/option.c b/usr.bin/find/option.c
index b89420cbf0c0..4fc63b6c7562 100644
--- a/usr.bin/find/option.c
+++ b/usr.bin/find/option.c
@@ -145,6 +145,7 @@ static OPTION const options[] = {
{ "-regex", c_regex, f_regex, 0 },
{ "-samefile", c_samefile, f_inum, 0 },
{ "-size", c_size, f_size, 0 },
+ { "-sparse", c_sparse, f_sparse, 0 },
{ "-true", c_simple, f_always_true, 0 },
{ "-type", c_type, f_type, 0 },
{ "-uid", c_user, f_user, 0 },