471,354 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

Check if a symlink is broken or circular

Hi there,
I would like to know if such function would be correct for verifying
if a link is broken and/or circular.

def isvalidlink(path):
assert os.path.islink(path)
try:
os.stat(path)
except os.error:
return 1
return 0
Nov 30 '07 #1
3 3953
I would like to know if such function would be correct for verifying
if a link is broken and/or circular.

def isvalidlink(path):
assert os.path.islink(path)
try:
os.stat(path)
except os.error:
return 1
return 0
You meant to flip the result values, right? 1 should mean that the
link is value, and 0 that it is not.

Mostly. If the link is correct, but you don't have permission to stat
the target file, you get 0. OTOH, in that case, you have no way of
finding out whether the link *is* correct.

Still, you could try to detect the errnos that indicate a problem
with the link itself, and pass all other errors through.

Regards,
Martin
Nov 30 '07 #2
On 1 Dic, 00:10, "Martin v. Lwis" <mar...@v.loewis.dewrote:
I would like to know if such function would be correct for verifying
if a link is broken and/or circular.
def isvalidlink(path):
assert os.path.islink(path)
try:
os.stat(path)
except os.error:
return 1
return 0

You meant to flip the result values, right? 1 should mean that the
link is value, and 0 that it is not.

Mostly. If the link is correct, but you don't have permission to stat
the target file, you get 0. OTOH, in that case, you have no way of
finding out whether the link *is* correct.

Still, you could try to detect the errnos that indicate a problem
with the link itself, and pass all other errors through.

Regards,
Martin
Mmmm... do you mean something like this?
Could it be ok?

import os, errno

def isvalidlink(path):
assert os.path.lexists(path)
try:
os.stat(path)
except os.error, err:
# broken link
# "No such file or directory"
if err.errno == errno.ENOENT:
return 1
# circular link
# "Too many levels of symlinks"
elif err.errno == errno.ELOOP:
return 2
# something else occurred,
# assume it as invalid anyway
else:
return 3
return 0

Dec 1 '07 #3
Giampaolo Rodola' wrote:
On 1 Dic, 00:10, "Martin v. L�wis" <mar...@v.loewis.dewrote:
I would like to know if such function would be correct for verifying
if a link is broken and/or circular.
def isvalidlink(path):
assert os.path.islink(path)
try:
os.stat(path)
except os.error:
return 1
return 0
You meant to flip the result values, right? 1 should mean that the
link is value, and 0 that it is not.

Mostly. If the link is correct, but you don't have permission to stat
the target file, you get 0. OTOH, in that case, you have no way of
finding out whether the link *is* correct.

Still, you could try to detect the errnos that indicate a problem
with the link itself, and pass all other errors through.

Regards,
Martin

Mmmm... do you mean something like this?
Could it be ok?

I think he meant something like this (see below):
>
import os, errno

def isvalidlink(path):
assert os.path.lexists(path)
try:
os.stat(path)
except os.error, err:
# broken link
# "No such file or directory"
if err.errno == errno.ENOENT:
return 1
Right, except that it should be return 0 (or preferably return False)
unless you want inverted logic.
# circular link
# "Too many levels of symlinks"
elif err.errno == errno.ELOOP:
return 2
Change to return 1/True (inverted) or return 0/False (not inverted)
# something else occurred,
# assume it as invalid anyway
else:
return 3
Change "return 3" to "raise", so the exception gets propagated.
return 0
Again, if you do not want the logic to be inverted, use return 1/True
instead

--Nathan Davis
Dec 1 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Patrick Lioi | last post: by
1 post views Thread by marco | last post: by
reply views Thread by A. Murat Eren | last post: by
reply views Thread by lynn | last post: by
10 posts views Thread by Raymond | last post: by
7 posts views Thread by barias | last post: by
1 post views Thread by | last post: by
2 posts views Thread by Nikolaus Rath | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.