| 1 | package Catalyst::Controller::SimpleCAS::Store::File; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | with qw( | |||||||||||||||||||||||||||
| 7 | Catalyst::Controller::SimpleCAS::Store | ||||||||||||||||||||||||||||
| 8 | ); | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use IO::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use File::Spec::Functions 'catdir', 'catfile'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use Path::Class qw( file dir ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use IO::All; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use File::Copy 'move'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 2 | has 'store_dir' => ( is => 'ro', isa => 'Str', required => 1 ); | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 4 | ▶ | sub init_store_dir { | ||||||||||||||||||||||||||
|
Subroutine
init_store_dir: called CC=3 SCAR=15.4
Pod
documented
| |||||||||||||||||||||||||||||
| 21 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 22 | 4 | ▶ | return if (-d $self->store_dir); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 0 | ▶ | mkdir $self->store_dir or die "Failed to create directory: " . $self->store_dir; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | sub add_content { | ||||||||||||||||||||||||||
|
Subroutine
add_content: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 28 | 0 | my $data = shift; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 0 | $self->init_store_dir; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 0 | my $checksum = $self->calculate_checksum($data); | |||||||||||||||||||||||||||
| 33 | 0 | ▶ | return $checksum if ($self->content_exists($checksum)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0 | my $save_path = $self->checksum_to_path($checksum,1); | |||||||||||||||||||||||||||
| 36 | 0 | ▶ | my $fd= IO::File->new($save_path, '>:raw') or die $!; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 0 | $fd->write($data); | |||||||||||||||||||||||||||
| 38 | 0 | $fd->close; | |||||||||||||||||||||||||||
| 39 | 0 | return $checksum; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | ▶ | sub add_content_file { | ||||||||||||||||||||||||||
|
Subroutine
add_content_file: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 43 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 44 | 0 | my $file = shift; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | $self->init_store_dir; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | my $checksum = $self->file_checksum($file); | |||||||||||||||||||||||||||
| 49 | 0 | ▶ | return $checksum if ($self->content_exists($checksum)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | my $save_path = $self->checksum_to_path($checksum,1); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | try { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 54 | # This is cleaner, but will fail for various reasons like source/dest | ||||||||||||||||||||||||||||
| 55 | # on different file systems: | ||||||||||||||||||||||||||||
| 56 | 0 | ▶ | link $file, $save_path or die "Failed to create hard link: '$file' -> '$save_path'"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | 0 | ▶ | catch { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 59 | 0 | ▶ | move($file, $save_path) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | or die "SimpleCAS: Failed to move file '$file' -> '$save_path': $!"; | ||||||||||||||||||||||||||||
| 61 | 0 | }; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | return $checksum; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 3 | ▶ | sub split_checksum { | ||||||||||||||||||||||||||
|
Subroutine
split_checksum: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 67 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 68 | 3 | my $checksum = shift; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 3 | return ( substr($checksum,0,2), substr($checksum,2) ); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 1 | ▶ | sub add_content_file_mv { | ||||||||||||||||||||||||||
|
Subroutine
add_content_file_mv: called CC=3 SCAR=11.7
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 75 | 1 | my $file = shift; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 1 | $self->init_store_dir; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 1 | my $checksum = $self->file_checksum($file); | |||||||||||||||||||||||||||
| 80 | 1 | ▶ | if ($self->content_exists($checksum)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 0 | unlink $file; | |||||||||||||||||||||||||||
| 82 | 0 | return $checksum; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 1 | my $save_path = $self->checksum_to_path($checksum,1); | |||||||||||||||||||||||||||
| 86 | 1 | ▶ | move($file, $save_path) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | or die "SimpleCAS: Failed to move file '$file' -> '$save_path'"; | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 1 | return $checksum; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 3 | ▶ | sub checksum_to_path { | ||||||||||||||||||||||||||
|
Subroutine
checksum_to_path: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 93 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 94 | 3 | my $checksum = shift; | |||||||||||||||||||||||||||
| 95 | 3 | my $init = shift; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 3 | $self->init_store_dir; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 3 | my ($d, $f) = $self->split_checksum($checksum); | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 3 | my $dir = catdir($self->store_dir, $d); | |||||||||||||||||||||||||||
| 102 | 3 | ▶ | if($init and not -d $dir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$init and not -d $dir
MC/DC50%1/2
$init and not -d $dir
$init
not -d $dir
Truth table66%2/3
A and not(B)
A= $initB= not -d $dir
| |||||||||||||||||||||||||||||
| 103 | 1 | ▶ | mkdir $dir or die "Failed to create directory: " . $dir; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 3 | return catfile( $dir, $f ); | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | sub fetch_content { | ||||||||||||||||||||||||||
|
Subroutine
fetch_content: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 110 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 111 | 0 | my $checksum = shift; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | my $file = $self->checksum_to_path($checksum); | |||||||||||||||||||||||||||
| 114 | 0 | ▶ | return undef unless ( -f $file); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | return io($file)->binary->slurp; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 1 | ▶ | sub content_exists { | ||||||||||||||||||||||||||
|
Subroutine
content_exists: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 120 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 121 | 1 | my $checksum = shift; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 1 | ▶ | return 1 if ( -f $self->checksum_to_path($checksum) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 1 | return 0; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | #### --------------------- #### | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 2 | ▶ | no Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 130 | #__PACKAGE__->meta->make_immutable; | ||||||||||||||||||||||||||||
| 131 | 2 | 1; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | __END__ | ||||||||||||||||||||||||||||