1 |
sashby |
1.10 |
=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 |
muzaffar |
1.14.2.1 |
package Utilities::AddDir;
|
18 |
williamc |
1.2 |
require 5.001;
|
19 |
williamc |
1.1 |
require Exporter;
|
20 |
|
|
use Cwd;
|
21 |
|
|
@ISA = qw(Exporter);
|
22 |
sashby |
1.9 |
@EXPORT = qw(adddir copydir copydirwithskip);
|
23 |
williamc |
1.4 |
|
24 |
sashby |
1.10 |
=item C<adddir($dir)>
|
25 |
|
|
|
26 |
|
|
Create a new directory.
|
27 |
|
|
|
28 |
|
|
=cut
|
29 |
williamc |
1.1 |
|
30 |
williamc |
1.3 |
sub adddir {
|
31 |
williamc |
1.1 |
my $indir=shift;
|
32 |
|
|
my $startdir=cwd;
|
33 |
|
|
my @dir=split /\//, $indir;
|
34 |
|
|
|
35 |
|
|
if ( $indir=~/^\// ) {
|
36 |
|
|
chdir "/";
|
37 |
|
|
shift @dir;
|
38 |
|
|
}
|
39 |
|
|
umask 02;
|
40 |
|
|
foreach $dirname ( @dir ) {
|
41 |
|
|
next if ( $dirname eq "" );
|
42 |
|
|
if ( ! -e $dirname ) {
|
43 |
sashby |
1.9 |
mkdir ( $dirname , 0755) ||
|
44 |
williamc |
1.1 |
die "cannot make directory ".$dirname." $!\n";
|
45 |
|
|
print $i." ".$dirname."\n" if $debug;
|
46 |
|
|
}
|
47 |
|
|
chdir $dirname;
|
48 |
|
|
}
|
49 |
|
|
chdir $startdir;
|
50 |
williamc |
1.4 |
}
|
51 |
|
|
|
52 |
sashby |
1.10 |
|
53 |
|
|
=item C<copydir($src, $dest)>
|
54 |
|
|
|
55 |
|
|
Copy a directory $src and contents to $dest.
|
56 |
|
|
|
57 |
|
|
=cut
|
58 |
|
|
|
59 |
sashby |
1.6 |
sub copydir
|
60 |
|
|
{
|
61 |
|
|
my $src=shift;
|
62 |
|
|
my $dest=shift;
|
63 |
|
|
|
64 |
|
|
use DirHandle;
|
65 |
|
|
use File::Copy;
|
66 |
|
|
|
67 |
|
|
adddir($dest);
|
68 |
|
|
my $dh=DirHandle->new($src);
|
69 |
|
|
|
70 |
|
|
if (defined $dh)
|
71 |
|
|
{
|
72 |
|
|
my @allfiles=$dh->read();
|
73 |
|
|
|
74 |
|
|
my $file;
|
75 |
|
|
foreach $file ( @allfiles )
|
76 |
|
|
{
|
77 |
williamc |
1.4 |
next if $file=~/^\.\.?/;
|
78 |
sashby |
1.6 |
if ( -d $src."/".$file )
|
79 |
|
|
{
|
80 |
|
|
copydir($src."/".$file,$dest."/".$file);
|
81 |
|
|
}
|
82 |
|
|
else
|
83 |
|
|
{
|
84 |
|
|
copy($src."/".$file,$dest."/".$file);
|
85 |
sashby |
1.12 |
if ( -x $src."/".$file || -X $src."/".$file ) {chmod(0755,$dest."/".$file);}
|
86 |
sashby |
1.6 |
}
|
87 |
williamc |
1.4 |
}
|
88 |
sashby |
1.6 |
undef $dh;
|
89 |
|
|
}
|
90 |
|
|
else
|
91 |
|
|
{
|
92 |
elmer |
1.14 |
die "Attempt to open a non-existent directory ($src). Exiting\n";
|
93 |
sashby |
1.6 |
}
|
94 |
|
|
}
|
95 |
sashby |
1.8 |
|
96 |
sashby |
1.10 |
=item C<copydirwithskip($src, $dest, @files_to_skip)>
|
97 |
|
|
|
98 |
|
|
Recursively copy a directory $src to $dest. All files
|
99 |
|
|
in @files_to_skip will be skipped.
|
100 |
|
|
|
101 |
|
|
=cut
|
102 |
|
|
|
103 |
sashby |
1.8 |
sub copydirwithskip
|
104 |
|
|
{
|
105 |
|
|
my $src=shift;
|
106 |
|
|
my $dest=shift;
|
107 |
muzaffar |
1.13 |
my $filetoskip=shift || [];
|
108 |
sashby |
1.8 |
|
109 |
|
|
use DirHandle;
|
110 |
|
|
use File::Copy;
|
111 |
|
|
|
112 |
|
|
adddir($dest);
|
113 |
|
|
|
114 |
|
|
my $dh=DirHandle->new($src);
|
115 |
|
|
|
116 |
|
|
if (defined $dh)
|
117 |
|
|
{
|
118 |
|
|
my @allfiles=$dh->read();
|
119 |
|
|
|
120 |
|
|
my $file;
|
121 |
|
|
foreach $file ( @allfiles )
|
122 |
|
|
{
|
123 |
|
|
next if $file=~/^\.\.?/;
|
124 |
|
|
# Skip backup files and x~ files:
|
125 |
muzaffar |
1.13 |
next if $file =~ /.*\.bak$/;
|
126 |
sashby |
1.8 |
next if $file =~ /.*~$/;
|
127 |
|
|
|
128 |
muzaffar |
1.13 |
my $skip=0;
|
129 |
|
|
foreach my $fskip (@$filetoskip)
|
130 |
|
|
{
|
131 |
|
|
my $fullfile = "${src}/${file}";
|
132 |
|
|
if ($fullfile eq $fskip)
|
133 |
|
|
{
|
134 |
|
|
$skip = 1;
|
135 |
|
|
last;
|
136 |
|
|
}
|
137 |
|
|
}
|
138 |
|
|
if ($skip)
|
139 |
sashby |
1.8 |
{
|
140 |
|
|
next;
|
141 |
|
|
}
|
142 |
|
|
|
143 |
|
|
if ( -d $src."/".$file )
|
144 |
|
|
{
|
145 |
muzaffar |
1.13 |
copydirwithskip($src."/".$file,$dest."/".$file,$filetoskip);
|
146 |
sashby |
1.8 |
}
|
147 |
|
|
else
|
148 |
|
|
{
|
149 |
|
|
copy($src."/".$file,$dest."/".$file);
|
150 |
sashby |
1.12 |
if ( -x $src."/".$file || -X $src."/".$file ) {chmod(0755,$dest."/".$file);}
|
151 |
sashby |
1.8 |
}
|
152 |
|
|
}
|
153 |
|
|
undef $dh;
|
154 |
|
|
}
|
155 |
|
|
else
|
156 |
|
|
{
|
157 |
elmer |
1.14 |
die "Attempt to open a non-existent directory ($src). Exiting\n";
|
158 |
sashby |
1.8 |
}
|
159 |
|
|
}
|
160 |
muzaffar |
1.14.2.2 |
1;
|
161 |
|
|
|
162 |
|
|
__END__
|
163 |
sashby |
1.10 |
|
164 |
|
|
=back
|
165 |
|
|
|
166 |
|
|
=head1 AUTHOR
|
167 |
|
|
|
168 |
|
|
Originally written by Christopher Williams.
|
169 |
|
|
|
170 |
|
|
=head1 MAINTAINER
|
171 |
|
|
|
172 |
sashby |
1.11 |
Shaun ASHBY
|
173 |
sashby |
1.10 |
|
174 |
|
|
=cut
|
175 |
|
|
|