user/ln.c
About this file
ln old new gives an existing file a second name: after ln a b, a and b are two
hard links to the same inode, with the same contents and the same
inode number (third column of ls). Neither is the original; rm a leaves the file
reachable as b.
The work is one system call, link (sys_link). The kernel increments the
inode’s link count (nlink) and adds a directory entry new pointing at the inode’s number,
inside one transaction. It refuses if old does not exist, if old is a directory
(a second name for a directory could make a cycle in the tree), if new already exists,
if new’s parent directory does not exist, or if the link count is at its maximum.
xv6 has no symbolic links, so there is no -s option.
Read before: user/rm.c. Read next: kernel/sysfile.c (sys_link).
Headers
user/user.h declares link. kernel/stat.h is not used.
Exactly two names
argc must be 3: the program name, the existing name and the new one. Anything else
prints the usage message on standard error and exits with
status 1 (exit status).
Make the link
link(old, new) traps into sys_link. If it fails, a message goes to
standard error, but the program still exits with status 0, so a caller cannot tell
success from failure by the exit status. With the two names given, the message
reads link a b: failed; the kernel does not say why (xv6 has no errno).
Add the directory entry argv[2] for the inode named by argv[1]
(sys_link).
Report the failure on standard error.
Exit with status 0 whether or not the link was made.