develooper Front page | perl.perl5.porters | Postings from July 2013

Re: Recommended file-copying method

Thread Previous | Thread Next
From:
David Golden
Date:
July 8, 2013 01:29
Subject:
Re: Recommended file-copying method
Message ID:
CAOeq1c_DmJr31g+bDfM0drR0u9+_DYo8K1+L2-cjJQ8t+mjx4g@mail.gmail.com
On Sun, Jul 7, 2013 at 6:38 PM, Ken Williams <kenahoo@gmail.com> wrote:
D
> My question about the best choice for the underlying copy operation still
> stands, if someone knows of a good choice.

I've been doing some background work on this for implementing saner
move/copy/rename for Path::Tiny.

My conclusion is that File::Copy isn't great, but it's probably the
best thing available in core.  I'd probably recommend the 'cp'
function as I think it's going to be the most portable and least
surprising to the most people.

What's wrong with it?  It has a number of suboptimal API and
implementation choices:

* copies are not atomic -- interruptions or errors leave half-copied
files around

* copying a file on top of itself warns and returns 0 -- technically
correct, but not very DWIMy

* copying to/from handles requires caller to set raw binmode -- plenty
of opportunity to shoot oneself in the foot, even if its documented

* uses sysread instead of read, so if your handle wasn't raw for some
reason, layers are ignored (except for the utf8 flag, which winds up
lax instead of possibly strict) so you're shooting your foot with a
machine gun

* cp preserves only mode, not ownership (if root) or timestamps

On the positive, it supposedly works on VMS (which isn't a
consideration for Path::Tiny).

For what Path::Class needs to do, it can probably avoid or work around
those issues.

Path::Tiny is probably going to reimplement it in terms of raw
slurp/spew with optional chown/chmod/utime (which is tricky-ish but
doesn't seem too bad).

David




--
David Golden <xdg@xdg.me>
Take back your inbox! → http://www.bunchmail.com/
Twitter/IRC: @xdg

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About