Date: Tue, 14 Jul 1998 00:40:17 +0900 From: Shigio Yamaguchi <shigio@wafu.netgate.net> To: hackers@FreeBSD.ORG Cc: shigio@wafu.netgate.net Subject: Improvement of ln(1). [Second RFC] Message-ID: <199807131538.IAA16298@hub.freebsd.org>
next in thread | raw e-mail | index | archive | help
Hi, About improved ln(1), I have written short examples and alpha version's manual. Current implementation is here. http://wafu.netgate.net/tama/unix/pathconvert.tar.gz Comments appreciated. Thank you in advance. ------------------------------------------------------------------------------ Short examples: Ex1: You can make right and relative (or absolute) symbolic link without thinking where you are and where symbolic link will be made. % cd /usr/src/sys % ls -l kern/tty.c -rw-r--r-- 1 root wheel 56525 Jul 3 09:27 kern/tty.c % ln -sf kern/tty.c ~/tmp % ls -l ~/tmp/tty.c lrwxrwxrwx 1 shigio users 10 Jul 13 18:08 /home/shigio/tmp/tty.c -> kern/tty.c ~~~~~~~~~~ (wrong symbolic link) % ln -sfr kern/tty.c ~/tmp % ls -l ~/tmp/tty.c lrwxrwxrwx 1 shigio users 31 Jul 13 13:28 /home/shigio/tmp/tty.c -> ../../../usr/src/sys/kern/tty.c ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ (right and relative symbolic link) % ln -sfa kern/tty.c ~/tmp % ls -l ~/tmp/tty.c lrwxrwxrwx 1 shigio users 31 Jul 13 13:28 /home/shigio/tmp/tty.c -> /usr/src/sys/kern/tty.c ~~~~~~~~~~~~~~~~~~~~~~~ (right and absolute symbolic link) Ex2: You can make symbolic link even if the target isn't exist. (This is a spec of symbolic link. ln(1) doens't print warning message.) % cd /usr/src/sys % ls -l kern/noexist.c ls: kern/noexist.c: No such file or directory % ln -sr kern/noexist.c ~/tmp % ls -l ~/tmp/noexist.c lrwxrwxrwx 1 shigio users 36 Jul 13 17:53 /home/shigio/tmp/noexist.c -> ../../../usr/src/sys/kern/noexist.c ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ (not exist but right symbolic link) Ex3: You can make shadow tree with relative symbolic link like this. % cd /usr/src/sys % set obj=/usr/obj`pwd` % mkdir -p $obj % find * -type d -exec mkdir $obj/'{}' ';' % find * -type f -exec ln -sr '{}' $obj/'{}' ';' ------------------------------------------------------------------------------ LN(1) FreeBSD General Commands Manual LN(1) NAME ln - make links SYNOPSIS ln [-afrs] source_file [target_file] ln [-afrs] source_file ... [target_dir] DESCRIPTION The ln utility creates a new directory entry (linked file) which has the same modes as the original file. It is useful for maintaining multiple copies of a file in many places at once without using up storage for the ``copies''; instead, a link ``points'' to the original copy. There are two types of links; hard links and symbolic links. How a link ``points'' to a file is one of the differences between a hard or symbolic link. The options are as follows: -a If the -s option is specified, make absolute symbolic link. -f Unlink any already existing file, permitting the link to occur. -r If the -s option is specified, make relative symbolic link. -s Create a symbolic link. If neither -a nor -r option are speci- fied, all done is copy the exact 'source_file' name into the link as- is. The -a and -r options override each other; the last one specified deter- mines the method used. By default ln makes hard links. A hard link to a file is indistinguish- able from the original directory entry; any changes to a file are effec- tive independent of the name used to reference the file. Hard links may not normally refer to directories and may not span file systems. A symbolic link contains the name of the file to which it is linked. The referenced file is used when an open(2) operation is performed on the link. A stat(2) on a symbolic link will return the linked-to file; an lstat(2) must be done to obtain information about the link. The read- link(2) call may be used to read the contents of a symbolic link. Sym- bolic links may span file systems and may refer to directories. Given one or two arguments, ln creates a link to an existing file 'source_file'. If 'target_file' is given, the link has that name; 'target_file' may also be a directory in which to place the link; otherwise it is placed in the current directory. If only the directory is specified, the link will be made to the last component of 'source_file'. Given more than two arguments, ln makes links in target_dir to all the named source files. The links made will have the same name as the files being linked to. SEE ALSO link(2), lstat(2), readlink(2), stat(2), symlink(2), symlink(7) HISTORY A ln command appeared in Version 1 AT&T UNIX. 4th Berkeley Distribution December 30, 1993 1 ------------------------------------------------------------------------------ Following functions are used in improved ln(1). ------------------------------------------------------------------------------ ABS2REL(3) FreeBSD Library Functions Manual ABS2REL(3) NAME abs2rel - make a relative path name from an absolute path SYNOPSIS char * abs2rel(const char *path, const char *base, char *result, size_t size) DESCRIPTION The abs2rel() function makes a relative path name from an absolute path name 'path' based on a directory 'base' and copies the resulting path name into the memory referenced by 'result'. The 'result' argument must refer to a buffer capable of storing at least 'size' characters. The resulting path name may include symbolic links. The abs2rel() func- tion doesn't check whether or not any path exists. RETURN VALUES The abs2rel() function returns relative path name on success. If an er- ror occurs, it returns NULL. ERRORS The abs2rel() function may fail and set the external variable errno to indicate the error. [EINVAL] The 'base' directory isn't an absolute path name or the 'size' argument is zero. [ERANGE] The 'size' argument is greater than zero but smaller than the length of the pathname plus 1. EXAMPLE char result[MAXPATHLEN]; char *path = abs2rel("/usr/src/sys", "/usr/local/lib", result, MAX- PATHLEN); yields: path == "../../src/sys" Similarly, path1 = abs2rel("/usr/src/sys", "/usr", result, MAXPATHLEN); path2 = abs2rel("/usr/src/sys", "/usr/src/sys", result, MAXPATHLEN); yields: path1 == "src/sys" path2 == "." BUGS If the base directory includes symbolic links, the abs2rel() function produces the wrong path. For example, if '/sys' is a symbolic link to '/usr/src/sys', char *path = abs2rel("/usr/local/lib", "/sys", result, MAXPATHLEN); yields: path == "../usr/local/lib" /* It's wrong!! */ You should convert the base directory into a real path in advance. path = abs2rel("/sys/kern", realpath("/sys", resolvedname), result, MAXPATHLEN); yields: path == "../../../sys/kern" /* It's correct but ... */ That is correct, but a little redundant. If you wish get the simple an- swer 'kern', do the following. path = abs2rel(realpath("/sys/kern", r1), realpath("/sys", r2), result, MAXPATHLEN); The realpath() function assures correct result, but don't forget that realpath() requires that all but the last component of the path exist. SEE ALSO rel2abs(3) BSD Dec 15, 1997" 2 ------------------------------------------------------------------------------ REL2ABS(3) FreeBSD Library Functions Manual REL2ABS(3) NAME rel2abs - make an absolute path name from a relative path SYNOPSIS char * rel2abs(const char *path, const char *base, char *result, size_t size) DESCRIPTION The rel2abs() function makes an absolute path name from a relative path name 'path' based on a directory 'base' and copies the resulting path name into the memory referenced by 'result'. The 'result' argument must refer to a buffer capable of storing at least 'size' character The resulting path name may include symbolic links. abs2rel() doesn't check whether or not any path exists. RETURN VALUES The rel2abs() function returns absolute path name on success. If an er- ror occurs, it returns NULL. ERRORS The rel2abs() function may fail and set the external variable errno to indicate the error. [EINVAL] The 'base' directory isn't an absolute path name or the 'size' argument is zero. [ERANGE] The 'size' argument is greater than zero but smaller than the length of the pathname plus 1 EXAMPLE char result[MAXPATHLEN]; char *path = rel2abs("../../src/sys", "/usr/local/lib", result, MAX- PATHLEN); yields: path == "/usr/src/sys" Similarly, path1 = rel2abs("src/sys", "/usr", result, MAXPATHLEN); path2 = rel2abs(".", "/usr/src/sys", result, MAXPATHLEN); yields: path1 == "/usr/src/sys" path2 == "/usr/src/sys" SEE ALSO abs2rel(3) BSD Dec 3, 1997" 1 ------------------------------------------------------------------------------ -- Shigio Yamaguchi (Freelance programmer) Mail: shigio@wafu.netgate.net, WWW: http://wafu.netgate.net/tama/ To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199807131538.IAA16298>