1 |
=head1 NAME
|
2 |
|
3 |
Utilities::AddDir - Utility functions for creating or copying directories.
|
4 |
|
5 |
=head1 SYNOPSIS
|
6 |
|
7 |
&Utilities::AddDir::adddir($dir);
|
8 |
&Utilities::AddDir::copydir($src,$dest);
|
9 |
&Utilities::AddDir::copydirwithskip($src,$dest,@files_to_skip);
|
10 |
|
11 |
=head1 METHODS
|
12 |
|
13 |
=over
|
14 |
|
15 |
=cut
|
16 |
|
17 |
package Utilities::AddDir;
|
18 |
require 5.001;
|
19 |
require Exporter;
|
20 |
use Cwd;
|
21 |
@ISA = qw(Exporter);
|
22 |
@EXPORT = qw(adddir copydir copydirwithskip copydirexp getfileslist fixpath);
|
23 |
|
24 |
=item C<adddir($dir)>
|
25 |
|
26 |
Create a new directory.
|
27 |
|
28 |
=cut
|
29 |
|
30 |
|
31 |
sub fixpath {
|
32 |
my $dir=shift;
|
33 |
my @parts=();
|
34 |
my $p="/";
|
35 |
if($dir!~/^\//){$p="";}
|
36 |
foreach my $part (split /\//, $dir)
|
37 |
{
|
38 |
if($part eq ".."){pop @parts;}
|
39 |
elsif(($part ne "") && ($part ne ".")){push @parts, $part;}
|
40 |
}
|
41 |
return "$p".join("/",@parts);
|
42 |
}
|
43 |
|
44 |
sub adddir {
|
45 |
my $indir=shift;
|
46 |
my $startdir=cwd;
|
47 |
my @dir=split /\//, $indir;
|
48 |
|
49 |
if ( $indir=~/^\// ) {
|
50 |
chdir "/";
|
51 |
shift @dir;
|
52 |
}
|
53 |
umask 02;
|
54 |
foreach $dirname ( @dir ) {
|
55 |
next if ( $dirname eq "" );
|
56 |
if ( ! -e $dirname ) {
|
57 |
mkdir ( $dirname , 0755) ||
|
58 |
die "cannot make directory ".$dirname." $!\n";
|
59 |
print $i." ".$dirname."\n" if $debug;
|
60 |
}
|
61 |
chdir $dirname;
|
62 |
}
|
63 |
chdir $startdir;
|
64 |
}
|
65 |
|
66 |
|
67 |
=item C<copydir($src, $dest)>
|
68 |
|
69 |
Copy a directory $src and contents to $dest.
|
70 |
|
71 |
=cut
|
72 |
|
73 |
sub copydir
|
74 |
{
|
75 |
my $src=shift;
|
76 |
my $dest=shift;
|
77 |
|
78 |
use DirHandle;
|
79 |
use File::Copy;
|
80 |
|
81 |
adddir($dest);
|
82 |
my $dh=DirHandle->new($src);
|
83 |
|
84 |
if (defined $dh)
|
85 |
{
|
86 |
my @allfiles=$dh->read();
|
87 |
|
88 |
my $file;
|
89 |
foreach $file ( @allfiles )
|
90 |
{
|
91 |
next if $file=~/^\.\.?/;
|
92 |
if ( -d $src."/".$file )
|
93 |
{
|
94 |
copydir($src."/".$file,$dest."/".$file);
|
95 |
}
|
96 |
else
|
97 |
{
|
98 |
copy($src."/".$file,$dest."/".$file);
|
99 |
if ( -x $src."/".$file || -X $src."/".$file ) {chmod(0755,$dest."/".$file);}
|
100 |
}
|
101 |
}
|
102 |
undef $dh;
|
103 |
}
|
104 |
else
|
105 |
{
|
106 |
die "Attempt to open a non-existent directory ($src). Exiting\n";
|
107 |
}
|
108 |
}
|
109 |
|
110 |
=item C<copydirwithskip($src, $dest, @files_to_skip)>
|
111 |
|
112 |
Recursively copy a directory $src to $dest. All files
|
113 |
in @files_to_skip will be skipped.
|
114 |
|
115 |
=cut
|
116 |
|
117 |
sub copydirwithskip
|
118 |
{
|
119 |
my $src=shift;
|
120 |
my $dest=shift;
|
121 |
my $filetoskip=shift || [];
|
122 |
|
123 |
use DirHandle;
|
124 |
use File::Copy;
|
125 |
|
126 |
adddir($dest);
|
127 |
|
128 |
my $dh=DirHandle->new($src);
|
129 |
|
130 |
if (defined $dh)
|
131 |
{
|
132 |
my @allfiles=$dh->read();
|
133 |
|
134 |
my $file;
|
135 |
foreach $file ( @allfiles )
|
136 |
{
|
137 |
next if $file=~/^\.\.?/;
|
138 |
# Skip backup files and x~ files:
|
139 |
next if $file =~ /.*\.bak$/;
|
140 |
next if $file =~ /.*~$/;
|
141 |
|
142 |
my $skip=0;
|
143 |
foreach my $fskip (@$filetoskip)
|
144 |
{
|
145 |
my $fullfile = "${src}/${file}";
|
146 |
if ($fullfile eq $fskip)
|
147 |
{
|
148 |
$skip = 1;
|
149 |
last;
|
150 |
}
|
151 |
}
|
152 |
if ($skip)
|
153 |
{
|
154 |
next;
|
155 |
}
|
156 |
|
157 |
if ( -d $src."/".$file )
|
158 |
{
|
159 |
copydirwithskip($src."/".$file,$dest."/".$file,$filetoskip);
|
160 |
}
|
161 |
else
|
162 |
{
|
163 |
copy($src."/".$file,$dest."/".$file);
|
164 |
if ( -x $src."/".$file || -X $src."/".$file ) {chmod(0755,$dest."/".$file);}
|
165 |
}
|
166 |
}
|
167 |
undef $dh;
|
168 |
}
|
169 |
else
|
170 |
{
|
171 |
die "Attempt to open a non-existent directory ($src). Exiting\n";
|
172 |
}
|
173 |
}
|
174 |
|
175 |
sub copydirexp
|
176 |
{
|
177 |
my $src=shift;
|
178 |
my $dest=shift;
|
179 |
my $exp=shift || ".+";
|
180 |
my $op=shift || 0;
|
181 |
|
182 |
if ($exp=~/^!(.+)/){$exp=$1; $op=1;}
|
183 |
|
184 |
use DirHandle;
|
185 |
use File::Copy;
|
186 |
adddir($dest);
|
187 |
my $dh=DirHandle->new($src);
|
188 |
|
189 |
if (defined $dh)
|
190 |
{
|
191 |
my @allfiles=$dh->read();
|
192 |
|
193 |
my $file;
|
194 |
foreach $file ( @allfiles )
|
195 |
{
|
196 |
next if $file=~/^\.\.?/;
|
197 |
if ( -d $src."/".$file )
|
198 |
{
|
199 |
copydirexp($src."/".$file,$dest."/".$file,$exp,$op);
|
200 |
}
|
201 |
else
|
202 |
{
|
203 |
my $skip=1;
|
204 |
if ($file=~/$exp/)
|
205 |
{
|
206 |
if ($op == 0){$skip=0;}
|
207 |
}
|
208 |
elsif ($op == 1){$skip=0;}
|
209 |
if (!$skip)
|
210 |
{
|
211 |
copy($src."/".$file,$dest."/".$file);
|
212 |
if ( -x $src."/".$file || -X $src."/".$file ) {chmod(0755,$dest."/".$file);}
|
213 |
}
|
214 |
}
|
215 |
}
|
216 |
undef $dh;
|
217 |
}
|
218 |
else
|
219 |
{
|
220 |
die "Attempt to open a non-existent directory ($src). Exiting\n";
|
221 |
}
|
222 |
}
|
223 |
|
224 |
sub getfileslist ()
|
225 |
{
|
226 |
my $dir=shift;
|
227 |
my $data=shift || [];
|
228 |
my $base=shift || $dir;
|
229 |
my $ref;
|
230 |
opendir($ref,$dir) || die "ERROR: Can not open directory for reading: $dir";
|
231 |
foreach my $f (readdir($ref))
|
232 |
{
|
233 |
next if $f=~/^\.\.?/;
|
234 |
$f = "${dir}/${f}";
|
235 |
if (-d $f){&getfileslist($f,$data,$base);}
|
236 |
else
|
237 |
{
|
238 |
$f=~s/^$base\///;
|
239 |
push @$data,$f;
|
240 |
}
|
241 |
}
|
242 |
closedir($ref);
|
243 |
return $data;
|
244 |
}
|
245 |
|
246 |
1;
|
247 |
|
248 |
__END__
|
249 |
|
250 |
=back
|
251 |
|
252 |
=head1 AUTHOR
|
253 |
|
254 |
Originally written by Christopher Williams.
|
255 |
|
256 |
=head1 MAINTAINER
|
257 |
|
258 |
Shaun ASHBY
|
259 |
|
260 |
=cut
|
261 |
|