aboutsummaryrefslogtreecommitdiff
path: root/lib/App/Glacier/Command/ListVault.pm
blob: 3533cb1479a19368898de2b68fd75320387475b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package App::Glacier::Command::ListVault;

use strict;
use warnings;
use App::Glacier::Command;
use parent qw(App::Glacier::Command);
use App::Glacier::DateTime;
use App::Glacier::Timestamp;
use App::Glacier::Glob;
use App::Glacier::Directory qw(:status);

=head1 NAME

glacier ls - list vaults or archives

=head1 SYNOPSIS

B<glacier ls>
[B<-SUdlhtr>]
[B<--human-readable>]
[B<--sort=>B<none>|B<name>|B<time>|B<size>]
[B<--reverse>]
[B<--time-style=>B<default>|B<full-iso>|B<long-iso>|B<iso>|B<locale>|B<+I<FORMAT>>]    
[I<VAULT>]
[I<FILE>...]    

=head1 DESCRIPTION

Displays information about vaults and files.

Used without arguments, displays the list of existing vaults.

With one argument, lists files in the named vault, unless B<-d> is
also specified, in which case, lists information about this vault only.

With two or more arguments, lists files in I<VAULT> with names matching
I<FILE> arguments.  The latter can contain version numbers and globbing
patterns.  See B<glacier>(1), section B<On file versioning>, for the
information about file versioning scheme.   

=head1 OPTION

=over 4
    
=item B<-S>
    
=item B<--sort=size>

Sort by file size, largest first.
    
=item B<-U>

=item B<--sort=none>

Don't sort names.    
    
=item B<-d>, B<--directory>

List just the names of vaults, rather than listing their contents.  This is
the default if no arguments are supplied.    
    
=item B<-l>

Print additional information.  For vaults: total size, number of archives
in vault, creation date, and the vault name.  For files: file size, total
number of stored versions, creation date, and the file name.
    
=item B<-t>

=item B<--sort=time>

Sort by modification time, newest first.
        
=item B<-h>, B<--human-readable>

Append a size letter to each size (B<K>, B<M>, or B<G>).  Powers of 1024
are used, so that B<M> stands for 1,048,576 bytes. 
    
=item B<--sort=>I<KEY>

Sort output according to I<KEY>, which is one of: B<none>, B<name>, B<time>,
B<size>.  Default is B<name>.
    
=item B<-r>, B<--reverse>

Reverse the sort order.  E.g. with B<-t>, list youngest files first.    
    
=item B<--time-style=>I<STYLE>

List timestamps in style STYLE.  The STYLE should be one of the
following:

=over 8

=item B<+I<FORMAT>>

List timestamps using I<FORMAT>, which is interpreted as the I<format>
argument to B<strftime>(3) function.
    
=item B<default>

Default format.  For timestamps not older than 6 month: month, day, hour
and minute, e.g.: "May 23 15:00".  For older timestamps: month, day, and
year, e.g.: "May 23  2017".
    
=item B<full-iso>

List timestamps in full using ISO 8601 date, time, and time zone format with
nanosecond precision, e.g., "2017-05-23 15:53:10.633308971 +0000".  This style
is equivalent to "B<+%Y-%m-%d %H:%M:%S.%N %z>".

=item B<long-iso>

List ISO 8601 date and time in minutes, e.g., "2017-05-23 15:53".  Equivalent
to "B<+%Y-%m-%d %H:%M>".    
    
=item B<iso>

B<GNU ls>-compatible "iso": ISO 8601 dates for non-recent timestamps (e.g.,
"2017-05-23"), and ISO 8601 month, day, hour, and minute for recent
timestamps (e.g., "03-30 23:45").  Timestamp is considered "recent", if it
is not older than 6 months ago.    

=item B<locale>

List timestamps in a locale-dependent form.  This is equivalent to B<+%c>.

=back

=back
    
=head1 SEE ALSO

B<glacier>(1),    
B<strftime>(3).

=cut    

sub getopt {
    my ($self, %opts) = @_;
    my %sort_vaults = (
	none => undef,
	name => sub {
	    my ($a, $b) = @_;
	    $a->{VaultName} cmp $b->{VaultName}
	},
	time => sub {
	    my ($a, $b) = @_;
	    $a->{CreationDate}->epoch <=> $b->{CreationDate}->epoch;
	},
	size => sub {
	    my ($a, $b) = @_;
	    $a->{SizeInBytes} <=> $b->{SizeInBytes}
	}
    );
    my %sort_archives = (
	none => undef,
	name => sub {
	    my ($a, $b) = @_;
	    $a->{FileName} cmp $b->{FileName}
	},
	time => sub {
	    my ($a, $b) = @_;
	    $a->{CreationDate}->epoch <=> $b->{CreationDate}->epoch;
	},
	size => sub {
	    my ($a, $b) = @_;
	    $a->{Size} <=> $b->{Size}
	}
    );
    $self->{_options}{sort} = 'name';
    my $rc = $self->SUPER::getopt(
	'directory|d' => \$self->{_options}{d},
	'l' => \$self->{_options}{l},
	'sort=s' => \$self->{_options}{sort},
	't' => sub { $self->{_options}{sort} = 'time' },
	'S' => sub { $self->{_options}{sort} = 'size' },
	'U' => sub { $self->{_options}{sort} = 'none' },
	'human-readable|h' => \$self->{_options}{h},
	'reverse|r' => \$self->{_options}{r},
	'time-style=s' => sub { $self->set_time_style_option($_[1]) },
	%opts);
    return $rc unless $rc;

    $self->{_options}{d} = 1 if (@ARGV == 0);

    if (defined($self->{_options}{sort})) {
	my $sortfun = $self->{_options}{d}
	                ? \%sort_vaults : \%sort_archives;
	$self->abend(EX_USAGE, "unknown sort field")
	    unless exists($sortfun->{$self->{_options}{sort}});
	$self->{_options}{sort} = $sortfun->{$self->{_options}{sort}};
    }
}
    
