Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

72%
+6 −1
Q&A What's the fastest way to copy, preserving file attributes, on each platform?

I am looking copy files platform-dependently on Linux, Oracle Solaris, MacOS, BSDs, and Windows. I have found some system calls for each platform, namely sendfile(), copy_file_range(), fcopyfile()...

0 answers  ·  posted 5mo ago by Melkor-1‭  ·  last activity 5mo ago by Melkor-1‭

#8: Post undeleted by user avatar Melkor-1‭ · 2024-06-20T15:00:12Z (5 months ago)
#7: Post deleted by user avatar Melkor-1‭ · 2024-06-20T14:59:49Z (5 months ago)
#6: Nominated for promotion by user avatar Alexei‭ · 2024-06-20T08:15:48Z (5 months ago)
#5: Post edited by user avatar Melkor-1‭ · 2024-06-19T09:55:26Z (5 months ago)
  • I am looking copy files platform-dependently on Linux, Oracle Solaris, MacOS, BSDs, and Windows.
  • I have found some system calls for each platform, namely `sendfile()`, `copy_file_range()`, `fcopyfile()`, `CopyFile()` et cetera. But some of them are documented to copy file attributes, whilst some are not. I am uncertain whether better to do a `stat()` + `chmod()` for syscalls that do not copy attributes, or go with the classic route of `open()` + `stat()` + `chmod()` + `read()` + `write()`, that's guaranteed to work across all UNIX-like systems, and perhaps even Windows.
  • What is each platform's fastest solution to copy a file with attributes?
  • I am looking copy files platform-dependently on Linux, Oracle Solaris, MacOS, BSDs, and Windows.
  • I have found some system calls for each platform, namely `sendfile()`, `copy_file_range()`, `fcopyfile()`, `CopyFile()` et cetera. But some of them are documented to copy file attributes, whilst some are not. I am uncertain whether it would be better to do a `stat()` + `chmod()` prior the syscalls that do not copy attributes, or go with the classic route of `open()` + `stat()` + `chmod()` + `read()` + `write()`, that's guaranteed to work across all UNIX-like systems, and perhaps even Windows.
  • What is each platform's fastest solution to copy a file with attributes?
#4: Post edited by user avatar Melkor-1‭ · 2024-06-19T09:53:59Z (5 months ago)
  • Is Linux's 3 syscalls, and FreeBSD's and Solaris' sendfile function copying file attributes to the new file?
  • What's the fastest way to copy, preserving file attributes, on each platform?
  • I am looking for efficient solutions to copy files platform-dependently and discovered the following functions:
  • * Linux:
  • - `ioctl()` with `FICLONE` (`cp` uses this by default).
  • - `copy_file_range()`.
  • - `sendfile()`.
  • * Solaris:
  • - `sendfile()`.
  • * MacOS:
  • - `clonefile()`. (Apple's `cp -c` uses this).
  • - `fcopyfile()`.
  • * FreeBSD:
  • - `copy_file_range()`.
  • - `fcopyfile()`.
  • * Windows:
  • - `Copyfile()`.
  • Now Windows's [`Copyfile()`](https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-copyfile?redirectedfrom=MSDN) documents that it copies file attributes to the new file. MacOS's [`clonefile()`](https://www.unix.com/man-page/mojave/2/clonefile/) also documents the same. FreeBSD's and MacOs's `fcopyfile()` will also copy file attribute when specified proper flags.
  • But what about Linux's 3 syscalls, and FreeBSD's and Solaris' `sendfile()`? I did not see it documented in their respective manual pages. Do they copy file attributes as well, or do I have to resort to `stat()`/`fstat()` followed by `chmod()`/`fchmod()` prior to these syscalls to achieve the desired result?
  • I am looking copy files platform-dependently on Linux, Oracle Solaris, MacOS, BSDs, and Windows.
  • I have found some system calls for each platform, namely `sendfile()`, `copy_file_range()`, `fcopyfile()`, `CopyFile()` et cetera. But some of them are documented to copy file attributes, whilst some are not. I am uncertain whether better to do a `stat()` + `chmod()` for syscalls that do not copy attributes, or go with the classic route of `open()` + `stat()` + `chmod()` + `read()` + `write()`, that's guaranteed to work across all UNIX-like systems, and perhaps even Windows.
  • What is each platform's fastest solution to copy a file with attributes?
#3: Post edited by user avatar Melkor-1‭ · 2024-06-19T08:41:43Z (5 months ago)
  • Do these platform-dependent file copying functions copy file attributes?
  • Is Linux's 3 syscalls, and FreeBSD's and Solaris' sendfile function copying file attributes to the new file?
#2: Post edited by user avatar Melkor-1‭ · 2024-06-19T04:18:03Z (5 months ago)
  • Do these platform-dependent efficient file copying functions copy file attributes?
  • Do these platform-dependent file copying functions copy file attributes?
#1: Initial revision by user avatar Melkor-1‭ · 2024-06-19T04:17:34Z (5 months ago)
Do these platform-dependent efficient file copying functions copy file attributes?
I am looking for efficient solutions to copy files platform-dependently and discovered the following functions:

* Linux:
    - `ioctl()` with `FICLONE` (`cp` uses this by default).
    - `copy_file_range()`.
    - `sendfile()`.
    
* Solaris:
    - `sendfile()`.

* MacOS:
    - `clonefile()`. (Apple's `cp -c` uses this).
    - `fcopyfile()`.

* FreeBSD:
    - `copy_file_range()`.
    - `fcopyfile()`.

* Windows:
    - `Copyfile()`.

Now Windows's [`Copyfile()`](https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-copyfile?redirectedfrom=MSDN) documents that it copies file attributes to the new file. MacOS's [`clonefile()`](https://www.unix.com/man-page/mojave/2/clonefile/) also documents the same. FreeBSD's and MacOs's `fcopyfile()` will also copy file attribute when specified proper flags.

But what about Linux's 3 syscalls, and FreeBSD's and Solaris' `sendfile()`? I did not see it documented in their respective manual pages. Do they copy file attributes as well, or do I have to resort to `stat()`/`fstat()` followed by `chmod()`/`fchmod()` prior to these syscalls to achieve the desired result?