1e3a38431SPaul Bohm#!/bin/sh 2e3a38431SPaul Bohm# install - install a program, script, or datafile 3*93823e6cSPaul Bohm 4*93823e6cSPaul Bohmscriptversion=2011-11-20.07; # UTC 5*93823e6cSPaul Bohm 6e3a38431SPaul Bohm# This originates from X11R5 (mit/util/scripts/install.sh), which was 7e3a38431SPaul Bohm# later released in X11R6 (xc/config/util/install.sh) with the 8e3a38431SPaul Bohm# following copyright and license. 9e3a38431SPaul Bohm# 10e3a38431SPaul Bohm# Copyright (C) 1994 X Consortium 11e3a38431SPaul Bohm# 12e3a38431SPaul Bohm# Permission is hereby granted, free of charge, to any person obtaining a copy 13e3a38431SPaul Bohm# of this software and associated documentation files (the "Software"), to 14e3a38431SPaul Bohm# deal in the Software without restriction, including without limitation the 15e3a38431SPaul Bohm# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 16e3a38431SPaul Bohm# sell copies of the Software, and to permit persons to whom the Software is 17e3a38431SPaul Bohm# furnished to do so, subject to the following conditions: 18e3a38431SPaul Bohm# 19e3a38431SPaul Bohm# The above copyright notice and this permission notice shall be included in 20e3a38431SPaul Bohm# all copies or substantial portions of the Software. 21e3a38431SPaul Bohm# 22e3a38431SPaul Bohm# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23e3a38431SPaul Bohm# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24e3a38431SPaul Bohm# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25e3a38431SPaul Bohm# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 26e3a38431SPaul Bohm# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC- 27e3a38431SPaul Bohm# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 28e3a38431SPaul Bohm# 29e3a38431SPaul Bohm# Except as contained in this notice, the name of the X Consortium shall not 30e3a38431SPaul Bohm# be used in advertising or otherwise to promote the sale, use or other deal- 31e3a38431SPaul Bohm# ings in this Software without prior written authorization from the X Consor- 32e3a38431SPaul Bohm# tium. 33e3a38431SPaul Bohm# 34e3a38431SPaul Bohm# 35e3a38431SPaul Bohm# FSF changes to this file are in the public domain. 36e3a38431SPaul Bohm# 37e3a38431SPaul Bohm# Calling this script install-sh is preferred over install.sh, to prevent 38*93823e6cSPaul Bohm# 'make' implicit rules from creating a file called install from it 39e3a38431SPaul Bohm# when there is no Makefile. 40e3a38431SPaul Bohm# 41e3a38431SPaul Bohm# This script is compatible with the BSD install script, but was written 42*93823e6cSPaul Bohm# from scratch. 43e3a38431SPaul Bohm 44*93823e6cSPaul Bohmnl=' 45*93823e6cSPaul Bohm' 46*93823e6cSPaul BohmIFS=" "" $nl" 47e3a38431SPaul Bohm 48e3a38431SPaul Bohm# set DOITPROG to echo to test this script 49e3a38431SPaul Bohm 50e3a38431SPaul Bohm# Don't use :- since 4.3BSD and earlier shells don't like it. 51*93823e6cSPaul Bohmdoit=${DOITPROG-} 52*93823e6cSPaul Bohmif test -z "$doit"; then 53*93823e6cSPaul Bohm doit_exec=exec 54*93823e6cSPaul Bohmelse 55*93823e6cSPaul Bohm doit_exec=$doit 56*93823e6cSPaul Bohmfi 57e3a38431SPaul Bohm 58*93823e6cSPaul Bohm# Put in absolute file names if you don't have them in your path; 59*93823e6cSPaul Bohm# or use environment vars. 60e3a38431SPaul Bohm 61*93823e6cSPaul Bohmchgrpprog=${CHGRPPROG-chgrp} 62*93823e6cSPaul Bohmchmodprog=${CHMODPROG-chmod} 63*93823e6cSPaul Bohmchownprog=${CHOWNPROG-chown} 64*93823e6cSPaul Bohmcmpprog=${CMPPROG-cmp} 65*93823e6cSPaul Bohmcpprog=${CPPROG-cp} 66*93823e6cSPaul Bohmmkdirprog=${MKDIRPROG-mkdir} 67*93823e6cSPaul Bohmmvprog=${MVPROG-mv} 68*93823e6cSPaul Bohmrmprog=${RMPROG-rm} 69*93823e6cSPaul Bohmstripprog=${STRIPPROG-strip} 70e3a38431SPaul Bohm 71*93823e6cSPaul Bohmposix_glob='?' 72*93823e6cSPaul Bohminitialize_posix_glob=' 73*93823e6cSPaul Bohm test "$posix_glob" != "?" || { 74*93823e6cSPaul Bohm if (set -f) 2>/dev/null; then 75*93823e6cSPaul Bohm posix_glob= 76*93823e6cSPaul Bohm else 77*93823e6cSPaul Bohm posix_glob=: 78*93823e6cSPaul Bohm fi 79*93823e6cSPaul Bohm } 80*93823e6cSPaul Bohm' 81e3a38431SPaul Bohm 82*93823e6cSPaul Bohmposix_mkdir= 83*93823e6cSPaul Bohm 84*93823e6cSPaul Bohm# Desired mode of installed file. 85*93823e6cSPaul Bohmmode=0755 86*93823e6cSPaul Bohm 87*93823e6cSPaul Bohmchgrpcmd= 88*93823e6cSPaul Bohmchmodcmd=$chmodprog 89*93823e6cSPaul Bohmchowncmd= 90*93823e6cSPaul Bohmmvcmd=$mvprog 91e3a38431SPaul Bohmrmcmd="$rmprog -f" 92*93823e6cSPaul Bohmstripcmd= 93e3a38431SPaul Bohm 94*93823e6cSPaul Bohmsrc= 95*93823e6cSPaul Bohmdst= 96*93823e6cSPaul Bohmdir_arg= 97*93823e6cSPaul Bohmdst_arg= 98*93823e6cSPaul Bohm 99*93823e6cSPaul Bohmcopy_on_change=false 100*93823e6cSPaul Bohmno_target_directory= 101*93823e6cSPaul Bohm 102*93823e6cSPaul Bohmusage="\ 103*93823e6cSPaul BohmUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE 104*93823e6cSPaul Bohm or: $0 [OPTION]... SRCFILES... DIRECTORY 105*93823e6cSPaul Bohm or: $0 [OPTION]... -t DIRECTORY SRCFILES... 106*93823e6cSPaul Bohm or: $0 [OPTION]... -d DIRECTORIES... 107*93823e6cSPaul Bohm 108*93823e6cSPaul BohmIn the 1st form, copy SRCFILE to DSTFILE. 109*93823e6cSPaul BohmIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY. 110*93823e6cSPaul BohmIn the 4th, create DIRECTORIES. 111*93823e6cSPaul Bohm 112*93823e6cSPaul BohmOptions: 113*93823e6cSPaul Bohm --help display this help and exit. 114*93823e6cSPaul Bohm --version display version info and exit. 115*93823e6cSPaul Bohm 116*93823e6cSPaul Bohm -c (ignored) 117*93823e6cSPaul Bohm -C install only if different (preserve the last data modification time) 118*93823e6cSPaul Bohm -d create directories instead of installing files. 119*93823e6cSPaul Bohm -g GROUP $chgrpprog installed files to GROUP. 120*93823e6cSPaul Bohm -m MODE $chmodprog installed files to MODE. 121*93823e6cSPaul Bohm -o USER $chownprog installed files to USER. 122*93823e6cSPaul Bohm -s $stripprog installed files. 123*93823e6cSPaul Bohm -t DIRECTORY install into DIRECTORY. 124*93823e6cSPaul Bohm -T report an error if DSTFILE is a directory. 125*93823e6cSPaul Bohm 126*93823e6cSPaul BohmEnvironment variables override the default commands: 127*93823e6cSPaul Bohm CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG 128*93823e6cSPaul Bohm RMPROG STRIPPROG 129*93823e6cSPaul Bohm" 130*93823e6cSPaul Bohm 131*93823e6cSPaul Bohmwhile test $# -ne 0; do 132e3a38431SPaul Bohm case $1 in 133*93823e6cSPaul Bohm -c) ;; 134e3a38431SPaul Bohm 135*93823e6cSPaul Bohm -C) copy_on_change=true;; 136e3a38431SPaul Bohm 137*93823e6cSPaul Bohm -d) dir_arg=true;; 138e3a38431SPaul Bohm 139e3a38431SPaul Bohm -g) chgrpcmd="$chgrpprog $2" 140*93823e6cSPaul Bohm shift;; 141e3a38431SPaul Bohm 142*93823e6cSPaul Bohm --help) echo "$usage"; exit $?;; 143e3a38431SPaul Bohm 144*93823e6cSPaul Bohm -m) mode=$2 145*93823e6cSPaul Bohm case $mode in 146*93823e6cSPaul Bohm *' '* | *' '* | *' 147*93823e6cSPaul Bohm'* | *'*'* | *'?'* | *'['*) 148*93823e6cSPaul Bohm echo "$0: invalid mode: $mode" >&2 149*93823e6cSPaul Bohm exit 1;; 150*93823e6cSPaul Bohm esac 151*93823e6cSPaul Bohm shift;; 152e3a38431SPaul Bohm 153*93823e6cSPaul Bohm -o) chowncmd="$chownprog $2" 154*93823e6cSPaul Bohm shift;; 155e3a38431SPaul Bohm 156*93823e6cSPaul Bohm -s) stripcmd=$stripprog;; 157*93823e6cSPaul Bohm 158*93823e6cSPaul Bohm -t) dst_arg=$2 159*93823e6cSPaul Bohm # Protect names problematic for 'test' and other utilities. 160*93823e6cSPaul Bohm case $dst_arg in 161*93823e6cSPaul Bohm -* | [=\(\)!]) dst_arg=./$dst_arg;; 162*93823e6cSPaul Bohm esac 163*93823e6cSPaul Bohm shift;; 164*93823e6cSPaul Bohm 165*93823e6cSPaul Bohm -T) no_target_directory=true;; 166*93823e6cSPaul Bohm 167*93823e6cSPaul Bohm --version) echo "$0 $scriptversion"; exit $?;; 168*93823e6cSPaul Bohm 169*93823e6cSPaul Bohm --) shift 170*93823e6cSPaul Bohm break;; 171*93823e6cSPaul Bohm 172*93823e6cSPaul Bohm -*) echo "$0: invalid option: $1" >&2 173*93823e6cSPaul Bohm exit 1;; 174*93823e6cSPaul Bohm 175*93823e6cSPaul Bohm *) break;; 176*93823e6cSPaul Bohm esac 177*93823e6cSPaul Bohm shift 178*93823e6cSPaul Bohmdone 179*93823e6cSPaul Bohm 180*93823e6cSPaul Bohmif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then 181*93823e6cSPaul Bohm # When -d is used, all remaining arguments are directories to create. 182*93823e6cSPaul Bohm # When -t is used, the destination is already specified. 183*93823e6cSPaul Bohm # Otherwise, the last argument is the destination. Remove it from $@. 184*93823e6cSPaul Bohm for arg 185*93823e6cSPaul Bohm do 186*93823e6cSPaul Bohm if test -n "$dst_arg"; then 187*93823e6cSPaul Bohm # $@ is not empty: it contains at least $arg. 188*93823e6cSPaul Bohm set fnord "$@" "$dst_arg" 189*93823e6cSPaul Bohm shift # fnord 190e3a38431SPaul Bohm fi 191*93823e6cSPaul Bohm shift # arg 192*93823e6cSPaul Bohm dst_arg=$arg 193*93823e6cSPaul Bohm # Protect names problematic for 'test' and other utilities. 194*93823e6cSPaul Bohm case $dst_arg in 195*93823e6cSPaul Bohm -* | [=\(\)!]) dst_arg=./$dst_arg;; 196e3a38431SPaul Bohm esac 197e3a38431SPaul Bohm done 198*93823e6cSPaul Bohmfi 199e3a38431SPaul Bohm 200*93823e6cSPaul Bohmif test $# -eq 0; then 201*93823e6cSPaul Bohm if test -z "$dir_arg"; then 202*93823e6cSPaul Bohm echo "$0: no input file specified." >&2 203e3a38431SPaul Bohm exit 1 204*93823e6cSPaul Bohm fi 205*93823e6cSPaul Bohm # It's OK to call 'install-sh -d' without argument. 206*93823e6cSPaul Bohm # This can happen when creating conditional directories. 207*93823e6cSPaul Bohm exit 0 208e3a38431SPaul Bohmfi 209e3a38431SPaul Bohm 210*93823e6cSPaul Bohmif test -z "$dir_arg"; then 211*93823e6cSPaul Bohm do_exit='(exit $ret); exit $ret' 212*93823e6cSPaul Bohm trap "ret=129; $do_exit" 1 213*93823e6cSPaul Bohm trap "ret=130; $do_exit" 2 214*93823e6cSPaul Bohm trap "ret=141; $do_exit" 13 215*93823e6cSPaul Bohm trap "ret=143; $do_exit" 15 216*93823e6cSPaul Bohm 217*93823e6cSPaul Bohm # Set umask so as not to create temps with too-generous modes. 218*93823e6cSPaul Bohm # However, 'strip' requires both read and write access to temps. 219*93823e6cSPaul Bohm case $mode in 220*93823e6cSPaul Bohm # Optimize common cases. 221*93823e6cSPaul Bohm *644) cp_umask=133;; 222*93823e6cSPaul Bohm *755) cp_umask=22;; 223*93823e6cSPaul Bohm 224*93823e6cSPaul Bohm *[0-7]) 225*93823e6cSPaul Bohm if test -z "$stripcmd"; then 226*93823e6cSPaul Bohm u_plus_rw= 227*93823e6cSPaul Bohm else 228*93823e6cSPaul Bohm u_plus_rw='% 200' 229*93823e6cSPaul Bohm fi 230*93823e6cSPaul Bohm cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;; 231*93823e6cSPaul Bohm *) 232*93823e6cSPaul Bohm if test -z "$stripcmd"; then 233*93823e6cSPaul Bohm u_plus_rw= 234*93823e6cSPaul Bohm else 235*93823e6cSPaul Bohm u_plus_rw=,u+rw 236*93823e6cSPaul Bohm fi 237*93823e6cSPaul Bohm cp_umask=$mode$u_plus_rw;; 238*93823e6cSPaul Bohm esac 239*93823e6cSPaul Bohmfi 240*93823e6cSPaul Bohm 241*93823e6cSPaul Bohmfor src 242*93823e6cSPaul Bohmdo 243*93823e6cSPaul Bohm # Protect names problematic for 'test' and other utilities. 244*93823e6cSPaul Bohm case $src in 245*93823e6cSPaul Bohm -* | [=\(\)!]) src=./$src;; 246*93823e6cSPaul Bohm esac 247*93823e6cSPaul Bohm 248*93823e6cSPaul Bohm if test -n "$dir_arg"; then 249e3a38431SPaul Bohm dst=$src 250*93823e6cSPaul Bohm dstdir=$dst 251*93823e6cSPaul Bohm test -d "$dstdir" 252*93823e6cSPaul Bohm dstdir_status=$? 253e3a38431SPaul Bohm else 254e3a38431SPaul Bohm 255*93823e6cSPaul Bohm # Waiting for this to be detected by the "$cpprog $src $dsttmp" command 256e3a38431SPaul Bohm # might cause directories to be created, which would be especially bad 257e3a38431SPaul Bohm # if $src (and thus $dsttmp) contains '*'. 258*93823e6cSPaul Bohm if test ! -f "$src" && test ! -d "$src"; then 259*93823e6cSPaul Bohm echo "$0: $src does not exist." >&2 260e3a38431SPaul Bohm exit 1 261e3a38431SPaul Bohm fi 262e3a38431SPaul Bohm 263*93823e6cSPaul Bohm if test -z "$dst_arg"; then 264*93823e6cSPaul Bohm echo "$0: no destination specified." >&2 265e3a38431SPaul Bohm exit 1 266*93823e6cSPaul Bohm fi 267*93823e6cSPaul Bohm dst=$dst_arg 268*93823e6cSPaul Bohm 269*93823e6cSPaul Bohm # If destination is a directory, append the input filename; won't work 270*93823e6cSPaul Bohm # if double slashes aren't ignored. 271*93823e6cSPaul Bohm if test -d "$dst"; then 272*93823e6cSPaul Bohm if test -n "$no_target_directory"; then 273*93823e6cSPaul Bohm echo "$0: $dst_arg: Is a directory" >&2 274*93823e6cSPaul Bohm exit 1 275*93823e6cSPaul Bohm fi 276*93823e6cSPaul Bohm dstdir=$dst 277*93823e6cSPaul Bohm dst=$dstdir/`basename "$src"` 278*93823e6cSPaul Bohm dstdir_status=0 279e3a38431SPaul Bohm else 280*93823e6cSPaul Bohm # Prefer dirname, but fall back on a substitute if dirname fails. 281*93823e6cSPaul Bohm dstdir=` 282*93823e6cSPaul Bohm (dirname "$dst") 2>/dev/null || 283*93823e6cSPaul Bohm expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \ 284*93823e6cSPaul Bohm X"$dst" : 'X\(//\)[^/]' \| \ 285*93823e6cSPaul Bohm X"$dst" : 'X\(//\)$' \| \ 286*93823e6cSPaul Bohm X"$dst" : 'X\(/\)' \| . 2>/dev/null || 287*93823e6cSPaul Bohm echo X"$dst" | 288*93823e6cSPaul Bohm sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{ 289*93823e6cSPaul Bohm s//\1/ 290*93823e6cSPaul Bohm q 291*93823e6cSPaul Bohm } 292*93823e6cSPaul Bohm /^X\(\/\/\)[^/].*/{ 293*93823e6cSPaul Bohm s//\1/ 294*93823e6cSPaul Bohm q 295*93823e6cSPaul Bohm } 296*93823e6cSPaul Bohm /^X\(\/\/\)$/{ 297*93823e6cSPaul Bohm s//\1/ 298*93823e6cSPaul Bohm q 299*93823e6cSPaul Bohm } 300*93823e6cSPaul Bohm /^X\(\/\).*/{ 301*93823e6cSPaul Bohm s//\1/ 302*93823e6cSPaul Bohm q 303*93823e6cSPaul Bohm } 304*93823e6cSPaul Bohm s/.*/./; q' 305*93823e6cSPaul Bohm ` 306*93823e6cSPaul Bohm 307*93823e6cSPaul Bohm test -d "$dstdir" 308*93823e6cSPaul Bohm dstdir_status=$? 309*93823e6cSPaul Bohm fi 310e3a38431SPaul Bohm fi 311e3a38431SPaul Bohm 312*93823e6cSPaul Bohm obsolete_mkdir_used=false 313e3a38431SPaul Bohm 314*93823e6cSPaul Bohm if test $dstdir_status != 0; then 315*93823e6cSPaul Bohm case $posix_mkdir in 316*93823e6cSPaul Bohm '') 317*93823e6cSPaul Bohm # Create intermediate dirs using mode 755 as modified by the umask. 318*93823e6cSPaul Bohm # This is like FreeBSD 'install' as of 1997-10-28. 319*93823e6cSPaul Bohm umask=`umask` 320*93823e6cSPaul Bohm case $stripcmd.$umask in 321*93823e6cSPaul Bohm # Optimize common cases. 322*93823e6cSPaul Bohm *[2367][2367]) mkdir_umask=$umask;; 323*93823e6cSPaul Bohm .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;; 324*93823e6cSPaul Bohm 325*93823e6cSPaul Bohm *[0-7]) 326*93823e6cSPaul Bohm mkdir_umask=`expr $umask + 22 \ 327*93823e6cSPaul Bohm - $umask % 100 % 40 + $umask % 20 \ 328*93823e6cSPaul Bohm - $umask % 10 % 4 + $umask % 2 329*93823e6cSPaul Bohm `;; 330*93823e6cSPaul Bohm *) mkdir_umask=$umask,go-w;; 331*93823e6cSPaul Bohm esac 332*93823e6cSPaul Bohm 333*93823e6cSPaul Bohm # With -d, create the new directory with the user-specified mode. 334*93823e6cSPaul Bohm # Otherwise, rely on $mkdir_umask. 335*93823e6cSPaul Bohm if test -n "$dir_arg"; then 336*93823e6cSPaul Bohm mkdir_mode=-m$mode 337*93823e6cSPaul Bohm else 338*93823e6cSPaul Bohm mkdir_mode= 339*93823e6cSPaul Bohm fi 340*93823e6cSPaul Bohm 341*93823e6cSPaul Bohm posix_mkdir=false 342*93823e6cSPaul Bohm case $umask in 343*93823e6cSPaul Bohm *[123567][0-7][0-7]) 344*93823e6cSPaul Bohm # POSIX mkdir -p sets u+wx bits regardless of umask, which 345*93823e6cSPaul Bohm # is incompatible with FreeBSD 'install' when (umask & 300) != 0. 346*93823e6cSPaul Bohm ;; 347*93823e6cSPaul Bohm *) 348*93823e6cSPaul Bohm tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$ 349*93823e6cSPaul Bohm trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0 350*93823e6cSPaul Bohm 351*93823e6cSPaul Bohm if (umask $mkdir_umask && 352*93823e6cSPaul Bohm exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1 353e3a38431SPaul Bohm then 354*93823e6cSPaul Bohm if test -z "$dir_arg" || { 355*93823e6cSPaul Bohm # Check for POSIX incompatibilities with -m. 356*93823e6cSPaul Bohm # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or 357*93823e6cSPaul Bohm # other-writable bit of parent directory when it shouldn't. 358*93823e6cSPaul Bohm # FreeBSD 6.1 mkdir -m -p sets mode of existing directory. 359*93823e6cSPaul Bohm ls_ld_tmpdir=`ls -ld "$tmpdir"` 360*93823e6cSPaul Bohm case $ls_ld_tmpdir in 361*93823e6cSPaul Bohm d????-?r-*) different_mode=700;; 362*93823e6cSPaul Bohm d????-?--*) different_mode=755;; 363*93823e6cSPaul Bohm *) false;; 364*93823e6cSPaul Bohm esac && 365*93823e6cSPaul Bohm $mkdirprog -m$different_mode -p -- "$tmpdir" && { 366*93823e6cSPaul Bohm ls_ld_tmpdir_1=`ls -ld "$tmpdir"` 367*93823e6cSPaul Bohm test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1" 368*93823e6cSPaul Bohm } 369*93823e6cSPaul Bohm } 370*93823e6cSPaul Bohm then posix_mkdir=: 371*93823e6cSPaul Bohm fi 372*93823e6cSPaul Bohm rmdir "$tmpdir/d" "$tmpdir" 373e3a38431SPaul Bohm else 374*93823e6cSPaul Bohm # Remove any dirs left behind by ancient mkdir implementations. 375*93823e6cSPaul Bohm rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null 376e3a38431SPaul Bohm fi 377*93823e6cSPaul Bohm trap '' 0;; 378*93823e6cSPaul Bohm esac;; 379*93823e6cSPaul Bohm esac 380e3a38431SPaul Bohm 381*93823e6cSPaul Bohm if 382*93823e6cSPaul Bohm $posix_mkdir && ( 383*93823e6cSPaul Bohm umask $mkdir_umask && 384*93823e6cSPaul Bohm $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir" 385*93823e6cSPaul Bohm ) 386*93823e6cSPaul Bohm then : 387*93823e6cSPaul Bohm else 388e3a38431SPaul Bohm 389*93823e6cSPaul Bohm # The umask is ridiculous, or mkdir does not conform to POSIX, 390*93823e6cSPaul Bohm # or it failed possibly due to a race condition. Create the 391*93823e6cSPaul Bohm # directory the slow way, step by step, checking for races as we go. 392e3a38431SPaul Bohm 393*93823e6cSPaul Bohm case $dstdir in 394*93823e6cSPaul Bohm /*) prefix='/';; 395*93823e6cSPaul Bohm [-=\(\)!]*) prefix='./';; 396*93823e6cSPaul Bohm *) prefix='';; 397*93823e6cSPaul Bohm esac 398*93823e6cSPaul Bohm 399*93823e6cSPaul Bohm eval "$initialize_posix_glob" 400e3a38431SPaul Bohm 401e3a38431SPaul Bohm oIFS=$IFS 402*93823e6cSPaul Bohm IFS=/ 403*93823e6cSPaul Bohm $posix_glob set -f 404*93823e6cSPaul Bohm set fnord $dstdir 405*93823e6cSPaul Bohm shift 406*93823e6cSPaul Bohm $posix_glob set +f 407e3a38431SPaul Bohm IFS=$oIFS 408e3a38431SPaul Bohm 409*93823e6cSPaul Bohm prefixes= 410e3a38431SPaul Bohm 411*93823e6cSPaul Bohm for d 412*93823e6cSPaul Bohm do 413*93823e6cSPaul Bohm test X"$d" = X && continue 414e3a38431SPaul Bohm 415*93823e6cSPaul Bohm prefix=$prefix$d 416*93823e6cSPaul Bohm if test -d "$prefix"; then 417*93823e6cSPaul Bohm prefixes= 418e3a38431SPaul Bohm else 419*93823e6cSPaul Bohm if $posix_mkdir; then 420*93823e6cSPaul Bohm (umask=$mkdir_umask && 421*93823e6cSPaul Bohm $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break 422*93823e6cSPaul Bohm # Don't fail if two instances are running concurrently. 423*93823e6cSPaul Bohm test -d "$prefix" || exit 1 424*93823e6cSPaul Bohm else 425*93823e6cSPaul Bohm case $prefix in 426*93823e6cSPaul Bohm *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;; 427*93823e6cSPaul Bohm *) qprefix=$prefix;; 428*93823e6cSPaul Bohm esac 429*93823e6cSPaul Bohm prefixes="$prefixes '$qprefix'" 430e3a38431SPaul Bohm fi 431*93823e6cSPaul Bohm fi 432*93823e6cSPaul Bohm prefix=$prefix/ 433e3a38431SPaul Bohm done 434*93823e6cSPaul Bohm 435*93823e6cSPaul Bohm if test -n "$prefixes"; then 436*93823e6cSPaul Bohm # Don't fail if two instances are running concurrently. 437*93823e6cSPaul Bohm (umask $mkdir_umask && 438*93823e6cSPaul Bohm eval "\$doit_exec \$mkdirprog $prefixes") || 439*93823e6cSPaul Bohm test -d "$dstdir" || exit 1 440*93823e6cSPaul Bohm obsolete_mkdir_used=true 441*93823e6cSPaul Bohm fi 442*93823e6cSPaul Bohm fi 443e3a38431SPaul Bohm fi 444e3a38431SPaul Bohm 445*93823e6cSPaul Bohm if test -n "$dir_arg"; then 446*93823e6cSPaul Bohm { test -z "$chowncmd" || $doit $chowncmd "$dst"; } && 447*93823e6cSPaul Bohm { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } && 448*93823e6cSPaul Bohm { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false || 449*93823e6cSPaul Bohm test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1 450e3a38431SPaul Bohm else 451e3a38431SPaul Bohm 452e3a38431SPaul Bohm # Make a couple of temp file names in the proper directory. 453e3a38431SPaul Bohm dsttmp=$dstdir/_inst.$$_ 454e3a38431SPaul Bohm rmtmp=$dstdir/_rm.$$_ 455e3a38431SPaul Bohm 456*93823e6cSPaul Bohm # Trap to clean up those temp files at exit. 457*93823e6cSPaul Bohm trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0 458e3a38431SPaul Bohm 459*93823e6cSPaul Bohm # Copy the file name to the temp name. 460*93823e6cSPaul Bohm (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") && 461e3a38431SPaul Bohm 462*93823e6cSPaul Bohm # and set any options; do chmod last to preserve setuid bits. 463*93823e6cSPaul Bohm # 464e3a38431SPaul Bohm # If any of these fail, we abort the whole thing. If we want to 465e3a38431SPaul Bohm # ignore errors from any of these, just make sure not to ignore 466*93823e6cSPaul Bohm # errors from the above "$doit $cpprog $src $dsttmp" command. 467*93823e6cSPaul Bohm # 468*93823e6cSPaul Bohm { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } && 469*93823e6cSPaul Bohm { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } && 470*93823e6cSPaul Bohm { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } && 471*93823e6cSPaul Bohm { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } && 472e3a38431SPaul Bohm 473*93823e6cSPaul Bohm # If -C, don't bother to copy if it wouldn't change the file. 474*93823e6cSPaul Bohm if $copy_on_change && 475*93823e6cSPaul Bohm old=`LC_ALL=C ls -dlL "$dst" 2>/dev/null` && 476*93823e6cSPaul Bohm new=`LC_ALL=C ls -dlL "$dsttmp" 2>/dev/null` && 477e3a38431SPaul Bohm 478*93823e6cSPaul Bohm eval "$initialize_posix_glob" && 479*93823e6cSPaul Bohm $posix_glob set -f && 480*93823e6cSPaul Bohm set X $old && old=:$2:$4:$5:$6 && 481*93823e6cSPaul Bohm set X $new && new=:$2:$4:$5:$6 && 482*93823e6cSPaul Bohm $posix_glob set +f && 483e3a38431SPaul Bohm 484*93823e6cSPaul Bohm test "$old" = "$new" && 485*93823e6cSPaul Bohm $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1 486e3a38431SPaul Bohm then 487*93823e6cSPaul Bohm rm -f "$dsttmp" 488e3a38431SPaul Bohm else 489*93823e6cSPaul Bohm # Rename the file to the real destination. 490*93823e6cSPaul Bohm $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null || 491*93823e6cSPaul Bohm 492*93823e6cSPaul Bohm # The rename failed, perhaps because mv can't rename something else 493*93823e6cSPaul Bohm # to itself, or perhaps because mv is so ancient that it does not 494*93823e6cSPaul Bohm # support -f. 495*93823e6cSPaul Bohm { 496*93823e6cSPaul Bohm # Now remove or move aside any old file at destination location. 497*93823e6cSPaul Bohm # We try this two ways since rm can't unlink itself on some 498*93823e6cSPaul Bohm # systems and the destination file might be busy for other 499*93823e6cSPaul Bohm # reasons. In this case, the final cleanup might fail but the new 500*93823e6cSPaul Bohm # file should still install successfully. 501*93823e6cSPaul Bohm { 502*93823e6cSPaul Bohm test ! -f "$dst" || 503*93823e6cSPaul Bohm $doit $rmcmd -f "$dst" 2>/dev/null || 504*93823e6cSPaul Bohm { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null && 505*93823e6cSPaul Bohm { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; } 506*93823e6cSPaul Bohm } || 507*93823e6cSPaul Bohm { echo "$0: cannot unlink or rename $dst" >&2 508*93823e6cSPaul Bohm (exit 1); exit 1 509*93823e6cSPaul Bohm } 510e3a38431SPaul Bohm } && 511e3a38431SPaul Bohm 512e3a38431SPaul Bohm # Now rename the file to the real destination. 513*93823e6cSPaul Bohm $doit $mvcmd "$dsttmp" "$dst" 514e3a38431SPaul Bohm } 515*93823e6cSPaul Bohm fi || exit 1 516*93823e6cSPaul Bohm 517*93823e6cSPaul Bohm trap '' 0 518*93823e6cSPaul Bohm fi 519*93823e6cSPaul Bohmdone 520*93823e6cSPaul Bohm 521*93823e6cSPaul Bohm# Local variables: 522*93823e6cSPaul Bohm# eval: (add-hook 'write-file-hooks 'time-stamp) 523*93823e6cSPaul Bohm# time-stamp-start: "scriptversion=" 524*93823e6cSPaul Bohm# time-stamp-format: "%:y-%02m-%02d.%02H" 525*93823e6cSPaul Bohm# time-stamp-time-zone: "UTC" 526*93823e6cSPaul Bohm# time-stamp-end: "; # UTC" 527*93823e6cSPaul Bohm# End: 528