Discussion:
how copy file on linux?
(too old to reply)
Thiago Adams
2024-06-27 00:40:06 UTC
Permalink
(Followup set to comp.unix.programmer)
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.
If I uunderstand correctly, you want to do the equivalent of "cp -p",
Which /can/ be accomplished with POSIX calls
- the standard I/O functions (fopen()/open(), fread()/read(),
fwrite()/write(), fclose()/close() ) to copy the file data
- stat() to obtain the "original file information", and
- utime() to set the copy timestamps,
- chmod() to set the copy file permissions
- chown() to set the file ownership
But, perhaps Thiago would be satisfied with just a hardlinked file.
but from C rather than from a shell. You might consider using system(),
but that has some drawbacks, and there are probably better ways.
I am using this to create a "deploy" folder with all files necessary to
run a program. So on windows I am coping dlls and I think it is a good
idea to keep the original file attributes . (I am not sure if zip files
keep that.. I need to check)

On linux I may need to copy some files and I think is is a good idea to
keep the original attributes as well.
Scott Lurndal
2024-06-27 01:14:11 UTC
Permalink
Post by Thiago Adams
(Followup set to comp.unix.programmer)
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.
If I uunderstand correctly, you want to do the equivalent of "cp -p",
Which /can/ be accomplished with POSIX calls
- the standard I/O functions (fopen()/open(), fread()/read(),
fwrite()/write(), fclose()/close() ) to copy the file data
- stat() to obtain the "original file information", and
- utime() to set the copy timestamps,
- chmod() to set the copy file permissions
- chown() to set the file ownership
But, perhaps Thiago would be satisfied with just a hardlinked file.
but from C rather than from a shell. You might consider using system(),
but that has some drawbacks, and there are probably better ways.
I am using this to create a "deploy" folder with all files necessary to
run a program. So on windows I am coping dlls and I think it is a good
idea to keep the original file attributes . (I am not sure if zip files
keep that.. I need to check)
Both the tar(1) and cpio(1) utilities can be used to create a
compressed deployable archive. They're the most portable way
to distribute anything and they keep and restore the attributes
(although the ownership attributes uid and gid can only be
restored by root if they differ from the current users).


Windows, well 'nuf said.
Post by Thiago Adams
On linux I may need to copy some files and I think is is a good idea to
keep the original attributes as well.
Kaz Kylheku
2024-06-27 03:32:10 UTC
Permalink
Post by Scott Lurndal
Post by Thiago Adams
I am using this to create a "deploy" folder with all files necessary to
run a program. So on windows I am coping dlls and I think it is a good
idea to keep the original file attributes . (I am not sure if zip files
keep that.. I need to check)
Both the tar(1) and cpio(1) utilities can be used to create a
compressed deployable archive. They're the most portable way
to distribute anything and they keep and restore the attributes
(although the ownership attributes uid and gid can only be
restored by root if they differ from the current users).
Windows, well 'nuf said.
On Windows, use an installer generator like NSIS.
--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @***@mstdn.ca
Paul
2024-06-27 04:11:43 UTC
Permalink
Post by Scott Lurndal
Post by Thiago Adams
(Followup set to comp.unix.programmer)
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.
If I uunderstand correctly, you want to do the equivalent of "cp -p",
Which /can/ be accomplished with POSIX calls
- the standard I/O functions (fopen()/open(), fread()/read(),
fwrite()/write(), fclose()/close() ) to copy the file data
- stat() to obtain the "original file information", and
- utime() to set the copy timestamps,
- chmod() to set the copy file permissions
- chown() to set the file ownership
But, perhaps Thiago would be satisfied with just a hardlinked file.
but from C rather than from a shell. You might consider using system(),
but that has some drawbacks, and there are probably better ways.
I am using this to create a "deploy" folder with all files necessary to
run a program. So on windows I am coping dlls and I think it is a good
idea to keep the original file attributes . (I am not sure if zip files
keep that.. I need to check)
Both the tar(1) and cpio(1) utilities can be used to create a
compressed deployable archive. They're the most portable way
to distribute anything and they keep and restore the attributes
(although the ownership attributes uid and gid can only be
restored by root if they differ from the current users).
Windows, well 'nuf said.
For Windows, you need more than a USENETter.

I know of some programmers who have "carved out a niche", but, they
haven't tried all the packaging methods. They may not make good
candidates for bootstrapping (they may not know the absolute best
install method). It is difficult to find people "who have tried it all".

In the past, these guys were FOSS and they only did enough Windows
to get by. This is their page. MSI is a semi-popular option for
packaging (pretty complicated), and MSIX is the latest version
(not all that popular, only Windows might ship materials like that).

"NSIS"

https://firefox-source-docs.mozilla.org/browser/installer/windows/installer/index.html

That's not going to teach you very much. I don't know whether the installer
scripts are in the Firefox tarball or not. Some of their materials
are kept private.

When msiexec installs the materials in an MSI file, the dates and so on
would be preserved. If you compiled in 2015, the 2015 date is going to be
on the file when it is installed on my machine.

