stat("../bin") on /Users directory sometimes fails with errno=2 (ENOENT)
I saw this failure on several Mac machines. Especially:
- macOS Monterey 12.1, MacBook Pro 13-inch, 2020, M1
- macOS Monterey 12.1, MacBook Pro 16-inch, 2019, Intel
- macOS Catalina 10.15.7, MacBook Pro 15-inch, 2013, Intel
❯ cd ~
❯ cat a.c
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
extern int errno;
int main() {
struct stat buf;
long i = 0;
for (;;) {
++i;
if (stat("../bin", &buf)) {
printf("%ld: stat(\"../bin\"): errno=%d\n", i, errno);
}
}
}
❯ cc a.c
❯ ls a.out
a.out
❯ cd /Users
❯ ~/a.out
❯ ~/a.out
2091386: stat("../bin"): errno=2
2092797: stat("../bin"): errno=2
2094995: stat("../bin"): errno=2
...
Please note that the failures may increase if we execute some programs in other terminal, especially:
cd /Users
/usr/local/bin/gls /Users
(where gls comes from homebrew)/opt/homebrew/bin/gls /Users
(where gls comes from homebrew)
- The "dot-dot" notation (../bin) seems important; if we call stat("/bin"), then it doesn't fail very much.
- Calling stat from /Users directory also seems important; if we call stat("../bin") from /Applications directory, then it doesn't fail very much.
- I only mentioned stat function; but open("../bin/ls", O_RDONLY) also sometimes fails with errno=2 (ENOENT)
Fixed in macOS 12.3