• Russ Cox's avatar
    syscall: ignore EINVAL/ENOENT from readdirent on OS X 10.10 · a83bbc9c
    Russ Cox authored
    On OS X 10.10 Yosemite, if you have a directory that can be returned
    in a single getdirentries64 call (for example, a directory with one file),
    and you read from the directory at EOF twice, you get EOF both times:
            fd = open("dir")
            getdirentries64(fd) returns data
            getdirentries64(fd) returns 0 (EOF)
            getdirentries64(fd) returns 0 (EOF)
    
    But if you remove the file in the middle between the two calls, the
    second call returns an error instead.
            fd = open("dir")
            getdirentries64(fd) returns data
            getdirentries64(fd) returns 0 (EOF)
            remove("dir/file")
            getdirentries64(fd) returns ENOENT/EINVAL
    
    Whether you get ENOENT or EINVAL depends on exactly what was
    in the directory. It is deterministic, just data-dependent.
    
    This only happens in small directories. A directory containing more data
    than fits in a 4k getdirentries64 call will return EOF correctly.
    (It's not clear if the criteria is that the directory be split across multiple
    getdirentries64 calls or that it be split across multiple file system blocks.)
    
    We could change package os to avoid the second read at EOF,
    and maybe we should, but that's a bit involved.
    For now, treat the EINVAL/ENOENT as EOF.
    
    With this CL, all.bash passes on my MacBook Air running
    OS X 10.10 (14A299l) and Xcode 6 beta 5 (6A279r).
    
    I tried filing an issue with Apple using "Feedback Assistant", but it was
    unable to send the report and lost it.
    
    C program reproducing the issue, also at http://swtch.com/~rsc/readdirbug.c:
    
    #include <stdio.h>
    #include <dirent.h>
    #include <unistd.h>
    #include <sys/stat.h>
    #include <stdlib.h>
    #include <fcntl.h>
    #include <errno.h>
    #include <string.h>
    
    static void test(int);
    
    int
    main(void)
    {
            int fd, n;
            DIR *dir;
            struct dirent *dp;
            struct stat st;
            char buf[10000];
            long basep;
            int saw;
    
            if(stat("/tmp/readdirbug", &st) >= 0) {
                    fprintf(stderr, "please rm -r /tmp/readdirbug and run again\n");
                    exit(1);
            }
    
            fprintf(stderr, "mkdir /tmp/readdirbug\n");
            if(mkdir("/tmp/readdirbug", 0777) < 0) {
                    perror("mkdir /tmp/readdirbug");
                    exit(1);
            }
    
            fprintf(stderr, "create /tmp/readdirbug/file1\n");
            if((fd = creat("/tmp/readdirbug/file1", 0666)) < 0) {
                    perror("create /tmp/readdirbug/file1");
                    exit(1);
            }
            close(fd);
    
            test(0);
            test(1);
    
            fprintf(stderr, "ok - everything worked\n");
    }
    
    static void
    test(int doremove)
    {
            DIR *dir;
            struct dirent *dp;
            int numeof;
    
            fprintf(stderr, "\n");
            fprintf(stderr, "opendir /tmp/readdirbug\n");
            dir = opendir("/tmp/readdirbug");
            if(dir == 0) {
                    perror("open /tmp/readdirbug");
                    exit(1);
            }
    
            numeof = 0;
            for(;;) {
                    errno = 0;
                    dp = readdir(dir);
                    if(dp != 0) {
                            fprintf(stderr, "readdir: found %s\n", dp->d_name);
                            continue;
                    }
                    if(errno != 0) {
                            perror("readdir");
                            exit(1);
                    }
                    fprintf(stderr, "readdir: EOF\n");
                    if(++numeof == 3)
                            break;
                    if(doremove) {
                            fprintf(stderr, "rm /tmp/readdirbug/file1\n");
                            if(remove("/tmp/readdirbug/file1") < 0) {
                                    perror("remove");
                                    exit(1);
                            }
                    }
            }
            fprintf(stderr, "closedir\n");
            closedir(dir);
    }
    
    Fixes #8423.
    
    LGTM=bradfitz, r
    R=golang-codereviews, bradfitz, dsymonds, dave, r
    CC=golang-codereviews, iant
    https://golang.org/cl/119530044
    a83bbc9c
