As long as no_symlinks is set to something, ie no_symlinks='on' then symlinks will be resolved to the physical system. This also includes an environment setting no_symlinks that provides the ability to resolve symlinks to the physical system. What follows is a synopsis of the implementation:Īs Keith Smith astutely points out, readlink -f does two things: 1) resolves symlinks recursively, and 2) canonicalizes the result, hence: realpath() " # echo result. See my project on Github for tests and full code. You might need both a portable, pure shell implementation, and unit-test coverage, as the number of edge-cases for something like this is non-trivial. ![]() Ironically, the C version of this script ought to be shorter. Raise OSError(errno, "%s: %s" % (libc.strerror(errno), buffer.value)) Libc._error.restype = ctypes.POINTER(ctypes.c_int)īuffer = ctypes.create_string_buffer(1024) # PATH_MAX I know you said you'd prefer something more lightweight than another scripting language, but just in case compiling a binary is insufferable, you can use Python and ctypes (available on Mac OS X 10.5) to wrap the library call: #!/usr/bin/python $ python -c 'import os,sys print(os.path.realpath(sys.argv))' c Lrwxr-xr-x 1 miles wheel 1 Jul 11 20:49 c -> b Lrwxr-xr-x 1 miles wheel 1 Jul 11 20:49 b -> a The two aren't exactly the same the C library call requires that intermediary path components exist, while the Python version does not. You may be interested in realpath(3), or Python's os.path.realpath. script_name filename, no -f, change $1 to $2 if you want to be able to use with -f filename like GNU readlink. Note that this script expects to be called like. A simple way to do this would be to count the number of times you go around the loop and fail if you hit an improbably large number, such as 1,000. Of particular importance, it doesn't detect symlink cycles. Note that this doesn't include any error handling. # for the directory we're in and appending the target file. # Compute the canonicalized name by finding the physical path ![]() ![]() # Iterate down a (possible) chain of symlinks ![]() Obviously you could insert this in your own script where you'd like to call readlink -f #!/bin/sh If you want to, you can just build a shell script that uses vanilla readlink behavior to achieve the same thing.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |