Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #236: Fanout dropping voltage too much, try cutting some of those little traces


comp / comp.unix.programmer / Re: how copy file on linux?

SubjectAuthor
* Re: how copy file on linux?Keith Thompson
`* Re: how copy file on linux?Lew Pitcher
 `- Re: how copy file on linux?Lawrence D'Oliveiro

1
Subject: Re: how copy file on linux?
From: Keith Thompson
Newsgroups: comp.unix.programmer
Organization: None to speak of
Date: Thu, 27 Jun 2024 04:07 UTC
References: 1 2 3
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S.Thompson+u@gmail.com (Keith Thompson)
Newsgroups: comp.unix.programmer
Subject: Re: how copy file on linux?
Date: Wed, 26 Jun 2024 21:07:19 -0700
Organization: None to speak of
Lines: 28
Message-ID: <87msn7vxjc.fsf@nosuchdomain.example.com>
References: <v5huk1$2anbd$1@dont-email.me>
<87v81vs57v.fsf@nosuchdomain.example.com>
<v5i7vu$28id7$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Date: Thu, 27 Jun 2024 06:07:19 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="2dc86e450632c25820fa91c1ec368267";
logging-data="2708393"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/8U3hnKDw6UVT4mekUZcFT"
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:1vqnOu/pmb9HR7qQ4/6zAvUN9z4=
sha1:YGC8ZTYZlAZajz1tInpJNpOw8rM=
View all headers

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?

(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".)

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */

Subject: Re: how copy file on linux?
From: Lew Pitcher
Newsgroups: comp.unix.programmer
Organization: A noiseless patient Spider
Date: Thu, 27 Jun 2024 12:30 UTC
References: 1 2 3 4
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: lew.pitcher@digitalfreehold.ca (Lew Pitcher)
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=
View all headers

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"

Subject: Re: how copy file on linux?
From: Lawrence D'Oliv
Newsgroups: comp.unix.programmer
Organization: A noiseless patient Spider
Date: Fri, 28 Jun 2024 00:22 UTC
References: 1 2 3 4 5
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.unix.programmer
Subject: Re: how copy file on linux?
Date: Fri, 28 Jun 2024 00:22:24 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 7
Message-ID: <v5kvnv$2vslb$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> <v5jm1i$2ntni$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 28 Jun 2024 02:22:24 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="a0cbf97cdbac19c046a9f041c8314851";
logging-data="3142315"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19LmcgLjTZaF7LOpt9U6xfL"
User-Agent: Pan/0.158 (Avdiivka; )
Cancel-Lock: sha1:2gA3HfChsw7sUMktzOOByrH7B6o=
View all headers

On Thu, 27 Jun 2024 12:30:42 -0000 (UTC), Lew Pitcher wrote:

> Thiago would still need stat()/utime()/chmod()/chown() to make the
> metadata identical ...

Some Linux filesystems support extra attributes. Also don’t forget POSIX
ACLs.

1

rocksolid light 0.9.8
clearnet tor