mktar: Use `wc` instead of `du` in summary message
[sunny256-utils.git] / ga-fsck-size
blobac6a3aaf40cc1cb49c454b02c57a2ae67ba45b21
1 #!/usr/bin/env perl
3 #=======================================================================
4 # ga-fsck-size
5 # File ID: 071005ea-f6bc-11e4-a11c-000df06acc56
7 # Add byte count to output created by "git annex fsck".
9 # Character set: UTF-8
10 # ©opyleft 2015– Ø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 Number::Bytes::Human qw(format_bytes);
20 local $| = 1;
22 our %Opt = (
24 'help' => 0,
25 'quiet' => 0,
26 'verbose' => 0,
27 'version' => 0,
31 our $progname = $0;
32 $progname =~ s/^.*\/(.*?)$/$1/;
33 our $VERSION = '0.2.0';
35 Getopt::Long::Configure('bundling');
36 GetOptions(
38 'help|h' => \$Opt{'help'},
39 'quiet|q+' => \$Opt{'quiet'},
40 'verbose|v+' => \$Opt{'verbose'},
41 'version' => \$Opt{'version'},
43 ) || die("$progname: Option error. Use -h for help.\n");
45 $Opt{'verbose'} -= $Opt{'quiet'};
46 $Opt{'help'} && usage(0);
47 if ($Opt{'version'}) {
48 print_version();
49 exit(0);
52 exit(main());
54 sub main {
55 # {{{
56 my $Retval = 0;
58 my $total = 0;
59 my $fulltotal = 0;
61 while (my $line = <>) {
62 chomp($line);
63 if ($line =~ /Only (\d+) of (\d+) trustworthy copies exist of (.*)$/m) {
64 my ($actual, $wanted, $filename) = ($1, $2, $3);
65 my $bytes = file_size($filename);
66 if (length($bytes)) {
67 $total += $bytes;
68 $fulltotal += $bytes * ($wanted - $actual);
69 $line .= sprintf(" (%s bytes)",
70 format_bytes($bytes, bs => 1000),
74 if ($line !~ /^ Back it up with git-annex copy\.$/) {
75 print("$line\n");
78 print("\n");
79 printf("Total size of files that need more copies: %s\n",
80 format_bytes($total, bs => 1000));
81 printf("Total space needed to get enough copies : %s\n",
82 format_bytes($fulltotal, bs => 1000));
84 return $Retval;
85 # }}}
86 } # main()
88 sub file_size {
89 # Read file size from git-annex symlink {{{
90 my $filename = shift;
91 my $retval = '';
92 if (-l $filename) {
93 my $symlink = readlink($filename);
94 if ($symlink =~ /^.*\/[A-Z0-9_]+-s(\d+)-/) {
95 $retval = $1;
98 return($retval)
99 # }}}
100 } # file_size()
102 sub print_version {
103 # Print program version {{{
104 print("$progname $VERSION\n");
105 return;
106 # }}}
107 } # print_version()
109 sub usage {
110 # Send the help message to stdout {{{
111 my $Retval = shift;
113 if ($Opt{'verbose'}) {
114 print("\n");
115 print_version();
117 print(<<"END");
119 Usage: git annex fsck 2>&1 | $progname [options]
121 Add byte count to output created by "git annex fsck".
123 Options:
125 -h, --help
126 Show this help.
127 -q, --quiet
128 Be more quiet. Can be repeated to increase silence.
129 -v, --verbose
130 Increase level of verbosity. Can be repeated.
131 --version
132 Print version information.
135 exit($Retval);
136 # }}}
137 } # usage()
139 sub msg {
140 # Print a status message to stderr based on verbosity level {{{
141 my ($verbose_level, $Txt) = @_;
143 if ($Opt{'verbose'} >= $verbose_level) {
144 print(STDERR "$progname: $Txt\n");
146 return;
147 # }}}
148 } # msg()
150 __END__
152 # This program is free software; you can redistribute it and/or modify
153 # it under the terms of the GNU General Public License as published by
154 # the Free Software Foundation; either version 2 of the License, or (at
155 # your option) any later version.
157 # This program is distributed in the hope that it will be useful, but
158 # WITHOUT ANY WARRANTY; without even the implied warranty of
159 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
160 # See the GNU General Public License for more details.
162 # You should have received a copy of the GNU General Public License
163 # along with this program.
164 # If not, see L<http://www.gnu.org/licenses/>.
166 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :