Commit 7880525f authored by Piotr Caban's avatar Piotr Caban Committed by Alexandre Julliard

msvcrt: Handle read buffer flushing in msvcrt_flush_buffer helper.

parent 52171a8b
......@@ -606,16 +606,18 @@ void msvcrt_init_io(void)
/* INTERNAL: Flush stdio file buffer */
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;
if(cnt>0 && MSVCRT__write(file->_file, file->_base, cnt) != cnt) {
file->_flag |= MSVCRT__IOERR;
return MSVCRT_EOF;
}
file->_ptr=file->_base;
file->_cnt=0;
}
return 0;
}
file->_ptr=file->_base;
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 res;
if(!file) {
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;
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;
/* Flush output if needed */
if(file->_flag & MSVCRT__IOWRT)
msvcrt_flush_buffer(file);
if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) {
whence = SEEK_SET;
offset += MSVCRT__ftelli64_nolock(file);
}
/* Discard buffered input */
file->_cnt = 0;
file->_ptr = file->_base;
/* Flush output if needed */
msvcrt_flush_buffer(file);
/* Reset direction of i/o */
if(file->_flag & MSVCRT__IORW) {
file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
......@@ -4369,14 +4360,8 @@ int CDECL MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos)
int ret;
MSVCRT__lock_file(file);
/* Note that all this has been lifted 'as is' from fseek */
if(file->_flag & MSVCRT__IOWRT)
msvcrt_flush_buffer(file);
msvcrt_flush_buffer(file);
/* Discard buffered input */
file->_cnt = 0;
file->_ptr = file->_base;
/* Reset direction of i/o */
if(file->_flag & MSVCRT__IORW) {
file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT);
......
......@@ -2244,6 +2244,7 @@ static void test_write_flush_size(FILE *file, int bufsize)
char *inbuffer;
char *outbuffer;
int size, fd;
fpos_t pos, pos2;
fd = fileno(file);
inbuffer = calloc(bufsize + 1, 1);
......@@ -2275,6 +2276,19 @@ static void test_write_flush_size(FILE *file, int bufsize)
fseek(file, 0, SEEK_SET);
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(!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(outbuffer);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment