I tried to implement that in C++. Unfortunately an error branch of the
code is taken. What Do I have to change ?
bool CopyFileA( char const *from, char const *to, bool overwrite )
{
struct stat attrFrom, attrTo;
if( stat( from, &attrFrom ) )
return false;
if( !overwrite && !stat( to, &attrTo ) && errno != ENOENT )
return false;
union ndi { char c; };
vector<char> buf( attrFrom.st_size >= 0x100000 ? 0x100000 :
attrFrom.st_size );
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;
}
if( write( toFile, buf.data(), n ) )
return false;
}
closeTo();
utimbuf utb;
utb.actime = attrFrom.st_atime;
utb.modtime = attrFrom.st_mtime;
if( utime( to, &utb ) )
return false;
if( chmod( to, attrFrom.st_mode ) )
return false;
if( chown( to, attrFrom.st_uid, attrFrom.st_gid ) && errno != EACCES )
return false;
delTo.disable();
return to;
}
If everything works fine at last I'll make a C-rewrite.