App-Pvim
view release on metacpan or search on metacpan
my ($s, $path, $force) = @_;
$s->commit;
$path = $s->{path} unless defined($path) && length($path);
die "No filename; use :w filename\n" unless defined($path) && length($path);
die "Refusing to replace a symlink\n" if -l $path;
die "Not a regular file: $path\n" if -e $path && !-f $path;
my $same = defined($s->{path}) && File::Spec->rel2abs($path) eq File::Spec->rel2abs($s->{path});
my $expected = $same ? $s->{fingerprint} : '';
my $current = fingerprint($path);
die "File exists or changed on disk; use :w! to overwrite\n" if !$force && $current ne $expected;
my @stat = stat($path);
die "File has multiple hard links; refusing atomic replacement\n" if @stat && $stat[3] > 1;
die "File is read-only\n" if @stat && !($stat[2] & 0222);
my ($fh, $tmp) = tempfile('.pvim-save-XXXXXX', DIR => dirname(File::Spec->rel2abs($path)), UNLINK => 0);
binmode $fh;
my $ok = eval {
$s->write_to($fh); close($fh) or die "Close output: $!\n";
my $mode = @stat ? $stat[2] & 0777 : 0666 & ~umask;
chmod($mode, $tmp) or die "chmod: $!\n";
die "Target changed during save\n" if -l $path || fingerprint($path) ne $current;
rename($tmp, $path) or die "Rename: $!\n";
script/pvim view on Meta::CPAN
my ($s, $path, $force) = @_;
$s->commit;
$path = $s->{path} unless defined($path) && length($path);
die "No filename; use :w filename\n" unless defined($path) && length($path);
die "Refusing to replace a symlink\n" if -l $path;
die "Not a regular file: $path\n" if -e $path && !-f $path;
my $same = defined($s->{path}) && File::Spec->rel2abs($path) eq File::Spec->rel2abs($s->{path});
my $expected = $same ? $s->{fingerprint} : '';
my $current = fingerprint($path);
die "File exists or changed on disk; use :w! to overwrite\n" if !$force && $current ne $expected;
my @stat = stat($path);
die "File has multiple hard links; refusing atomic replacement\n" if @stat && $stat[3] > 1;
die "File is read-only\n" if @stat && !($stat[2] & 0222);
my ($fh, $tmp) = tempfile('.pvim-save-XXXXXX', DIR => dirname(File::Spec->rel2abs($path)), UNLINK => 0);
binmode $fh;
my $ok = eval {
$s->write_to($fh); close($fh) or die "Close output: $!\n";
my $mode = @stat ? $stat[2] & 0777 : 0666 & ~umask;
chmod($mode, $tmp) or die "chmod: $!\n";
die "Target changed during save\n" if -l $path || fingerprint($path) ne $current;
rename($tmp, $path) or die "Rename: $!\n";
t/01-store.t view on Meta::CPAN
put(file('new'),'external'); eval { $b->save(file('new')) }; like($@,qr/changed/,'external overwrite guarded');
$b->save(file('new'),1); is(get(file('new')), "hello\nworld\n",'forced write');
put(file('existing'),'keep'); eval { $b->save(file('existing')) }; like($@,qr/exists/,'save-as refuses existing');
SKIP: {
skip 'POSIX link semantics',4 if $^O eq 'MSWin32';
symlink(file('existing'),file('sym')) or die $!;
eval { $b->save(file('sym'),1) }; like($@,qr/symlink/,'symlink refused');
link(file('existing'),file('hard')) or die $!;
eval { $b->save(file('hard'),1) }; like($@,qr/hard links/,'hardlink refused');
chmod 0444,file('new'); eval { $b->save(file('new'),1) }; like($@,qr/read-only/,'read-only refused even as root');
chmod 0750,file('new'); $b->save(file('new'),1); is((stat(file('new')))[2]&0777,0750,'mode preserved');
}
# A failed write must leave both the destination and dirty state intact.
put(file('failure'),'original');
{
no warnings qw(redefine once);
local *PVim::Store::write_to = sub { die "simulated write failure\n" };
eval { $b->save(file('failure'),1) }; like($@,qr/simulated write failure/,'write failure reported');
}
is(get(file('failure')),'original','failed save preserves original bytes');
my @leftovers=glob(File::Spec->catfile($dir,'.pvim-save-*')); is(scalar @leftovers,0,'failed save removes temporary output');
( run in 1.411 second using v1.01-cache-2.11-cpan-500dd33cbaf )