Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: Why isn't there a "copy" function?

by iburrell (Chaplain)
on Sep 09, 2004 at 23:02 UTC ( [id://389914] : note . print w/replies, xml ) Need Help??

in reply to Why isn't there a "copy" function?

Because Unix does not have a copy function. Doing a file copy requires extra code, which is in File::Copy. This isn't too inconsistent since file moves across filesystems requires similar support, like with File::Copy::move.

A better question is why Unix does not have a copy function to copy files. The operating system can do a better job copying files than user code. It can be much more efficient with network file systems, knows more about file attributes, better optimize IO, and could implement copy-on-write behavior. A copy() that only worked within a filesystem would be similar to rename().

Replies are listed 'Best First'.
Re^2: Why isn't there a "copy" function?
by Anonymous Monk on Sep 10, 2004 at 04:18 UTC
    Consideration is currently going into Linux to add sendfile file-to-file semantics, which would be basically equivalent to a copyfile system call. The major problem is that, if you were to copy a 2GiB file, for example, it would freeze the system as the kernel took up all available processing time doing the copy. So, copyfile system calls would tend to expose a local DoS attack, because they happen in a place where execution can't normally be interrupted to allow for multitasking.
Re^2: Why isn't there a "copy" function?
by mce (Curate) on Sep 10, 2004 at 07:11 UTC
    Good point,
    On unix, File::Copy needs to be called when moving a file, but on Windows Win32::FileCopy is more reliable.
    copy-ing a file works best when the direct OS API's are called. (has to do with file permissions etc... ).

    Dr. Mark Ceulemans
    Senior Consultant
    BMC, Belgium
      On unix, File::Copy needs to be called when moving a file,

      If you like to be surprised or use non-flexible API, you'd use File::Copy. If you're smart, you'd use system cp. Things that are easy with system cp and awkward with File::Copy include:

          cp file1 file2 dir
          cp -r dir1 dir2
          cp -p file1 file2
          cp exe1 exe2
      Not to mention that I can stick an 'r' or an 's' in front of it, and copy from one machine to another.
        Unfortunately system(qw(cp ...)) is not portable. That's why there is File::NCopy, which I would like to see in the perl core instead of File::Copy.