[Perl] Two Filesystem Questions
shlomif at vipe.stud.technion.ac.il
Thu Jan 16 05:07:19 PST 2003
On Thu, 16 Jan 2003, Offer Kaye wrote:
> > 1. How do I copy a file while preserving all of its permissions, portably?
> > (i.e: it will work on VMS and NT as well).
> > File::Copy does not seem to do that with syscopy() or copy();
> This might work (taken from
> use warnings;
> use File::Copy;
> use strict;
> my $mode = (stat $ARGV); #decimal unmasked
> my $mode = sprintf("%04o", $mode & 07777) ; #octal masked off
> print $mode,"\n";
> copy( "$ARGV", "$ARGV\.bak" );
> chmod (oct($mode), "$ARGV\.bak");
> print "Backup completed.\n";
> exit 0;
> The conversion to octal above is not strictly necessary- see the comments by
> Randal L. Schwartz:
I eventually chmod'ed a relevant file explicitly because it's the only one
that needed to be executable. Maybe I'll need it back if people start to
put executables in CLAN packages. Or maybe I should ignore non-POSIX
systems altogether and use "cp -r". I'll see.
> > 2. How do I delete an entire directory tree portably? (without starting to
> > hack away on one of those File::Finders.)
> Use the "rmdir" function from the File::Path module:
OK. Write now someone on the IRC wrote one from me and I Fixed it a bit,
and included it in my Utils module. But I'll see. Better use CPAN code,
then my own ad-hoc code that might turn bug in.
> > Regards,
> > Shlomi Fish
> Hope this helps :-)
> Offer Kaye
It helped a lot. Thanks.
More information about the Perl