An example of a preservative copy routine, would be the Windows-bundled "robocopy.exe",
which was an employee programming effort that eventually went from
hobby-ware, to being fully supported in the OS. The "DATSO" option
would make a verbatim copy of a 2015 executable for example, nicely
stamped as 2015.

But these folders, these are owned by TrustedInstaller:

C:\Program Files
C:\Program Files (x86)

and you have to copy the TrustedInstaller token from a running "msiexec",
to have TrustedInstaller privileges and write to those folders. You can
attempt to write there, and your writes are "redirected". Without going
into more detail, "bootleg" copying really isn't supported, and even if
you developed a recipe, it could be broken tomorrow because it is not
supported. Nobody does installs using Robocopy.

I have a program that used to be able to make a program
"trusted installer", but that program as well as psexec, no longer work.
Similarly, some of the easy holes that allowed you to hack administrator,
no longer work (I helped one guy on USENET regain his administrator
account using one of those holes -- users can erase Administrator and
lose control!). That is a very nice thing for tech support purposes,
for remote work, but they've been steadily whittling away at the holes.
It means, if I know of one that still works, I have
to be careful to not "advertise it". Or they'll close the hole.

This is why installer packages are popular, as they grind down the
sharp edges for you. Using an actual installer, a popular installer,
Microsoft likely try not to break those.

My mention of Robocopy, is only so you can see a program make
a verbatim copy for you. Robocopy is really a folder-copying program,
so you have to be aware of the purpose, to craft a good syntax.
Be very careful with options such as /MIR (mirror), as mirror has
been used to erase the destination folder. It can do terrible damage,
and do it quickly, with a mis-crafted command.

Manual page:

https://ss64.com/nt/robocopy.html

Actually using it:

https://serverfault.com/questions/420105/robocopy-and-permissions

Paul
candycanearter07
2024-06-27 06:20:03 UTC
Permalink
Post by Scott Lurndal
Post by Thiago Adams
(Followup set to comp.unix.programmer)
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.
If I uunderstand correctly, you want to do the equivalent of "cp -p",
Which /can/ be accomplished with POSIX calls
- the standard I/O functions (fopen()/open(), fread()/read(),
fwrite()/write(), fclose()/close() ) to copy the file data
- stat() to obtain the "original file information", and
- utime() to set the copy timestamps,
- chmod() to set the copy file permissions
- chown() to set the file ownership
But, perhaps Thiago would be satisfied with just a hardlinked file.
but from C rather than from a shell. You might consider using system(),
but that has some drawbacks, and there are probably better ways.
I am using this to create a "deploy" folder with all files necessary to
run a program. So on windows I am coping dlls and I think it is a good
idea to keep the original file attributes . (I am not sure if zip files
keep that.. I need to check)
Both the tar(1) and cpio(1) utilities can be used to create a
compressed deployable archive. They're the most portable way
to distribute anything and they keep and restore the attributes
(although the ownership attributes uid and gid can only be
restored by root if they differ from the current users).
You do have to specify it for tar tho (-p for preserve permissions)
Post by Scott Lurndal
Windows, well 'nuf said.
I think the /newest/ version of W11 has tar support built in. Not that
W11 is any good.
Post by Scott Lurndal
Post by Thiago Adams
On linux I may need to copy some files and I think is is a good idea to
keep the original attributes as well.
--
user <candycane> is generated from /dev/urandom
Keith Thompson
2024-06-27 04:07:19 UTC
Permalink
(Followup set to comp.unix.programmer)
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+***@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Lew Pitcher
2024-06-27 12:30:42 UTC
Permalink
Post by Keith Thompson
(Followup set to comp.unix.programmer)
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.
Post by Keith Thompson
(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"
Lawrence D'Oliveiro
2024-06-28 00:22:24 UTC
Permalink
Post by Lew Pitcher
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.

Janis Papanagnou
2024-06-27 06:05:20 UTC
Permalink
Post by Thiago Adams
(Followup set to comp.unix.programmer)
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.
If I uunderstand correctly, you want to do the equivalent of "cp -p",
Which /can/ be accomplished with POSIX calls
- the standard I/O functions (fopen()/open(), fread()/read(),
fwrite()/write(), fclose()/close() ) to copy the file data
- stat() to obtain the "original file information", and
- utime() to set the copy timestamps,
- chmod() to set the copy file permissions
- chown() to set the file ownership
But, perhaps Thiago would be satisfied with just a hardlinked file.
but from C rather than from a shell. You might consider using system(),
but that has some drawbacks, and there are probably better ways.
I am using this to create a "deploy" folder with all files necessary to
run a program. So on windows I am coping dlls and I think it is a good
idea to keep the original file attributes . (I am not sure if zip files
keep that.. I need to check)
On linux I may need to copy some files and I think is is a good idea to
keep the original attributes as well.
This sounds like you want some shell command line tool or shell script.

IME, to "deploy" a package I have an own directory structure. So 'cp -p'
is the key command (or 'cp -a' if you also have whole directories to be
copied with all contents); this will preserve attributes on the file
system. To preserve them in the bundled package I use the 'tar' command
(that also preserves attributes, as far as possible); e.g. 'tar cvzf'
(see man page for details on options).

Janis
Loading...