Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
There's more than one way to do things.
 
PerlMonks

Copy preserving attributes

by puterboy (Acolyte)
 | Log in | Create a new user | The Monastery Gates | Super Search | 
 | Seekers of Perl Wisdom | Meditations | PerlMonks Discussion | 
 | Obfuscation | Reviews | Cool Uses For Perl | Perl News | Q&A | Tutorials | 
 | Poetry | Recent Threads | Newest Nodes | Donate | What's New | 

on Nov 02, 2009 at 16:59 UTC ( #804509=perlquestion: print w/ replies, xml ) Need Help??
puterboy has asked for the wisdom of the Perl Monks concerning the following question:

I would like to copy files preserving attributes (something like cp -cdpP) within perl rather than using a system call to *nix 'cp'.

Now from the documentation of File::Copy, it seems that this module won't do that for me at least on *nix since there is no syscopy routine.

I also saw an old thread (Circa 2003) saying that since the potential attributes to be preserved are not well-defined across systems (and may change in the future), this functionality has not been implemented in CPAN.

So, I ask is that still true?

Is there any (easy) way other than forking a syscall to preserve the following attributes:

timestamps ownership permissions SELinux Other ACL/extended attributes

Comment on Copy preserving attributes
Download Code
Re: Copy preserving attributes
by thunders (Curate) on Nov 02, 2009 at 17:32 UTC

    Which version of Perl and File::Copy are you using? In CPAN File::Copy does indeed provided a syscopy.

    Update: sorry read the docs and you are correct. On UNIX syscopy is just an alias to File::Copy::copy() Here's a thread with the same essential question.

[reply]
      Yes - that was exactly the thread from 2003 that I was alluding to. But since it has been 6+ years, I was wondering whether there were any new approaches or solutions... One can always hope...
[reply]
Re: Copy preserving attributes
by stefbv (Sexton) on Nov 02, 2009 at 18:00 UTC
    Maybe Sysadm::Install can do the job.
[reply]
Re: Copy preserving attributes
by ikegami (Saint) on Nov 02, 2009 at 18:10 UTC

    I would like to copy files preserving attributes (something like cp -cdpP) within perl rather than using a system call to *nix 'cp'.

    Calling cp is your best option. It can be done safely with multi-arg system

    my $rv = system('cp', '-cdpP', ...); $rv == -1 and die("Can't execute cp: $!\n"); $rv & 0x7F and die("cp died from signal ", ($rv & 0x7F), "\n"); $rv >> 8 and die("Error ", ($rv >> 8), "from cp\n");
[reply]
[d/l]
[select]
Re: Copy preserving attributes
by keszler (Pilgrim) on Nov 02, 2009 at 18:24 UTC
[reply]
      any solution for extended attributes/ACLs? Or is that too system specific.

      Also, how does the efficiency of a single cp system call compare to the code you would have to read and then set timestamp/ownership/permissions internally in perl?

[reply]
        Considering that the running time of the 'cp' is most likely to be bound by disk I/O, I wouldn't worry about the overhead of calling 'cp'.

        Specially if you have the need to take care of system specifics (like extended attributes/ACLs), I would prefer calling the systems 'cp' then doing it all yourself. Specially if you don't have access to all the environments the program is supposed to run in.

[reply]
Re: Copy preserving attributes
by zentara (Chancellor) on Nov 02, 2009 at 18:58 UTC
[reply]
Re: Copy preserving attributes
by djp (Friar) on Nov 03, 2009 at 09:41 UTC
    I've used File-Rsync with the --archive option set to achieve this. Not the most elegant solution, but it will do what you want.
[reply]
      Interesting idea, but I imagine that calling rsync even if coded all in Perl will be *slower* than a 'cp' system call.
[reply]

Back to Seekers of Perl Wisdom


Login:
Password
remember me
What's my password?
Create A New User

Node Status
node history
Node Type: perlquestion [id://804509]
Approved by MidLifeXis
Front-paged by MidLifeXis
help
Community Ads
Chatterbox
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users
Others contemplating the Monastery: (10)
Old_Gray_Bear
Moriarty
atcroft
herveus
Eyck
Utilitarian
biohisham
keszler
gnosti
im2
As of 2009-11-21 11:00 GMT
Sections
The Monastery Gates
Seekers of Perl Wisdom
Meditations
PerlMonks Discussion
Categorized Q&A
Tutorials
Obfuscated Code
Perl Poetry
Cool Uses for Perl
Perl News
Information
PerlMonks FAQ
Guide to the Monastery
What's New at PerlMonks
Voting/Experience System
Tutorials
Reviews
Library
Perl FAQs
Other Info Sources
Find Nodes
Nodes You Wrote
Super Search
List Nodes By Users
Newest Nodes
Recently Active Threads
Selected Best Nodes
Best Nodes
Worst Nodes
Saints in our Book
Leftovers
The St. Larry Wall Shrine
Offering Plate
Awards
Craft
Snippets Section
Code Catacombs
Quests
Editor Requests
Buy PerlMonks Gear
PerlMonks Merchandise
Planet Perl
Perlsphere
Use Perl
Perl.com
Perl 5 Wiki
Perl Jobs
Perl Mongers
Perl Directory
Perl documentation
CPAN
Random Node
Voting Booth

Future historians will find that the material characteristic of the current era is...

Aluminium
Plastic
Oil
Water
Carbon dioxide
Copper
Iron
Silicon
Salt
Uranium
Hydrogen
Other

Results (730 votes), past polls