• Re: how copy file on linux?

    From Janis Papanagnou@21:1/5 to Thiago Adams on Wed Jun 26 23:48:15 2024
    On 26.06.2024 22:44, Thiago Adams wrote:
    How to copy a file on linux keeping the original file information?
    timestamp etc?

    This is an OS question and I'm not sure you want the shell command
    or the library function.

    On a Linux shell I do

    cp -p # to preserve attributes
    cp -a # incl. -p, to do a lot like copying recursive a directory


    Or in other words the equivalent of CopyFileA from windows.

    And what exactly does it do? (Windows has - for example, since you
    mentioned time stamps above - other time information than Unix
    systems. So it may not be what you expect.)

    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lew Pitcher@21:1/5 to Keith Thompson on Wed Jun 26 23:24:47 2024
    (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.


    HTH
    --
    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 Thiago Adams on Wed Jun 26 23:43:04 2024
    On Wed, 26 Jun 2024 17:44:48 -0300, Thiago Adams wrote:

    How to copy a file on linux keeping the original file information?
    timestamp etc?

    Last-modified is usually the only timestamp people care about.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Scott Lurndal@21:1/5 to Lawrence D'Oliveiro on Thu Jun 27 00:36:17 2024
    Lawrence D'Oliveiro <[email protected]d> writes:
    On Wed, 26 Jun 2024 17:44:48 -0300, Thiago Adams wrote:

    How to copy a file on linux keeping the original file information?
    timestamp etc?

    Last-modified is usually the only timestamp people care about.

    Do you think before you post?

    That's not true.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Scott Lurndal@21:1/5 to Bonita Montero on Fri Jun 28 19:22:55 2024
    Bonita Montero <[email protected]> writes:
    I tried to implement that in C++. Unfortunately an error branch of the
    code is taken. What Do I have to change ?

    You need to figure out which error branch is taken and
    look at errno and the surrounding logic at that point
    to see why the call failed.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lew Pitcher@21:1/5 to Lew Pitcher on Fri Jun 28 23:27:49 2024
    Oops. missed part of my edit. See unquoted line below

    On Fri, 28 Jun 2024 23:25:20 +0000, Lew Pitcher wrote:

    On Sat, 29 Jun 2024 00:04:00 +0100, Malcolm McLean wrote:

    On 28/06/2024 19:15, Bonita Montero wrote:
    int fromFile = open( from, O_RDONLY | O_NOATIME );
        if( !fromFile )
            return false;

    open() returns -1 on failure, and some non-negative number
    on success. That non-negative number /can be/ 0 and still
    be a valid success value.

    The above test /will not/ detect an open() failure, as !(-1) == 0
    However, the above /will/ falsely call
    a FAILURE (and return a "false" from the function)
    a valid open() that returns
    fd 0 (slim chance unless you've previously closed stdin).


       invoke_on_destruct closeFrom( [&] { close( fromFile ); } );
    //    int toFile = open( to, O_CREAT );
    //    if( !toFile )
    //        return false;
        invoke_on_destruct closeTo( [&] { close( toFile ); } );
    //     invoke_on_destruct delTo( [&] { unlink( to ); } );
        for( int64_t remaining = attrFrom.st_size; remaining > 0; remaining
    -= 0x100000 )
        {
            size_t n = (size_t)(remaining >= 0x100000 ? 0x100000 : remaining);
            buf.resize( n );
            if( read( fromFile, buf.data(), n ) )
            {
                // this branch is taken
                cout << strerror( errno ) << endl;
                return false;
            }

    So it claims the file opens, then won't read it?

    Check the call. Is 0 the success return?

    Then I must admit I'm stumped. "to" doesn't alias "from" I suppose?

    I'd also wonder if somehow the C++ compiler has invoked the on destruct
    code, and closed the file.




    --
    Lew Pitcher
    "In Skills We Trust"

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lew Pitcher@21:1/5 to Malcolm McLean on Fri Jun 28 23:25:20 2024
    On Sat, 29 Jun 2024 00:04:00 +0100, Malcolm McLean wrote:

    On 28/06/2024 19:15, Bonita Montero wrote:
    int fromFile = open( from, O_RDONLY | O_NOATIME );
        if( !fromFile )
            return false;

    open() returns -1 on failure, and some non-negative number
    on success. That non-negative number /can be/ 0 and still
    be a valid success value.

    The above test /will not/ detect an open() failure, as !(-1) == 0
    However, the above /will/ falsely call a valid open() that returns
    fd 0 (slim chance unless you've previously closed stdin).


       invoke_on_destruct closeFrom( [&] { close( fromFile ); } );
    //    int toFile = open( to, O_CREAT );
    //    if( !toFile )
    //        return false;
        invoke_on_destruct closeTo( [&] { close( toFile ); } );
    //     invoke_on_destruct delTo( [&] { unlink( to ); } );
        for( int64_t remaining = attrFrom.st_size; remaining > 0; remaining >> -= 0x100000 )
        {
            size_t n = (size_t)(remaining >= 0x100000 ? 0x100000 : remaining);
            buf.resize( n );
            if( read( fromFile, buf.data(), n ) )
            {
                // this branch is taken
                cout << strerror( errno ) << endl;
                return false;
            }

    So it claims the file opens, then won't read it?

    Check the call. Is 0 the success return?

    Then I must admit I'm stumped. "to" doesn't alias "from" I suppose?

    I'd also wonder if somehow the C++ compiler has invoked the on destruct
    code, and closed the file.




    --
    Lew Pitcher
    "In Skills We Trust"

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