sub run {
    my $self = shift;
    
    if ($self->{_options}{d}) {
	$self->list_vaults($self->get_vault_list(@_));
    } else {
	$self->list_archives($self->get_vault_inventory(@_));
    }
}

sub get_vault_list {
    my $self = shift;

    my $glob = new App::Glacier::Glob(@_);
    if ($glob->is_literal) {
	return [$self->describe_vault(@_)];
    } else {
	my $res = $self->glacier_eval('list_vaults');
	if ($self->lasterr) {
	    $self->abend(EX_FAILURE, "can't list vaults: ",
			 $self->last_error_message);
	}
	return [map { timestamp_deserialize($_) }
	           $glob->filter(sub {
		                    my ($x) = @_;
				    return $x->{VaultName}
				 }, @$res)];
    }
}

sub list_vaults {
    my ($self, $ref) = @_;

    foreach my $v (defined($self->{_options}{sort}) ?
		        sort {
			    &{$self->{_options}{sort}} 
			      ($self->{_options}{r} ? ($b, $a) : ($a, $b))
		        } @$ref
		      : @$ref) {
	$self->show_vault($v);
    }
}

sub format_size {
    my ($self, $size, $width) = @_;
    my $suf = '';
    if ($self->{_options}{h}) {
	my @suffixes = ('K', 'M', 'G');
	while ($size >= 1024 && @suffixes) {
	    $size /= 1024;
	    $suf = shift @suffixes;
	}
    }
    my $l = ($width || 10);
    return sprintf("%*.*s", $l, $l, int($size) . $suf);
}

sub show_vault {
    my ($self, $vault) = @_;
    if ($self->{_options}{l}) {
	printf("%8s % 8u %s %-24s\n",
	       $self->format_size($vault->{SizeInBytes}),
	       $vault->{NumberOfArchives},
	       $vault->{CreationDate}->canned_format($self->{_options}{time_style}),
	       $vault->{VaultName});
    } else {
	print $vault->{VaultName},"\n";
    }
}

sub list_archives {
    my ($self, $ref) = @_;

    foreach my $v (defined($self->{_options}{sort}) ?
		        sort {
			    &{$self->{_options}{sort}} 
			      ($self->{_options}{r} ? ($b, $a) : ($a, $b))
		        } @$ref
		      : @$ref) {
	$self->show_archive($v);
    }
}

sub show_archive {
    my ($self, $arch) = @_;

    if ($self->{_options}{l}) {
	printf("%8s % 8u %s %-24s\n",
	       $self->format_size($arch->{Size}),
	       $arch->{FileTotalVersions},
	       $arch->{CreationDate}->canned_format($self->{_options}{time_style}),
	       $arch->{FileName});
	
    } else {
	print "$arch->{FileName}\n";
    }
}

sub get_vault_inventory {
    my ($self, $vault_name, @file_list) = @_;
    my $dir = $self->directory($vault_name);
    $self->abend(EX_FAILURE, "no such vault: $vault_name")
	unless defined $dir;

    if ($dir->status == DIR_PENDING) {
	require App::Glacier::Command::Sync;	
	my $sync = new App::Glacier::Command::Sync;
	$sync->sync($vault_name) or exit(EX_TEMPFAIL);
    }
    
    my @glob;
    if (@file_list) {
	my %vtab;
	my @unversioned;
	foreach my $f (@file_list) {
	    if ($f =~ /^(?<pat>.+?)(?<!\\);(?<ver>\d+)$/) {
		push @{$vtab{$+{ver}}}, $+{pat};
	    } else {
		push @unversioned, $f;
	    }
	}

	push @glob, [ new App::Glacier::Glob(@unversioned), 1 ]
	    if @unversioned;
	while (my ($ver, $pat) = each %vtab) {
	    push @glob, [ new App::Glacier::Glob(@$pat), $ver ];
	}
    } else {
	push @glob, [ new App::Glacier::Glob, 1 ];
    }
    
    my @result;
    $dir->foreach(sub {
	my ($file, $info) = @_;
	foreach my $ver (map { $_->[0]->match($file) ? $_->[1] : () } @glob) {
	    next unless $ver <= @$info;
	    push @result, {@{[ %{$info->[$ver-1]},
                               ( 'FileName' => $file,
				 'FileVersion' => $ver,
				 'FileTotalVersions' => $#{$info} + 1 ) ]}};
	}
		  });
    
    return \@result;
}

1;

Return to:

Send suggestions and report system problems to the System administrator.