Use SAFE_STAT instead of stat to avoid unnecessary failure on systems for which stat can return EINTR.