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

Re: Recommended file-copying method

Thread Previous | Thread Next
From:
Aristotle Pagaltzis
Date:
July 9, 2013 03:17
Subject:
Re: Recommended file-copying method
Message ID:
20130709031720.GA9697@fernweh.plasmasturm.org
* David Golden <xdg@xdg.me> [2013-07-09 03:25]:
> Even /bin/cp doesn't preserve properties by default and that's what
> a lot of people think of when they think about file copying semantics.

Uh.

    $ touch foo
    $ chmod +x foo
    $ /bin/cp foo bar
    $ [ -x bar ] && echo 'preserved'
    preserved

Your allegation is news to me. It won’t preserve much else by default,
but the file mode, it will. (In fact I don’t even know how to prevent
a BSDish cp from doing that.)

> So I think what "copy" means is different for different users in
> different situations. I think File::Copy does a fair job of providing
> various alternatives. I think it has some weird and/or unsafe
> behaviors that are unfortunate to have in the Perl core. (Oh well,
> back compatibility pretty much screws us there.)
>
> If Ken wants to offer a version of a "copy" operation in Path::Class,
> as long as it's documented clearly what it does and what its
> limitations are, I don't think it's going to be any more confusing to
> people than File::Copy already is.

Given your previous paragraph, that is not exactly a high bar to aim it,
now is it?

For copying from path to path you should probably follow /bin/cp’s lead
and duplicate the file mode onto the target by default; with an option
to preserve more, if the user asks for it. And just as you stipulate it
should be clearly documented as such.

For copying to a file handle you should most certainly not meddle with
any properties of the target file – if it even is a file. You should
also not meddle with the layers on the handle or do anything else than
stream the source file content down that handle as quickest you can.
Here too with the documentation, of course.

I also think these operations should be resolutely separate: passing
a handle to the path-to-path copy method, or a destination path to the
path-to-handle streaming method, should die screaming. User code should
have to be completely, but *completely*, explicit about what it’s asking
for.

That, if you ask me, is an interface that offers different things for
different situations, while at the same time *not* confusing people.

Regards,
-- 
Aristotle Pagaltzis // <http://plasmasturm.org/>

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