install-sh revision 81440437
164ce7165Smrg#!/bin/sh 264ce7165Smrg# install - install a program, script, or datafile 364ce7165Smrg 481440437Smrgscriptversion=2011-11-20.07; # UTC 564ce7165Smrg 664ce7165Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was 764ce7165Smrg# later released in X11R6 (xc/config/util/install.sh) with the 864ce7165Smrg# following copyright and license. 964ce7165Smrg# 1064ce7165Smrg# Copyright (C) 1994 X Consortium 1164ce7165Smrg# 1264ce7165Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy 1364ce7165Smrg# of this software and associated documentation files (the "Software"), to 1464ce7165Smrg# deal in the Software without restriction, including without limitation the 1564ce7165Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 1664ce7165Smrg# sell copies of the Software, and to permit persons to whom the Software is 1764ce7165Smrg# furnished to do so, subject to the following conditions: 1864ce7165Smrg# 1964ce7165Smrg# The above copyright notice and this permission notice shall be included in 2064ce7165Smrg# all copies or substantial portions of the Software. 2164ce7165Smrg# 2264ce7165Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 2364ce7165Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 2464ce7165Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 2564ce7165Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 2664ce7165Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC- 2764ce7165Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 2864ce7165Smrg# 2964ce7165Smrg# Except as contained in this notice, the name of the X Consortium shall not 3064ce7165Smrg# be used in advertising or otherwise to promote the sale, use or other deal- 3164ce7165Smrg# ings in this Software without prior written authorization from the X Consor- 3264ce7165Smrg# tium. 3364ce7165Smrg# 3464ce7165Smrg# 3564ce7165Smrg# FSF changes to this file are in the public domain. 3664ce7165Smrg# 3764ce7165Smrg# Calling this script install-sh is preferred over install.sh, to prevent 3881440437Smrg# 'make' implicit rules from creating a file called install from it 3964ce7165Smrg# when there is no Makefile. 4064ce7165Smrg# 4164ce7165Smrg# This script is compatible with the BSD install script, but was written 4264ce7165Smrg# from scratch. 4364ce7165Smrg 4464ce7165Smrgnl=' 4564ce7165Smrg' 4664ce7165SmrgIFS=" "" $nl" 4764ce7165Smrg 4864ce7165Smrg# set DOITPROG to echo to test this script 4964ce7165Smrg 5064ce7165Smrg# Don't use :- since 4.3BSD and earlier shells don't like it. 51fb23d3a8Smrgdoit=${DOITPROG-} 5264ce7165Smrgif test -z "$doit"; then 5364ce7165Smrg doit_exec=exec 5464ce7165Smrgelse 5564ce7165Smrg doit_exec=$doit 5664ce7165Smrgfi 5764ce7165Smrg 5864ce7165Smrg# Put in absolute file names if you don't have them in your path; 5964ce7165Smrg# or use environment vars. 6064ce7165Smrg 61fb23d3a8Smrgchgrpprog=${CHGRPPROG-chgrp} 62fb23d3a8Smrgchmodprog=${CHMODPROG-chmod} 63fb23d3a8Smrgchownprog=${CHOWNPROG-chown} 64fb23d3a8Smrgcmpprog=${CMPPROG-cmp} 65fb23d3a8Smrgcpprog=${CPPROG-cp} 66fb23d3a8Smrgmkdirprog=${MKDIRPROG-mkdir} 67fb23d3a8Smrgmvprog=${MVPROG-mv} 68fb23d3a8Smrgrmprog=${RMPROG-rm} 69fb23d3a8Smrgstripprog=${STRIPPROG-strip} 70fb23d3a8Smrg 71fb23d3a8Smrgposix_glob='?' 72fb23d3a8Smrginitialize_posix_glob=' 73fb23d3a8Smrg test "$posix_glob" != "?" || { 74fb23d3a8Smrg if (set -f) 2>/dev/null; then 75fb23d3a8Smrg posix_glob= 76fb23d3a8Smrg else 77fb23d3a8Smrg posix_glob=: 78fb23d3a8Smrg fi 79fb23d3a8Smrg } 80fb23d3a8Smrg' 8164ce7165Smrg 8264ce7165Smrgposix_mkdir= 8364ce7165Smrg 8464ce7165Smrg# Desired mode of installed file. 8564ce7165Smrgmode=0755 8664ce7165Smrg 87fb23d3a8Smrgchgrpcmd= 8864ce7165Smrgchmodcmd=$chmodprog 8964ce7165Smrgchowncmd= 90fb23d3a8Smrgmvcmd=$mvprog 9164ce7165Smrgrmcmd="$rmprog -f" 92fb23d3a8Smrgstripcmd= 93fb23d3a8Smrg 9464ce7165Smrgsrc= 9564ce7165Smrgdst= 9664ce7165Smrgdir_arg= 97fb23d3a8Smrgdst_arg= 98fb23d3a8Smrg 99fb23d3a8Smrgcopy_on_change=false 10064ce7165Smrgno_target_directory= 10164ce7165Smrg 102fb23d3a8Smrgusage="\ 103fb23d3a8SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE 10464ce7165Smrg or: $0 [OPTION]... SRCFILES... DIRECTORY 10564ce7165Smrg or: $0 [OPTION]... -t DIRECTORY SRCFILES... 10664ce7165Smrg or: $0 [OPTION]... -d DIRECTORIES... 10764ce7165Smrg 10864ce7165SmrgIn the 1st form, copy SRCFILE to DSTFILE. 10964ce7165SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY. 11064ce7165SmrgIn the 4th, create DIRECTORIES. 11164ce7165Smrg 11264ce7165SmrgOptions: 113fb23d3a8Smrg --help display this help and exit. 114fb23d3a8Smrg --version display version info and exit. 115fb23d3a8Smrg 116fb23d3a8Smrg -c (ignored) 117fb23d3a8Smrg -C install only if different (preserve the last data modification time) 118fb23d3a8Smrg -d create directories instead of installing files. 119fb23d3a8Smrg -g GROUP $chgrpprog installed files to GROUP. 120fb23d3a8Smrg -m MODE $chmodprog installed files to MODE. 121fb23d3a8Smrg -o USER $chownprog installed files to USER. 122fb23d3a8Smrg -s $stripprog installed files. 123fb23d3a8Smrg -t DIRECTORY install into DIRECTORY. 124fb23d3a8Smrg -T report an error if DSTFILE is a directory. 12564ce7165Smrg 12664ce7165SmrgEnvironment variables override the default commands: 127fb23d3a8Smrg CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG 128fb23d3a8Smrg RMPROG STRIPPROG 12964ce7165Smrg" 13064ce7165Smrg 13164ce7165Smrgwhile test $# -ne 0; do 13264ce7165Smrg case $1 in 133fb23d3a8Smrg -c) ;; 134fb23d3a8Smrg 135fb23d3a8Smrg -C) copy_on_change=true;; 13664ce7165Smrg 137fb23d3a8Smrg -d) dir_arg=true;; 13864ce7165Smrg 13964ce7165Smrg -g) chgrpcmd="$chgrpprog $2" 140fb23d3a8Smrg shift;; 14164ce7165Smrg 14264ce7165Smrg --help) echo "$usage"; exit $?;; 14364ce7165Smrg 14464ce7165Smrg -m) mode=$2 14564ce7165Smrg case $mode in 14664ce7165Smrg *' '* | *' '* | *' 14764ce7165Smrg'* | *'*'* | *'?'* | *'['*) 14864ce7165Smrg echo "$0: invalid mode: $mode" >&2 14964ce7165Smrg exit 1;; 15064ce7165Smrg esac 151fb23d3a8Smrg shift;; 15264ce7165Smrg 15364ce7165Smrg -o) chowncmd="$chownprog $2" 154fb23d3a8Smrg shift;; 15564ce7165Smrg 156fb23d3a8Smrg -s) stripcmd=$stripprog;; 15764ce7165Smrg 158fb23d3a8Smrg -t) dst_arg=$2 15981440437Smrg # Protect names problematic for 'test' and other utilities. 16081440437Smrg case $dst_arg in 16181440437Smrg -* | [=\(\)!]) dst_arg=./$dst_arg;; 16281440437Smrg esac 163fb23d3a8Smrg shift;; 16464ce7165Smrg 165fb23d3a8Smrg -T) no_target_directory=true;; 16664ce7165Smrg 16764ce7165Smrg --version) echo "$0 $scriptversion"; exit $?;; 16864ce7165Smrg 16964ce7165Smrg --) shift 17064ce7165Smrg break;; 17164ce7165Smrg 17264ce7165Smrg -*) echo "$0: invalid option: $1" >&2 17364ce7165Smrg exit 1;; 17464ce7165Smrg 17564ce7165Smrg *) break;; 17664ce7165Smrg esac 177fb23d3a8Smrg shift 17864ce7165Smrgdone 17964ce7165Smrg 180fb23d3a8Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then 18164ce7165Smrg # When -d is used, all remaining arguments are directories to create. 18264ce7165Smrg # When -t is used, the destination is already specified. 18364ce7165Smrg # Otherwise, the last argument is the destination. Remove it from $@. 18464ce7165Smrg for arg 18564ce7165Smrg do 186fb23d3a8Smrg if test -n "$dst_arg"; then 18764ce7165Smrg # $@ is not empty: it contains at least $arg. 188fb23d3a8Smrg set fnord "$@" "$dst_arg" 18964ce7165Smrg shift # fnord 19064ce7165Smrg fi 19164ce7165Smrg shift # arg 192fb23d3a8Smrg dst_arg=$arg 19381440437Smrg # Protect names problematic for 'test' and other utilities. 19481440437Smrg case $dst_arg in 19581440437Smrg -* | [=\(\)!]) dst_arg=./$dst_arg;; 19681440437Smrg esac 19764ce7165Smrg done 19864ce7165Smrgfi 19964ce7165Smrg 20064ce7165Smrgif test $# -eq 0; then 20164ce7165Smrg if test -z "$dir_arg"; then 20264ce7165Smrg echo "$0: no input file specified." >&2 20364ce7165Smrg exit 1 20464ce7165Smrg fi 20581440437Smrg # It's OK to call 'install-sh -d' without argument. 20664ce7165Smrg # This can happen when creating conditional directories. 20764ce7165Smrg exit 0 20864ce7165Smrgfi 20964ce7165Smrg 21064ce7165Smrgif test -z "$dir_arg"; then 21181440437Smrg do_exit='(exit $ret); exit $ret' 21281440437Smrg trap "ret=129; $do_exit" 1 21381440437Smrg trap "ret=130; $do_exit" 2 21481440437Smrg trap "ret=141; $do_exit" 13 21581440437Smrg trap "ret=143; $do_exit" 15 21664ce7165Smrg 21764ce7165Smrg # Set umask so as not to create temps with too-generous modes. 21864ce7165Smrg # However, 'strip' requires both read and write access to temps. 21964ce7165Smrg case $mode in 22064ce7165Smrg # Optimize common cases. 22164ce7165Smrg *644) cp_umask=133;; 22264ce7165Smrg *755) cp_umask=22;; 22364ce7165Smrg 22464ce7165Smrg *[0-7]) 22564ce7165Smrg if test -z "$stripcmd"; then 22664ce7165Smrg u_plus_rw= 22764ce7165Smrg else 22864ce7165Smrg u_plus_rw='% 200' 22964ce7165Smrg fi 23064ce7165Smrg cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;; 23164ce7165Smrg *) 23264ce7165Smrg if test -z "$stripcmd"; then 23364ce7165Smrg u_plus_rw= 23464ce7165Smrg else 23564ce7165Smrg u_plus_rw=,u+rw 23664ce7165Smrg fi 23764ce7165Smrg cp_umask=$mode$u_plus_rw;; 23864ce7165Smrg esac 23964ce7165Smrgfi 24064ce7165Smrg 24164ce7165Smrgfor src 24264ce7165Smrgdo 24381440437Smrg # Protect names problematic for 'test' and other utilities. 24464ce7165Smrg case $src in 24581440437Smrg -* | [=\(\)!]) src=./$src;; 24664ce7165Smrg esac 24764ce7165Smrg 24864ce7165Smrg if test -n "$dir_arg"; then 24964ce7165Smrg dst=$src 25064ce7165Smrg dstdir=$dst 25164ce7165Smrg test -d "$dstdir" 25264ce7165Smrg dstdir_status=$? 25364ce7165Smrg else 25464ce7165Smrg 25564ce7165Smrg # Waiting for this to be detected by the "$cpprog $src $dsttmp" command 25664ce7165Smrg # might cause directories to be created, which would be especially bad 25764ce7165Smrg # if $src (and thus $dsttmp) contains '*'. 25864ce7165Smrg if test ! -f "$src" && test ! -d "$src"; then 25964ce7165Smrg echo "$0: $src does not exist." >&2 26064ce7165Smrg exit 1 26164ce7165Smrg fi 26264ce7165Smrg 263fb23d3a8Smrg if test -z "$dst_arg"; then 26464ce7165Smrg echo "$0: no destination specified." >&2 26564ce7165Smrg exit 1 26664ce7165Smrg fi 267fb23d3a8Smrg dst=$dst_arg 26864ce7165Smrg 26964ce7165Smrg # If destination is a directory, append the input filename; won't work 27064ce7165Smrg # if double slashes aren't ignored. 27164ce7165Smrg if test -d "$dst"; then 27264ce7165Smrg if test -n "$no_target_directory"; then 273fb23d3a8Smrg echo "$0: $dst_arg: Is a directory" >&2 27464ce7165Smrg exit 1 27564ce7165Smrg fi 27664ce7165Smrg dstdir=$dst 27764ce7165Smrg dst=$dstdir/`basename "$src"` 27864ce7165Smrg dstdir_status=0 27964ce7165Smrg else 28064ce7165Smrg # Prefer dirname, but fall back on a substitute if dirname fails. 28164ce7165Smrg dstdir=` 28264ce7165Smrg (dirname "$dst") 2>/dev/null || 28364ce7165Smrg expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \ 28464ce7165Smrg X"$dst" : 'X\(//\)[^/]' \| \ 28564ce7165Smrg X"$dst" : 'X\(//\)$' \| \ 28664ce7165Smrg X"$dst" : 'X\(/\)' \| . 2>/dev/null || 28764ce7165Smrg echo X"$dst" | 28864ce7165Smrg sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{ 28964ce7165Smrg s//\1/ 29064ce7165Smrg q 29164ce7165Smrg } 29264ce7165Smrg /^X\(\/\/\)[^/].*/{ 29364ce7165Smrg s//\1/ 29464ce7165Smrg q 29564ce7165Smrg } 29664ce7165Smrg /^X\(\/\/\)$/{ 29764ce7165Smrg s//\1/ 29864ce7165Smrg q 29964ce7165Smrg } 30064ce7165Smrg /^X\(\/\).*/{ 30164ce7165Smrg s//\1/ 30264ce7165Smrg q 30364ce7165Smrg } 30464ce7165Smrg s/.*/./; q' 30564ce7165Smrg ` 30664ce7165Smrg 30764ce7165Smrg test -d "$dstdir" 30864ce7165Smrg dstdir_status=$? 30964ce7165Smrg fi 31064ce7165Smrg fi 31164ce7165Smrg 31264ce7165Smrg obsolete_mkdir_used=false 31364ce7165Smrg 31464ce7165Smrg if test $dstdir_status != 0; then 31564ce7165Smrg case $posix_mkdir in 31664ce7165Smrg '') 31764ce7165Smrg # Create intermediate dirs using mode 755 as modified by the umask. 31864ce7165Smrg # This is like FreeBSD 'install' as of 1997-10-28. 31964ce7165Smrg umask=`umask` 32064ce7165Smrg case $stripcmd.$umask in 32164ce7165Smrg # Optimize common cases. 32264ce7165Smrg *[2367][2367]) mkdir_umask=$umask;; 32364ce7165Smrg .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;; 32464ce7165Smrg 32564ce7165Smrg *[0-7]) 32664ce7165Smrg mkdir_umask=`expr $umask + 22 \ 32764ce7165Smrg - $umask % 100 % 40 + $umask % 20 \ 32864ce7165Smrg - $umask % 10 % 4 + $umask % 2 32964ce7165Smrg `;; 33064ce7165Smrg *) mkdir_umask=$umask,go-w;; 33164ce7165Smrg esac 33264ce7165Smrg 33364ce7165Smrg # With -d, create the new directory with the user-specified mode. 33464ce7165Smrg # Otherwise, rely on $mkdir_umask. 33564ce7165Smrg if test -n "$dir_arg"; then 33664ce7165Smrg mkdir_mode=-m$mode 33764ce7165Smrg else 33864ce7165Smrg mkdir_mode= 33964ce7165Smrg fi 34064ce7165Smrg 34164ce7165Smrg posix_mkdir=false 34264ce7165Smrg case $umask in 34364ce7165Smrg *[123567][0-7][0-7]) 34464ce7165Smrg # POSIX mkdir -p sets u+wx bits regardless of umask, which 34564ce7165Smrg # is incompatible with FreeBSD 'install' when (umask & 300) != 0. 34664ce7165Smrg ;; 34764ce7165Smrg *) 34864ce7165Smrg tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$ 34964ce7165Smrg trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0 35064ce7165Smrg 35164ce7165Smrg if (umask $mkdir_umask && 35264ce7165Smrg exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1 35364ce7165Smrg then 35464ce7165Smrg if test -z "$dir_arg" || { 35564ce7165Smrg # Check for POSIX incompatibilities with -m. 35664ce7165Smrg # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or 35781440437Smrg # other-writable bit of parent directory when it shouldn't. 35864ce7165Smrg # FreeBSD 6.1 mkdir -m -p sets mode of existing directory. 35964ce7165Smrg ls_ld_tmpdir=`ls -ld "$tmpdir"` 36064ce7165Smrg case $ls_ld_tmpdir in 36164ce7165Smrg d????-?r-*) different_mode=700;; 36264ce7165Smrg d????-?--*) different_mode=755;; 36364ce7165Smrg *) false;; 36464ce7165Smrg esac && 36564ce7165Smrg $mkdirprog -m$different_mode -p -- "$tmpdir" && { 36664ce7165Smrg ls_ld_tmpdir_1=`ls -ld "$tmpdir"` 36764ce7165Smrg test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1" 36864ce7165Smrg } 36964ce7165Smrg } 37064ce7165Smrg then posix_mkdir=: 37164ce7165Smrg fi 37264ce7165Smrg rmdir "$tmpdir/d" "$tmpdir" 37364ce7165Smrg else 37464ce7165Smrg # Remove any dirs left behind by ancient mkdir implementations. 37564ce7165Smrg rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null 37664ce7165Smrg fi 37764ce7165Smrg trap '' 0;; 37864ce7165Smrg esac;; 37964ce7165Smrg esac 38064ce7165Smrg 38164ce7165Smrg if 38264ce7165Smrg $posix_mkdir && ( 38364ce7165Smrg umask $mkdir_umask && 38464ce7165Smrg $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir" 38564ce7165Smrg ) 38664ce7165Smrg then : 38764ce7165Smrg else 38864ce7165Smrg 38964ce7165Smrg # The umask is ridiculous, or mkdir does not conform to POSIX, 39064ce7165Smrg # or it failed possibly due to a race condition. Create the 39164ce7165Smrg # directory the slow way, step by step, checking for races as we go. 39264ce7165Smrg 39364ce7165Smrg case $dstdir in 394fb23d3a8Smrg /*) prefix='/';; 39581440437Smrg [-=\(\)!]*) prefix='./';; 396fb23d3a8Smrg *) prefix='';; 39764ce7165Smrg esac 39864ce7165Smrg 399fb23d3a8Smrg eval "$initialize_posix_glob" 40064ce7165Smrg 40164ce7165Smrg oIFS=$IFS 40264ce7165Smrg IFS=/ 403fb23d3a8Smrg $posix_glob set -f 40464ce7165Smrg set fnord $dstdir 40564ce7165Smrg shift 406fb23d3a8Smrg $posix_glob set +f 40764ce7165Smrg IFS=$oIFS 40864ce7165Smrg 40964ce7165Smrg prefixes= 41064ce7165Smrg 41164ce7165Smrg for d 41264ce7165Smrg do 41381440437Smrg test X"$d" = X && continue 41464ce7165Smrg 41564ce7165Smrg prefix=$prefix$d 41664ce7165Smrg if test -d "$prefix"; then 41764ce7165Smrg prefixes= 41864ce7165Smrg else 41964ce7165Smrg if $posix_mkdir; then 42064ce7165Smrg (umask=$mkdir_umask && 42164ce7165Smrg $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break 42264ce7165Smrg # Don't fail if two instances are running concurrently. 42364ce7165Smrg test -d "$prefix" || exit 1 42464ce7165Smrg else 42564ce7165Smrg case $prefix in 42664ce7165Smrg *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;; 42764ce7165Smrg *) qprefix=$prefix;; 42864ce7165Smrg esac 42964ce7165Smrg prefixes="$prefixes '$qprefix'" 43064ce7165Smrg fi 43164ce7165Smrg fi 43264ce7165Smrg prefix=$prefix/ 43364ce7165Smrg done 43464ce7165Smrg 43564ce7165Smrg if test -n "$prefixes"; then 43664ce7165Smrg # Don't fail if two instances are running concurrently. 43764ce7165Smrg (umask $mkdir_umask && 43864ce7165Smrg eval "\$doit_exec \$mkdirprog $prefixes") || 43964ce7165Smrg test -d "$dstdir" || exit 1 44064ce7165Smrg obsolete_mkdir_used=true 44164ce7165Smrg fi 44264ce7165Smrg fi 44364ce7165Smrg fi 44464ce7165Smrg 44564ce7165Smrg if test -n "$dir_arg"; then 44664ce7165Smrg { test -z "$chowncmd" || $doit $chowncmd "$dst"; } && 44764ce7165Smrg { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } && 44864ce7165Smrg { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false || 44964ce7165Smrg test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1 45064ce7165Smrg else 45164ce7165Smrg 45264ce7165Smrg # Make a couple of temp file names in the proper directory. 45364ce7165Smrg dsttmp=$dstdir/_inst.$$_ 45464ce7165Smrg rmtmp=$dstdir/_rm.$$_ 45564ce7165Smrg 45664ce7165Smrg # Trap to clean up those temp files at exit. 45764ce7165Smrg trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0 45864ce7165Smrg 45964ce7165Smrg # Copy the file name to the temp name. 46064ce7165Smrg (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") && 46164ce7165Smrg 46264ce7165Smrg # and set any options; do chmod last to preserve setuid bits. 46364ce7165Smrg # 46464ce7165Smrg # If any of these fail, we abort the whole thing. If we want to 46564ce7165Smrg # ignore errors from any of these, just make sure not to ignore 46664ce7165Smrg # errors from the above "$doit $cpprog $src $dsttmp" command. 46764ce7165Smrg # 468fb23d3a8Smrg { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } && 469fb23d3a8Smrg { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } && 470fb23d3a8Smrg { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } && 471fb23d3a8Smrg { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } && 472fb23d3a8Smrg 473fb23d3a8Smrg # If -C, don't bother to copy if it wouldn't change the file. 474fb23d3a8Smrg if $copy_on_change && 475fb23d3a8Smrg old=`LC_ALL=C ls -dlL "$dst" 2>/dev/null` && 476fb23d3a8Smrg new=`LC_ALL=C ls -dlL "$dsttmp" 2>/dev/null` && 477fb23d3a8Smrg 478fb23d3a8Smrg eval "$initialize_posix_glob" && 479fb23d3a8Smrg $posix_glob set -f && 480fb23d3a8Smrg set X $old && old=:$2:$4:$5:$6 && 481fb23d3a8Smrg set X $new && new=:$2:$4:$5:$6 && 482fb23d3a8Smrg $posix_glob set +f && 483fb23d3a8Smrg 484fb23d3a8Smrg test "$old" = "$new" && 485fb23d3a8Smrg $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1 486fb23d3a8Smrg then 487fb23d3a8Smrg rm -f "$dsttmp" 488fb23d3a8Smrg else 489fb23d3a8Smrg # Rename the file to the real destination. 490fb23d3a8Smrg $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null || 491fb23d3a8Smrg 492fb23d3a8Smrg # The rename failed, perhaps because mv can't rename something else 493fb23d3a8Smrg # to itself, or perhaps because mv is so ancient that it does not 494fb23d3a8Smrg # support -f. 495fb23d3a8Smrg { 496fb23d3a8Smrg # Now remove or move aside any old file at destination location. 497fb23d3a8Smrg # We try this two ways since rm can't unlink itself on some 498fb23d3a8Smrg # systems and the destination file might be busy for other 499fb23d3a8Smrg # reasons. In this case, the final cleanup might fail but the new 500fb23d3a8Smrg # file should still install successfully. 501fb23d3a8Smrg { 502fb23d3a8Smrg test ! -f "$dst" || 503fb23d3a8Smrg $doit $rmcmd -f "$dst" 2>/dev/null || 504fb23d3a8Smrg { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null && 505fb23d3a8Smrg { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; } 506fb23d3a8Smrg } || 507fb23d3a8Smrg { echo "$0: cannot unlink or rename $dst" >&2 508fb23d3a8Smrg (exit 1); exit 1 509fb23d3a8Smrg } 510fb23d3a8Smrg } && 511fb23d3a8Smrg 512fb23d3a8Smrg # Now rename the file to the real destination. 513fb23d3a8Smrg $doit $mvcmd "$dsttmp" "$dst" 514fb23d3a8Smrg } 515fb23d3a8Smrg fi || exit 1 51664ce7165Smrg 51764ce7165Smrg trap '' 0 51864ce7165Smrg fi 51964ce7165Smrgdone 52064ce7165Smrg 52164ce7165Smrg# Local variables: 52264ce7165Smrg# eval: (add-hook 'write-file-hooks 'time-stamp) 52364ce7165Smrg# time-stamp-start: "scriptversion=" 52464ce7165Smrg# time-stamp-format: "%:y-%02m-%02d.%02H" 525fb23d3a8Smrg# time-stamp-time-zone: "UTC" 526fb23d3a8Smrg# time-stamp-end: "; # UTC" 52764ce7165Smrg# End: 528