diff --git a/lib/Simba/CA.pm b/lib/Simba/CA.pm index c6624ec..f7570a2 100644 --- a/lib/Simba/CA.pm +++ b/lib/Simba/CA.pm @@ -389,34 +389,77 @@ sub db_record_version { } if ($f->{t} eq 'f' && !defined($f->{checksum})) { # this must be a link to the previous version - my $db_pv = $self->{dbh}->selectall_arrayref("select * from versions where file=? and session=?", - { Slice => {} }, - $db_f->[0]{id}, - $self->{last_backup_id}); + my $db_pv = $self->{dbh}->selectall_arrayref( + "select * from versions2 v, instances i + where file=? and session=? and v.id = i.version", + { Slice => {} }, + $db_f->[0]{id}, + $self->{last_backup_id} + ); $f->{checksum} = $db_pv->[0]{checksum}; } - $self->{dbh}->do("insert into versions(file, - file_id, file_type, file_size, file_mtime, - file_owner, file_group, file_acl, - file_unix_bits, - file_rdev, - date, checksum, online, file_linktarget, - session) + + my $query = + "select id from versions2 + where + file_type=? and file_size=? and file_mtime=? and + file_owner=? and file_group=? and file_acl=? and + file_unix_bits=? and + checksum=? + "; + my @args = ( + $f->{t}, $f->{s}, $f->{m}, + $f->{o}, $f->{g}, $f->{acl}, + join(',', map {$f->{$_} ? ($_) : ()} qw(setuid setgid sticky)), + $f->{checksum} + ); + if ($f->{t} eq 'b' || $f->{t} eq 'c') { + $query .= " and file_rdev=?"; + push @args, $f->{rdev}; + } + if ($f->{t} eq 'l') { + $query .= " and file_linktarget=?"; + push @args, $f->{lt}; + } + + my $version_id = $self->{dbh}->selectrow_array($query, {}, @args); + unless ($version_id) { + $self->{dbh}->do("insert into versions2( + file_type, file_size, file_mtime, + file_owner, file_group, file_acl, + file_unix_bits, + file_rdev, + checksum, file_linktarget, + ) + values( + ?, ?, ?, + ?, ?, ?, + ?, + ?, + ?, ?, + )", + {}, + $f->{t}, $f->{s}, $f->{m}, + $f->{o}, $f->{g}, $f->{acl}, + join(',', map {$f->{$_} ? ($_) : ()} qw(setuid setgid sticky)), + $f->{rdev}, + $f->{checksum}, $f->{lt}, + ); + $version_id = $self->{dbh}->{mysql_insertid}; + } + $self->{dbh}->do("insert into instances(file, + file_id, + date, online, + session, version) values(?, - ?, ?, ?, ?, - ?, ?, ?, - ?, - ?, - ?, ?, ?, ?, - ?)", + ?, + ?, ?, + ?, ?)", {}, $db_f->[0]{id}, - $f->{id}, $f->{t}, $f->{s}, $f->{m}, - $f->{o}, $f->{g}, $f->{acl}, - join(',', map {$f->{$_} ? ($_) : ()} qw(setuid setgid sticky)), - $f->{rdev}, - time(), $f->{checksum}, 1, $f->{lt}, - $self->{session_id} + $f->{id}, + time(), 1, + $self->{session_id}, $version_id ); } @@ -468,13 +511,15 @@ and it is more likely that we can link to new copies than to old ones. sub linkdup { my ($self, $f, $backup_filename) = @_; - my $sth = $self->{dbh}->prepare("select * from versions, files, sessions + # XXX + my $sth = $self->{dbh}->prepare("select * from versions2, instances, files where file_type=? and file_size=? and file_mtime=? and file_owner=? and file_group=? and file_acl=? and file_unix_bits=? and checksum=? and online=1 - and versions.file=files.id and versions.session=sessions.id - order by sessions.id desc + and instances.file=files.id + and versions2.id=instances.version + order by instances.session desc "); $sth->execute( $f->{t}, $f->{s}, $f->{m},