mktar: Use `wc` instead of `du` in summary message
[sunny256-utils.git] / manyfiles
blob7d3f3629da172ecdb6fffbba57b6b06889475130
1 #!/usr/bin/env perl
3 #=======================================================================
4 # manyfiles
5 # File ID: 7046da86-f743-11dd-8d25-000475e441b9
6 # Creates loads of files in the current directory. Used for
7 # stresstesting file systems and stuff.
9 # Character set: UTF-8
10 # ©opyleft 2004– Øyvind A. Holm <sunny@sunbase.org>
11 # License: GNU General Public License version 2 or later, see end of
12 # file for legal stuff.
13 #=======================================================================
15 use strict;
16 use warnings;
17 use Getopt::Long;
18 use Time::HiRes qw{ gettimeofday };
20 $| = 1;
22 our $Debug = 0;
24 our %Opt = (
26 'char' => "",
27 'count' => 0,
28 'debug' => 0,
29 'extension' => "",
30 'help' => 0,
31 'verbose' => 0,
32 'version' => 0,
36 our $progname = $0;
37 $progname =~ s/^.*\/(.*?)$/$1/;
38 our $VERSION = "0.00";
40 Getopt::Long::Configure("bundling");
41 GetOptions(
43 "char=s" => \$Opt{'char'},
44 "count|c=i" => \$Opt{'count'},
45 "debug" => \$Opt{'debug'},
46 "extension|e=s" => \$Opt{'extension'},
47 "help|h" => \$Opt{'help'},
48 "verbose|v+" => \$Opt{'verbose'},
49 "version" => \$Opt{'version'},
51 ) || die("$progname: Option error. Use -h for help.\n");
53 $Opt{'debug'} && ($Debug = 1);
54 $Opt{'help'} && usage(0);
55 if ($Opt{'version'}) {
56 print_version();
57 exit(0);
60 $Opt{'count'} && $Opt{'count'}++;
62 my $Retval = 0;
64 if ($#ARGV < 0) {
65 die("$progname: No directory specified. Use \"$progname -h\" for help.\n");
68 my ($char_start, $char_end);
70 if (length($Opt{'char'})) {
71 my $Str = $Opt{'char'};
72 if ($Str =~ /^(\d+)-(\d+)$/) {
73 ($char_start, $char_end) = ($1, $2);
74 if ($char_start > 255 || $char_start > 255) {
75 die("$progname: Invalid character value in --char value, " .
76 "has to be in range 0-255.\n");
81 for my $Curr (@ARGV) {
82 D("\$Curr = '$Curr'");
83 $Retval ||= fill_directory($Curr);
84 D("After fill_directory(i'$Curr')");
87 D("exit($Retval)");
88 exit($Retval);
90 sub fill_directory {
91 # {{{
92 my $Dir = shift;
93 my $start_time = gettimeofday;
94 my $total_created = 0;
95 my $a;
97 D("fill_directory(\"$Dir\")");
98 if (!-d "$Dir/.") {
99 if (!mkdir($Dir)) {
100 warn("$progname: $Dir: Cannot create directory: $!\n");
101 return(1);
104 if ($Opt{'char'}) {
105 if (defined($char_start) && defined($char_end)) {
106 D("char_start = '$char_start', char_end = '$char_end'");
107 for ($a = $char_start; $a <= $char_end; $a++) {
108 my $hex_val = sprintf("%02x", $a);
109 my $File = sprintf("%s/%s_%c%s", $Dir, ${hex_val}, $a, $Opt{'extension'});
110 D("a = '$a', File = '$File'");
111 unless (open(FP, ">$File")) {
112 warn("$progname: $File: Cannot create file: $!\n");
113 } else {
114 $total_created++;
118 } else {
119 for ($a = 1; $a != $Opt{'count'}; $a++) {
120 my $File = "$Dir/$a$Opt{'extension'}";
121 unless (open(FP, ">$File")) {
122 warn("$0: $File: Cannot create file: $!");
123 } else {
124 $total_created++;
129 my $end_time = gettimeofday;
130 my $total_time = $end_time - $start_time;
131 $a--;
133 printf("$Dir: Created %u file%s in %.6f second%s%s.\n",
134 $total_created,
135 $total_created == 1 ? "" : "s",
136 $total_time,
137 $total_time == 1 ? "" : "s",
138 $total_time
139 ? sprintf(", %.2f files/second on average",
140 $total_created / $total_time)
141 : ""
143 return(0);
144 # }}}
145 } # fill_directory()
147 sub print_version {
148 # Print program version {{{
149 print("$progname v$VERSION\n");
150 # }}}
151 } # print_version()
153 sub usage {
154 # Send the help message to stdout {{{
155 my $Retval = shift;
157 if ($Opt{'verbose'}) {
158 print("\n");
159 print_version();
161 print(<<END);
163 Usage: $progname [options] directory [directories [...]]
165 Creates loads of empty files in the specified directories. Used for file
166 system profiling and other interesting stuff.
168 Options:
170 -c, --count x
171 Stop after x files are created.
172 --char start-end
173 Create files with filenames containing bytes in the area specified
174 (0-255).
175 -e, --extension x
176 Use x as extension of the created files.
177 -h, --help
178 Show this help.
179 -v, --verbose
180 Increase level of verbosity. Can be repeated.
181 --version
182 Print version information.
183 --debug
184 Print debugging messages.
187 exit($Retval);
188 # }}}
189 } # usage()
191 sub msg {
192 # Print a status message to stderr based on verbosity level {{{
193 my ($verbose_level, $Txt) = @_;
195 if ($Opt{'verbose'} >= $verbose_level) {
196 print(STDERR "$progname: $Txt\n");
198 # }}}
199 } # msg()
201 sub D {
202 # Print a debugging message {{{
203 $Debug || return;
204 my @call_info = caller;
205 chomp(my $Txt = shift);
206 my $File = $call_info[1];
207 $File =~ s#\\#/#g;
208 $File =~ s#^.*/(.*?)$#$1#;
209 print(STDERR "$File:$call_info[2] $$ $Txt\n");
210 return("");
211 # }}}
212 } # D()
214 __END__
216 # Plain Old Documentation (POD) {{{
218 =pod
220 =head1 NAME
224 =head1 SYNOPSIS
226 [options] [file [files [...]]]
228 =head1 DESCRIPTION
232 =head1 OPTIONS
234 =over 4
236 =item B<-h>, B<--help>
238 Print a brief help summary.
240 =item B<-v>, B<--verbose>
242 Increase level of verbosity. Can be repeated.
244 =item B<--version>
246 Print version information.
248 =item B<--debug>
250 Print debugging messages.
252 =back
254 =head1 BUGS
258 =head1 AUTHOR
260 Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.
262 =head1 COPYRIGHT
264 Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
265 This is free software; see the file F<COPYING> for legalese stuff.
267 =head1 LICENCE
269 This program is free software: you can redistribute it and/or modify it
270 under the terms of the GNU General Public License as published by the
271 Free Software Foundation, either version 2 of the License, or (at your
272 option) any later version.
274 This program is distributed in the hope that it will be useful, but
275 WITHOUT ANY WARRANTY; without even the implied warranty of
276 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
277 See the GNU General Public License for more details.
279 You should have received a copy of the GNU General Public License along
280 with this program.
281 If not, see L<http://www.gnu.org/licenses/>.
283 =head1 SEE ALSO
285 =cut
287 # }}}
289 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :