Re: how copy file on linux?

Liste des GroupesRevenir à cl c  
Sujet : Re: how copy file on linux?
De : Bonita.Montero (at) *nospam* gmail.com (Bonita Montero)
Groupes : comp.lang.c
Date : 28. Jun 2024, 20:15:20
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v5mujo$3f10p$1@raubtier-asyl.eternal-september.org>
References : 1
User-Agent : Mozilla Thunderbird
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.

Date Sujet#  Auteur
26 Jun 24 * how copy file on linux?10Thiago Adams
26 Jun 24 +- Re: how copy file on linux?1Janis Papanagnou
27 Jun 24 +* Re: how copy file on linux?2Keith Thompson
27 Jun 24 i`- Re: how copy file on linux?1Lew Pitcher
27 Jun 24 +* Re: how copy file on linux?2Lawrence D'Oliveiro
28 Jun 24 i`- Re: how copy file on linux?1Bonita Montero
28 Jun 24 `* Re: how copy file on linux?4Bonita Montero
29 Jun 24  `* Re: how copy file on linux?3Malcolm McLean
29 Jun 24   `* Re: how copy file on linux?2Lew Pitcher
29 Jun 24    `- Re: how copy file on linux?1Lew Pitcher

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal