Eric Blake wrote: > > Why not test both? Like this proposed patch. ... > > Looks good to me. Maybe we should also test reopening file in O_RDONLY > mode, since fsync on a tty is much different than fsync on a regular file.
The idea of checking fd 0 is that it's likely in O_RDONLY mode. > POSIX requires fsync to work on read-only regular files, but I'm not > sure if all implementations support that (if they don't, we could > probably fake it by doing a no-op; read-only files can't be changing > anything but atime metadata, which isn't the end of the world if it is > not sync'd). Feel free to commit it. Testing will tell us which platforms are affected... 2011-09-16 Bruno Haible <br...@clisp.org> Enhance fsync, fdatasync tests. * tests/test-fsync.c (main): Test both STDIN_FILENO and STDOUT_FILENO. * tests/test-fdatasync.c (main): Likewise. --- tests/test-fdatasync.c.orig Fri Sep 16 23:52:22 2011 +++ tests/test-fdatasync.c Fri Sep 16 23:52:04 2011 @@ -32,21 +32,25 @@ int fd; const char *file = "test-fdatasync.txt"; - if (fdatasync (STDOUT_FILENO) != 0) - { - ASSERT (errno == EINVAL /* POSIX */ - || errno == ENOTSUP /* seen on MacOS X 10.5 */ - || errno == EBADF /* seen on AIX 7.1 */ - ); - } + for (fd = 0; fd < 2; fd++) + if (fdatasync (fd) != 0) + { + ASSERT (errno == EINVAL /* POSIX */ + || errno == ENOTSUP /* seen on MacOS X 10.5 */ + || errno == EBADF /* seen on AIX 7.1 */ + ); + } + errno = 0; ASSERT (fdatasync (-1) == -1); ASSERT (errno == EBADF); + fd = open (file, O_WRONLY|O_CREAT|O_TRUNC, 0644); ASSERT (0 <= fd); ASSERT (write (fd, "hello", 5) == 5); ASSERT (fdatasync (fd) == 0); ASSERT (close (fd) == 0); + #if 0 /* POSIX is self-contradictory on whether fdatasync must fail on read-only file descriptors. Glibc allows it, as does our @@ -58,6 +62,7 @@ ASSERT (errno == EBADF); ASSERT (close (fd) == 0); #endif + ASSERT (unlink (file) == 0); return 0; --- tests/test-fsync.c.orig Fri Sep 16 23:52:22 2011 +++ tests/test-fsync.c Fri Sep 16 22:34:09 2011 @@ -32,16 +32,19 @@ int fd; const char *file = "test-fsync.txt"; - if (fsync (STDOUT_FILENO) != 0) - { - ASSERT (errno == EINVAL /* POSIX */ - || errno == ENOTSUP /* seen on MacOS X 10.5 */ - || errno == EBADF /* seen on AIX 7.1 */ - ); - } + for (fd = 0; fd < 2; fd++) + if (fsync (fd) != 0) + { + ASSERT (errno == EINVAL /* POSIX */ + || errno == ENOTSUP /* seen on MacOS X 10.5 */ + || errno == EBADF /* seen on AIX 7.1 */ + ); + } + errno = 0; ASSERT (fsync (-1) == -1); ASSERT (errno == EBADF); + fd = open (file, O_WRONLY|O_CREAT|O_TRUNC, 0644); ASSERT (0 <= fd); ASSERT (write (fd, "hello", 5) == 5); -- In memoriam Georgiy Gongadze <http://en.wikipedia.org/wiki/Georgiy_Gongadze>