Skip to content

Commit

Permalink
use less syscalls for appending to assets
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 21, 2014
1 parent 5facbf6 commit 76842a4
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions lib/Mojo/Asset/File.pm
Expand Up @@ -3,7 +3,7 @@ use Mojo::Base 'Mojo::Asset';

use Carp 'croak';
use Errno 'EEXIST';
use Fcntl qw(O_CREAT O_EXCL O_RDONLY O_RDWR);
use Fcntl qw(O_APPEND O_CREAT O_EXCL O_RDONLY O_RDWR);
use File::Copy 'move';
use File::Spec::Functions 'catfile';
use IO::File;
Expand All @@ -17,15 +17,15 @@ has handle => sub {
my $handle = IO::File->new;
my $path = $self->path;
if (defined $path && -f $path) {
$handle->open($path, -w _ ? O_RDWR : O_RDONLY)
$handle->open($path, -w _ ? O_APPEND | O_RDWR : O_RDONLY)
or croak qq{Can't open file "$path": $!};
return $handle;
}

# Open new or temporary file
my $base = catfile $self->tmpdir, 'mojo.tmp';
my $name = $path // $base;
until ($handle->open($name, O_CREAT | O_EXCL | O_RDWR)) {
until ($handle->open($name, O_APPEND | O_CREAT | O_EXCL | O_RDWR)) {
croak qq{Can't open file "$name": $!} if defined $path || $! != $!{EEXIST};
$name = "$base." . md5_sum(time . $$ . rand 999);
}
Expand All @@ -47,13 +47,9 @@ sub DESTROY {

sub add_chunk {
my ($self, $chunk) = @_;

my $handle = $self->handle;
$handle->sysseek(0, SEEK_END);
$chunk //= '';
croak "Can't write to asset: $!"
unless defined $handle->syswrite($chunk, length $chunk);

unless defined $self->handle->syswrite($chunk, length $chunk);
return $self;
}

Expand Down

0 comments on commit 76842a4

Please sign in to comment.