/usr/share/perl5/Mojo/Asset/File.pm is in libmojolicious-perl 2.23-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | package Mojo::Asset::File;
use Mojo::Base 'Mojo::Asset';
use Carp 'croak';
use Errno;
use Fcntl;
use File::Copy ();
use File::Spec;
use IO::File;
use Mojo::Util 'md5_sum';
has [qw/cleanup path/];
has handle => sub {
my $self = shift;
# Already got a file without handle
my $handle = IO::File->new;
my $file = $self->path;
if ($file && -f $file) {
$handle->open("< $file")
or croak qq/Can't open file "$file": $!/;
return $handle;
}
# Open existing or temporary file
my $base = File::Spec->catfile($self->tmpdir, 'mojo.tmp');
my $name = $file || $base;
my $fh;
until (sysopen $fh, $name, O_CREAT | O_EXCL | O_RDWR) {
croak qq/Can't open file "$name": $!/ if $file || $! != $!{EEXIST};
$name = "$base." . md5_sum(time . $$ . rand 9999999);
}
$file = $name;
$self->path($file);
# Enable automatic cleanup
$self->cleanup(1);
# Open for read/write access
$handle->fdopen(fileno($fh), "+>") or croak qq/Can't open file "$name": $!/;
return $handle;
};
has tmpdir => sub { $ENV{MOJO_TMPDIR} || File::Spec->tmpdir };
# "The only monster here is the gambling monster that has enslaved your
# mother!
# I call him Gamblor, and it's time to snatch your mother from his neon
# claws!"
sub DESTROY {
my $self = shift;
my $path = $self->path;
if ($self->cleanup && -f $path) {
close $self->handle;
unlink $path;
}
}
sub add_chunk {
my ($self, $chunk) = @_;
# Seek to end
$self->handle->sysseek(0, SEEK_END);
# Append to file
$chunk //= '';
$self->handle->syswrite($chunk, length $chunk);
return $self;
}
sub contains {
my ($self, $pattern) = @_;
# Seek to start
$self->handle->sysseek($self->start_range, SEEK_SET);
my $end = $self->end_range // $self->size;
my $window_size = length($pattern) * 2;
$window_size = $end - $self->start_range
if $window_size > $end - $self->start_range;
# Read
my $read = $self->handle->sysread(my $window, $window_size);
my $offset = $read;
my $pattern_size = length($pattern);
# Moving window search
my $range = $self->end_range;
while ($offset <= $end) {
if (defined $range) {
$pattern_size = $end + 1 - $offset;
return -1 if $pattern_size <= 0;
}
$read = $self->handle->sysread(my $buffer, $pattern_size);
$offset += $read;
$window .= $buffer;
my $pos = index $window, $pattern;
return $pos if $pos >= 0;
return -1 if $read == 0;
substr $window, 0, $read, '';
}
return -1;
}
sub get_chunk {
my ($self, $start) = @_;
# Seek to start
$start += $self->start_range;
$self->handle->sysseek($start, SEEK_SET);
my $end = $self->end_range;
my $buffer;
# Chunk size
my $size = $ENV{MOJO_CHUNK_SIZE} || 131072;
# Range support
if (defined $end) {
my $chunk = $end + 1 - $start;
return '' if $chunk <= 0;
$chunk = $size if $chunk > $size;
$self->handle->sysread($buffer, $chunk);
}
else { $self->handle->sysread($buffer, $size) }
return $buffer;
}
sub is_file {1}
sub move_to {
my ($self, $path) = @_;
# Windows requires that the handle is closed
close $self->handle;
delete $self->{handle};
# Move
my $src = $self->path;
File::Copy::move($src, $path)
or croak qq/Can't move file "$src" to "$path": $!/;
$self->path($path);
# Don't clean up a moved file
$self->cleanup(0);
return $self;
}
sub size {
my $self = shift;
# File size
my $file = $self->path;
return -s $file if $file;
return 0;
}
sub slurp {
my $self = shift;
# Seek to start
$self->handle->sysseek(0, SEEK_SET);
# Slurp
my $content = '';
while ($self->handle->sysread(my $buffer, 131072)) { $content .= $buffer }
return $content;
}
1;
__END__
=head1 NAME
Mojo::Asset::File - File storage for HTTP 1.1 content
=head1 SYNOPSIS
use Mojo::Asset::File;
my $file = Mojo::Asset::File->new;
$file->add_chunk('foo bar baz');
say $file->slurp;
my $file = Mojo::Asset::File->new(path => '/foo/bar/baz.txt');
say $file->slurp;
=head1 DESCRIPTION
L<Mojo::Asset::File> is a file storage backend for HTTP 1.1 content.
=head1 ATTRIBUTES
L<Mojo::Asset::File> inherits all attributes from L<Mojo::Asset> and
implements the following new ones.
=head2 C<cleanup>
my $cleanup = $file->cleanup;
$file = $file->cleanup(1);
Delete file automatically once it's not used anymore.
=head2 C<handle>
my $handle = $file->handle;
$file = $file->handle(IO::File->new);
Actual file handle.
=head2 C<path>
my $path = $file->path;
$file = $file->path('/foo/bar/baz.txt');
Actual file path.
=head2 C<tmpdir>
my $tmpdir = $file->tmpdir;
$file = $file->tmpdir('/tmp');
Temporary directory.
=head1 METHODS
L<Mojo::Asset::File> inherits all methods from L<Mojo::Asset> and implements
the following new ones.
=head2 C<add_chunk>
$file = $file->add_chunk('foo bar baz');
Add chunk of data.
=head2 C<contains>
my $position = $file->contains('bar');
Check if asset contains a specific string.
=head2 C<get_chunk>
my $chunk = $file->get_chunk($start);
Get chunk of data starting from a specific position.
=head2 C<is_file>
my $true = $file->is_file;
True.
=head2 C<move_to>
$file = $file->move_to('/foo/bar/baz.txt');
Move asset data into a specific file.
=head2 C<size>
my $size = $file->size;
Size of asset data in bytes.
=head2 C<slurp>
my $string = $file->slurp;
Read all asset data at once.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
=cut
|