| 1 | 
 #!/bin/sh | 
 
 
 
 
 
 | 2 | 
 # | 
 
 
 
 
 
 | 3 | 
 # File: mount | 
 
 
 
 
 
 | 4 | 
 # $Id: mount,v 1.2 2010/03/24 21:28:27 keithmarshall Exp $ | 
 
 
 
 
 
 | 5 | 
 # | 
 
 
 
 
 
 | 6 | 
 # ===================================================================== | 
 
 
 
 
 
 | 7 | 
 # | 
 
 
 
 
 
 | 8 | 
 # Copyright (C) 2006, 2007, 2009, 2010 by Keith Marshall | 
 
 
 
 
 
 | 9 | 
 #  mailto:keithmarshall@users.sourceforge.net | 
 
 
 
 
 
 | 10 | 
 # | 
 
 
 
 
 
 | 11 | 
 # This file is part of MSYS | 
 
 
 
 
 
 | 12 | 
 #  http://www.mingw.org/msys.shtml | 
 
 
 
 
 
 | 13 | 
 # | 
 
 
 
 
 
 | 14 | 
 # 2009-04-06: First published implementation for MSYS-1.0.11 | 
 
 
 
 
 
 | 15 | 
 # 2010-03-24: Updated for MSYS-1.0.14: --replace option added | 
 
 
 
 
 
 | 16 | 
 # | 
 
 
 
 
 
 | 17 | 
 # MSYS is free software.  It is provided "as is", in the hope that it | 
 
 
 
 
 
 | 18 | 
 # may be useful; there is NO WARRANTY OF ANY KIND, not even an implied | 
 
 
 
 
 
 | 19 | 
 # warranty of MERCHANTABILITY or FITNESS FOR ANY PARTICULAR PURPOSE. | 
 
 
 
 
 
 | 20 | 
 # At no time will the author accept liability for damages, however | 
 
 
 
 
 
 | 21 | 
 # caused, resulting from the use of this software. | 
 
 
 
 
 
 | 22 | 
 # | 
 
 
 
 
 
 | 23 | 
 # Permission is granted to copy and redistribute this software, either | 
 
 
 
 
 
 | 24 | 
 # as is, or in modified form, provided that:-- | 
 
 
 
 
 
 | 25 | 
 # | 
 
 
 
 
 
 | 26 | 
 #   1) All such copies are distributed with the same rights | 
 
 
 
 
 
 | 27 | 
 #      of redistribution. | 
 
 
 
 
 
 | 28 | 
 # | 
 
 
 
 
 
 | 29 | 
 #   2) The preceding disclaimer of warranty and liabality is | 
 
 
 
 
 
 | 30 | 
 #      retained verbatim, in all copies. | 
 
 
 
 
 
 | 31 | 
 # | 
 
 
 
 
 
 | 32 | 
 #   3) Accreditation of the original author remains in place. | 
 
 
 
 
 
 | 33 | 
 # | 
 
 
 
 
 
 | 34 | 
 #   4) Modified copies are clearly identified as such, with | 
 
 
 
 
 
 | 35 | 
 #      additional accreditation given to the authors of each | 
 
 
 
 
 
 | 36 | 
 #      modified version. | 
 
 
 
 
 
 | 37 | 
 # | 
 
 
 
 
 
 | 38 | 
 # ===================================================================== | 
 
 
 
 
 
 | 39 | 
 # | 
 
 
 
 
 
 | 40 | 
 # | 
 
 
 
 
 
 | 41 | 
 # Ensure the the `fstab' mount registry exists, and is writeable. | 
 
 
 
 
 
 | 42 | 
 # | 
 
 
 
 
 
 | 43 | 
   >> ${MNTTAB="/etc/fstab"} | 
 
 
 
 
 
 | 44 | 
 # | 
 
 
 
 
 
 | 45 | 
 # Check for '--replace' option, so we can honour requests to reassign | 
 
 
 
 
 
 | 46 | 
 # an existing mount table entry. | 
 
 
 
 
 
 | 47 | 
 # | 
 
 
 
 
 
 | 48 | 
   case $1 in | 
 
 
 
 
 
 | 49 | 
     --replace | --replac | --repla | --repl) shift ; replace=yes ;; | 
 
 
 
 
 
 | 50 | 
     *) replace=no ;; | 
 
 
 
 
 
 | 51 | 
   esac | 
 
 
 
 
 
 | 52 | 
 # | 
 
 
 
 
 
 | 53 | 
 # Select the operation to be performed, based on number of argumemnts. | 
 
 
 
 
 
 | 54 | 
 # | 
 
 
 
 
 
 | 55 | 
   case $# in | 
 
 
 
 
 
 | 56 | 
     0) | 
 
 
 
 
 
 | 57 | 
 # | 
 
 
 
 
 
 | 58 | 
 #     No arguments specified... | 
 
 
 
 
 
 | 59 | 
 #     Simply report the current state of the mount table. | 
 
 
 
 
 
 | 60 | 
 # | 
 
 
 
 
 
 | 61 | 
       /bin/msysmnt.exe ;; | 
 
 
 
 
 
 | 62 | 
 # | 
 
 
 
 
 
 | 63 | 
     1) | 
 
 
 
 
 
 | 64 | 
 # | 
 
 
 
 
 
 | 65 | 
 #     One argument specified... | 
 
 
 
 
 
 | 66 | 
 #     Check for any pre-existing mount which may conflict; if none, | 
 
 
 
 
 
 | 67 | 
 #     try to match it to a default mount specification from the `fstab' | 
 
 
 
 
 
 | 68 | 
 #     configuration file, and mount the specified directory, if any, | 
 
 
 
 
 
 | 69 | 
 #     on its associated mount point. | 
 
 
 
 
 
 | 70 | 
 # | 
 
 
 
 
 
 | 71 | 
       FSTAB=${FSTAB-"$MNTTAB.conf"} | 
 
 
 
 
 
 | 72 | 
       MNTPATH=`echo "$1" | tr '\\\\' /` | 
 
 
 
 
 
 | 73 | 
 # | 
 
 
 
 
 
 | 74 | 
       if cat $MNTTAB | tr '\\' / | awk ' | 
 
 
 
 
 
 | 75 | 
 # | 
 
 
 
 
 
 | 76 | 
 #       Check for pre-existing mount of specified argument; | 
 
 
 
 
 
 | 77 | 
 #       set exit status to:-- | 
 
 
 
 
 
 | 78 | 
 #         0: if no such mount exists; | 
 
 
 
 
 
 | 79 | 
 #         1: if argument is an already mounted directory; | 
 
 
 
 
 
 | 80 | 
 #         2: if argument is a mount point already in use. | 
 
 
 
 
 
 | 81 | 
 # | 
 
 
 
 
 
 | 82 | 
         BEGIN { status = 0 } | 
 
 
 
 
 
 | 83 | 
         /^#/ { $0 = "" } | 
 
 
 
 
 
 | 84 | 
         $1 == "'$MNTPATH'" { status = 1 } | 
 
 
 
 
 
 | 85 | 
         $2 == "'$MNTPATH'" { status = 2 } | 
 
 
 
 
 
 | 86 | 
         END { exit status }' | 
 
 
 
 
 
 | 87 | 
       then | 
 
 
 
 
 
 | 88 | 
 # | 
 
 
 
 
 
 | 89 | 
 #       No pre-existing mount conflicts... | 
 
 
 
 
 
 | 90 | 
 # | 
 
 
 
 
 
 | 91 | 
         if WINPATH=`cat 2>/dev/null $FSTAB | tr '\\' / | awk ' | 
 
 
 
 
 
 | 92 | 
 # | 
 
 
 
 
 
 | 93 | 
 #         Look up the default mount point specification; | 
 
 
 
 
 
 | 94 | 
 #         set exit status, (assigned to "errno"), to:-- | 
 
 
 
 
 
 | 95 | 
 #           0: if found; (it is assigned to WINPATH); | 
 
 
 
 
 
 | 96 | 
 #           1: if found, but multiply and ambiguously defined; | 
 
 
 
 
 
 | 97 | 
 #           2: if not found. | 
 
 
 
 
 
 | 98 | 
 # | 
 
 
 
 
 
 | 99 | 
           BEGIN { status = 0; mount = "" } | 
 
 
 
 
 
 | 100 | 
           /^#/ { $0 = "" } | 
 
 
 
 
 
 | 101 | 
           $1 == "'$MNTPATH'" { | 
 
 
 
 
 
 | 102 | 
              if( mount == "" ) mount = $0 | 
 
 
 
 
 
 | 103 | 
              else if( mount != $0 ) status = 1 | 
 
 
 
 
 
 | 104 | 
            } | 
 
 
 
 
 
 | 105 | 
           $2 == "'$MNTPATH'" { | 
 
 
 
 
 
 | 106 | 
              if( mount == "" ) mount = $0 | 
 
 
 
 
 
 | 107 | 
              else if( mount != $0 ) status = 1 | 
 
 
 
 
 
 | 108 | 
            } | 
 
 
 
 
 
 | 109 | 
           END { | 
 
 
 
 
 
 | 110 | 
              if( mount == "" ) exit 2 | 
 
 
 
 
 
 | 111 | 
              print mount | 
 
 
 
 
 
 | 112 | 
              exit status | 
 
 
 
 
 
 | 113 | 
            }'` errno=$? | 
 
 
 
 
 
 | 114 | 
         then | 
 
 
 
 
 
 | 115 | 
 # | 
 
 
 
 
 
 | 116 | 
 #         Found a default mount specification; activate it. | 
 
 
 
 
 
 | 117 | 
 # | 
 
 
 
 
 
 | 118 | 
           echo $WINPATH >> $MNTTAB | 
 
 
 
 
 
 | 119 | 
 # | 
 
 
 
 
 
 | 120 | 
         elif test -f $FSTAB && test -r $FSTAB | 
 
 
 
 
 
 | 121 | 
         then | 
 
 
 
 
 
 | 122 | 
 # | 
 
 
 
 
 
 | 123 | 
 #         Read the configuration file, but could not find | 
 
 
 
 
 
 | 124 | 
 #         a mount specification matching the argument. | 
 
 
 
 
 
 | 125 | 
 # | 
 
 
 
 
 
 | 126 | 
           case $errno in | 
 
 
 
 
 
 | 127 | 
             1) echo >&2 "$0: $FSTAB: ambiguous reference for $MNTPATH" ;; | 
 
 
 
 
 
 | 128 | 
             2) echo >&2 "$0: $FSTAB: no mount specification for $MNTPATH" ;; | 
 
 
 
 
 
 | 129 | 
           esac | 
 
 
 
 
 
 | 130 | 
 # | 
 
 
 
 
 
 | 131 | 
         elif test -f $FSTAB | 
 
 
 
 
 
 | 132 | 
         then | 
 
 
 
 
 
 | 133 | 
 # | 
 
 
 
 
 
 | 134 | 
 #         Found the configuration file, but could not read it. | 
 
 
 
 
 
 | 135 | 
 # | 
 
 
 
 
 
 | 136 | 
           echo >&2 "$0: $FSTAB: cannot read configuration file" | 
 
 
 
 
 
 | 137 | 
 # | 
 
 
 
 
 
 | 138 | 
         else | 
 
 
 
 
 
 | 139 | 
 # | 
 
 
 
 
 
 | 140 | 
 #         Could not find the configuration file. | 
 
 
 
 
 
 | 141 | 
 # | 
 
 
 
 
 
 | 142 | 
           echo >&2 "$0: $FSTAB: configuration file not found" | 
 
 
 
 
 
 | 143 | 
         fi | 
 
 
 
 
 
 | 144 | 
 # | 
 
 
 
 
 
 | 145 | 
       elif test x"$replace" = xyes | 
 
 
 
 
 
 | 146 | 
       then | 
 
 
 
 
 
 | 147 | 
 # | 
 
 
 
 
 
 | 148 | 
 #       Found a conflicting active mount, | 
 
 
 
 
 
 | 149 | 
 #       but the 'replace' option WAS specified... | 
 
 
 
 
 
 | 150 | 
 #       Tear it down, and remount. | 
 
 
 
 
 
 | 151 | 
 # | 
 
 
 
 
 
 | 152 | 
         umount $MNTPATH | 
 
 
 
 
 
 | 153 | 
         mount $MNTPATH | 
 
 
 
 
 
 | 154 | 
         exit $? | 
 
 
 
 
 
 | 155 | 
 # | 
 
 
 
 
 
 | 156 | 
       else | 
 
 
 
 
 
 | 157 | 
 # | 
 
 
 
 
 
 | 158 | 
 #       Found a conflicting active mount, | 
 
 
 
 
 
 | 159 | 
 #       and the 'replace' option was NOT specified... | 
 
 
 
 
 
 | 160 | 
 #       Display an appropriate diagnostic message, depending on | 
 
 
 
 
 
 | 161 | 
 #       whether the argument represented a directory path name, | 
 
 
 
 
 
 | 162 | 
 #       or a mount point, and bail out. | 
 
 
 
 
 
 | 163 | 
 # | 
 
 
 
 
 
 | 164 | 
         case $? in | 
 
 
 
 
 
 | 165 | 
           1) echo >&2 "$0: '$MNTPATH' is already mounted" ;; | 
 
 
 
 
 
 | 166 | 
           2) echo >&2 "$0: mount point '$MNTPATH' is already in use" ;; | 
 
 
 
 
 
 | 167 | 
         esac | 
 
 
 
 
 
 | 168 | 
         exit 1 | 
 
 
 
 
 
 | 169 | 
       fi ;; | 
 
 
 
 
 
 | 170 | 
 # | 
 
 
 
 
 
 | 171 | 
     2) | 
 
 
 
 
 
 | 172 | 
 # | 
 
 
 
 
 
 | 173 | 
 #     Two arguments specified... | 
 
 
 
 
 
 | 174 | 
 #     First is directory path name, second is mount point. | 
 
 
 
 
 
 | 175 | 
 # | 
 
 
 
 
 
 | 176 | 
       WINPATH=`echo "$1" | tr '\\\\' /` | 
 
 
 
 
 
 | 177 | 
       MNTPATH=`echo "$2" | tr '\\\\' /` | 
 
 
 
 
 
 | 178 | 
 # | 
 
 
 
 
 
 | 179 | 
       if cat $MNTTAB | tr '\\' / | awk ' | 
 
 
 
 
 
 | 180 | 
 # | 
 
 
 
 
 
 | 181 | 
 #       Check that the mount point is not already in use; | 
 
 
 
 
 
 | 182 | 
 #       set exit status to:-- | 
 
 
 
 
 
 | 183 | 
 #         0: if no existing mount table entry matches; | 
 
 
 
 
 
 | 184 | 
 #         1: if mount point already in mount table. | 
 
 
 
 
 
 | 185 | 
 # | 
 
 
 
 
 
 | 186 | 
         BEGIN { status = 0 } | 
 
 
 
 
 
 | 187 | 
         /^#/ { $0 = "" } | 
 
 
 
 
 
 | 188 | 
         $2 == "'$MNTPATH'" { status = 1 } | 
 
 
 
 
 
 | 189 | 
         END { exit status }' | 
 
 
 
 
 
 | 190 | 
       then | 
 
 
 
 
 
 | 191 | 
 # | 
 
 
 
 
 
 | 192 | 
 #       Mount point not yet assigned... | 
 
 
 
 
 
 | 193 | 
 #       Update the mount table, to include the new specification. | 
 
 
 
 
 
 | 194 | 
 # | 
 
 
 
 
 
 | 195 | 
         echo -e "$WINPATH\t$MNTPATH" >> "$MNTTAB" | 
 
 
 
 
 
 | 196 | 
 # | 
 
 
 
 
 
 | 197 | 
       elif test x"$replace" = xyes | 
 
 
 
 
 
 | 198 | 
       then | 
 
 
 
 
 
 | 199 | 
 # | 
 
 
 
 
 
 | 200 | 
 #       The mount point is already in use, | 
 
 
 
 
 
 | 201 | 
 #       but the 'replace' option WAS specified... | 
 
 
 
 
 
 | 202 | 
 #       Tear it down, and remount. | 
 
 
 
 
 
 | 203 | 
 # | 
 
 
 
 
 
 | 204 | 
         umount $MNTPATH | 
 
 
 
 
 
 | 205 | 
         echo -e "$WINPATH\t$MNTPATH" >> "$MNTTAB" | 
 
 
 
 
 
 | 206 | 
 # | 
 
 
 
 
 
 | 207 | 
       else | 
 
 
 
 
 
 | 208 | 
 # | 
 
 
 
 
 
 | 209 | 
 #       Mount point is already in use, | 
 
 
 
 
 
 | 210 | 
 #       and the 'replace' option was NOT specified... | 
 
 
 
 
 
 | 211 | 
 #       Diagnose, and bail out. | 
 
 
 
 
 
 | 212 | 
 # | 
 
 
 
 
 
 | 213 | 
         echo >&2 "$0: mount point '$MNTPATH' is already in use" | 
 
 
 
 
 
 | 214 | 
         exit 1 | 
 
 
 
 
 
 | 215 | 
       fi ;; | 
 
 
 
 
 
 | 216 | 
 # | 
 
 
 
 
 
 | 217 | 
     *) | 
 
 
 
 
 
 | 218 | 
 # | 
 
 
 
 
 
 | 219 | 
 #     More than two arguments specified... | 
 
 
 
 
 
 | 220 | 
 #     Complain, and bail out. | 
 
 
 
 
 
 | 221 | 
 # | 
 
 
 
 
 
 | 222 | 
       echo >&2 "$0: incorrect number of arguments" | 
 
 
 
 
 
 | 223 | 
       echo >&2 "usage: mount [--replace] [<win32path> <msyspath>]" | 
 
 
 
 
 
 | 224 | 
       exit 2 ;; | 
 
 
 
 
 
 | 225 | 
   esac | 
 
 
 
 
 
 | 226 | 
 # | 
 
 
 
 
 
 | 227 | 
 # On successful completion, ensure we set the exit status appropriately. | 
 
 
 
 
 
 | 228 | 
 # | 
 
 
 
 
 
 | 229 | 
   exit 0 | 
 
 
 
 
 
 | 230 | 
 # | 
 
 
 
 
 
 | 231 | 
 # $RCSfile: mount,v $: end of file |