mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-02-04 20:46:28 +01:00
replace fwrite etc with write so fsync can be added before close.
This commit is contained in:
parent
bcf767d66e
commit
553bee4956
1 changed files with 10 additions and 6 deletions
|
@ -105,7 +105,7 @@ void
|
|||
writeToFile( XWStreamCtxt* stream, void* closure )
|
||||
{
|
||||
void* buf;
|
||||
FILE* file;
|
||||
int fd;
|
||||
XP_U16 len;
|
||||
CommonGlobals* cGlobals = (CommonGlobals*)closure;
|
||||
|
||||
|
@ -113,17 +113,21 @@ writeToFile( XWStreamCtxt* stream, void* closure )
|
|||
buf = malloc( len );
|
||||
stream_getBytes( stream, buf, len );
|
||||
|
||||
file = fopen( cGlobals->params->fileName, "w" );
|
||||
if ( !file ) {
|
||||
XP_LOGF( "%s: fopen => %d (%s)", __func__, errno, strerror(errno) );
|
||||
fd = open( cGlobals->params->fileName, O_CREAT|O_TRUNC|O_WRONLY,
|
||||
S_IRUSR|S_IWUSR );
|
||||
if ( fd < 0 ) {
|
||||
XP_LOGF( "%s: open => %d (%s)", __func__, errno, strerror(errno) );
|
||||
} else {
|
||||
if ( 1 == fwrite( buf, len, 1, file ) ) {
|
||||
ssize_t nWritten = write( fd, buf, len );
|
||||
if ( len == nWritten ) {
|
||||
XP_LOGF( "%s: wrote %d bytes to %s", __func__, len,
|
||||
cGlobals->params->fileName );
|
||||
} else {
|
||||
XP_LOGF( "%s: write => %s", __func__, strerror(errno) );
|
||||
XP_ASSERT( 0 );
|
||||
}
|
||||
fclose( file );
|
||||
fsync( fd );
|
||||
close( fd );
|
||||
}
|
||||
|
||||
free( buf );
|
||||
|
|
Loading…
Add table
Reference in a new issue