• Re: how copy file on linux?

    From Scott Lurndal@21:1/5 to Thiago Adams on Thu Jun 27 01:14:11 2024
    Thiago Adams <[email protected]> writes:
    Em 6/26/2024 8:24 PM, Lew Pitcher escreveu:
    (Followup set to comp.unix.programmer)

    On Wed, 26 Jun 2024 15:35:00 -0700, Keith Thompson wrote:

    Thiago Adams <[email protected]> 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.

    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.


    On linux I may need to copy some files and I think is is a good idea to
    keep the original attributes as well.



    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to Scott Lurndal on Thu Jun 27 03:32:10 2024
    On 2024-06-27, Scott Lurndal <[email protected]> wrote:
    Thiago Adams <[email protected]> writes:
    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: @[email protected]

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paul@21:1/5 to Scott Lurndal on Thu Jun 27 00:11:43 2024
    On 6/26/2024 9:14 PM, Scott Lurndal wrote:
    Thiago Adams <[email protected]> writes:
    Em 6/26/2024 8:24 PM, Lew Pitcher escreveu:
    (Followup set to comp.unix.programmer)

    On Wed, 26 Jun 2024 15:35:00 -0700, Keith Thompson wrote:

    Thiago Adams <[email protected]> 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.

    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to Thiago Adams on Thu Jun 27 08:05:20 2024
    On 27.06.2024 02:40, Thiago Adams wrote:
    Em 6/26/2024 8:24 PM, Lew Pitcher escreveu:
    (Followup set to comp.unix.programmer)

    On Wed, 26 Jun 2024 15:35:00 -0700, Keith Thompson wrote:

    Thiago Adams <[email protected]> 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.

    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From candycanearter07@21:1/5 to Scott Lurndal on Thu Jun 27 06:20:03 2024
    Scott Lurndal <[email protected]> wrote at 01:14 this Thursday (GMT):
    Thiago Adams <[email protected]> writes:
    Em 6/26/2024 8:24 PM, Lew Pitcher escreveu:
    (Followup set to comp.unix.programmer)

    On Wed, 26 Jun 2024 15:35:00 -0700, Keith Thompson wrote:

    Thiago Adams <[email protected]> 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.

    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)

    Windows, well 'nuf said.

    I think the /newest/ version of W11 has tar support built in. Not that
    W11 is any good.


    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lew Pitcher@21:1/5 to Keith Thompson on Thu Jun 27 12:30:42 2024
    On Wed, 26 Jun 2024 21:07:19 -0700, Keith Thompson wrote:

    Lew Pitcher <[email protected]> writes:
    (Followup set to comp.unix.programmer)

    On Wed, 26 Jun 2024 15:35:00 -0700, Keith Thompson wrote:
    Thiago Adams <[email protected]> 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"

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lawrence D'Oliveiro@21:1/5 to Lew Pitcher on Fri Jun 28 00:22:24 2024
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)