Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

copy a direcory

by Anonymous Monk
on May 15, 2002 at 07:01 UTC ( [id://166656]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How to copy a directory which has only read permissions. I use File::NCopy module to recursive copy. It works well but if we give all permission to all the files, subfolders and deep subfolders. Is there any way to overcome with this situation. This copy a directory is used in the same server.

Replies are listed 'Best First'.
Re: copy a directory
by tfrayner (Curate) on May 15, 2002 at 11:27 UTC
    Hi,

    Here's how I might do it (if I understand you correctly):

    #!/usr/bin/perl use strict; use warnings; use File::Copy; use File::Find; use File::Spec; my $source="your_source_dir"; my $dest="your_destination_dir"; my $abs_source=File::Spec->rel2abs($source); my $abs_dest=File::Spec->rel2abs($dest); print("Source directory: $abs_source\n". "Destination directory: $abs_dest\n"); sub fileprocess{ print("Processing file: $_ ($File::Find::name)\n"); my $rel_file=File::Spec->abs2rel($File::Find::name, $abs_source); my $dest_file=File::Spec->rel2abs($rel_file, $abs_dest); if (-d $_){ # deal with directories as special case mkdir $dest_file or die("$!\n"); }else{ # copy the files copy($File::Find::name, $dest_file) or die("$!\n"); # change the permissions to those of the source my $mode=(stat($File::Find::name))[2]; chmod($mode, $dest_file) or die("$!\n"); } } sub postprocess{ my $rel_dir=File::Spec->abs2rel($File::Find::dir, $abs_source); my $dest_dir=File::Spec->rel2abs($rel_dir, $abs_dest); # change the permissions to those of the source my $mode=(stat($File::Find::dir))[2]; chmod($mode, $dest_dir) or die("$!\n"); } find ({wanted => \&fileprocess, postprocess => \&postprocess}, $abs_source);
    Hopefully this should also be moderately portable.

    Update:Bug fix - the original code would fail to process files in a given directory if the user did not have write permission for that directory (which is what you want to do...duh, silly me). Fixed using the 'postprocess' feature of File::Find to adjust directory permissions after processing the files in that directory.

    Good luck,
    Tim

    Update2: I just noticed that the perlmonks documentation on File::Find appears to be incomplete. Check out your local 'perldoc File::Find' or go here.

Re: copy a direcory
by kappa (Chaplain) on May 15, 2002 at 10:15 UTC
    If I understand your problem, you won't be able to read files from a directory which has only read permissions (and not search permissions).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://166656]
Approved by ar0n
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-25 14:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found