lib/Fops/native.pm
changeset 2 1bd03bcf26b4
parent 1 9416fc33d2a0
equal deleted inserted replaced
1:9416fc33d2a0 2:1bd03bcf26b4
     3 use strict;
     3 use strict;
     4 use warnings;
     4 use warnings;
     5 use Carp;
     5 use Carp;
     6 use Cwd qw(abs_path cwd);
     6 use Cwd qw(abs_path cwd);
     7 use base qw(Fops);
     7 use base qw(Fops);
       
     8 use File::Path ();
       
     9 use Fops qw(_path);
     8 
    10 
     9 our $VERSION = "0.0";
    11 our $VERSION = "0.0";
    10 
    12 
    11 sub new {
    13 sub new {
    12     my $class = ref $_[0] ? ref shift : shift;
    14     my $class = ref $_[0] ? ref shift : shift;
    23     my $dst  = shift;
    25     my $dst  = shift;
    24 
    26 
    25     $dst = $self->{cwd} .= "/$dst"
    27     $dst = $self->{cwd} .= "/$dst"
    26       if $dst !~ /^\//;
    28       if $dst !~ /^\//;
    27 
    29 
    28     my @path;
    30     $self->{cwd} = _path($dst);
    29     my @parts = grep {length} split /\// => $dst;
    31 }
    30     while (@parts) {
       
    31         my $_;
       
    32         given ($_ = pop @parts) {
       
    33             when (".")  { }
       
    34             when ("..") { pop @parts }
       
    35             default { unshift @path => $_ }
       
    36         }
       
    37     }
       
    38 
    32 
    39     #$self->{cwd} =~ s{/\.(?=/|$)}{}g;	# dots
    33 sub stat {
    40     #$self->{cwd} =~ s{/[^/]+/\.\.(?=/|$)}{}g;
    34     my $self = shift;
    41     #$self->{cwd} =~ s{/+}{/}g;
    35     stat(_path($self->{root}, shift));
       
    36 }
    42 
    37 
    43     $self->{cwd} = "/" . join "/" => @path;
    38 sub is_dir {
       
    39     my $self = shift;
       
    40     -d _path($self->{root}, shift);
       
    41 }
    44 
    42 
    45     $self;
    43 sub is_file {
       
    44     my $self = shift;
       
    45     -f _path($self->{root}, shift);
       
    46 }
       
    47 
       
    48 sub mkpath {
       
    49     my $self = shift;
       
    50     File::Path::mkpath([ map { _path($self->{root}, $_) } @_ ]);
    46 }
    51 }
    47 
    52 
    48 1;
    53 1;