SUMMARY: "Canonicalizing" an automounter path?

From: Eric Peterson (eric@xylan.com)
Date: Wed Mar 13 1996 - 10:34:07 CST


Whew!
        I got quite a few answers to my question:

> I have a need to find out the "canonical" directory name for an
> automounter-mounted directory. To clarify: when using the automounter,
> the server machine typically will typically have a different mount
> point than the client, for example:
>
> on machine 1 (client):
> % echo ~
> /home/eric
> % cd /home/eric
> % pwd
> /tmp_mnt/home/eric
>
> on machine 1 (server)
> % echo ~
> /home/eric
> % cd
> % pwd
> /export/home/eric
>
> What I would like is a reasonable means to get "/home/eric" from a
> routine. (Note that since they are not all home dirs, no cheating with
> the passwd file :-) Granted, I could walk through all the maps, and
> come up with my mapping, but there must (!?) be a more direct method.

        Unfortunately, I didn't make it clear that I wanted a programatic
        way to do it (e.g. get_canon_dir dirname); and also should have noted
        that wouldn't always know the way I got to a directory (I could really
        have cd'd to /tmp_mnt/..., but _still_ want to get back /home/...).

        Anyway, the great majority suggested getting Solaris 2.3 (or later) due
        to its more intelligent automounter. It still wouldn't completely fix
        the problem, as I could still get into the server scenario above, but
        one could hopefully come up with a direct mapping from the /export/...
        local directory to the automounter mount point.

        Several folks suggested $cwd in csh, or using the ksh. (Another
        similarly suggested $PWD in the sh, it looks like he had a special sh,
        mine just inherits the (nonchanging) PWD from the parent csh!) These
        work in limited cases, but again, if I specifically want the
        "canonical" form of /tmp_mnt/..., no dice. Also, sometimes even
        relative cd's will confuse $cwd.

        Peter Allan supplied the script (appended below) that tries to
        do it, but even he admits to a few places it has trouble. I
        tried it, and it does do correctly in many places, (and even
        admits most of the times when it has difficulty!) .

My thanks to all who made suggestions:

Hal Stern stern@sunrise.East.Sun.COM
Casper Dik casper@holland.Sun.COM
Bill Reed reedwv@stp.xfi.bp.com
Jens Fischer jefi@kat.ina.de
Kevin Davidson tkld@cogsci.ed.ac.uk
Peter Allan peter.allan@aeat.co.uk
Kalpesh Dharia kdharia@fh.us.bosch.com
John Valdes valdes@geosun.uchicago.edu
Colin Johnson cj@mssls3.mssl.ucl.ac.uk

Here's Peter Allan's script:

###### cut here ####

#!/bin/sh
## wfr 1.5 (29/9/1995)
## to find the device a file
## resides on
## can fail if 'df' wraps text
##
## Usage: wfr filename
##
## peter.allan@aeat.co.uk
 
 
#### Mod for Solaris 2:
#### needs a different nawk syntax
#### put the query setting with a -v
#### before the nawk program (see below)

 
PATH=.:/bin:/usr/bin:$PATH
export PATH
 
for filename in "$@"
{
# Need to check file exists.
# a) so we avoid reporting on an absent file
# b) so we get a true location if automounted

 
first=`echo "$filename" |cut -c1`
#echo :$first:
 
if [ "$first" = "/" -o "$first" = "~" ];
then
   tmpname="$filename"
else
   tmpname="`pwd`/$filename"
fi
 
#echo LOOKING FOR $tmpname
 
# to defeat automount
tmpname=`echo $tmpname | sed -e 's ^/tmp_mnt '`
 
if [ -d "$tmpname" -o -f "$tmpname" ];
then
###
### FOR SOL2
### df | nawk -v query="$tmpname" '
###
df | nawk '
BEGIN {
# Avoid being fooled by . in names
sub(/\/\.\//,"/",query)
sub(/\/\.$/,"",query)
 
# Avoid being fooled by .. in names
sub(/\/[^\/]+\/\.\.\//,"/",query)
sub(/\/[^\/]+\/\.\.$/,"",query)
 
qdepth=split(query,qfnc,"/");
# BSF is best-so-far
# we record the best depth achieved
# and the resource that achieved it
bsfd=0;
bsfr="UNKNOWN:";
}
 {
# ignore broken lines
if ( NF < 2 ) break
 
if ( match( $3 , /:/) ) {
# Solaris 2 df fields come in a funny order
       mntpt=$1
       resource=$2
# and an extraneous (
       sub( /\(/ , "" ,resource)
}
else
{
# SunOS 4.1.x and Irix 5.x
# have this field order
       resource=$1
       mntpt=$NF
}
 
 
# Avoid being fooled by automount
sub(/^\/tmp_mnt/,"",mntpt)
 
 
mdepth=split(mntpt,mfnc,"/")
 
if ( mdepth <= qdepth ) {
if ( mdepth == qdepth && query != mntpt )
           break
## mount point is not too deep,
## so worth examining
    for(i=1; i <= mdepth && mfnc[i] == qfnc[i]; i++) {
            if ( i > bsfd ) {
                         bsfd=i
                         bsfr=resource
#### print "FOUND -->> " bsfr " " resource" " bsfd
            }
    }
 
}
 
}
 
 
END {
# If df hs wrapped text
# and we are beaten,
# at least admit it.
     if ( match(bsfr,/[0-9]/) == 1 )
        { printf("UNSURE ABOUT: \t\t\t%s\n",query)}
  else
     if (bsfd != 0 )
         printf("%s \t\t\t%s\n", bsfr,query)
     
 }
' query="$tmpname"
fi
}

###### cut here ####

Thanks again,
Eric

-- 
Eric Peterson WB6PYK  (818)880-3500 x3537 WORK: eric@xylan.com
                                          HOME: eric@rain.org



This archive was generated by hypermail 2.1.2 : Fri Sep 28 2001 - 23:10:55 CDT