[PATCH 5/7] Add possibility for callbacks
Michael Hanselmann
public at hansmi.ch
Wed Aug 13 21:16:00 UTC 2008
From: Michael Hanselmann <hansmi at hansmi-mac-zrh.local>
These can be used in test cases.
---
lib/Brackup/Backup.pm | 23 ++++++++++++++++++-----
1 files changed, 18 insertions(+), 5 deletions(-)
diff --git a/lib/Brackup/Backup.pm b/lib/Brackup/Backup.pm
index 5ec0945..727c6ac 100644
--- a/lib/Brackup/Backup.pm
+++ b/lib/Brackup/Backup.pm
@@ -25,7 +25,13 @@ sub new {
# returns true (a Brackup::BackupStats object) on success, or dies with error
sub backup {
my ($self, $backup_file) = @_;
- my $backup = Brackup::Backup::_Sync->new($self, $backup_file);
+
+ return $self->backup2(backup_file => $backup_file);
+}
+
+sub backup2 {
+ my ($self, %opts) = @_;
+ my $backup = Brackup::Backup::_Sync->new(parent => $self, %opts);
$backup->run();
@@ -41,14 +47,16 @@ sub backup_time {
package Brackup::Backup::_Base;
use strict;
use warnings;
-use Brackup::Util qw/tempfile/;
+use Brackup::Util qw/tempfile valid_params/;
sub new {
- my ($class, $parent, $backup_file) = @_;
+ my $class = shift;
my $self = bless {}, $class;
- $self->{parent} = $parent;
- $self->{backup_file} = $backup_file;
+ my %opts = valid_params(['parent', 'backup_file', 'cb_end_file'], @_);
+
+ $self->{parent} = $opts{parent} or die;
+ $self->{backup_file} = $opts{backup_file} or die;
$self->{stats} = Brackup::BackupStats->new;
$self->{comp_chunk} = undef;
@@ -68,6 +76,9 @@ sub new {
# Amount of uploaded data
$self->{n_kb_up} = 0.0;
+ # Callbacks for tests
+ $self->{cb_end_file} = $opts{cb_end_file} || sub {};
+
return $self;
}
@@ -234,6 +245,8 @@ sub end_file {
return unless $file;
+ $self->{cb_end_file}->($file, $self->{stored_chunks});
+
push @{$self->{stored_files}}, [$file, $self->{stored_chunks}];
$self->{n_files_done}++;
--
1.5.5.3
More information about the brackup
mailing list