install-sh revision 9511053f
1a850946eSmrg#!/bin/sh 2a850946eSmrg# install - install a program, script, or datafile 370f7c90cSmrg 49511053fSmrgscriptversion=2011-11-20.07; # UTC 570f7c90cSmrg 6a850946eSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was 7a850946eSmrg# later released in X11R6 (xc/config/util/install.sh) with the 8a850946eSmrg# following copyright and license. 9a850946eSmrg# 10a850946eSmrg# Copyright (C) 1994 X Consortium 11a850946eSmrg# 12a850946eSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy 13a850946eSmrg# of this software and associated documentation files (the "Software"), to 14a850946eSmrg# deal in the Software without restriction, including without limitation the 15a850946eSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 16a850946eSmrg# sell copies of the Software, and to permit persons to whom the Software is 17a850946eSmrg# furnished to do so, subject to the following conditions: 18a850946eSmrg# 19a850946eSmrg# The above copyright notice and this permission notice shall be included in 20a850946eSmrg# all copies or substantial portions of the Software. 21a850946eSmrg# 22a850946eSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23a850946eSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24a850946eSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25a850946eSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 26a850946eSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC- 27a850946eSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 28a850946eSmrg# 29a850946eSmrg# Except as contained in this notice, the name of the X Consortium shall not 30a850946eSmrg# be used in advertising or otherwise to promote the sale, use or other deal- 31a850946eSmrg# ings in this Software without prior written authorization from the X Consor- 32a850946eSmrg# tium. 33a850946eSmrg# 34a850946eSmrg# 35a850946eSmrg# FSF changes to this file are in the public domain. 36a850946eSmrg# 37a850946eSmrg# Calling this script install-sh is preferred over install.sh, to prevent 389511053fSmrg# 'make' implicit rules from creating a file called install from it 39a850946eSmrg# when there is no Makefile. 40a850946eSmrg# 41a850946eSmrg# This script is compatible with the BSD install script, but was written 4270f7c90cSmrg# from scratch. 43a850946eSmrg 4470f7c90cSmrgnl=' 4570f7c90cSmrg' 4670f7c90cSmrgIFS=" "" $nl" 47a850946eSmrg 48a850946eSmrg# set DOITPROG to echo to test this script 49a850946eSmrg 50a850946eSmrg# Don't use :- since 4.3BSD and earlier shells don't like it. 5170f7c90cSmrgdoit=${DOITPROG-} 5270f7c90cSmrgif test -z "$doit"; then 5370f7c90cSmrg doit_exec=exec 54a850946eSmrgelse 5570f7c90cSmrg doit_exec=$doit 56a850946eSmrgfi 57a850946eSmrg 5870f7c90cSmrg# Put in absolute file names if you don't have them in your path; 5970f7c90cSmrg# or use environment vars. 6070f7c90cSmrg 6170f7c90cSmrgchgrpprog=${CHGRPPROG-chgrp} 6270f7c90cSmrgchmodprog=${CHMODPROG-chmod} 6370f7c90cSmrgchownprog=${CHOWNPROG-chown} 6470f7c90cSmrgcmpprog=${CMPPROG-cmp} 6570f7c90cSmrgcpprog=${CPPROG-cp} 6670f7c90cSmrgmkdirprog=${MKDIRPROG-mkdir} 6770f7c90cSmrgmvprog=${MVPROG-mv} 6870f7c90cSmrgrmprog=${RMPROG-rm} 6970f7c90cSmrgstripprog=${STRIPPROG-strip} 7070f7c90cSmrg 7170f7c90cSmrgposix_glob='?' 7270f7c90cSmrginitialize_posix_glob=' 7370f7c90cSmrg test "$posix_glob" != "?" || { 7470f7c90cSmrg if (set -f) 2>/dev/null; then 7570f7c90cSmrg posix_glob= 7670f7c90cSmrg else 7770f7c90cSmrg posix_glob=: 7870f7c90cSmrg fi 7970f7c90cSmrg } 8070f7c90cSmrg' 8170f7c90cSmrg 8270f7c90cSmrgposix_mkdir= 8370f7c90cSmrg 8470f7c90cSmrg# Desired mode of installed file. 8570f7c90cSmrgmode=0755 8670f7c90cSmrg 8770f7c90cSmrgchgrpcmd= 8870f7c90cSmrgchmodcmd=$chmodprog 8970f7c90cSmrgchowncmd= 9070f7c90cSmrgmvcmd=$mvprog 9170f7c90cSmrgrmcmd="$rmprog -f" 9270f7c90cSmrgstripcmd= 93a850946eSmrg 9470f7c90cSmrgsrc= 9570f7c90cSmrgdst= 9670f7c90cSmrgdir_arg= 9770f7c90cSmrgdst_arg= 98a850946eSmrg 9970f7c90cSmrgcopy_on_change=false 10070f7c90cSmrgno_target_directory= 101a850946eSmrg 10270f7c90cSmrgusage="\ 10370f7c90cSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE 10470f7c90cSmrg or: $0 [OPTION]... SRCFILES... DIRECTORY 10570f7c90cSmrg or: $0 [OPTION]... -t DIRECTORY SRCFILES... 10670f7c90cSmrg or: $0 [OPTION]... -d DIRECTORIES... 107a850946eSmrg 10870f7c90cSmrgIn the 1st form, copy SRCFILE to DSTFILE. 10970f7c90cSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY. 11070f7c90cSmrgIn the 4th, create DIRECTORIES. 111a850946eSmrg 11270f7c90cSmrgOptions: 11370f7c90cSmrg --help display this help and exit. 11470f7c90cSmrg --version display version info and exit. 115a850946eSmrg 11670f7c90cSmrg -c (ignored) 11770f7c90cSmrg -C install only if different (preserve the last data modification time) 11870f7c90cSmrg -d create directories instead of installing files. 11970f7c90cSmrg -g GROUP $chgrpprog installed files to GROUP. 12070f7c90cSmrg -m MODE $chmodprog installed files to MODE. 12170f7c90cSmrg -o USER $chownprog installed files to USER. 12270f7c90cSmrg -s $stripprog installed files. 12370f7c90cSmrg -t DIRECTORY install into DIRECTORY. 12470f7c90cSmrg -T report an error if DSTFILE is a directory. 125a850946eSmrg 12670f7c90cSmrgEnvironment variables override the default commands: 12770f7c90cSmrg CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG 12870f7c90cSmrg RMPROG STRIPPROG 12970f7c90cSmrg" 130a850946eSmrg 13170f7c90cSmrgwhile test $# -ne 0; do 13270f7c90cSmrg case $1 in 13370f7c90cSmrg -c) ;; 134a850946eSmrg 13570f7c90cSmrg -C) copy_on_change=true;; 136a850946eSmrg 13770f7c90cSmrg -d) dir_arg=true;; 138a850946eSmrg 13970f7c90cSmrg -g) chgrpcmd="$chgrpprog $2" 14070f7c90cSmrg shift;; 141a850946eSmrg 14270f7c90cSmrg --help) echo "$usage"; exit $?;; 143a850946eSmrg 14470f7c90cSmrg -m) mode=$2 14570f7c90cSmrg case $mode in 14670f7c90cSmrg *' '* | *' '* | *' 14770f7c90cSmrg'* | *'*'* | *'?'* | *'['*) 14870f7c90cSmrg echo "$0: invalid mode: $mode" >&2 14970f7c90cSmrg exit 1;; 15070f7c90cSmrg esac 15170f7c90cSmrg shift;; 152a850946eSmrg 15370f7c90cSmrg -o) chowncmd="$chownprog $2" 15470f7c90cSmrg shift;; 155a850946eSmrg 15670f7c90cSmrg -s) stripcmd=$stripprog;; 157a850946eSmrg 15870f7c90cSmrg -t) dst_arg=$2 1599511053fSmrg # Protect names problematic for 'test' and other utilities. 1609511053fSmrg case $dst_arg in 1619511053fSmrg -* | [=\(\)!]) dst_arg=./$dst_arg;; 1629511053fSmrg esac 16370f7c90cSmrg shift;; 164a850946eSmrg 16570f7c90cSmrg -T) no_target_directory=true;; 166a850946eSmrg 16770f7c90cSmrg --version) echo "$0 $scriptversion"; exit $?;; 168a850946eSmrg 16970f7c90cSmrg --) shift 17070f7c90cSmrg break;; 171a850946eSmrg 17270f7c90cSmrg -*) echo "$0: invalid option: $1" >&2 17370f7c90cSmrg exit 1;; 174a850946eSmrg 17570f7c90cSmrg *) break;; 17670f7c90cSmrg esac 17770f7c90cSmrg shift 17870f7c90cSmrgdone 179a850946eSmrg 18070f7c90cSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then 18170f7c90cSmrg # When -d is used, all remaining arguments are directories to create. 18270f7c90cSmrg # When -t is used, the destination is already specified. 18370f7c90cSmrg # Otherwise, the last argument is the destination. Remove it from $@. 18470f7c90cSmrg for arg 18570f7c90cSmrg do 18670f7c90cSmrg if test -n "$dst_arg"; then 18770f7c90cSmrg # $@ is not empty: it contains at least $arg. 18870f7c90cSmrg set fnord "$@" "$dst_arg" 18970f7c90cSmrg shift # fnord 19070f7c90cSmrg fi 19170f7c90cSmrg shift # arg 19270f7c90cSmrg dst_arg=$arg 1939511053fSmrg # Protect names problematic for 'test' and other utilities. 1949511053fSmrg case $dst_arg in 1959511053fSmrg -* | [=\(\)!]) dst_arg=./$dst_arg;; 1969511053fSmrg esac 19770f7c90cSmrg done 19870f7c90cSmrgfi 199a850946eSmrg 20070f7c90cSmrgif test $# -eq 0; then 20170f7c90cSmrg if test -z "$dir_arg"; then 20270f7c90cSmrg echo "$0: no input file specified." >&2 20370f7c90cSmrg exit 1 20470f7c90cSmrg fi 2059511053fSmrg # It's OK to call 'install-sh -d' without argument. 20670f7c90cSmrg # This can happen when creating conditional directories. 20770f7c90cSmrg exit 0 20870f7c90cSmrgfi 209a850946eSmrg 21070f7c90cSmrgif test -z "$dir_arg"; then 2119511053fSmrg do_exit='(exit $ret); exit $ret' 2129511053fSmrg trap "ret=129; $do_exit" 1 2139511053fSmrg trap "ret=130; $do_exit" 2 2149511053fSmrg trap "ret=141; $do_exit" 13 2159511053fSmrg trap "ret=143; $do_exit" 15 21670f7c90cSmrg 21770f7c90cSmrg # Set umask so as not to create temps with too-generous modes. 21870f7c90cSmrg # However, 'strip' requires both read and write access to temps. 21970f7c90cSmrg case $mode in 22070f7c90cSmrg # Optimize common cases. 22170f7c90cSmrg *644) cp_umask=133;; 22270f7c90cSmrg *755) cp_umask=22;; 22370f7c90cSmrg 22470f7c90cSmrg *[0-7]) 22570f7c90cSmrg if test -z "$stripcmd"; then 22670f7c90cSmrg u_plus_rw= 22770f7c90cSmrg else 22870f7c90cSmrg u_plus_rw='% 200' 22970f7c90cSmrg fi 23070f7c90cSmrg cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;; 23170f7c90cSmrg *) 23270f7c90cSmrg if test -z "$stripcmd"; then 23370f7c90cSmrg u_plus_rw= 23470f7c90cSmrg else 23570f7c90cSmrg u_plus_rw=,u+rw 23670f7c90cSmrg fi 23770f7c90cSmrg cp_umask=$mode$u_plus_rw;; 23870f7c90cSmrg esac 23970f7c90cSmrgfi 240a850946eSmrg 24170f7c90cSmrgfor src 24270f7c90cSmrgdo 2439511053fSmrg # Protect names problematic for 'test' and other utilities. 24470f7c90cSmrg case $src in 2459511053fSmrg -* | [=\(\)!]) src=./$src;; 24670f7c90cSmrg esac 24770f7c90cSmrg 24870f7c90cSmrg if test -n "$dir_arg"; then 24970f7c90cSmrg dst=$src 25070f7c90cSmrg dstdir=$dst 25170f7c90cSmrg test -d "$dstdir" 25270f7c90cSmrg dstdir_status=$? 25370f7c90cSmrg else 25470f7c90cSmrg 25570f7c90cSmrg # Waiting for this to be detected by the "$cpprog $src $dsttmp" command 25670f7c90cSmrg # might cause directories to be created, which would be especially bad 25770f7c90cSmrg # if $src (and thus $dsttmp) contains '*'. 25870f7c90cSmrg if test ! -f "$src" && test ! -d "$src"; then 25970f7c90cSmrg echo "$0: $src does not exist." >&2 26070f7c90cSmrg exit 1 26170f7c90cSmrg fi 26270f7c90cSmrg 26370f7c90cSmrg if test -z "$dst_arg"; then 26470f7c90cSmrg echo "$0: no destination specified." >&2 26570f7c90cSmrg exit 1 26670f7c90cSmrg fi 26770f7c90cSmrg dst=$dst_arg 268a850946eSmrg 26970f7c90cSmrg # If destination is a directory, append the input filename; won't work 27070f7c90cSmrg # if double slashes aren't ignored. 27170f7c90cSmrg if test -d "$dst"; then 27270f7c90cSmrg if test -n "$no_target_directory"; then 27370f7c90cSmrg echo "$0: $dst_arg: Is a directory" >&2 27470f7c90cSmrg exit 1 27570f7c90cSmrg fi 27670f7c90cSmrg dstdir=$dst 27770f7c90cSmrg dst=$dstdir/`basename "$src"` 27870f7c90cSmrg dstdir_status=0 27970f7c90cSmrg else 28070f7c90cSmrg # Prefer dirname, but fall back on a substitute if dirname fails. 28170f7c90cSmrg dstdir=` 28270f7c90cSmrg (dirname "$dst") 2>/dev/null || 28370f7c90cSmrg expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \ 28470f7c90cSmrg X"$dst" : 'X\(//\)[^/]' \| \ 28570f7c90cSmrg X"$dst" : 'X\(//\)$' \| \ 28670f7c90cSmrg X"$dst" : 'X\(/\)' \| . 2>/dev/null || 28770f7c90cSmrg echo X"$dst" | 28870f7c90cSmrg sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{ 28970f7c90cSmrg s//\1/ 29070f7c90cSmrg q 29170f7c90cSmrg } 29270f7c90cSmrg /^X\(\/\/\)[^/].*/{ 29370f7c90cSmrg s//\1/ 29470f7c90cSmrg q 29570f7c90cSmrg } 29670f7c90cSmrg /^X\(\/\/\)$/{ 29770f7c90cSmrg s//\1/ 29870f7c90cSmrg q 29970f7c90cSmrg } 30070f7c90cSmrg /^X\(\/\).*/{ 30170f7c90cSmrg s//\1/ 30270f7c90cSmrg q 30370f7c90cSmrg } 30470f7c90cSmrg s/.*/./; q' 30570f7c90cSmrg ` 30670f7c90cSmrg 30770f7c90cSmrg test -d "$dstdir" 30870f7c90cSmrg dstdir_status=$? 30970f7c90cSmrg fi 31070f7c90cSmrg fi 31170f7c90cSmrg 31270f7c90cSmrg obsolete_mkdir_used=false 31370f7c90cSmrg 31470f7c90cSmrg if test $dstdir_status != 0; then 31570f7c90cSmrg case $posix_mkdir in 31670f7c90cSmrg '') 31770f7c90cSmrg # Create intermediate dirs using mode 755 as modified by the umask. 31870f7c90cSmrg # This is like FreeBSD 'install' as of 1997-10-28. 31970f7c90cSmrg umask=`umask` 32070f7c90cSmrg case $stripcmd.$umask in 32170f7c90cSmrg # Optimize common cases. 32270f7c90cSmrg *[2367][2367]) mkdir_umask=$umask;; 32370f7c90cSmrg .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;; 32470f7c90cSmrg 32570f7c90cSmrg *[0-7]) 32670f7c90cSmrg mkdir_umask=`expr $umask + 22 \ 32770f7c90cSmrg - $umask % 100 % 40 + $umask % 20 \ 32870f7c90cSmrg - $umask % 10 % 4 + $umask % 2 32970f7c90cSmrg `;; 33070f7c90cSmrg *) mkdir_umask=$umask,go-w;; 33170f7c90cSmrg esac 33270f7c90cSmrg 33370f7c90cSmrg # With -d, create the new directory with the user-specified mode. 33470f7c90cSmrg # Otherwise, rely on $mkdir_umask. 33570f7c90cSmrg if test -n "$dir_arg"; then 33670f7c90cSmrg mkdir_mode=-m$mode 337a850946eSmrg else 33870f7c90cSmrg mkdir_mode= 339a850946eSmrg fi 340a850946eSmrg 34170f7c90cSmrg posix_mkdir=false 34270f7c90cSmrg case $umask in 34370f7c90cSmrg *[123567][0-7][0-7]) 34470f7c90cSmrg # POSIX mkdir -p sets u+wx bits regardless of umask, which 34570f7c90cSmrg # is incompatible with FreeBSD 'install' when (umask & 300) != 0. 34670f7c90cSmrg ;; 34770f7c90cSmrg *) 34870f7c90cSmrg tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$ 34970f7c90cSmrg trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0 35070f7c90cSmrg 35170f7c90cSmrg if (umask $mkdir_umask && 35270f7c90cSmrg exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1 35370f7c90cSmrg then 35470f7c90cSmrg if test -z "$dir_arg" || { 35570f7c90cSmrg # Check for POSIX incompatibilities with -m. 35670f7c90cSmrg # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or 3579511053fSmrg # other-writable bit of parent directory when it shouldn't. 35870f7c90cSmrg # FreeBSD 6.1 mkdir -m -p sets mode of existing directory. 35970f7c90cSmrg ls_ld_tmpdir=`ls -ld "$tmpdir"` 36070f7c90cSmrg case $ls_ld_tmpdir in 36170f7c90cSmrg d????-?r-*) different_mode=700;; 36270f7c90cSmrg d????-?--*) different_mode=755;; 36370f7c90cSmrg *) false;; 36470f7c90cSmrg esac && 36570f7c90cSmrg $mkdirprog -m$different_mode -p -- "$tmpdir" && { 36670f7c90cSmrg ls_ld_tmpdir_1=`ls -ld "$tmpdir"` 36770f7c90cSmrg test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1" 36870f7c90cSmrg } 36970f7c90cSmrg } 37070f7c90cSmrg then posix_mkdir=: 37170f7c90cSmrg fi 37270f7c90cSmrg rmdir "$tmpdir/d" "$tmpdir" 37370f7c90cSmrg else 37470f7c90cSmrg # Remove any dirs left behind by ancient mkdir implementations. 37570f7c90cSmrg rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null 37670f7c90cSmrg fi 37770f7c90cSmrg trap '' 0;; 37870f7c90cSmrg esac;; 37970f7c90cSmrg esac 380a850946eSmrg 38170f7c90cSmrg if 38270f7c90cSmrg $posix_mkdir && ( 38370f7c90cSmrg umask $mkdir_umask && 38470f7c90cSmrg $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir" 38570f7c90cSmrg ) 38670f7c90cSmrg then : 38770f7c90cSmrg else 38870f7c90cSmrg 38970f7c90cSmrg # The umask is ridiculous, or mkdir does not conform to POSIX, 39070f7c90cSmrg # or it failed possibly due to a race condition. Create the 39170f7c90cSmrg # directory the slow way, step by step, checking for races as we go. 39270f7c90cSmrg 39370f7c90cSmrg case $dstdir in 39470f7c90cSmrg /*) prefix='/';; 3959511053fSmrg [-=\(\)!]*) prefix='./';; 39670f7c90cSmrg *) prefix='';; 39770f7c90cSmrg esac 39870f7c90cSmrg 39970f7c90cSmrg eval "$initialize_posix_glob" 40070f7c90cSmrg 40170f7c90cSmrg oIFS=$IFS 40270f7c90cSmrg IFS=/ 40370f7c90cSmrg $posix_glob set -f 40470f7c90cSmrg set fnord $dstdir 40570f7c90cSmrg shift 40670f7c90cSmrg $posix_glob set +f 40770f7c90cSmrg IFS=$oIFS 40870f7c90cSmrg 40970f7c90cSmrg prefixes= 41070f7c90cSmrg 41170f7c90cSmrg for d 41270f7c90cSmrg do 4139511053fSmrg test X"$d" = X && continue 41470f7c90cSmrg 41570f7c90cSmrg prefix=$prefix$d 41670f7c90cSmrg if test -d "$prefix"; then 41770f7c90cSmrg prefixes= 41870f7c90cSmrg else 41970f7c90cSmrg if $posix_mkdir; then 42070f7c90cSmrg (umask=$mkdir_umask && 42170f7c90cSmrg $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break 42270f7c90cSmrg # Don't fail if two instances are running concurrently. 42370f7c90cSmrg test -d "$prefix" || exit 1 42470f7c90cSmrg else 42570f7c90cSmrg case $prefix in 42670f7c90cSmrg *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;; 42770f7c90cSmrg *) qprefix=$prefix;; 42870f7c90cSmrg esac 42970f7c90cSmrg prefixes="$prefixes '$qprefix'" 43070f7c90cSmrg fi 43170f7c90cSmrg fi 43270f7c90cSmrg prefix=$prefix/ 43370f7c90cSmrg done 43470f7c90cSmrg 43570f7c90cSmrg if test -n "$prefixes"; then 43670f7c90cSmrg # Don't fail if two instances are running concurrently. 43770f7c90cSmrg (umask $mkdir_umask && 43870f7c90cSmrg eval "\$doit_exec \$mkdirprog $prefixes") || 43970f7c90cSmrg test -d "$dstdir" || exit 1 44070f7c90cSmrg obsolete_mkdir_used=true 44170f7c90cSmrg fi 44270f7c90cSmrg fi 44370f7c90cSmrg fi 44470f7c90cSmrg 44570f7c90cSmrg if test -n "$dir_arg"; then 44670f7c90cSmrg { test -z "$chowncmd" || $doit $chowncmd "$dst"; } && 44770f7c90cSmrg { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } && 44870f7c90cSmrg { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false || 44970f7c90cSmrg test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1 45070f7c90cSmrg else 45170f7c90cSmrg 45270f7c90cSmrg # Make a couple of temp file names in the proper directory. 45370f7c90cSmrg dsttmp=$dstdir/_inst.$$_ 45470f7c90cSmrg rmtmp=$dstdir/_rm.$$_ 45570f7c90cSmrg 45670f7c90cSmrg # Trap to clean up those temp files at exit. 45770f7c90cSmrg trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0 45870f7c90cSmrg 45970f7c90cSmrg # Copy the file name to the temp name. 46070f7c90cSmrg (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") && 46170f7c90cSmrg 46270f7c90cSmrg # and set any options; do chmod last to preserve setuid bits. 46370f7c90cSmrg # 46470f7c90cSmrg # If any of these fail, we abort the whole thing. If we want to 46570f7c90cSmrg # ignore errors from any of these, just make sure not to ignore 46670f7c90cSmrg # errors from the above "$doit $cpprog $src $dsttmp" command. 46770f7c90cSmrg # 46870f7c90cSmrg { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } && 46970f7c90cSmrg { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } && 47070f7c90cSmrg { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } && 47170f7c90cSmrg { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } && 47270f7c90cSmrg 47370f7c90cSmrg # If -C, don't bother to copy if it wouldn't change the file. 47470f7c90cSmrg if $copy_on_change && 47570f7c90cSmrg old=`LC_ALL=C ls -dlL "$dst" 2>/dev/null` && 47670f7c90cSmrg new=`LC_ALL=C ls -dlL "$dsttmp" 2>/dev/null` && 47770f7c90cSmrg 47870f7c90cSmrg eval "$initialize_posix_glob" && 47970f7c90cSmrg $posix_glob set -f && 48070f7c90cSmrg set X $old && old=:$2:$4:$5:$6 && 48170f7c90cSmrg set X $new && new=:$2:$4:$5:$6 && 48270f7c90cSmrg $posix_glob set +f && 48370f7c90cSmrg 48470f7c90cSmrg test "$old" = "$new" && 48570f7c90cSmrg $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1 48670f7c90cSmrg then 48770f7c90cSmrg rm -f "$dsttmp" 48870f7c90cSmrg else 48970f7c90cSmrg # Rename the file to the real destination. 49070f7c90cSmrg $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null || 49170f7c90cSmrg 49270f7c90cSmrg # The rename failed, perhaps because mv can't rename something else 49370f7c90cSmrg # to itself, or perhaps because mv is so ancient that it does not 49470f7c90cSmrg # support -f. 49570f7c90cSmrg { 49670f7c90cSmrg # Now remove or move aside any old file at destination location. 49770f7c90cSmrg # We try this two ways since rm can't unlink itself on some 49870f7c90cSmrg # systems and the destination file might be busy for other 49970f7c90cSmrg # reasons. In this case, the final cleanup might fail but the new 50070f7c90cSmrg # file should still install successfully. 50170f7c90cSmrg { 50270f7c90cSmrg test ! -f "$dst" || 50370f7c90cSmrg $doit $rmcmd -f "$dst" 2>/dev/null || 50470f7c90cSmrg { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null && 50570f7c90cSmrg { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; } 50670f7c90cSmrg } || 50770f7c90cSmrg { echo "$0: cannot unlink or rename $dst" >&2 50870f7c90cSmrg (exit 1); exit 1 50970f7c90cSmrg } 51070f7c90cSmrg } && 51170f7c90cSmrg 51270f7c90cSmrg # Now rename the file to the real destination. 51370f7c90cSmrg $doit $mvcmd "$dsttmp" "$dst" 51470f7c90cSmrg } 51570f7c90cSmrg fi || exit 1 51670f7c90cSmrg 51770f7c90cSmrg trap '' 0 51870f7c90cSmrg fi 51970f7c90cSmrgdone 520a850946eSmrg 52170f7c90cSmrg# Local variables: 52270f7c90cSmrg# eval: (add-hook 'write-file-hooks 'time-stamp) 52370f7c90cSmrg# time-stamp-start: "scriptversion=" 52470f7c90cSmrg# time-stamp-format: "%:y-%02m-%02d.%02H" 52570f7c90cSmrg# time-stamp-time-zone: "UTC" 52670f7c90cSmrg# time-stamp-end: "; # UTC" 52770f7c90cSmrg# End: 528