
于 2010年12月21日 22:40, Eric Blake 写道:
On 12/20/2010 11:47 PM, Osier Yang wrote:
a more efficient solution would be to check if errno
is ELOOP or ENOENT (the only possibilities for a dangling symlink; any other error should return -1), and in those two cases a successful lstat() is sufficient to detect a broken symlink without resorting to reading its contents.
I guess you mean stat, lstat will not work here, as it doesn't follow the *symbolic* link. what we need to do is to determine if the symbolic link is dangling, so use "stat" to update the patch, v3 send, thanks again.
No, I really meant lstat(). If stat() would have failed because of a dangling symlink, then open() will fail for the same reasons. Therefore, check errno before lstat, and use the successful lstat as proof that a symlink is in place but that stat()ing the symlink would fail because it is dangling.
if (open(f)< 0) { if ((errno == ENOENT || errno == ELOOP)&& lstat(f, buf) == 0) { /* Dangling symlink, since lstat() passed but open failed. */ log message about ignored file return -2; } either an unrelated errno, like EACCES, or we got ENOENT because the file was deleted after readdir but before open/lstat error message about unaccessible file return -1; }
Eric, thanks for the detailed knowledge.. - Osier