Skip to content

Commit

Permalink
Add an option for finding sparse files.
Browse files Browse the repository at this point in the history
Reviewed by:	iedowse
MFC after:	3 weeks
  • Loading branch information
dwmalone committed Mar 3, 2013
1 parent b6bc5f5 commit 9ed0c92
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
2 changes: 2 additions & 0 deletions usr.bin/find/extern.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
6 changes: 5 additions & 1 deletion usr.bin/find/find.1
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions usr.bin/find/function.c
Original file line number Diff line number Diff line change
Expand Up @@ -1496,6 +1496,29 @@ c_size(OPTION *option, char ***argvp)
return new;
}

/*
* -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 --
*
Expand Down
1 change: 1 addition & 0 deletions usr.bin/find/option.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down

0 comments on commit 9ed0c92

Please sign in to comment.