mt2k has asked for the wisdom of the Perl Monks concerning the following question:
Now, say that the string "C:/Apache/www-root/images/bgs" is passed to the CGI script and stored in $input{'directory'}. Let's say that $input{'directory'} is the name of the directory I want to copy to another directory somewhere on the system, specified as $input{'copy_to'}. Here's the code that accomplishes this:
use File::NCopy; File::NCopy::copy (\1, "$input{'directory'}", "$input{'copy_to'}");
But there is a small problem with this code! If $input{'directory'} is "C:/Apache/www-root/images" and $input{'copy_to'} is "C:/Apache/www-root/images/bgs", the copy does a horrible thing. It continually, over and over again, copies the C:/Apache/www-root/images directory deeper and deeper into directory C:/Apache/www-root/images/bgs, so that one gets a filesystem such as this:
And that pattern continues on for whoever knows how many times. So in the end I have a directory C:/Apache/www-root/images/bgs/ images/bgs/ images/bgs/ images/bgs/ images/bgs/ images/bgs/ images/bgs/ images/bgs/ images/bgs/ images/bgs/images (minus the spaces) and that is DEFINITELY not what I want to ever happen. So to finally get to the actual question: How can I make sure that $input{'copy_to'} is not a subdirectory of $input{'directory'}?? Here is some more code. I believe that I know where the missing code has to go. It is colored in red. This is what I use to display a list of options to the web user of which directories s/he can copy to:
print qq|<select name="blah">\n|;
find(\&{ sub {
if (-d "$File::Find::dir/$_"){
if ($count++ == 0) {
push @dirs, $File::Find::dir;
} else {
push @dirs, $File::Find::dir . "/" . $_;
}
}
}
}, "C:/Apache/www-root");
foreach (sort { lc($a) cmp lc($b) } @dirs) {
if (something needs to go here) { print qq|<option>$_\n|; }
}
print qq|</select>|;
So what code has to go in that red if statement (or maybe somewhere in that find sub) to make sure that $input{'copy_to'} is not a subdirectory of $input{'directory'}??
Let me sum this up in one sentence:
"How do I make sure that a directory is not copied to a subdirectory of the directory I'm copying?"
Sorry for all the head scratching and confusion with this question. That was hard to describe...
Edit Petruchio Fri Mar 15 06:46:11 UTC 2002 - Fixed typo in title on author's behalf
Edit by tye as long path string made page render very wide in some browsers
|
|---|