473,569 Members | 2,793 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(pat h):
assert os.path.islink( path)
try:
os.stat(path)
except os.error:
return 1
return 0
Nov 30 '07 #1
3 4129
I would like to know if such function would be correct for verifying
if a link is broken and/or circular.

def isvalidlink(pat h):
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. Löwis" <mar...@v.loewi s.dewrote:
I would like to know if such function would be correct for verifying
if a link is broken and/or circular.
def isvalidlink(pat h):
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(pat h):
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.loewi s.dewrote:
I would like to know if such function would be correct for verifying
if a link is broken and/or circular.
def isvalidlink(pat h):
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(pat h):
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5940
by: Patrick Lioi | last post by:
I've developed a simple content management system in PHP, and I'd like to be able to put the code for it in a single place, and have several sites on one server seamlessly share the code. I want to put the app itself in /DOCROOT/MyCMS/. I want to put a site that uses it in, say, /DOCROOT/site1, /DOCROOT/site2, etc. I want to place a...
1
4482
by: marco | last post by:
Hi all, I know about os.symlink(src, dst), but is there a way to overwrite an existing symlink e.g. "ln -sf"? Short of checking and, if necessary, having to delete the existing link by hand... Thanks! --
0
1851
by: A. Murat Eren | last post by:
Hi, I have a problem about zipfile. I'm trying to add files from local file system into a zip file via zipfile module of python.. I have a function in my class which recursively adds files or dirs into zip: -------8<-------------------8<--------------------8<------------------8<----- def add_file(self, fileName):
0
1437
by: lynn | last post by:
Hello, I tried to install on my mac osx 10.3.3 my mysql 4.0.13 databases on an other partition than its application; I know it is possible because I managed to do it previously with jaguar. So, as I did before, I copied complete var of mysql into the other partition. I eliminated the var in mysql directory and I made a "var" symlink...
10
4499
by: Raymond | last post by:
Hi All: To find a file exists using the file name, I have tow routings on UNIX system. 1. access(2) 2. lstat(2) This tow function also can do. When the return value is "-1" and errno is "ENOENT", The file doesn't exist.
2
1682
by: wheresjim | last post by:
I have a project in which I only need read access to a Microsoft Access database (.mdb file) via JDBC and unfortunately, the file is frequently locked by another application when I need access to it. I figured that I could bypass the lock file by creating a symlink (my os is Fedora Core 3 Linux) and just putting the url to the Symlink...
7
13512
by: barias | last post by:
Although circular dependencies are something developers should normally avoid, unfortunately they are very easy to create accidentally between classes in a VS project (i.e. circular compile-time dependencies). But then I started wondering how "easy" it would be to similarly make a NON-RUNTIME circular dependency between (implicitly linked)...
1
2262
by: | last post by:
I'm developing a website (PHP/Apache), and I need to be able to use a 'symlink' inside a URL where the symlink is a folder not a file. I have been able to use symlinks that point directly to a file. Now I need to be able to have the symlink point to a folder (which is a parent to other content). I have used PHP code to generate a SYMLINK...
2
7337
by: Nikolaus Rath | last post by:
Hello, symlink(...) symlink(src, dst) Create a symbolic link pointing to src named dst. Is there any reason why this is so deliberately confusing? Why is the
0
7695
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7922
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8119
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7668
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6281
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.