| 1 | package App::SimpleBackuper::RegularFile; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 2 | ▶ | use Fcntl qw(:DEFAULT :flock); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 2 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 0 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: not called CC=5 SCAR=34.0
| |||||||||||||||
| 9 | 0 | my($class, $filepath, $options, $state) = @_; | |||||||||||||
| 10 | |||||||||||||||
| 11 | 0 | ▶ | confess "No filepath" if ! $filepath; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | 0 | my $self = bless { | |||||||||||||
| 14 | options => $options, | ||||||||||||||
| 15 | state => $state, | ||||||||||||||
| 16 | filepath=> $filepath, | ||||||||||||||
| 17 | readable=> 1, | ||||||||||||||
| 18 | writable=> 0, | ||||||||||||||
| 19 | offset => 0, | ||||||||||||||
| 20 | } => $class; | ||||||||||||||
| 21 | |||||||||||||||
| 22 | 0 | my($handler); | |||||||||||||
| 23 | 0 | ▶ | eval { sysopen($handler, $filepath, O_RDONLY) // die "$!\n" }; | ||||||||||||
|
Condition0%0/2
sysopen($handler, $filepath, 0) // die("$!\n")
MC/DC0%0/1
sysopen($handler, $filepath, 0) // die("$!\n")
sysopen($handler, $filepath, 0)
Truth table0%0/2
A
A= sysopen($handler, $filepath, 0)
| |||||||||||||||
| 24 | 0 | ▶ | if($@) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 25 | 0 | ▶ | if($!{ENOENT}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 26 | 0 | $self->{readable} = 0; | |||||||||||||
| 27 | } else { | ||||||||||||||
| 28 | 0 | die "$@\n"; | |||||||||||||
| 29 | } | ||||||||||||||
| 30 | } | ||||||||||||||
| 31 | 0 | $self->{handler} = $handler; | |||||||||||||
| 32 | |||||||||||||||
| 33 | 0 | return $self; | |||||||||||||
| 34 | } | ||||||||||||||
| 35 | |||||||||||||||
| 36 | 0 | ▶ | sub set_write_mode { | ||||||||||||
|
Subroutine
set_write_mode: not called CC=5 SCAR=34.0
| |||||||||||||||
| 37 | 0 | my($self) = @_; | |||||||||||||
| 38 | 0 | ▶ | return if $self->{writable}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 39 | |||||||||||||||
| 40 | 0 | ▶ | sysopen(my $handler, $self->{filepath}, O_RDWR|O_CREAT) or die "$!\n"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 41 | 0 | ▶ | flock($handler, LOCK_EX|LOCK_NB) or die "$!\n"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 42 | 0 | $self->{writable} = 1; | |||||||||||||
| 43 | 0 | $self->{handler} = $handler; | |||||||||||||
| 44 | 0 | ▶ | sysseek($handler, $self->{offset}, 0) // die "$!\n"; | ||||||||||||
|
Condition0%0/2
sysseek($handler, $self->{"offset"}, 0) // die("$!\n")
MC/DC0%0/1
sysseek($handler, $self->{"offset"}, 0) // die("$!\n")
sysseek($handler, $self->{"offset"}, 0)
Truth table0%0/2
A
A= sysseek($handler, $self->{"offset"}, 0)
| |||||||||||||||
| 45 | } | ||||||||||||||
| 46 | |||||||||||||||
| 47 | 0 | ▶ | sub _num2offset { ! $_[0] ? 0 : $_[0] < 3 ? 10 ** ($_[0] - 1) * 1e6 : ($_[0] - 2) * 100e6 } | ||||||||||||
|
Subroutine
_num2offset: not called CC=3 SCAR=24.8
Branch: 2 branches
| |||||||||||||||
| 48 | |||||||||||||||
| 49 | 0 | ▶ | sub read { | ||||||||||||
|
Subroutine
read: not called CC=6 SCAR=37.4
| |||||||||||||||
| 50 | 0 | my($self, $num) = @_; | |||||||||||||
| 51 | |||||||||||||||
| 52 | 0 | ▶ | confess "File '$self->{filepath}' doesn't exists" if ! $self->{readable}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 53 | |||||||||||||||
| 54 | 0 | $self->{data} = ''; | |||||||||||||
| 55 | 0 | ▶ | if(! defined $num) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 56 | 0 | ▶ | my $read = sysread($self->{handler}, $self->{data}, -s $self->{filepath}) // die "$!\n"; | ||||||||||||
|
Condition0%0/2
sysread($self->{"handler"}, $self->{"data"}, -s $self->{"filepath"}) // die("$!\n")
MC/DC0%0/1
sysread($self->{"handler"}, $self->{"data"}, -s $self->{"filepath"}) // die("$!\n")
sysread($self->{"handler"}, $self->{"data"}, -s $self->{"filepath"})
Truth table0%0/2
A
A= sysread($self->{"handler"}, $self->{"data"}, -s $self->{"filepath"})
| |||||||||||||||
| 57 | #printf "\t%d bytes of whole file '$self->{filepath}' read\n", $read; | ||||||||||||||
| 58 | 0 | $self->{offset} += $read; | |||||||||||||
| 59 | 0 | return !! $read; | |||||||||||||
| 60 | } else { | ||||||||||||||
| 61 | # Parts: 0-1M, 1M-10M, 10M-100M, 100M-200M, 200M-300M,.. | ||||||||||||||
| 62 | |||||||||||||||
| 63 | 0 | my $offset = _num2offset($num); | |||||||||||||
| 64 | 0 | my $length = _num2offset($num + 1) - $offset; | |||||||||||||
| 65 | |||||||||||||||
| 66 | 0 | ▶ | sysseek($self->{handler}, $offset, 0) // die "$!\n"; | ||||||||||||
|
Condition0%0/2
sysseek($self->{"handler"}, $offset, 0) // die("$!\n")
MC/DC0%0/1
sysseek($self->{"handler"}, $offset, 0) // die("$!\n")
sysseek($self->{"handler"}, $offset, 0)
Truth table0%0/2
A
A= sysseek($self->{"handler"}, $offset, 0)
| |||||||||||||||
| 67 | 0 | ▶ | my $read = sysread($self->{handler}, $self->{data}, $length) // die "$!\n"; | ||||||||||||
|
Condition0%0/2
sysread($self->{"handler"}, $self->{"data"}, $length) // die("$!\n")
MC/DC0%0/1
sysread($self->{"handler"}, $self->{"data"}, $length) // die("$!\n")
sysread($self->{"handler"}, $self->{"data"}, $length)
Truth table0%0/2
A
A= sysread($self->{"handler"}, $self->{"data"}, $length)
| |||||||||||||||
| 68 | #printf "\t%d bytes of %d bytes from %d offset of part #%d read\n", $read, $length, $offset, $num; | ||||||||||||||
| 69 | |||||||||||||||
| 70 | 0 | $self->{offset} += $read; | |||||||||||||
| 71 | 0 | return $read; | |||||||||||||
| 72 | } | ||||||||||||||
| 73 | } | ||||||||||||||
| 74 | |||||||||||||||
| 75 | 0 | ▶ | sub write { | ||||||||||||
|
Subroutine
write: not called CC=5 SCAR=34.0
| |||||||||||||||
| 76 | 0 | my($self, $num) = @_; | |||||||||||||
| 77 | |||||||||||||||
| 78 | 0 | $self->set_write_mode(); | |||||||||||||
| 79 | |||||||||||||||
| 80 | 0 | ▶ | if(! defined $num) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 81 | # TODO: замерить время FS | ||||||||||||||
| 82 | 0 | ▶ | syswrite($self->{handler}, $self->{data}) // die "$!\n"; | ||||||||||||
|
Condition0%0/2
syswrite($self->{"handler"}, $self->{"data"}) // die("$!\n")
MC/DC0%0/1
syswrite($self->{"handler"}, $self->{"data"}) // die("$!\n")
syswrite($self->{"handler"}, $self->{"data"})
Truth table0%0/2
A
A= syswrite($self->{"handler"}, $self->{"data"})
| |||||||||||||||
| 83 | |||||||||||||||
| 84 | #printf "\t%d bytes written to %s\n", length( $self->{data} ), $self->{filepath}; | ||||||||||||||
| 85 | 0 | $self->{offset} += length $self->{data}; | |||||||||||||
| 86 | } else { | ||||||||||||||
| 87 | 0 | my $offset = _num2offset($num); | |||||||||||||
| 88 | 0 | my $length = _num2offset($num + 1) - $offset; | |||||||||||||
| 89 | 0 | ▶ | sysseek($self->{handler}, $offset, 0) // die "$!\n"; | ||||||||||||
|
Condition0%0/2
sysseek($self->{"handler"}, $offset, 0) // die("$!\n")
MC/DC0%0/1
sysseek($self->{"handler"}, $offset, 0) // die("$!\n")
sysseek($self->{"handler"}, $offset, 0)
Truth table0%0/2
A
A= sysseek($self->{"handler"}, $offset, 0)
| |||||||||||||||
| 90 | |||||||||||||||
| 91 | 0 | ▶ | syswrite($self->{handler}, $self->{data}) // die "$!\n"; | ||||||||||||
|
Condition0%0/2
syswrite($self->{"handler"}, $self->{"data"}) // die("$!\n")
MC/DC0%0/1
syswrite($self->{"handler"}, $self->{"data"}) // die("$!\n")
syswrite($self->{"handler"}, $self->{"data"})
Truth table0%0/2
A
A= syswrite($self->{"handler"}, $self->{"data"})
| |||||||||||||||
| 92 | #printf "\t%d bytes written to part #%d of %s\n", length( $self->{data} ), $num, $self->{filepath}; | ||||||||||||||
| 93 | |||||||||||||||
| 94 | 0 | $self->{offset} = $offset + $length; | |||||||||||||
| 95 | } | ||||||||||||||
| 96 | } | ||||||||||||||
| 97 | |||||||||||||||
| 98 | 2 | ▶ | use Digest::SHA qw(sha512_hex); # apt install libdigest-sha-perl | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 99 | 0 | ▶ | sub hash { | ||||||||||||
|
Subroutine
hash: not called CC=1 SCAR=6.9
| |||||||||||||||
| 100 | 0 | my($self) = @_; | |||||||||||||
| 101 | 0 | my $hash = sha512_hex($self->{data}); | |||||||||||||
| 102 | #printf "\tsha512 is %s\n", $hash; | ||||||||||||||
| 103 | 0 | return $hash; | |||||||||||||
| 104 | } | ||||||||||||||
| 105 | |||||||||||||||
| 106 | 0 | ▶ | use Compress::Raw::Lzma; # apt install libcompress-raw-lzma-perl | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||||||||||||||
| 107 | sub compress { | ||||||||||||||
| 108 | my($self) = @_; | ||||||||||||||
| 109 | |||||||||||||||
| 110 | my $compression_level = $self->{options}->{compression_level}; | ||||||||||||||
| 111 | # Because is useless | ||||||||||||||
| 112 | $compression_level = 0 if $self->{filepath} =~ /\.(jpg|jpeg|mp3|mp4|avi|mkv|mov|mpg|mpeg|nef)$/i; | ||||||||||||||
| 113 | |||||||||||||||
| 114 | my($z, $status) = Compress::Raw::Lzma::EasyEncoder->new( | ||||||||||||||
| 115 | Preset => $compression_level, | ||||||||||||||
| 116 | Check => LZMA_CHECK_SHA256, | ||||||||||||||
| 117 | AppendOutput => 1, | ||||||||||||||
| 118 | ); | ||||||||||||||
| 119 | die "$status\n" if $status != LZMA_OK; | ||||||||||||||
| 120 | |||||||||||||||
| 121 | $status = $z->code($self->{data}, my $out); | ||||||||||||||
| 122 | die "$status\n" if $status != LZMA_OK; | ||||||||||||||
| 123 | |||||||||||||||
| 124 | $status = $z->flush($out); | ||||||||||||||
| 125 | die "$status\n" if $status != LZMA_STREAM_END; | ||||||||||||||
| 126 | |||||||||||||||
| 127 | my $ratio = length($out) / (length($self->{data}) || 1); | ||||||||||||||
| 128 | $self->{data} = $out; | ||||||||||||||
| 129 | |||||||||||||||
| 130 | return $ratio; | ||||||||||||||
| 131 | } | ||||||||||||||
| 132 | |||||||||||||||
| 133 | sub decompress { | ||||||||||||||
| 134 | my($self) = @_; | ||||||||||||||
| 135 | |||||||||||||||
| 136 | my($z, $status) = Compress::Raw::Lzma::AutoDecoder->new(); | ||||||||||||||
| 137 | die "$status\n" if $status != LZMA_OK; | ||||||||||||||
| 138 | |||||||||||||||
| 139 | $status = $z->code($self->{data}, my $out); | ||||||||||||||
| 140 | confess $status if $status != LZMA_STREAM_END; | ||||||||||||||
| 141 | |||||||||||||||
| 142 | my $ratio = length($self->{data}) / length($out); | ||||||||||||||
| 143 | $self->{data} = $out; | ||||||||||||||
| 144 | |||||||||||||||
| 145 | return $ratio; | ||||||||||||||
| 146 | } | ||||||||||||||
| 147 | |||||||||||||||
| 148 | # key, iv | ||||||||||||||
| 149 | sub gen_keys { pack("C32", map {int rand 256} 1..32), pack("C16", map {int rand 256} 1..16) } | ||||||||||||||
| 150 | |||||||||||||||
| 151 | use Crypt::Rijndael; # apt install libcrypt-rijndael-perl | ||||||||||||||
| 152 | srand(time ^ $$ ^ (unpack("%L*", `head /dev/urandom`) * unpack("%L*", `head /dev/urandom`))); # For randomness | ||||||||||||||
| 153 | sub encrypt { | ||||||||||||||
| 154 | my($self, $key, $iv) = @_; | ||||||||||||||
| 155 | |||||||||||||||
| 156 | my $cipher = Crypt::Rijndael->new($key, Crypt::Rijndael::MODE_CBC()); | ||||||||||||||
| 157 | $cipher->set_iv($iv); | ||||||||||||||
| 158 | |||||||||||||||
| 159 | $self->{data} = $cipher->encrypt($self->{data} | ||||||||||||||
| 160 | . "\0" x (length($self->{data}) % length($key) ? length($key) - length($self->{data}) % length($key) : 0) | ||||||||||||||
| 161 | ) # aligning data for crypt. decompressor ignores it. | ||||||||||||||
| 162 | ; | ||||||||||||||
| 163 | |||||||||||||||
| 164 | return; | ||||||||||||||
| 165 | } | ||||||||||||||
| 166 | |||||||||||||||
| 167 | sub decrypt { | ||||||||||||||
| 168 | my($self, $key, $iv) = @_; | ||||||||||||||
| 169 | |||||||||||||||
| 170 | my $cipher = Crypt::Rijndael->new($key, Crypt::Rijndael::MODE_CBC()); | ||||||||||||||
| 171 | $cipher->set_iv($iv); | ||||||||||||||
| 172 | |||||||||||||||
| 173 | $self->{data} = $cipher->decrypt($self->{data}); | ||||||||||||||
| 174 | |||||||||||||||
| 175 | return; | ||||||||||||||
| 176 | } | ||||||||||||||
| 177 | |||||||||||||||
| 178 | sub size { length shift->{data} } | ||||||||||||||
| 179 | |||||||||||||||
| 180 | sub data_ref { | ||||||||||||||
| 181 | my $self = shift; | ||||||||||||||
| 182 | if(@_) { | ||||||||||||||
| 183 | $self->{data} = shift; | ||||||||||||||
| 184 | confess if ref($self->{data}) ne 'SCALAR'; | ||||||||||||||
| 185 | $self->{data} = ${ $self->{data} }; | ||||||||||||||
| 186 | return $self; | ||||||||||||||
| 187 | } else { | ||||||||||||||
| 188 | return \$self->{data}; | ||||||||||||||
| 189 | } | ||||||||||||||
| 190 | } | ||||||||||||||
| 191 | |||||||||||||||
| 192 | sub truncate { | ||||||||||||||
| 193 | my($self, $size) = @_; | ||||||||||||||
| 194 | $self->set_write_mode(); | ||||||||||||||
| 195 | truncate($self->{handler}, $size) // die "$!\n"; | ||||||||||||||
| 196 | return $self; | ||||||||||||||
| 197 | } | ||||||||||||||
| 198 | |||||||||||||||
| 199 | 1; | ||||||||||||||