Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement IO.move() and move()
  • Loading branch information
hoelzro committed Aug 28, 2015
1 parent 97ae81f commit eb6de6d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/core/IO/Path.pm
Expand Up @@ -334,6 +334,26 @@ my class IO::Path is Cool {
self.copy($to.IO(:$!SPEC,:$CWD),|c);
}

method move(IO::Path:D: |c) {
my $result = self.copy(|c);

fail X::IO::Move.new(
:from($result.exception.from),
:to($result.exception.to),
:os-error($result.exception.os-error),
) unless $result.defined;

$result = self.unlink();

fail X::IO::Move.new(
:from($result.exception.from),
:to($result.exception.to),
:os-error($result.exception.os-error),
) unless $result.defined;

True
}

method chmod(IO::Path:D: Int() $mode) {
nqp::chmod($.abspath, nqp::unbox_i($mode));
CATCH { default {
Expand Down
17 changes: 17 additions & 0 deletions src/core/io_operators.pm
Expand Up @@ -242,6 +242,23 @@ sub copy($from, $to, :$SPEC = $*SPEC, :$CWD = $*CWD, :$createonly) {
my $result := $from.IO(:$SPEC,:$CWD).copy($to,:$SPEC,:$CWD, :$createonly);
$result // $result.throw;
}
sub move($from, $to, :$createonly) {
try {
copy($from, $to, :$createonly);
unlink($from);
return True;

CATCH {
when X::IO::Copy|X::IO::Unlink {
fail X::IO::Move.new(
:from(.from),
:to(.to),
:os-error(.os-error),
);
}
}
}
}
sub symlink($target, $name, :$SPEC = $*SPEC, :$CWD = $*CWD) {
my $result := $target.IO(:$SPEC,:$CWD).symlink($name,:$SPEC,:$CWD);
$result // $result.throw;
Expand Down

0 comments on commit eb6de6d

Please sign in to comment.