summaryrefslogtreecommitdiffabout
path: root/lib/App/Glacier/Directory.pm
blob: 5e3ad4605d6935ee1c00dc66577289e17b36fff9 (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
package App::Glacier::Directory;
use strict;
use warnings;
use parent 'App::Glacier::DB';
use Carp;
use App::Glacier::Timestamp;

our @EXPORT_OK = qw(DIR_UPTODATE DIR_PENDING DIR_OUTDATED);
our %EXPORT_TAGS = ( status => [ qw(DIR_UPTODATE DIR_PENDING DIR_OUTDATED) ] );

use constant DB_INFO_KEY => ';00INFO';

sub new {
    my ($class, $backend, $vault, $glacier, %opts) = @_;
    (my $vault_name = $vault) =~
	s/([^A-Za-z_0-9\.-])/sprintf("%%%02X", ord($1))/gex;
    map { $opts{$_} =~ s/\$(?:vault|\{vault\})/$vault_name/g } keys %opts;
    my $self = $class->SUPER::new($backend,
				  %opts,
		       create => sub { $glacier->describe_vault($vault_name) },
    );
    if ($self) {
	$self->{_vault} = $vault;
	$self->{_glacier} = $glacier;
    }
    return $self;
}

sub vault { shift->{_vault} }
sub glacier { shift->{_glacier} }

# locate(FILE, VERSION)
sub locate {
    my ($self, $file, $version) = @_;
    $version = 1 unless defined $version;
    my $rec = $self->SUPER::retrieve($file);
    return undef unless defined $rec || $version-1 > $#{$rec};
    return wantarray ? ($rec->[$version-1], $version) : $rec->[$version-1];
}

sub info {
    my ($self, $key) = @_;
    my $rec = $self->retrieve(DB_INFO_KEY);
    return undef unless defined($rec);
    return $rec->{$key};
}

sub set_info {
    my ($self, $key, $val) = @_;
    my $rec = $self->retrieve(DB_INFO_KEY) || {};
    $rec->{$key} = $val;
    $self->SUPER::store(DB_INFO_KEY, $rec);
}

sub last_sync_time {
    my ($self) = @_;
    return $self->info('SyncTimeStamp');
}

sub update_sync_time {
    my ($self) = @_;
    $self->set_info('SyncTimeStamp', time);
}

sub foreach {
    my ($self, $code) = @_;
    $self->SUPER::foreach(sub {
	                      my ($k, $v) = @_;
			      &{$code}($k, $v) unless $k eq DB_INFO_KEY;
			  });
}
	    
sub add_version {
    my ($self, $file_name, $val) = @_;
    my $rec = $self->retrieve($file_name);
    my $i;
    if ($rec) {
	my $t = $val->{CreationDate}->epoch;
	for ($i = 0; $i <= $#{$rec}; $i++) {
	    last if $t >= $rec->[$i]{CreationDate}->epoch;
	}
	splice(@{$rec}, $i, 0, $val);
    } else {
	$i = 0;
	$rec = [ $val ];
    }
    $self->SUPER::store($file_name, $rec);
    return $i + 1;
}

sub delete_version {
    my ($self, $file_name, $ver) = @_;
    $ver--;
    my $rec = $self->retrieve($file_name);
    if ($rec && $ver <= $#{$rec}) {
	splice(@{$rec}, $ver, 1);
	if (@{$rec}) {
	    $self->SUPER::store($file_name, $rec);
	} else {
	    $self->delete($file_name);
	}
    } else {
	++$ver;
	croak "can't remove $file_name;$ver: no such version";
    }
}

sub tempname {
    my ($self, $namelen) = @_;
    $namelen = 10 unless defined $namelen; 
    my @alphabet =
	split //,
	      '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
    my @name;

    for (my $i = 0; $i < $namelen; $i++) {
	push @name, rand($#alphabet);
    }
    my @orig = @name;
    my $s;
    while ($self->has($s = 'TMP_'.join('', map { $alphabet[$_] } @name))) {
	for (my $i = 0; ; $i++) {
	    die "all permutations exhausted" if ($i > $namelen);
	    $name[$i] = ($name[$i] + 1) % @alphabet;
	    last if $name[$i] != $orig[$i];
	}
    }
    return $s;
}

use constant {
    DIR_UPTODATE => 0,  # Directory is up to date
    DIR_PENDING  => 1,  # Directory is empty, needs synchronization
    DIR_OUTDATED => 2   # Directory needs update
};

sub status {
    my ($self) = @_;
    
    if (defined($self->last_sync_time)) {
	my $dsc = timestamp_deserialize($self->glacier->Describe_vault($self->vault));
	unless ($dsc
		&& $dsc->{LastInventoryDate}->epoch < $self->last_sync_time) {
	    return DIR_OUTDATED;
	}
    } else {
	return DIR_PENDING;
    }
    return DIR_UPTODATE;
}

1;

Return to:

Send suggestions and report system problems to the System administrator.