-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirLog.pm
More file actions
191 lines (161 loc) · 4.13 KB
/
DirLog.pm
File metadata and controls
191 lines (161 loc) · 4.13 KB
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
package DirLog;
use strict;
use Carp;
use FileUtil ('copy_file');
my $file='.dirlog';
my $sep=' ';
#status A=added, R=removed, F=found, L=lost, T=transient (lost/removed+found)
my %_exists_now = (A=>1, T=>1, F=>1);
sub _read_log {
my $stats = shift;
my $logfh = shift;
while (<$logfh>) {
chomp;
next unless m/([A-Z])$sep(.+)/;
$stats->{$2}=$1;
}
}
sub new {
my $class = shift;
my $dir = shift;
my $opts = shift;
my %stats;
my $logfh;
my %files;
my $found_state = 'F';
if (defined $opts->{state}) {
$found_state = $opts->{state};
croak "Invalid state: $found_state"
unless $found_state =~ m/^[ARFLT]$/;
};
#if log exists, scan it in
open $logfh, '<', "$dir/$file" and do {
_read_log(\%stats,$logfh);
close $logfh;
};
opendir my($dh), $dir or croak "$dir: $!";
while (my $file = readdir $dh) {
next if $file =~ /^\./;
next if -d "$dir/$file";
next unless -f "$dir/$file";
$files{$file} = 1;
};
closedir $dh;
while (my ($k,$v) = each %stats) {
if (not -f "$dir/$k") {
if ($_exists_now{$v}) {
$stats{$k} = 'L';
}
} elsif ($v eq 'R' or $v eq 'L') {
$stats{$k} = 'T';
}
};
while (my ($k,$v) = each %files ) {
$v = $stats{$k};
if (not defined $v) {
$stats{$k} = $found_state;
} elsif ($v eq 'R' or $v eq 'L') {
$stats{$k} = 'T';
}
}
delete $stats{$file};
bless \%stats, $class;
}
sub from_file {
my $class = shift;
my $file = shift;
my %stats;
open my($logfh), '<', $file or croak "$file: $!";
_read_log(\%stats, $logfh);
close $logfh;
bless \%stats, $class;
}
sub write {
my $self=shift;
my $dir = shift;
my $logfh;
croak "Need a directory to write the log in" unless defined $dir;
open $logfh, ">$dir/$file" or die "$dir/$file";
foreach my $file (sort keys %{$self}) {
print $logfh "$self->{$file}$sep$file\n";
}
close $logfh;
}
sub existed {
my $self = shift;
my $file = shift;
return defined $self->{$file};
}
sub exists_now {
my $self = shift;
my $file = shift;
return (defined($self->{$file}) and $_exists_now{$self->{$file}});
}
sub set {
$_[0]{$_[1]}=$_[2];
}
sub add {
set (@_, 'A');
}
sub remove {
set (@_, 'R');
}
my @order = ('R', 'L', 'A', 'F', 'T');
my %order = map { $order[$_] => $_ } 0..$#order;
sub combine {
my ($class, $lhs, $rhs) = @_;
my %stats = %$lhs;
while (my($file,$state) = each %$rhs) {
if (not defined $stats{$file}) {
$stats{$file} = $state;
} elsif ($order{$state} < $order{ $stats{$file} } ){
$stats{$file} = $state;
}
}
bless \%stats, $class;
}
sub sync_dir_to_file {
my ($self, $self_dir, $other) = @_;
while (my($file,$other_state) = each %$other) {
my $self_file = $self->{$file};
$self_file = '' unless defined $self_file;
warn "$file $self_file -> $other_state"
unless $self_file eq $other_state;
if (not defined $self->{$file}) {
$self->{$file} = $other_state;
warn "Missing $file" if $_exists_now{$other_state};
} elsif ($order{$other_state} < $order{ $self->{$file} } ){
#warn "$file $self->{$file} -> $other_state";
if ($self->exists_now($file)) {
unlink "$self_dir/$file" unless $other->exists_now($file);
warn "rm $self_dir/$file" unless $other->exists_now($file);
} else {
warn "missing $file" if $other->exists_now($file);
}
$self->{$file} = $other_state;
} else {
#warn "$file $self->{$file} -> $other_state";
}
}
}
sub sync_dir {
my ($self,$self_dir,$other_dir) = @_;
my $other = ref($other_dir)? $other_dir : ref($self)->new($other_dir);
while (my($file,$state) = each %$other) {
if (not defined $self->{$file}) {
$self->{$file} = $state;
copy_file("$other_dir/$file", "$self_dir/$file")
if $_exists_now{$state};
} elsif ($order{$state} < $order{ $self->{$file} } ){
warn "$file $self->{$file} -> $state";
if ($self->exists_now($file)) {
unlink "$self_dir/$file" unless $other->exists_now($file);
} else {
copy_file("$other_dir/$file", "$self_dir/$file")
if $other->exists_now($file);
}
$self->{$file} = $state;
}
}
}
1;