1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
package File::Find::Object::TreeCreate;
use strict;
use warnings;
use File::Spec;
sub new
{
my $class = shift;
my $self = {};
bless $self, $class;
$self->_initialize(@_);
return $self;
}
sub _initialize
{
}
sub get_path
{
my $self = shift;
my $path = shift;
my @components;
if ($path =~ s{^\./}{})
{
push @components, File::Spec->curdir();
}
my $is_dir = ($path =~ s{/$}{});
push @components, split(/\//, $path);
if ($is_dir)
{
return File::Spec->catdir(@components);
}
else
{
return File::Spec->catfile(@components);
}
}
sub exist
{
my $self = shift;
return (-e $self->get_path(@_));
}
sub is_file
{
my $self = shift;
return (-f $self->get_path(@_));
}
sub is_dir
{
my $self = shift;
return (-d $self->get_path(@_));
}
sub cat
{
my $self = shift;
open my $in, "<", $self->get_path(@_) or
return 0;
my $data;
{
local $/;
$data = <$in>;
}
close($in);
return $data;
}
sub ls
{
my $self = shift;
opendir my $dir, $self->get_path(@_) or
return undef;
my @files =
sort { $a cmp $b }
grep { !(($_ eq ".") || ($_ eq "..")) }
readdir($dir);
closedir($dir);
return \@files;
}
sub create_tree
{
my ($self, $unix_init_path, $tree) = @_;
my $real_init_path = $self->get_path($unix_init_path);
return $self->_real_create_tree($real_init_path, $tree);
}
sub _real_create_tree
{
my ($self, $init_path, $tree) = @_;
my $name = $tree->{'name'};
if ($name =~ s{/$}{})
{
my $dir_name = File::Spec->catfile($init_path, $name);
mkdir($dir_name);
if (exists($tree->{'subs'}))
{
foreach my $sub (@{$tree->{'subs'}})
{
$self->_real_create_tree($dir_name, $sub);
}
}
}
else
{
open my $out, ">", File::Spec->catfile($init_path, $name);
print {$out} +(exists($tree->{'contents'}) ? $tree->{'contents'} : "");
close($out);
}
return 0;
}
1;
|