Deutsch   English   Français   Italiano  
<v5jm1i$2ntni$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!feed.opticnetworks.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Lew Pitcher <lew.pitcher@digitalfreehold.ca>
Newsgroups: comp.unix.programmer
Subject: Re: how copy file on linux?
Date: Thu, 27 Jun 2024 12:30:42 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 82
Message-ID: <v5jm1i$2ntni$1@dont-email.me>
References: <v5huk1$2anbd$1@dont-email.me>
	<87v81vs57v.fsf@nosuchdomain.example.com> <v5i7vu$28id7$1@dont-email.me>
	<87msn7vxjc.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 27 Jun 2024 14:30:43 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="455a3cb0350599fb9065298926fa125c";
	logging-data="2881266"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/Q4BiMd5Ml3JiVEvTRt85q+cIsoBvLFsE="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
 git://git.gnome.org/pan2)
Cancel-Lock: sha1:aa8Y2dialDPM/YeX9kNJNby49Ho=
Bytes: 4484

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"