Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

cp -p in perl?

by BuddhaNature (Beadle)
on Apr 20, 2004 at 22:06 UTC ( [id://346817]=perlquestion: print w/replies, xml ) Need Help??

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

Is there a perl module or a perl builtin that will perform a file copy but preserve the permissions of the copied file? Or is there just `cp -p $file $destination` or some combination of copy and then chmod?

Replies are listed 'Best First'.
Re: cp -p in perl?
by Abigail-II (Bishop) on Apr 20, 2004 at 22:51 UTC
    Is there a perl module or a perl builtin that will perform a file copy but preserve the permissions of the copied file?
    Unless something has changed recently, the most cited module for copying files, File::Copy doesn't do it. In fact, it doesn't even mimic the default cp behaviour, dropping permissions when you least expect it.

    My advice for copying files is to use system cp => @files, $destination. cp is far more useful than File::Copy::copy that a potential loss in portability (but bare in mind that cp has been ported outside of Unix systems) is well worth the gain.

    Abigail

      It looks like (so far) the best way to do this is to do all your work on a tempfile ( I like File::Temp myself) version of the file you want to copy and then do your chmod and/or chown on the tempfile and then use rename(). This seems to work correctly.
        That doesn't sound like a good idea at all to me. First of all, we're talking about just copying a file, not about any modification. Secondly, using chmod/chown is a lot more work than just using cp. But most importantly, rename will not rename from one device to another.

        Abigail

OT: Beware subtle cross-platform differences in cp
by cLive ;-) (Prior) on Apr 21, 2004 at 03:32 UTC
    I know this is slightly OT, but since we're talking cp here...

    There is a small, but important difference between cp -f on Linux and FreeBSD (and, I assume, other BSDs). On Linux, if the destination file exists, it is overwritten with target permissions preserved. On FreeBSD, target is removed, and the default umask permissions are used when new file is created.

    I was bitten bad by that one a year ago when developing a test app on Linux then moving to a FreeBSD environment.

    I guess the point is, get into the habit of chmodding (and chowning - depends on context) a file when cp'ing it. If you rely on the OS, you can get bitten when you least expect it.

    .02

    cLive ;-)

    janitored by ybiC: Retitled from "OT: note on cp" for slight improvement in descriptiveness

Re: cp -p in perl?
by eserte (Deacon) on Apr 21, 2004 at 08:46 UTC
    It seems that the Perl Power Tools version of cp understands the -i, -f, -p and -v options. This is a command line tool, but it should be easy to wrap the code into a package/module. See here for the current CVS version.
Re: cp -p in perl?
by amonroy (Scribe) on Apr 21, 2004 at 03:21 UTC

    I had the same problem. I ended up using a system call.

    Pax vobiscum,
    -Andrés

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2024-04-18 09:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found