msvcrt: Handle read buffer flushing in msvcrt_flush_buffer helper.

This commit is contained in:
Piotr Caban 2015-01-23 17:27:23 +01:00 committed by Alexandre Julliard
parent 52171a8b15
commit 7880525f44
2 changed files with 31 additions and 32 deletions

View File

@ -606,16 +606,18 @@ void msvcrt_init_io(void)
/* INTERNAL: Flush stdio file buffer */ /* INTERNAL: Flush stdio file buffer */
static int msvcrt_flush_buffer(MSVCRT_FILE* file) static int msvcrt_flush_buffer(MSVCRT_FILE* file)
{ {
if(file->_flag & (MSVCRT__IOMYBUF | MSVCRT__USERBUF)) { if((file->_flag & (MSVCRT__IOREAD|MSVCRT__IOWRT)) == MSVCRT__IOWRT &&
file->_flag & (MSVCRT__IOMYBUF|MSVCRT__USERBUF)) {
int cnt=file->_ptr-file->_base; int cnt=file->_ptr-file->_base;
if(cnt>0 && MSVCRT__write(file->_file, file->_base, cnt) != cnt) { if(cnt>0 && MSVCRT__write(file->_file, file->_base, cnt) != cnt) {
file->_flag |= MSVCRT__IOERR; file->_flag |= MSVCRT__IOERR;
return MSVCRT_EOF; return MSVCRT_EOF;
} }
file->_ptr=file->_base; }
file->_cnt=0;
} file->_ptr=file->_base;
return 0; file->_cnt=0;
return 0;
} }
/********************************************************************* /*********************************************************************
@ -964,23 +966,17 @@ int CDECL MSVCRT_fflush(MSVCRT_FILE* file)
*/ */
int CDECL MSVCRT__fflush_nolock(MSVCRT_FILE* file) int CDECL MSVCRT__fflush_nolock(MSVCRT_FILE* file)
{ {
int res;
if(!file) { if(!file) {
msvcrt_flush_all_buffers(MSVCRT__IOWRT); msvcrt_flush_all_buffers(MSVCRT__IOWRT);
} else if(file->_flag & MSVCRT__IOWRT) {
int res;
res = msvcrt_flush_buffer(file);
if(!res && (file->_flag & MSVCRT__IOCOMMIT))
res = MSVCRT__commit(file->_file) ? MSVCRT_EOF : 0;
return res;
} else if(file->_flag & MSVCRT__IOREAD) {
file->_cnt = 0;
file->_ptr = file->_base;
return 0; return 0;
} }
return 0;
res = msvcrt_flush_buffer(file);
if(!res && (file->_flag & MSVCRT__IOCOMMIT))
res = MSVCRT__commit(file->_file) ? MSVCRT_EOF : 0;
return res;
} }
/********************************************************************* /*********************************************************************
@ -1333,18 +1329,13 @@ int CDECL MSVCRT__fseeki64_nolock(MSVCRT_FILE* file, __int64 offset, int whence)
{ {
int ret; int ret;
/* Flush output if needed */
if(file->_flag & MSVCRT__IOWRT)
msvcrt_flush_buffer(file);
if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) { if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
whence = SEEK_SET; whence = SEEK_SET;
offset += MSVCRT__ftelli64_nolock(file); offset += MSVCRT__ftelli64_nolock(file);
} }
/* Discard buffered input */ /* Flush output if needed */
file->_cnt = 0; msvcrt_flush_buffer(file);
file->_ptr = file->_base;
/* Reset direction of i/o */ /* Reset direction of i/o */
if(file->_flag & MSVCRT__IORW) { if(file->_flag & MSVCRT__IORW) {
file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT); file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
@ -4369,14 +4360,8 @@ int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
int ret; int ret;
MSVCRT__lock_file(file); MSVCRT__lock_file(file);
/* Note that all this has been lifted 'as is' from fseek */ msvcrt_flush_buffer(file);
if(file->_flag & MSVCRT__IOWRT)
msvcrt_flush_buffer(file);
/* Discard buffered input */
file->_cnt = 0;
file->_ptr = file->_base;
/* Reset direction of i/o */ /* Reset direction of i/o */
if(file->_flag & MSVCRT__IORW) { if(file->_flag & MSVCRT__IORW) {
file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT); file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);

View File

@ -2244,6 +2244,7 @@ static void test_write_flush_size(FILE *file, int bufsize)
char *inbuffer; char *inbuffer;
char *outbuffer; char *outbuffer;
int size, fd; int size, fd;
fpos_t pos, pos2;
fd = fileno(file); fd = fileno(file);
inbuffer = calloc(bufsize + 1, 1); inbuffer = calloc(bufsize + 1, 1);
@ -2275,6 +2276,19 @@ static void test_write_flush_size(FILE *file, int bufsize)
fseek(file, 0, SEEK_SET); fseek(file, 0, SEEK_SET);
ok(fread(inbuffer, 1, bufsize, file) == bufsize, "read failed\n"); ok(fread(inbuffer, 1, bufsize, file) == bufsize, "read failed\n");
ok(memcmp(outbuffer, inbuffer, bufsize) != 0, "unexpected flush by %d/2 byte double write\n", bufsize); ok(memcmp(outbuffer, inbuffer, bufsize) != 0, "unexpected flush by %d/2 byte double write\n", bufsize);
ok(!fseek(file, -1, SEEK_END), "fseek failed\n");
ok(!fgetpos(file, &pos), "fgetpos failed\n");
ok(fread(inbuffer, 1, 1, file) == 1, "fread failed\n");
ok(file->_flag & _IOREAD, "file->_flag = %x\n", file->_flag);
ok(!file->_cnt, "file->_cnt = %d\n", file->_cnt);
ok(file->_ptr != file->_base, "file->_ptr == file->_base\n");
ok(fwrite(outbuffer, 1, bufsize, file), "fwrite failed\n");
ok(file->_flag & _IOREAD, "file->_flag = %x\n", file->_flag);
ok(!file->_cnt, "file->_cnt = %d\n", file->_cnt);
ok(file->_ptr == file->_base, "file->_ptr == file->_base\n");
ok(!fgetpos(file, &pos2), "fgetpos failed\n");
ok(pos+bufsize+1 == pos2, "pos = %d (%d)\n", (int)pos, (int)pos2);
free(inbuffer); free(inbuffer);
free(outbuffer); free(outbuffer);
} }