From: <pj...@us...> - 2008-01-10 04:18:41
|
Revision: 4014 http://jython.svn.sourceforge.net/jython/?rev=4014&view=rev Author: pjenvey Date: 2008-01-09 20:18:33 -0800 (Wed, 09 Jan 2008) Log Message: ----------- o implement lstat by cleverly comparing file's absolute paths vs their canonical paths. lstat can't determine anything about links except the fact that they are links o utilize lstat for os.path.islink Modified Paths: -------------- trunk/jython/Lib/javaos.py trunk/jython/Lib/javapath.py Modified: trunk/jython/Lib/javaos.py =================================================================== --- trunk/jython/Lib/javaos.py 2008-01-10 01:33:14 UTC (rev 4013) +++ trunk/jython/Lib/javaos.py 2008-01-10 04:18:33 UTC (rev 4014) @@ -291,6 +291,34 @@ mode = mode | _stat.S_IWRITE return stat_result((mode, 0, 0, 0, 0, 0, size, mtime, mtime, 0)) +def lstat(path): + """lstat(path) -> stat result + + Like stat(path), but do not follow symbolic links. + """ + f = File(sys.getPath(path)) + abs_parent = f.getAbsoluteFile().getParentFile() + can_parent = abs_parent.getCanonicalFile() + + if can_parent.getAbsolutePath() == abs_parent.getAbsolutePath(): + # The parent directory's absolute path is canonical.. + if f.getAbsolutePath() != f.getCanonicalPath(): + # but the file's absolute and paths differ (a link) + return stat_result((_stat.S_IFLNK, 0, 0, 0, 0, 0, 0, 0, 0, 0)) + + # The parent directory's path is not canonical (one of the parent + # directories is a symlink). Build a new path with the parent's + # canonical path and compare the files + f = File(_path.join(can_parent.getAbsolutePath(), f.getName())) + if f.getAbsolutePath() != f.getCanonicalPath(): + return stat_result((_stat.S_IFLNK, 0, 0, 0, 0, 0, 0, 0, 0, 0)) + + # Not a link, only now can we determine if it exists (because + # File.exists() returns False for dead links) + if not f.exists(): + raise OSError(0, 'No such file or directory', path) + return stat(path) + def utime(path, times): """utime(path, (atime, mtime)) utime(path, None) Modified: trunk/jython/Lib/javapath.py =================================================================== --- trunk/jython/Lib/javapath.py 2008-01-10 01:33:14 UTC (rev 4013) +++ trunk/jython/Lib/javapath.py 2008-01-10 04:18:33 UTC (rev 4014) @@ -6,7 +6,6 @@ """ # Incompletely implemented: -# islink -- How? # ismount -- How? # normcase -- How? @@ -14,6 +13,7 @@ # sameopenfile -- Java doesn't have fstat nor file descriptors? # samestat -- How? +import stat import sys from java.io import File import java.io.IOException @@ -143,13 +143,13 @@ return prefix def islink(path): - """Test whether a path is a symbolic link. + """Test whether a path is a symbolic link""" + try: + st = os.lstat(path) + except (os.error, AttributeError): + return False + return stat.S_ISLNK(st.st_mode) - XXX This incorrectly always returns false under JDK. - - """ - return 0 - def samefile(path, path2): """Test whether two pathnames reference the same actual file""" path = _tostr(path, "samefile") This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |