| 1 | # Copyright (c) 2014, cPanel, Inc. | ||||||||||||||||||||||||||||
| 2 | # All rights reserved. | ||||||||||||||||||||||||||||
| 3 | # http://cpanel.net/ | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # This is free software; you can redistribute it and/or modify it under the same | ||||||||||||||||||||||||||||
| 6 | # terms as Perl itself. See the LICENSE file for further details. | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | package Filesys::POSIX::Mem::Bucket; | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 27 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 27 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 27 | ▶ | use Filesys::POSIX::Bits; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 27 | ▶ | use Filesys::POSIX::Bits::System; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 27 | ▶ | use Filesys::POSIX::IO::Handle (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 27 | ▶ | use Filesys::POSIX::Error qw(throw); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 27 | ▶ | use Fcntl; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 27 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 27 | ▶ | use File::Temp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =head1 NAME | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | Filesys::POSIX::Mem::Bucket - Regular file I/O handle | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | C<Filesys::POSIX::Mem::Bucket> provides an implementation of the interface in | ||||||||||||||||||||||||||||
| 29 | L<Filesys::POSIX::IO::Handle> that allows access to the regular file data of a | ||||||||||||||||||||||||||||
| 30 | file in a L<Filesys::POSIX::Mem> filesystem hierarchy. | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | Internally, the bucket can store up to a specified maximum number of bytes until | ||||||||||||||||||||||||||||
| 33 | said data is flushed to a temporary file on disk, backed by L<File::Temp>. | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =cut | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 27 | our @ISA = ('Filesys::POSIX::IO::Handle'); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 27 | my $DEFAULT_MAX = 16384; | |||||||||||||||||||||||||||
| 40 | 27 | my $DEFAULT_DIR = '/tmp'; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 73 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 43 | 73 | my ( $class, %opts ) = @_; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | return bless { | ||||||||||||||||||||||||||||
| 46 | 'fh' => undef, | ||||||||||||||||||||||||||||
| 47 | 'buf' => '', | ||||||||||||||||||||||||||||
| 48 | 'max' => defined $opts{'max'} ? $opts{'max'} : $DEFAULT_MAX, | ||||||||||||||||||||||||||||
| 49 | 'dir' => defined $opts{'dir'} ? $opts{'dir'} : $DEFAULT_DIR, | ||||||||||||||||||||||||||||
| 50 | 73 | ▶ | 'inode' => $opts{'inode'}, | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 51 | 'size' => 0, | ||||||||||||||||||||||||||||
| 52 | 'pos' => 0 | ||||||||||||||||||||||||||||
| 53 | }, $class; | ||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 12 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 57 | 12 | my ($self) = @_; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 12 | ▶ | close $self->{'fh'} if $self->{'fh'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 12 | ▶ | if ( $self->{'file'} && -f $self->{'file'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"file"} and -f $self->{"file"}
MC/DC100%2/2
$self->{"file"} and -f $self->{"file"}
$self->{"file"}
-f $self->{"file"}
Truth table100%3/3
A and B
A= $self->{"file"}B= -f $self->{"file"}
| |||||||||||||||||||||||||||||
| 62 | 4 | unlink $self->{'file'}; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 97 | ▶ | sub open { | ||||||||||||||||||||||||||
|
Subroutine
open: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 67 | 97 | my ( $self, $flags ) = @_; | |||||||||||||||||||||||||||
| 68 | 97 | ▶ | $flags ||= 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$flags ||= 0
MC/DC100%1/1
$flags ||= 0
$flags
Truth table100%2/2
A
A= $flags
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 97 | ▶ | throw &Errno::EBUSY if $self->{'fh'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 96 | $self->{'pos'} = 0; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 96 | ▶ | if ( $flags & $O_APPEND ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 75 | 4 | $self->{'pos'} = $self->{'size'}; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | elsif ( $flags & ( $O_CREAT | $O_TRUNC ) ) { | ||||||||||||||||||||||||||||
| 78 | 73 | $self->{'size'} = 0; | |||||||||||||||||||||||||||
| 79 | 73 | $self->{'inode'}->{'size'} = 0; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 73 | undef $self->{'buf'}; | |||||||||||||||||||||||||||
| 82 | 73 | $self->{'buf'} = ''; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 96 | ▶ | if ( $self->{'file'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 5 | my $fcntl_flags = Filesys::POSIX::Bits::System::convertFlagsToSystem($flags); | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 5 | ▶ | sysopen( my $fh, $self->{'file'}, $fcntl_flags ) or Carp::confess("$!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 4 | $self->{'fh'} = $fh; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 95 | return $self; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 8 | ▶ | sub _flush_to_disk { | ||||||||||||||||||||||||||
|
Subroutine
_flush_to_disk: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 97 | 8 | my ( $self, $len ) = @_; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 8 | ▶ | throw &Errno::EALREADY if $self->{'file'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | my ( $fh, $file ) = | ||||||||||||||||||||||||||||
| 102 | 7 | eval { File::Temp::mkstemp("$self->{'dir'}/.bucket-XXXXXX") }; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 7 | ▶ | Carp::confess("mkstemp() failure: $@") if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 6 | my $offset = 0; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 6 | ▶ | for ( my $left = $self->{'size'}; $left > 0; $left -= $len ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 128 | ▶ | my $wrlen = $left > $len ? $len : $left; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 128 | syswrite( $fh, substr( $self->{'buf'}, $offset, $wrlen ), $wrlen ); | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 128 | $offset += $wrlen; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 6 | @{$self}{qw(fh file)} = ( $fh, $file ); | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 739 | ▶ | sub write { | ||||||||||||||||||||||||||
|
Subroutine
write: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 120 | 739 | my ( $self, $buf, $len ) = @_; | |||||||||||||||||||||||||||
| 121 | 739 | my $ret = 0; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # | ||||||||||||||||||||||||||||
| 124 | # If the current file position, plus the length of the intended write | ||||||||||||||||||||||||||||
| 125 | # is to exceed the maximum memory bucket threshold, then dump the file | ||||||||||||||||||||||||||||
| 126 | # to disk if it hasn't already happened. | ||||||||||||||||||||||||||||
| 127 | # | ||||||||||||||||||||||||||||
| 128 | 739 | ▶ | if ( $self->{'pos'} + $len > $self->{'max'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 72 | ▶ | $self->_flush_to_disk($len) unless $self->{'fh'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 738 | ▶ | if ( $self->{'fh'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | Carp::confess("Unable to write to disk bucket") | ||||||||||||||||||||||||||||
| 134 | 71 | ▶ | unless fileno( $self->{'fh'} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 70 | $ret = syswrite( $self->{'fh'}, $buf ); | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | else { | ||||||||||||||||||||||||||||
| 138 | 667 | ▶ | if ( ( my $gap = $self->{'pos'} - $self->{'size'} ) > 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 1 | $self->{'buf'} .= "\x00" x $gap; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 667 | substr( $self->{'buf'}, $self->{'pos'}, $len ) = | |||||||||||||||||||||||||||
| 143 | substr( $buf, 0, $len ); | ||||||||||||||||||||||||||||
| 144 | 667 | $ret = $len; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 737 | $self->{'pos'} += $ret; | |||||||||||||||||||||||||||
| 148 | 737 | $self->{'size'} += $ret; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 737 | ▶ | if ( $self->{'pos'} > $self->{'size'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 1 | $self->{'size'} = $self->{'pos'}; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 737 | $self->{'inode'}->{'size'} = $self->{'size'}; | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 737 | return $ret; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 216 | ▶ | sub read { | ||||||||||||||||||||||||||
|
Subroutine
read: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 160 | 216 | my $self = shift; | |||||||||||||||||||||||||||
| 161 | 216 | my $len = pop; | |||||||||||||||||||||||||||
| 162 | 216 | my $ret = 0; | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 216 | ▶ | if ( $self->{'fh'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | Carp::confess("Unable to read bucket: $!") | ||||||||||||||||||||||||||||
| 166 | 198 | ▶ | unless fileno( $self->{'fh'} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 197 | $ret = sysread( $self->{'fh'}, $_[0], $len ); | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | else { | ||||||||||||||||||||||||||||
| 170 | my $pos = | ||||||||||||||||||||||||||||
| 171 | 18 | ▶ | $self->{'pos'} > $self->{'size'} ? $self->{'size'} : $self->{'pos'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 18 | my $maxlen = $self->{'size'} - $pos; | |||||||||||||||||||||||||||
| 173 | 18 | ▶ | $len = $maxlen if $len > $maxlen; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 18 | ▶ | unless ($len) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 4 | $_[0] = ''; | |||||||||||||||||||||||||||
| 177 | 4 | return 0; | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 14 | $_[0] = substr( $self->{'buf'}, $self->{'pos'}, $len ); | |||||||||||||||||||||||||||
| 181 | 14 | $ret = $len; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 211 | $self->{'pos'} += $ret; | |||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 211 | return $ret; | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 14 | ▶ | sub seek { | ||||||||||||||||||||||||||
|
Subroutine
seek: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 190 | 14 | my ( $self, $pos, $whence ) = @_; | |||||||||||||||||||||||||||
| 191 | 14 | my $newpos; | |||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 14 | ▶ | if ( $self->{'fh'} ) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 194 | 5 | $newpos = sysseek( $self->{'fh'}, $pos, $whence ); | |||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | elsif ( $whence == $SEEK_SET ) { | ||||||||||||||||||||||||||||
| 197 | 6 | $newpos = $pos; | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | elsif ( $whence == $SEEK_CUR ) { | ||||||||||||||||||||||||||||
| 200 | 1 | $newpos = $self->{'pos'} + $pos; | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | elsif ( $whence == $SEEK_END ) { | ||||||||||||||||||||||||||||
| 203 | 1 | $newpos = $self->{'size'} + $pos; | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | else { | ||||||||||||||||||||||||||||
| 206 | 1 | throw &Errno::EINVAL; | |||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 13 | return $self->{'pos'} = $newpos; | |||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 5 | ▶ | sub tell { | ||||||||||||||||||||||||||
|
Subroutine
tell: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 213 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 5 | ▶ | if ( $self->{'fh'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 216 | 1 | return sysseek $self->{'fh'}, 0, 1; | |||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 4 | return $self->{'pos'}; | |||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | 86 | ▶ | sub close { | ||||||||||||||||||||||||||
|
Subroutine
close: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 223 | 86 | my ($self) = @_; | |||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 86 | ▶ | if ( $self->{'fh'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 226 | 7 | close $self->{'fh'}; | |||||||||||||||||||||||||||
| 227 | 7 | undef $self->{'fh'}; | |||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 86 | $self->{'pos'} = 0; | |||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | =over | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | =item L<Filesys::POSIX::IO::Handle> | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | =back | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | =cut | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 27 | 1; | |||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | __END__ | ||||||||||||||||||||||||||||