Fix incorrect usage of S_IFDIR flag

Discovered while working on PR #5309.
If `st.st_mode & S_IFDIR != 0` it doesn't mean that it's directory. Correct check is `S_ISDIR(st.st_mode)`.