Re: how copy file on linux?

Liste des GroupesRevenir à cu programmer 
Sujet : Re: how copy file on linux?
De : lew.pitcher (at) *nospam* digitalfreehold.ca (Lew Pitcher)
Groupes : comp.unix.programmer
Date : 27. Jun 2024, 13:30:42
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v5jm1i$2ntni$1@dont-email.me>
References : 1 2 3 4
User-Agent : Pan/0.139 (Sexual Chocolate; GIT bf56508 git://git.gnome.org/pan2)
On Wed, 26 Jun 2024 21:07:19 -0700, Keith Thompson wrote:

Lew Pitcher <lew.pitcher@digitalfreehold.ca> writes:
(Followup set to comp.unix.programmer)
>
On Wed, 26 Jun 2024 15:35:00 -0700, Keith Thompson wrote:
Thiago Adams <thiago.adams@gmail.com> writes:
How to copy a file on linux keeping the original file information?
timestamp etc?
>
Or in other words the equivalent of CopyFileA from windows.
 
comp.unix.programmer is likely to give you better answers.  I don't
think POSIX defines any functions that copy files.
>
No, AFAIK, POSIX doesn't define an "all-in-one" file copy function.
However, Linux (the OS Thiago asks about) defines a number of them.
[...]
 
What file copy functions does Linux define?  I know there are plenty of
file copy *commands*, and those commands can be invoked from a C
program.  Is that what you meant?

There are:
  sendfile(2)
       #include <sys/sendfile.h>

       ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count);

       sendfile()  copies  data  between  one  file  descriptor  and  another.
       Because this copying is done within  the  kernel,  sendfile()  is  more
       efficient  than  the  combination  of read(2) and write(2), which would
       require transferring data to and from user space.

  copy_file_range(2)
       #include <sys/syscall.h>
       #include <unistd.h>

       ssize_t copy_file_range(int fd_in, loff_t *off_in,
                               int fd_out, loff_t *off_out,
                               size_t len, unsigned int flags);

       The  copy_file_range()  system  call performs an in-kernel copy between
       two file descriptors without the additional cost of  transferring  data
       from the kernel to user space and then back into the kernel.  It copies
       up to len bytes of data from file descriptor fd_in to  file  descriptor
       fd_out,  overwriting any data that exists within the requested range of
       the target file.

and splice(2)
       #define _GNU_SOURCE         /* See feature_test_macros(7) */
       #include <fcntl.h>

       ssize_t splice(int fd_in, loff_t *off_in, int fd_out,
                      loff_t *off_out, size_t len, unsigned int flags);

       splice()  moves  data  between  two  file  descriptors  without copying
       between kernel address space and user address space.  It  transfers  up
       to  len  bytes  of  data  from  the  file  descriptor fd_in to the file
       descriptor fd_out, where one of the file descriptors must  refer  to  a
       pipe.

(and there may be others, I don't know)

Granted that none of these syscalls are "the equivalent of CopyFileA from windows",
and that only sendfile(2) comes close, but these are the tools we have.

Thiago would still need stat()/utime()/chmod()/chown() to make the metadata
identical, but at least the read/write loop is taken care of efficiently.

(By "Linux", I presume we're referring to Linux-based OSs like Debian et
al, not to the kernel.  Trying not to trigger a discussion of "Linux"
vs. "GNU/Linux".)

Thiago didn't make the distinction. Granted that these file-copy APIs are
syscalls, and thus part of the kernel, but I doubt that it matters to
Thiago.


--
Lew Pitcher
"In Skills We Trust"

Date Sujet#  Auteur
27 Jun 24 * Re: how copy file on linux?3Keith Thompson
27 Jun 24 `* Re: how copy file on linux?2Lew Pitcher
28 Jun 24  `- Re: how copy file on linux?1Lawrence D'Oliveiro

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal