-
-
Save FROGGS/9a327698fccb244ac44b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/core/IO/Path.pm b/src/core/IO/Path.pm | |
index b8566ed..d2ceaf2 100644 | |
--- a/src/core/IO/Path.pm | |
+++ b/src/core/IO/Path.pm | |
@@ -1,300 +1,195 @@ | |
my class IO::Path is Cool does IO::FileTestable { | |
- has IO::Spec $.SPEC; | |
- has Str $.CWD; | |
+ method SPEC { IO::Spec.MODULE }; | |
has Str $.path; | |
- has Bool $.is-absolute; | |
- has Str $!abspath; # should be native for faster file tests, but segfaults | |
- has %!parts; | |
- multi method ACCEPTS(IO::Path:D: IO::Path:D \other) { | |
- nqp::p6bool(nqp::iseq_s($!path, nqp::unbox_s(other.path))); | |
+ method dir() { | |
+ die "IO::Path.dir is deprecated in favor of .directory"; | |
} | |
- multi method ACCEPTS(IO::Path:D: Mu \that) { | |
- nqp::p6bool(nqp::iseq_s($!path,nqp::unbox_s(IO::Path.new(|that).path))); | |
+ multi method ACCEPTS(IO::Path:D: IO::Path:D \other) { | |
+ self.cleanup.parts eqv other.cleanup.parts | |
} | |
- submethod BUILD(:$!path! as Str, :$!SPEC!, :$!CWD! as Str) { } | |
- | |
- multi method new(IO::Path: $path, :$SPEC = $*SPEC, :$CWD = $*CWD) { | |
- self.bless(:$path, :$SPEC, :$CWD); | |
- } | |
- multi method new(IO::Path: | |
- :$basename!, | |
- :$dirname = '.', | |
- :$volume = '', | |
- :$SPEC = $*SPEC, | |
- :$CWD = $*CWD, | |
- ) { | |
- self.bless(:path($SPEC.join($volume,$dirname,$basename)),:$SPEC,:$CWD); | |
- } | |
- multi method new(IO::Path: | |
- :$basename, | |
- :$directory!, | |
- :$volume = '', | |
- :$SPEC = $*SPEC, | |
- :$CWD = $*CWD, | |
- ) { | |
-# DEPRECATED(':dirname', :what<IO::Path.new with :directory>); # after 2014.10 | |
- self.bless( | |
- :path($SPEC.join($volume,$directory,$basename)), :$SPEC, :$CWD); | |
+ multi method ACCEPTS(IO::Path:D: Mu \other) { | |
+ self.cleanup.parts eqv IO::Path.new(|other).cleanup.parts | |
} | |
- method abspath() { | |
- $!abspath //= $!path.substr(0,1) eq '-' | |
- ?? '' | |
- !! $!SPEC.rel2abs($!path,$!CWD); | |
+ submethod BUILD(:$!path!, :$dir) { | |
+ die "Named paramter :dir in IO::Path.new deprecated in favor of :directory" | |
+ if defined $dir; | |
} | |
- method is-absolute() { | |
- $!is-absolute //= $!SPEC.is-absolute($!path); | |
+ | |
+ multi method new(IO::Path: :$basename!, :$directory = '.', :$volume = '') { | |
+ self.bless: path=>$.SPEC.join($volume, $directory, $basename); | |
} | |
- method is-relative() { | |
- !( $!is-absolute //= $!SPEC.is-absolute($!path) ); | |
+ | |
+ multi method new(IO::Path: $path as Str) { | |
+ self.bless(:$path) | |
} | |
- method parts { | |
- %!parts ||= $!SPEC.split($!path); | |
+ method path(IO::Path:D:) { | |
+ self; | |
} | |
- method volume(IO::Path:D:) { %.parts<volume> } | |
- method dirname(IO::Path:D:) { %.parts<dirname> } | |
- method basename(IO::Path:D:) { %.parts<basename> } | |
- # core can't do 'basename handles <Numeric Bridge Int>' | |
- method Numeric(IO::Path:D:) { self.basename.Numeric } | |
- method Bridge (IO::Path:D:) { self.basename.Bridge } | |
- method Int (IO::Path:D:) { self.basename.Int } | |
+ method parts { | |
+ $.SPEC.split($!path).hash | |
+ } | |
+ method basename(IO::Path:D:) { | |
+ self.parts<basename> | |
+ } | |
+ method dirname(IO::Path:D:) { | |
+ self.parts<dirname> | |
+ } | |
+ method directory(IO::Path:D:) { | |
+ DEPRECATED("dirname"); | |
+ self.parts<dirname> | |
+ } | |
+ method volume(IO::Path:D:) { | |
+ self.parts<volume> | |
+ } | |
- multi method Str (IO::Path:D:) { $!path } | |
+ multi method Str(IO::Path:D:) { | |
+ $!path; | |
+ } | |
multi method gist(IO::Path:D:) { | |
- "q|$.abspath|.IO"; | |
+ "{self.^name}<{ $!path }>"; | |
} | |
multi method perl(IO::Path:D:) { | |
- ($.is-absolute | |
- ?? "q|$.abspath|.IO(:SPEC({$!SPEC.^name}))" | |
- !! "q|$.path|.IO(:SPEC({$!SPEC.^name}),:CWD<$!CWD>)" | |
- ).subst(:global, '\\', '\\\\'); | |
+ "IO::Path.new(path => " ~ $.Str.perl ~ ")"; | |
+ } | |
+ multi method Numeric(IO::Path:D:) { | |
+ self.basename.Numeric; | |
+ } | |
+ method Bridge(IO::Path:D:) { | |
+ self.basename.Bridge; | |
+ } | |
+ method Int(IO::Path:D:) { | |
+ self.basename.Int; | |
} | |
method succ(IO::Path:D:) { | |
- self.bless( | |
- :path($!SPEC.join($.volume,$.dirname,$.basename.succ)), | |
- :$!SPEC, | |
- :$!CWD, | |
- ); | |
+ self.new(:$.volume, :$.directory, basename=> $.basename.succ) | |
} | |
method pred(IO::Path:D:) { | |
- self.bless( | |
- :path($!SPEC.join($.volume,$.dirname,$.basename.pred)), | |
- :$!SPEC, | |
- :$!CWD, | |
- ); | |
+ self.new(:$.volume, :$.directory, basename=> $.basename.pred) | |
} | |
method IO(IO::Path:D: |c) { | |
- $?CLASS.new($!path, :$!SPEC, :$!CWD, |c); | |
+ IO::Path.new($!path, |c); | |
} | |
- | |
method open(IO::Path:D: |c) { | |
- my $handle = IO::Handle.new(:path($.abspath)); | |
+ my $handle = IO::Handle.new(:path($!path)); | |
$handle && $handle.open(|c); | |
} | |
#?if moar | |
method watch(IO::Path:D:) { | |
- IO::Notification.watch_path($.abspath); | |
+ IO::Notification.watch_path($!path); | |
} | |
#?endif | |
- proto method absolute(|) { * } | |
- multi method absolute (IO::Path:D:) { $.abspath } | |
- multi method absolute (IO::Path:D: $CWD) { | |
- self.is-absolute | |
- ?? $.abspath | |
- !! $!SPEC.rel2abs($!path, $CWD); | |
+ method is-absolute(IO::Path:D:) { | |
+ $.SPEC.is-absolute($!path); | |
} | |
- | |
- method relative (IO::Path:D: $CWD = $*CWD) { | |
- $!SPEC.abs2rel($.abspath, $CWD); | |
+ method is-relative(IO::Path:D:) { | |
+ ! $.SPEC.is-absolute($!path); | |
+ } | |
+ method absolute (IO::Path:D: $base = ~$*CWD) { | |
+ return self.new($.SPEC.rel2abs($!path, $base)); | |
+ } | |
+ method relative (IO::Path:D: $relative_to_directory = ~$*CWD) { | |
+ return self.new($.SPEC.abs2rel($!path, $relative_to_directory)); | |
} | |
- method cleanup (IO::Path:D:) { | |
- self.bless(:path($!SPEC.canonpath($!path)), :$!SPEC, :$!CWD); | |
+ method cleanup (IO::Path:D: :$parent) { | |
+ return self.new($.SPEC.canonpath($!path, :$parent)); | |
} | |
method resolve (IO::Path:D:) { | |
# NYI: requires readlink() | |
X::NYI.new(feature=>'IO::Path.resolve').fail; | |
} | |
- method parent(IO::Path:D:) { # XXX needs work | |
- my $curdir := $!SPEC.curdir; | |
- my $updir := $!SPEC.updir; | |
- | |
+ method parent(IO::Path:D:) { | |
if self.is-absolute { | |
- return self.bless( | |
- :path($!SPEC.join($.volume, $.dirname, '')), | |
- :$!SPEC, | |
- :$!CWD, | |
- ); | |
+ return self.new($.SPEC.join($.volume, $.directory, '')); | |
} | |
- elsif $.dirname eq $curdir and $.basename eq $curdir { | |
- return self.bless( | |
- :path($!SPEC.join($.volume,$curdir,$updir)), | |
- :$!SPEC, | |
- :$!CWD, | |
- ); | |
+ elsif all($.basename, $.directory) eq $.SPEC.curdir { | |
+ return self.new(:$.volume, directory=>$.SPEC.curdir, | |
+ basename=>$.SPEC.updir); | |
} | |
- elsif $.dirname eq $curdir && $.basename eq $updir | |
- or !grep({$_ ne $updir}, $!SPEC.splitdir($.dirname)) { | |
- return self.bless( # If all updirs, then add one more | |
- :path($!SPEC.join($.volume,$!SPEC.catdir($.dirname,$updir),$.basename)), | |
- :$!SPEC, | |
- :$!CWD, | |
- ); | |
+ elsif $.basename eq $.SPEC.updir && $.directory eq $.SPEC.curdir | |
+ or !grep({$_ ne $.SPEC.updir}, $.SPEC.splitdir($.directory)) { | |
+ return self.new( # If all updirs, then add one more | |
+ :$.volume, | |
+ directory => $.SPEC.catdir($.directory, $.SPEC.updir), | |
+ :$.basename ); | |
} | |
else { | |
- return self.bless( | |
- :path($!SPEC.join($.volume, $.dirname, '')), | |
- :$!SPEC, | |
- :$!CWD, | |
- ); | |
+ return self.new( $.SPEC.join($.volume, $.directory, '') ); | |
} | |
} | |
- method child (IO::Path:D: $child) { | |
- self.bless(:path($!SPEC.catfile($!path,$child)), :$!SPEC, :$!CWD); | |
+ method child (IO::Path:D: $childname) { | |
+ self.new: path => $.SPEC.catfile($!path, $childname); | |
} | |
- proto method rename(|) { * } | |
- multi method rename(IO::Path:D: IO::Path:D $to, :$createonly) { | |
- if $createonly and $to.e { | |
- fail X::IO::Rename.new( | |
- :from($.abspath), | |
- :$to, | |
- :os-error(':createonly specified and destination exists'), | |
- ); | |
+ method copy(IO::Path:D: $dest, :$createonly = False) { | |
+ my $absdest = IO::Spec.rel2abs($dest); | |
+ if $createonly and $absdest.e { | |
+ fail(X::IO::Copy.new(from => $!path, to => $dest, | |
+ os-error => "Destination file $dest exists and :createonly passed to copy.")); | |
} | |
- nqp::rename($.abspath, nqp::unbox_s($to.abspath)); | |
- CATCH { default { | |
- fail X::IO::Rename.new( | |
- :from($!abspath), :$to($to.abspath), :os-error(.Str) ); | |
- } } | |
- True; | |
- } | |
- multi method rename(IO::Path:D: $to, :$CWD = $*CWD, |c) { | |
- self.rename($to.IO(:$!SPEC,:$CWD),|c); | |
- } | |
- | |
- proto method copy(|) { * } | |
- multi method copy(IO::Path:D: IO::Path:D $to, :$createonly) { | |
- if $createonly and $to.e { | |
- fail X::IO::Copy.new( | |
- :from($.abspath), | |
- :$to, | |
- :os-error(':createonly specified and destination exists'), | |
- ); | |
+ try { | |
+ nqp::copy(nqp::unbox_s(IO::Spec.rel2abs($!path)), nqp::unbox_s(~$absdest)); | |
+ } | |
+ $! ?? fail(X::IO::Copy.new(from => $!path, to => $dest, os-error => ~$!)) !! True | |
+ } | |
+ | |
+ method chmod(IO::Path:D: Int $mode) { | |
+ nqp::chmod(nqp::unbox_s(IO::Spec.rel2abs($!path)), nqp::unbox_i($mode.Int)); | |
+ return True; | |
+ CATCH { | |
+ default { | |
+ X::IO::Chmod.new( | |
+ :$!path, | |
+ :$mode, | |
+ os-error => .Str, | |
+ ).throw; | |
+ } | |
} | |
- nqp::copy($.abspath, nqp::unbox_s($to.abspath)); | |
- CATCH { default { | |
- fail X::IO::Copy.new( | |
- :from($!abspath), :$to, :os-error(.Str) ); | |
- } } | |
- True; | |
- } | |
- multi method copy(IO::Path:D: $to, :$CWD = $*CWD, |c) { | |
- self.copy($to.IO(:$!SPEC,:$CWD),|c); | |
- } | |
- | |
- method chmod(IO::Path:D: $mode as Int) { | |
- nqp::chmod($.abspath, nqp::unbox_i($mode)); | |
- CATCH { default { | |
- fail X::IO::Chmod.new( | |
- :path($!abspath), :$mode, :os-error(.Str) ); | |
- } } | |
- True; | |
- } | |
- method unlink(IO::Path:D:) { | |
- nqp::unlink($.abspath); | |
- CATCH { default { | |
- fail X::IO::Unlink.new( :path($!abspath), os-error => .Str ); | |
- } } | |
- True; | |
- } | |
- | |
- method symlink(IO::Path:D: $name is copy, :$CWD = $*CWD) { | |
- $name = $name.IO(:$!SPEC,:$CWD).path; | |
- nqp::symlink(nqp::unbox_s($name), $.abspath); | |
- CATCH { default { | |
- fail X::IO::Symlink.new(:target($!abspath), :$name, os-error => .Str); | |
- } } | |
- True; | |
- } | |
- | |
- method link(IO::Path:D: $name is copy, :$CWD = $*CWD) { | |
- $name = $name.IO(:$!SPEC,:$CWD).path; | |
- nqp::link(nqp::unbox_s($name), $.abspath); | |
- CATCH { default { | |
- fail X::IO::Link.new(:target($!abspath), :$name, os-error => .Str); | |
- } } | |
- True; | |
- } | |
- | |
- method mkdir(IO::Path:D: $mode = 0o777) { | |
- nqp::mkdir($.abspath, $mode); | |
- CATCH { default { | |
- fail X::IO::Mkdir.new(:path($!abspath), :$mode, os-error => .Str); | |
- } } | |
- True; | |
- } | |
- | |
- method rmdir(IO::Path:D:) { | |
- nqp::rmdir($.abspath); | |
- CATCH { default { | |
- fail X::IO::Rmdir.new(:path($!abspath), os-error => .Str); | |
- } } | |
- True; | |
- } | |
- | |
- method contents(IO::Path:D: |c) { | |
-# DEPRECATED('dir'); # after 2014.10 | |
- self.dir(|c); | |
} | |
- method dir(IO::Path:D: # XXX needs looking at | |
- Mu :$test = $*SPEC.curupdir, | |
- :$absolute, | |
- :$CWD = $*CWD, | |
- ) { | |
- | |
- CATCH { default { | |
- fail X::IO::Dir.new( | |
- :path(nqp::box_s($.abspath,Str)), :os-error(.Str) ); | |
- } } | |
- my $cwd_chars = $CWD.chars; | |
- | |
- my str $cwd = nqp::cwd(); | |
- nqp::chdir(nqp::unbox_s($.abspath)); | |
+ method contents(IO::Path:D: Mu :$test = { $_ ne '.' && $_ ne '..' }) { | |
+ CATCH { | |
+ default { | |
+ X::IO::Dir.new( | |
+ :$!path, | |
+ os-error => .Str, | |
+ ).throw; | |
+ } | |
+ } | |
#?if parrot | |
- my Mu $RSA := pir::new__PS('OS').readdir($!abspath); | |
+ my Mu $RSA := pir::new__PS('OS').readdir(nqp::unbox_s(self.absolute.Str)); | |
my int $elems = nqp::elems($RSA); | |
- gather { | |
- loop (my int $i = 0; $i < $elems; $i = $i + 1) { | |
- my Str $file := nqp::p6box_s(pir::trans_encoding__Ssi( | |
- nqp::atpos_s($RSA, $i), | |
- pir::find_encoding__Is('utf8'))); | |
- if $file ~~ $test { | |
- take self.child($file); # XXX needs looking at | |
- } | |
+ gather loop (my int $i = 0; $i < $elems; $i = $i + 1) { | |
+ my Str $file := nqp::p6box_s(pir::trans_encoding__Ssi( | |
+ nqp::atpos_s($RSA, $i), | |
+ pir::find_encoding__Is('utf8'))); | |
+ if $file ~~ $test { | |
+ take self.child($file); | |
} | |
- nqp::chdir($cwd); | |
} | |
#?endif | |
+#?if jvm | |
+ my $cwd_chars = $*CWD.chars; | |
+#?endif | |
#?if !parrot | |
- | |
- my Mu $dirh := nqp::opendir($!abspath); | |
+ my Mu $dirh := nqp::opendir(self.absolute.Str); | |
my $next = 1; | |
gather { | |
- take $_.IO(:$!SPEC,:$*CWD) if $_ ~~ $test for ".", ".."; | |
+ take $_.path if $_ ~~ $test for ".", ".."; | |
+ my $SPEC = $.SPEC; | |
loop { | |
my str $elem = nqp::nextfiledir($dirh); | |
if nqp::isnull_s($elem) || nqp::chars($elem) == 0 { | |
@@ -303,15 +198,20 @@ my class IO::Path is Cool does IO::FileTestable { | |
} | |
elsif $elem ne '.' | '..' { | |
#?endif | |
+#?if jvm | |
+ # jvm's nextfiledir gives us absolute paths back, moar does not. | |
+ $elem = nqp::substr($elem, $cwd_chars + 1) if self.is-relative; | |
+#?endif | |
#?if moar | |
- $elem = $!SPEC.catfile($!abspath, $elem); # moar = relative | |
+ $elem = $SPEC.catfile($!path, $elem) if $!path ne '.'; | |
#?endif | |
#?if !parrot | |
- $elem = nqp::substr($elem, $cwd_chars + 1) if !$absolute; | |
- take $elem.IO(:$!SPEC,:$CWD) if $test.ACCEPTS($elem); | |
+ if nqp::substr($elem, 0, 2) eq "./" | ".\\" { | |
+ $elem = nqp::substr($elem, 2); | |
+ } | |
+ take IO::Path.new($elem) if $test.ACCEPTS($elem); | |
} | |
} | |
- nqp::chdir($cwd); | |
} | |
#?endif | |
} | |
@@ -351,24 +251,11 @@ my class IO::Path is Cool does IO::FileTestable { | |
my $handle = self.open(|c); | |
$handle && $handle.words(:close, |c); | |
} | |
- | |
- method directory() { | |
-# DEPRECATED("dirname"); # after 2014.10 | |
- self.dirname; | |
- } | |
} | |
-my class IO::Path::Cygwin is IO::Path { | |
- method new(|c) { IO::Path.new(|c, :SPEC(IO::Spec::Cygwin) ) } | |
-} | |
-my class IO::Path::QNX is IO::Path { | |
- method new(|c) { IO::Path.new(|c, :SPEC(IO::Spec::QNX) ) } | |
-} | |
-my class IO::Path::Unix is IO::Path { | |
- method new(|c) { IO::Path.new(|c, :SPEC(IO::Spec::Unix) ) } | |
-} | |
-my class IO::Path::Win32 is IO::Path { | |
- method new(|c) { IO::Path.new(|c, :SPEC(IO::Spec::Win32) ) } | |
-} | |
+my class IO::Path::Unix is IO::Path { method SPEC { IO::Spec::Unix }; } | |
+my class IO::Path::Win32 is IO::Path { method SPEC { IO::Spec::Win32 }; } | |
+my class IO::Path::Cygwin is IO::Path { method SPEC { IO::Spec::Cygwin }; } | |
+my class IO::Path::QNX is IO::Path { method SPEC { IO::Spec::QNX }; } | |
# vim: ft=perl6 expandtab sw=4 | |
diff --git a/src/core/io_operators.pm b/src/core/io_operators.pm | |
index edeb2d0..34df944 100644 | |
--- a/src/core/io_operators.pm | |
+++ b/src/core/io_operators.pm | |
@@ -49,8 +49,36 @@ sub prompt($msg) { | |
$*IN.get; | |
} | |
-sub dir(Cool $path = '.', |c) { | |
- $path.IO.dir(|c) | |
+sub dir(Cool $path = '.', Mu :$test = none('.', '..')) { | |
+ $path.path.contents(:$test) | |
+} | |
+ | |
+sub unlink($path as Str) { | |
+ my $abspath = IO::Spec.rel2abs($path); | |
+ nqp::unlink($abspath); | |
+ return True; | |
+ CATCH { | |
+ default { | |
+ X::IO::Unlink.new( | |
+ :$path, | |
+ os-error => .Str, | |
+ ).throw; | |
+ } | |
+ } | |
+} | |
+ | |
+sub rmdir($path as Str) { | |
+ my $abspath = IO::Spec.rel2abs($path); | |
+ nqp::rmdir($abspath); | |
+ return True; | |
+ CATCH { | |
+ default { | |
+ X::IO::Rmdir.new( | |
+ :$path, | |
+ os-error => .Str, | |
+ ).throw; | |
+ } | |
+ } | |
} | |
proto sub open(|) { * } | |
@@ -96,13 +124,37 @@ multi sub spurt(IO::Handle $fh, $what, :$enc = 'utf8', |c ) { | |
my $result := $fh.spurt($what, :$enc, |c); | |
$result // $result.throw; | |
} | |
+ | |
multi sub spurt(Cool $path, $what, :$enc = 'utf8', |c) { | |
my $result := $path.IO.spurt($what, :$enc, |c); | |
$result // $result.throw; | |
} | |
{ | |
- sub chdir($path as Str) { | |
+ proto sub cwd(|) { * } | |
+ multi sub cwd() { | |
+ return nqp::p6box_s( | |
+ nqp::cwd() | |
+ ); | |
+ CATCH { | |
+ default { | |
+ X::IO::Cwd.new( | |
+ os-error => .Str, | |
+ ).throw; | |
+ } | |
+ } | |
+ } | |
+ PROCESS::<&cwd> := &cwd; | |
+} | |
+ | |
+proto sub cwd(|) { * } | |
+multi sub cwd() { | |
+ $*CWD | |
+} | |
+ | |
+{ | |
+ proto sub chdir(|) { * } | |
+ multi sub chdir($path as Str) { | |
nqp::chdir(nqp::unbox_s($path)); | |
$*CWD = IO::Path.new(cwd()); | |
return True; | |
@@ -143,65 +195,91 @@ multi sub chdir($path as Str) { | |
} | |
} | |
+proto sub mkdir(|) { * } | |
+multi sub mkdir($path as Str, $mode = 0o777) { | |
+ my $abspath = IO::Spec.rel2abs($path); | |
+ nqp::mkdir($abspath, $mode); | |
+ return True; | |
+ CATCH { | |
+ default { | |
+ X::IO::Mkdir.new( | |
+ :$path, | |
+ :$mode, | |
+ os-error => .Str, | |
+ ).throw; | |
+ } | |
+ } | |
+} | |
+ | |
PROCESS::<$OUT> = open('-', :w); | |
PROCESS::<$IN> = open('-'); | |
PROCESS::<$ERR> = IO::Handle.new; | |
nqp::bindattr(nqp::decont(PROCESS::<$ERR>), | |
IO::Handle, '$!PIO', nqp::getstderr()); | |
-sub chmod($mode, *@filenames, :$SPEC = $*SPEC, :$CWD = $*CWD) { | |
- @filenames.grep( *.IO(:$SPEC,:$CWD).chmod($mode) ).eager; | |
-} | |
-sub unlink(*@filenames, :$SPEC = $*SPEC, :$CWD = $*CWD) { | |
- @filenames.grep( *.IO(:$SPEC,:$CWD).unlink ).eager; | |
-} | |
-sub rmdir(*@filenames, :$SPEC = $*SPEC, :$CWD = $*CWD) { | |
- @filenames.grep( *.IO(:$SPEC,:$CWD).rmdir ).eager; | |
-} | |
- | |
-proto sub mkdir(|) { * } | |
-multi sub mkdir(Int $mode, *@dirnames, :$SPEC = $*SPEC, :$CWD = $*CWD) { | |
- @dirnames.grep( *.IO(:$SPEC,:$CWD).mkdir($mode) ).eager; | |
-} | |
-multi sub mkdir($path, $mode = 0o777, :$SPEC = $*SPEC, :$CWD = $*CWD) { | |
- $path.IO(:$SPEC,:$CWD).mkdir($mode) ?? ($path,) !! (); | |
-} | |
- | |
-sub rename($from, $to, :$SPEC = $*SPEC, :$CWD = $*CWD) { | |
- my $result := $from.IO(:$SPEC,:$CWD).rename($to,:$SPEC,:$CWD); | |
- $result // $result.throw; | |
-} | |
-sub copy($from, $to, :$SPEC = $*SPEC, :$CWD = $*CWD) { | |
- my $result := $from.IO(:$SPEC,:$CWD).copy($to,:$SPEC,:$CWD); | |
- $result // $result.throw; | |
+sub rename(Cool $from as Str, Cool $to as Str) { | |
+ my $absfrom = IO::Spec.rel2abs($from); | |
+ my $absto = IO::Spec.rel2abs($to); | |
+ nqp::rename(nqp::unbox_s($absfrom), nqp::unbox_s($absto)); | |
+ return True; | |
+ CATCH { | |
+ default { | |
+ if .Str ~~ /'rename failed: '(.*)/ { | |
+ X::IO::Rename.new( | |
+ :$from, | |
+ :$to, | |
+ os-error => $0.Str, | |
+ ).throw; | |
+ } else { | |
+ die "Unexpected error: $_"; | |
+ } | |
+ } | |
+ } | |
} | |
-sub symlink($target, $name, :$SPEC = $*SPEC, :$CWD = $*CWD) { | |
- my $result := $target.IO(:$SPEC,:$CWD).symlink($name,:$SPEC,:$CWD); | |
- $result // $result.throw; | |
+sub copy(Cool $from as Str, Cool $to as Str) { | |
+ my $absfrom = IO::Spec.rel2abs($from); | |
+ my $absto = IO::Spec.rel2abs($to); | |
+ nqp::copy(nqp::unbox_s($absfrom), nqp::unbox_s($absto)); | |
+ return True; | |
+ CATCH { | |
+ default { | |
+ X::IO::Copy.new( | |
+ :$from, | |
+ :$to, | |
+ os-error => .Str, | |
+ ).throw; | |
+ } | |
+ } | |
} | |
-sub link($target, $name, :$SPEC = $*SPEC, :$CWD = $*CWD) { | |
- my $result := $target.IO(:$SPEC,:$CWD).link($name,:$SPEC,:$CWD); | |
- $result // $result.throw; | |
+sub symlink(Cool $target as Str, Cool $name as Str) { | |
+ my $abstarget = IO::Spec.rel2abs($target); | |
+ nqp::symlink(nqp::unbox_s($abstarget), nqp::unbox_s($name)); | |
+ return True; | |
+ CATCH { | |
+ default { | |
+ X::IO::Symlink.new( | |
+ :$target, | |
+ :$name, | |
+ os-error => .Str, | |
+ ).throw; | |
+ } | |
+ } | |
} | |
- | |
-# deprecations | |
-{ | |
- sub cwd() { | |
- return nqp::p6box_s(nqp::cwd()); | |
- CATCH { default { | |
- fail X::IO::Cwd.new( os-error => .Str,); | |
- } } | |
+sub link(Cool $target as Str, Cool $name as Str) { | |
+ my $abstarget = IO::Spec.rel2abs($target); | |
+ nqp::link(nqp::unbox_s($abstarget), nqp::unbox_s($name)); | |
+ return True; | |
+ CATCH { | |
+ default { | |
+ X::IO::Link.new( | |
+ :$target, | |
+ :$name, | |
+ os-error => .Str, | |
+ ).throw; | |
+ } | |
} | |
-# PROCESS::<&cwd> := Deprecation.obsolete( | |
-# :name('&*cwd'), | |
-# :value(&cwd), | |
-# :instead('chdir'), | |
-# ); | |
} | |
-sub cwd() { | |
- DEPRECATED('$*CWD'); | |
- $*CWD; | |
-} | |
+sub chmod($mode, $filename) { $filename.path.absolute.chmod($mode); $filename } | |
# vim: ft=perl6 expandtab sw=4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment