How to copy a file on linux keeping the original file information?
timestamp etc?
Or in other words the equivalent of CopyFileA from windows.
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.
If I uunderstand correctly, you want to do the equivalent of "cp -p",
but from C rather than from a shell. You might consider using system(),
but that has some drawbacks, and there are probably better ways.
How to copy a file on linux keeping the original file information?
timestamp etc?
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.
I tried to implement that in C++. Unfortunately an error branch of the
code is taken. What Do I have to change ?
On Sat, 29 Jun 2024 00:04:00 +0100, Malcolm McLean wrote:a FAILURE (and return a "false" from the function)
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.
On 28/06/2024 19:15, Bonita Montero wrote:
int fromFile = open( from, O_RDONLY | O_NOATIME );
if( !fromFile )
return false;
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.
| Sysop: | Keyop |
|---|---|
| Location: | Huddersfield, West Yorkshire, UK |
| Users: | 715 |
| Nodes: | 16 (2 / 14) |
| Uptime: | 145:39:07 |
| Calls: | 12,089 |
| Calls today: | 2 |
| Files: | 15,000 |
| Messages: | 6,517,497 |