Name
Last commit
Last update
..
asm_darwin_386.s Loading commit data...
asm_darwin_amd64.s Loading commit data...
asm_dragonfly_386.s Loading commit data...
asm_dragonfly_amd64.s Loading commit data...
asm_freebsd_386.s Loading commit data...
asm_freebsd_amd64.s Loading commit data...
asm_freebsd_arm.s Loading commit data...
asm_linux_386.s Loading commit data...
asm_linux_amd64.s Loading commit data...
asm_linux_arm.s Loading commit data...
asm_nacl_386.s Loading commit data...
asm_nacl_amd64p32.s Loading commit data...
asm_nacl_arm.s Loading commit data...
asm_netbsd_386.s Loading commit data...
asm_netbsd_amd64.s Loading commit data...
asm_netbsd_arm.s Loading commit data...
asm_openbsd_386.s Loading commit data...
asm_openbsd_amd64.s Loading commit data...
asm_plan9_386.s Loading commit data...
asm_plan9_amd64.s Loading commit data...
asm_solaris_amd64.s Loading commit data...
asm_windows_386.s Loading commit data...
asm_windows_amd64.s Loading commit data...
bpf_bsd.go Loading commit data...
creds_test.go Loading commit data...
dir_plan9.go Loading commit data...
dll_windows.go Loading commit data...
env_plan9.go Loading commit data...
env_unix.go Loading commit data...
env_windows.go Loading commit data...
exec_bsd.go Loading commit data...
exec_linux.go Loading commit data...
exec_plan9.go Loading commit data...
exec_solaris.go Loading commit data...
exec_unix.go Loading commit data...
exec_windows.go Loading commit data...
fd_nacl.go Loading commit data...
flock.go Loading commit data...
flock_linux_32bit.go Loading commit data...
fs_nacl.go Loading commit data...
lsf_linux.go Loading commit data...
mkall.sh Loading commit data...
mkall_windows.bat Loading commit data...
mkerrors.sh Loading commit data...
mksyscall.pl Loading commit data...
mksyscall_solaris.pl Loading commit data...
mksyscall_windows.go Loading commit data...
mksysctl_openbsd.pl Loading commit data...
mksysnum_darwin.pl Loading commit data...
mksysnum_dragonfly.pl Loading commit data...
mksysnum_freebsd.pl Loading commit data...
mksysnum_linux.pl Loading commit data...
mksysnum_netbsd.pl Loading commit data...
mksysnum_openbsd.pl Loading commit data...
mksysnum_plan9.sh Loading commit data...
mmap_unix_test.go Loading commit data...
net_nacl.go Loading commit data...
netlink_linux.go Loading commit data...
race.go Loading commit data...
race0.go Loading commit data...
route_bsd.go Loading commit data...
route_darwin.go Loading commit data...
route_dragonfly.go Loading commit data...
route_freebsd.go Loading commit data...
route_freebsd_32bit.go Loading commit data...
route_freebsd_64bit.go Loading commit data...
route_netbsd.go Loading commit data...
route_openbsd.go Loading commit data...
security_windows.go Loading commit data...
so_solaris.go Loading commit data...
sockcmsg_linux.go Loading commit data...
sockcmsg_unix.go Loading commit data...
srpc_nacl.go Loading commit data...
str.go Loading commit data...
syscall.go Loading commit data...
syscall_bsd.go Loading commit data...
syscall_bsd_test.go Loading commit data...
syscall_darwin.go Loading commit data...
syscall_darwin_386.go Loading commit data...
syscall_darwin_amd64.go Loading commit data...
syscall_dragonfly.go Loading commit data...
syscall_dragonfly_386.go Loading commit data...
syscall_dragonfly_amd64.go Loading commit data...
syscall_freebsd.go Loading commit data...
syscall_freebsd_386.go Loading commit data...
syscall_freebsd_amd64.go Loading commit data...
syscall_freebsd_arm.go Loading commit data...
syscall_linux.go Loading commit data...
syscall_linux_386.go Loading commit data...
syscall_linux_amd64.go Loading commit data...
syscall_linux_arm.go Loading commit data...
syscall_nacl.go Loading commit data...
syscall_nacl_386.go Loading commit data...
syscall_nacl_amd64p32.go Loading commit data...
syscall_nacl_arm.go Loading commit data...
syscall_netbsd.go Loading commit data...
syscall_netbsd_386.go Loading commit data...
syscall_netbsd_amd64.go Loading commit data...
syscall_netbsd_arm.go Loading commit data...
syscall_no_getwd.go Loading commit data...
syscall_openbsd.go Loading commit data...
syscall_openbsd_386.go Loading commit data...
syscall_openbsd_amd64.go Loading commit data...
syscall_plan9.go Loading commit data...
syscall_solaris.go Loading commit data...
syscall_solaris_amd64.go Loading commit data...
syscall_test.go Loading commit data...
syscall_unix.go Loading commit data...
syscall_unix_test.go Loading commit data...
syscall_windows.go Loading commit data...
syscall_windows_386.go Loading commit data...
syscall_windows_amd64.go Loading commit data...
syscall_windows_test.go Loading commit data...
tables_nacl.go Loading commit data...
time_nacl_386.s Loading commit data...
time_nacl_amd64p32.s Loading commit data...
time_nacl_arm.s Loading commit data...
types_darwin.go Loading commit data...
types_dragonfly.go Loading commit data...
types_freebsd.go Loading commit data...
types_linux.go Loading commit data...
types_netbsd.go Loading commit data...
types_openbsd.go Loading commit data...
types_plan9.c Loading commit data...
types_solaris.go Loading commit data...
unzip_nacl.go Loading commit data...
zerrors_darwin_386.go Loading commit data...
zerrors_darwin_amd64.go Loading commit data...
zerrors_dragonfly_386.go Loading commit data...
zerrors_dragonfly_amd64.go Loading commit data...
zerrors_freebsd_386.go Loading commit data...
zerrors_freebsd_amd64.go Loading commit data...
zerrors_freebsd_arm.go Loading commit data...
zerrors_linux_386.go Loading commit data...
zerrors_linux_amd64.go Loading commit data...
zerrors_linux_arm.go Loading commit data...
zerrors_netbsd_386.go Loading commit data...
zerrors_netbsd_amd64.go Loading commit data...
zerrors_netbsd_arm.go Loading commit data...
zerrors_openbsd_386.go Loading commit data...
zerrors_openbsd_amd64.go Loading commit data...
zerrors_plan9_386.go Loading commit data...
zerrors_plan9_amd64.go Loading commit data...
zerrors_solaris_amd64.go Loading commit data...
zerrors_windows.go Loading commit data...
zerrors_windows_386.go Loading commit data...
zerrors_windows_amd64.go Loading commit data...
zsyscall_darwin_386.go Loading commit data...
zsyscall_darwin_amd64.go Loading commit data...
zsyscall_dragonfly_386.go Loading commit data...
zsyscall_dragonfly_amd64.go Loading commit data...
zsyscall_freebsd_386.go Loading commit data...
zsyscall_freebsd_amd64.go Loading commit data...
zsyscall_freebsd_arm.go Loading commit data...
zsyscall_linux_386.go Loading commit data...
zsyscall_linux_amd64.go Loading commit data...
zsyscall_linux_arm.go Loading commit data...
zsyscall_nacl_386.go Loading commit data...
zsyscall_nacl_amd64p32.go Loading commit data...
zsyscall_nacl_arm.go Loading commit data...
zsyscall_netbsd_386.go Loading commit data...
zsyscall_netbsd_amd64.go Loading commit data...
zsyscall_netbsd_arm.go Loading commit data...
zsyscall_openbsd_386.go Loading commit data...
zsyscall_openbsd_amd64.go Loading commit data...
zsyscall_plan9_386.go Loading commit data...
zsyscall_plan9_amd64.go Loading commit data...
zsyscall_solaris_amd64.go Loading commit data...
zsyscall_windows_386.go Loading commit data...
zsyscall_windows_amd64.go Loading commit data...
zsysctl_openbsd.go Loading commit data...
zsysnum_darwin_386.go Loading commit data...
zsysnum_darwin_amd64.go Loading commit data...
zsysnum_dragonfly_386.go Loading commit data...
zsysnum_dragonfly_amd64.go Loading commit data...
zsysnum_freebsd_386.go Loading commit data...
zsysnum_freebsd_amd64.go Loading commit data...
zsysnum_freebsd_arm.go Loading commit data...
zsysnum_linux_386.go Loading commit data...
zsysnum_linux_amd64.go Loading commit data...
zsysnum_linux_arm.go Loading commit data...
zsysnum_netbsd_386.go Loading commit data...
zsysnum_netbsd_amd64.go Loading commit data...
zsysnum_netbsd_arm.go Loading commit data...
zsysnum_openbsd_386.go Loading commit data...
zsysnum_openbsd_amd64.go Loading commit data...
zsysnum_plan9_386.go Loading commit data...
zsysnum_plan9_amd64.go Loading commit data...
zsysnum_solaris_amd64.go Loading commit data...
zsysnum_windows_386.go Loading commit data...
zsysnum_windows_amd64.go Loading commit data...
ztypes_darwin_386.go Loading commit data...
ztypes_darwin_amd64.go Loading commit data...
ztypes_dragonfly_386.go Loading commit data...
ztypes_dragonfly_amd64.go Loading commit data...
ztypes_freebsd_386.go Loading commit data...
ztypes_freebsd_amd64.go Loading commit data...
ztypes_freebsd_arm.go Loading commit data...
ztypes_linux_386.go Loading commit data...
ztypes_linux_amd64.go Loading commit data...
ztypes_linux_arm.go Loading commit data...
ztypes_netbsd_386.go Loading commit data...
ztypes_netbsd_amd64.go Loading commit data...
ztypes_netbsd_arm.go Loading commit data...
ztypes_openbsd_386.go Loading commit data...
ztypes_openbsd_amd64.go Loading commit data...
ztypes_plan9_386.go Loading commit data...
ztypes_plan9_amd64.go Loading commit data...
ztypes_solaris_amd64.go Loading commit data...
ztypes_windows.go Loading commit data...
ztypes_windows_386.go Loading commit data...
ztypes_windows_amd64.go Loading commit data...