[Perl] Two Filesystem Questions

Shlomi Fish 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
> http://archive.develooper.com/beginners@perl.org/msg20814.html):
>
> #!/usr/bin/perl
> use warnings;
> use File::Copy;
> use strict;
>
> my $mode = (stat $ARGV[0])[2]; #decimal unmasked
> my $mode = sprintf("%04o", $mode & 07777) ; #octal masked off
> print $mode,"\n";
> copy( "$ARGV[0]", "$ARGV[0]\.bak" );
> chmod (oct($mode), "$ARGV[0]\.bak");
> print "Backup completed.\n";
> exit 0;
>
>
> The conversion to octal above is not strictly necessary- see the comments by
> Randal L. Schwartz:
> http://archive.develooper.com/beginners@perl.org/msg20815.html
>

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:
> http://search.cpan.org/author/JHI/perl-5.8.0/lib/File/Path.pm
>

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

> > Regards,
> >
> > 	Shlomi Fish
> >
>
> Hope this helps :-)
> Offer Kaye
>

It helped a lot. Thanks.




More information about the Perl mailing list