Here is the filesystem pathname translation algorithm
discussed in class 2003-11-11 (all sections):
/*
p: a pathname string
pi: ith component (left-to-right) of p
d: an i-number
output: i-number of the file denoted by p
restrictions: doesn't work for symbolic-links
error conditions: no error-checking performed, must be added
*/
Translate(p)
{
d := i-number of root directory;
for i := 1 to num_components(p)
{
get_inode(d);
get_directory_entries(d);
d := lookup( d, pi );
}
return(d);
}
The enhancement of the algorithm to support symbolic links
requires the addition of code at two points in the above:
1) to handle symbolic links found in the middle of a pathname
2) to handle a leaf-node symbolic link
/*
p: a pathname string
pi: ith component (left-to-right) of p
d: an i-number
l: a symbolic-link string (contents of the symbolic-link file)
output: i-number of the file denoted by p
error conditions: no error-checking performed, must be added
note: This version uses an "if" test for the symbolic link. The
version presented in one of the sections used a while-loop,
but after discussion it was decided that "if" was sufficient.
*/
Translate(p)
{
d := i-number of root directory;
for i := 1 to num_components(p)
{
get_inode(d);
if is_sym_link(d)
{
l := get_symlink_data(d);
d := translate( l );
}
get_directory_entries(d);
d := lookup( d, pi );
}
if is_sym_link(d)
{
l := get_symlink_data(d);
d := translate( l );
}
return(d);
}