install-sh revision b68e1f86
16df26cacSmrg#!/bin/sh 26df26cacSmrg# install - install a program, script, or datafile 36df26cacSmrg 4b68e1f86Smrgscriptversion=2006-12-25.00 56df26cacSmrg 66df26cacSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was 76df26cacSmrg# later released in X11R6 (xc/config/util/install.sh) with the 86df26cacSmrg# following copyright and license. 96df26cacSmrg# 106df26cacSmrg# Copyright (C) 1994 X Consortium 116df26cacSmrg# 126df26cacSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy 136df26cacSmrg# of this software and associated documentation files (the "Software"), to 146df26cacSmrg# deal in the Software without restriction, including without limitation the 156df26cacSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 166df26cacSmrg# sell copies of the Software, and to permit persons to whom the Software is 176df26cacSmrg# furnished to do so, subject to the following conditions: 186df26cacSmrg# 196df26cacSmrg# The above copyright notice and this permission notice shall be included in 206df26cacSmrg# all copies or substantial portions of the Software. 216df26cacSmrg# 226df26cacSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 236df26cacSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 246df26cacSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 256df26cacSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 266df26cacSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC- 276df26cacSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 286df26cacSmrg# 296df26cacSmrg# Except as contained in this notice, the name of the X Consortium shall not 306df26cacSmrg# be used in advertising or otherwise to promote the sale, use or other deal- 316df26cacSmrg# ings in this Software without prior written authorization from the X Consor- 326df26cacSmrg# tium. 336df26cacSmrg# 346df26cacSmrg# 356df26cacSmrg# FSF changes to this file are in the public domain. 366df26cacSmrg# 376df26cacSmrg# Calling this script install-sh is preferred over install.sh, to prevent 386df26cacSmrg# `make' implicit rules from creating a file called install from it 396df26cacSmrg# when there is no Makefile. 406df26cacSmrg# 416df26cacSmrg# This script is compatible with the BSD install script, but was written 42b68e1f86Smrg# from scratch. 43b68e1f86Smrg 44b68e1f86Smrgnl=' 45b68e1f86Smrg' 46b68e1f86SmrgIFS=" "" $nl" 476df26cacSmrg 486df26cacSmrg# set DOITPROG to echo to test this script 496df26cacSmrg 506df26cacSmrg# Don't use :- since 4.3BSD and earlier shells don't like it. 51b68e1f86Smrgdoit=${DOITPROG-} 52b68e1f86Smrgif test -z "$doit"; then 53b68e1f86Smrg doit_exec=exec 54b68e1f86Smrgelse 55b68e1f86Smrg doit_exec=$doit 56b68e1f86Smrgfi 576df26cacSmrg 58b68e1f86Smrg# Put in absolute file names if you don't have them in your path; 59b68e1f86Smrg# or use environment vars. 60b68e1f86Smrg 61b68e1f86Smrgchgrpprog=${CHGRPPROG-chgrp} 62b68e1f86Smrgchmodprog=${CHMODPROG-chmod} 63b68e1f86Smrgchownprog=${CHOWNPROG-chown} 64b68e1f86Smrgcmpprog=${CMPPROG-cmp} 65b68e1f86Smrgcpprog=${CPPROG-cp} 66b68e1f86Smrgmkdirprog=${MKDIRPROG-mkdir} 67b68e1f86Smrgmvprog=${MVPROG-mv} 68b68e1f86Smrgrmprog=${RMPROG-rm} 69b68e1f86Smrgstripprog=${STRIPPROG-strip} 70b68e1f86Smrg 71b68e1f86Smrgposix_glob='?' 72b68e1f86Smrginitialize_posix_glob=' 73b68e1f86Smrg test "$posix_glob" != "?" || { 74b68e1f86Smrg if (set -f) 2>/dev/null; then 75b68e1f86Smrg posix_glob= 76b68e1f86Smrg else 77b68e1f86Smrg posix_glob=: 78b68e1f86Smrg fi 79b68e1f86Smrg } 80b68e1f86Smrg' 816df26cacSmrg 82b68e1f86Smrgposix_mkdir= 83b68e1f86Smrg 84b68e1f86Smrg# Desired mode of installed file. 85b68e1f86Smrgmode=0755 866df26cacSmrg 876df26cacSmrgchgrpcmd= 88b68e1f86Smrgchmodcmd=$chmodprog 89b68e1f86Smrgchowncmd= 90b68e1f86Smrgmvcmd=$mvprog 916df26cacSmrgrmcmd="$rmprog -f" 92b68e1f86Smrgstripcmd= 93b68e1f86Smrg 946df26cacSmrgsrc= 956df26cacSmrgdst= 966df26cacSmrgdir_arg= 97b68e1f86Smrgdst_arg= 98b68e1f86Smrg 99b68e1f86Smrgcopy_on_change=false 1006df26cacSmrgno_target_directory= 1016df26cacSmrg 102b68e1f86Smrgusage="\ 103b68e1f86SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE 1046df26cacSmrg or: $0 [OPTION]... SRCFILES... DIRECTORY 1056df26cacSmrg or: $0 [OPTION]... -t DIRECTORY SRCFILES... 1066df26cacSmrg or: $0 [OPTION]... -d DIRECTORIES... 1076df26cacSmrg 1086df26cacSmrgIn the 1st form, copy SRCFILE to DSTFILE. 1096df26cacSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY. 1106df26cacSmrgIn the 4th, create DIRECTORIES. 1116df26cacSmrg 1126df26cacSmrgOptions: 113b68e1f86Smrg --help display this help and exit. 114b68e1f86Smrg --version display version info and exit. 115b68e1f86Smrg 116b68e1f86Smrg -c (ignored) 117b68e1f86Smrg -C install only if different (preserve the last data modification time) 118b68e1f86Smrg -d create directories instead of installing files. 119b68e1f86Smrg -g GROUP $chgrpprog installed files to GROUP. 120b68e1f86Smrg -m MODE $chmodprog installed files to MODE. 121b68e1f86Smrg -o USER $chownprog installed files to USER. 122b68e1f86Smrg -s $stripprog installed files. 123b68e1f86Smrg -t DIRECTORY install into DIRECTORY. 124b68e1f86Smrg -T report an error if DSTFILE is a directory. 1256df26cacSmrg 1266df26cacSmrgEnvironment variables override the default commands: 127b68e1f86Smrg CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG 128b68e1f86Smrg RMPROG STRIPPROG 1296df26cacSmrg" 1306df26cacSmrg 131b68e1f86Smrgwhile test $# -ne 0; do 1326df26cacSmrg case $1 in 133b68e1f86Smrg -c) ;; 134b68e1f86Smrg 135b68e1f86Smrg -C) copy_on_change=true;; 1366df26cacSmrg 137b68e1f86Smrg -d) dir_arg=true;; 1386df26cacSmrg 1396df26cacSmrg -g) chgrpcmd="$chgrpprog $2" 140b68e1f86Smrg shift;; 1416df26cacSmrg 1426df26cacSmrg --help) echo "$usage"; exit $?;; 1436df26cacSmrg 144b68e1f86Smrg -m) mode=$2 145b68e1f86Smrg case $mode in 146b68e1f86Smrg *' '* | *' '* | *' 147b68e1f86Smrg'* | *'*'* | *'?'* | *'['*) 148b68e1f86Smrg echo "$0: invalid mode: $mode" >&2 149b68e1f86Smrg exit 1;; 150b68e1f86Smrg esac 151b68e1f86Smrg shift;; 1526df26cacSmrg 1536df26cacSmrg -o) chowncmd="$chownprog $2" 154b68e1f86Smrg shift;; 1556df26cacSmrg 156b68e1f86Smrg -s) stripcmd=$stripprog;; 1576df26cacSmrg 158b68e1f86Smrg -t) dst_arg=$2 159b68e1f86Smrg shift;; 1606df26cacSmrg 161b68e1f86Smrg -T) no_target_directory=true;; 1626df26cacSmrg 1636df26cacSmrg --version) echo "$0 $scriptversion"; exit $?;; 1646df26cacSmrg 165b68e1f86Smrg --) shift 1666df26cacSmrg break;; 167b68e1f86Smrg 168b68e1f86Smrg -*) echo "$0: invalid option: $1" >&2 169b68e1f86Smrg exit 1;; 170b68e1f86Smrg 171b68e1f86Smrg *) break;; 1726df26cacSmrg esac 173b68e1f86Smrg shift 1746df26cacSmrgdone 1756df26cacSmrg 176b68e1f86Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then 177b68e1f86Smrg # When -d is used, all remaining arguments are directories to create. 178b68e1f86Smrg # When -t is used, the destination is already specified. 179b68e1f86Smrg # Otherwise, the last argument is the destination. Remove it from $@. 180b68e1f86Smrg for arg 181b68e1f86Smrg do 182b68e1f86Smrg if test -n "$dst_arg"; then 183b68e1f86Smrg # $@ is not empty: it contains at least $arg. 184b68e1f86Smrg set fnord "$@" "$dst_arg" 185b68e1f86Smrg shift # fnord 186b68e1f86Smrg fi 187b68e1f86Smrg shift # arg 188b68e1f86Smrg dst_arg=$arg 189b68e1f86Smrg done 190b68e1f86Smrgfi 191b68e1f86Smrg 192b68e1f86Smrgif test $# -eq 0; then 1936df26cacSmrg if test -z "$dir_arg"; then 1946df26cacSmrg echo "$0: no input file specified." >&2 1956df26cacSmrg exit 1 1966df26cacSmrg fi 1976df26cacSmrg # It's OK to call `install-sh -d' without argument. 1986df26cacSmrg # This can happen when creating conditional directories. 1996df26cacSmrg exit 0 2006df26cacSmrgfi 2016df26cacSmrg 202b68e1f86Smrgif test -z "$dir_arg"; then 203b68e1f86Smrg trap '(exit $?); exit' 1 2 13 15 204b68e1f86Smrg 205b68e1f86Smrg # Set umask so as not to create temps with too-generous modes. 206b68e1f86Smrg # However, 'strip' requires both read and write access to temps. 207b68e1f86Smrg case $mode in 208b68e1f86Smrg # Optimize common cases. 209b68e1f86Smrg *644) cp_umask=133;; 210b68e1f86Smrg *755) cp_umask=22;; 211b68e1f86Smrg 212b68e1f86Smrg *[0-7]) 213b68e1f86Smrg if test -z "$stripcmd"; then 214b68e1f86Smrg u_plus_rw= 215b68e1f86Smrg else 216b68e1f86Smrg u_plus_rw='% 200' 217b68e1f86Smrg fi 218b68e1f86Smrg cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;; 219b68e1f86Smrg *) 220b68e1f86Smrg if test -z "$stripcmd"; then 221b68e1f86Smrg u_plus_rw= 222b68e1f86Smrg else 223b68e1f86Smrg u_plus_rw=,u+rw 224b68e1f86Smrg fi 225b68e1f86Smrg cp_umask=$mode$u_plus_rw;; 226b68e1f86Smrg esac 227b68e1f86Smrgfi 228b68e1f86Smrg 2296df26cacSmrgfor src 2306df26cacSmrgdo 2316df26cacSmrg # Protect names starting with `-'. 2326df26cacSmrg case $src in 233b68e1f86Smrg -*) src=./$src;; 2346df26cacSmrg esac 2356df26cacSmrg 2366df26cacSmrg if test -n "$dir_arg"; then 2376df26cacSmrg dst=$src 238b68e1f86Smrg dstdir=$dst 239b68e1f86Smrg test -d "$dstdir" 240b68e1f86Smrg dstdir_status=$? 24116fd1166Smrg else 242b68e1f86Smrg 2436df26cacSmrg # Waiting for this to be detected by the "$cpprog $src $dsttmp" command 2446df26cacSmrg # might cause directories to be created, which would be especially bad 2456df26cacSmrg # if $src (and thus $dsttmp) contains '*'. 2466df26cacSmrg if test ! -f "$src" && test ! -d "$src"; then 2476df26cacSmrg echo "$0: $src does not exist." >&2 2486df26cacSmrg exit 1 2496df26cacSmrg fi 2506df26cacSmrg 251b68e1f86Smrg if test -z "$dst_arg"; then 2526df26cacSmrg echo "$0: no destination specified." >&2 2536df26cacSmrg exit 1 2546df26cacSmrg fi 2556df26cacSmrg 256b68e1f86Smrg dst=$dst_arg 2576df26cacSmrg # Protect names starting with `-'. 2586df26cacSmrg case $dst in 259b68e1f86Smrg -*) dst=./$dst;; 2606df26cacSmrg esac 2616df26cacSmrg 2626df26cacSmrg # If destination is a directory, append the input filename; won't work 2636df26cacSmrg # if double slashes aren't ignored. 2646df26cacSmrg if test -d "$dst"; then 2656df26cacSmrg if test -n "$no_target_directory"; then 266b68e1f86Smrg echo "$0: $dst_arg: Is a directory" >&2 2676df26cacSmrg exit 1 2686df26cacSmrg fi 269b68e1f86Smrg dstdir=$dst 270b68e1f86Smrg dst=$dstdir/`basename "$src"` 271b68e1f86Smrg dstdir_status=0 272b68e1f86Smrg else 273b68e1f86Smrg # Prefer dirname, but fall back on a substitute if dirname fails. 274b68e1f86Smrg dstdir=` 275b68e1f86Smrg (dirname "$dst") 2>/dev/null || 276b68e1f86Smrg expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \ 277b68e1f86Smrg X"$dst" : 'X\(//\)[^/]' \| \ 278b68e1f86Smrg X"$dst" : 'X\(//\)$' \| \ 279b68e1f86Smrg X"$dst" : 'X\(/\)' \| . 2>/dev/null || 280b68e1f86Smrg echo X"$dst" | 281b68e1f86Smrg sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{ 282b68e1f86Smrg s//\1/ 283b68e1f86Smrg q 284b68e1f86Smrg } 285b68e1f86Smrg /^X\(\/\/\)[^/].*/{ 286b68e1f86Smrg s//\1/ 287b68e1f86Smrg q 288b68e1f86Smrg } 289b68e1f86Smrg /^X\(\/\/\)$/{ 290b68e1f86Smrg s//\1/ 291b68e1f86Smrg q 292b68e1f86Smrg } 293b68e1f86Smrg /^X\(\/\).*/{ 294b68e1f86Smrg s//\1/ 295b68e1f86Smrg q 296b68e1f86Smrg } 297b68e1f86Smrg s/.*/./; q' 298b68e1f86Smrg ` 299b68e1f86Smrg 300b68e1f86Smrg test -d "$dstdir" 301b68e1f86Smrg dstdir_status=$? 3026df26cacSmrg fi 3036df26cacSmrg fi 3046df26cacSmrg 305b68e1f86Smrg obsolete_mkdir_used=false 306b68e1f86Smrg 307b68e1f86Smrg if test $dstdir_status != 0; then 308b68e1f86Smrg case $posix_mkdir in 309b68e1f86Smrg '') 310b68e1f86Smrg # Create intermediate dirs using mode 755 as modified by the umask. 311b68e1f86Smrg # This is like FreeBSD 'install' as of 1997-10-28. 312b68e1f86Smrg umask=`umask` 313b68e1f86Smrg case $stripcmd.$umask in 314b68e1f86Smrg # Optimize common cases. 315b68e1f86Smrg *[2367][2367]) mkdir_umask=$umask;; 316b68e1f86Smrg .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;; 317b68e1f86Smrg 318b68e1f86Smrg *[0-7]) 319b68e1f86Smrg mkdir_umask=`expr $umask + 22 \ 320b68e1f86Smrg - $umask % 100 % 40 + $umask % 20 \ 321b68e1f86Smrg - $umask % 10 % 4 + $umask % 2 322b68e1f86Smrg `;; 323b68e1f86Smrg *) mkdir_umask=$umask,go-w;; 324b68e1f86Smrg esac 325b68e1f86Smrg 326b68e1f86Smrg # With -d, create the new directory with the user-specified mode. 327b68e1f86Smrg # Otherwise, rely on $mkdir_umask. 328b68e1f86Smrg if test -n "$dir_arg"; then 329b68e1f86Smrg mkdir_mode=-m$mode 330b68e1f86Smrg else 331b68e1f86Smrg mkdir_mode= 332b68e1f86Smrg fi 333b68e1f86Smrg 334b68e1f86Smrg posix_mkdir=false 335b68e1f86Smrg case $umask in 336b68e1f86Smrg *[123567][0-7][0-7]) 337b68e1f86Smrg # POSIX mkdir -p sets u+wx bits regardless of umask, which 338b68e1f86Smrg # is incompatible with FreeBSD 'install' when (umask & 300) != 0. 339b68e1f86Smrg ;; 340b68e1f86Smrg *) 341b68e1f86Smrg tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$ 342b68e1f86Smrg trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0 343b68e1f86Smrg 344b68e1f86Smrg if (umask $mkdir_umask && 345b68e1f86Smrg exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1 346b68e1f86Smrg then 347b68e1f86Smrg if test -z "$dir_arg" || { 348b68e1f86Smrg # Check for POSIX incompatibilities with -m. 349b68e1f86Smrg # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or 350b68e1f86Smrg # other-writeable bit of parent directory when it shouldn't. 351b68e1f86Smrg # FreeBSD 6.1 mkdir -m -p sets mode of existing directory. 352b68e1f86Smrg ls_ld_tmpdir=`ls -ld "$tmpdir"` 353b68e1f86Smrg case $ls_ld_tmpdir in 354b68e1f86Smrg d????-?r-*) different_mode=700;; 355b68e1f86Smrg d????-?--*) different_mode=755;; 356b68e1f86Smrg *) false;; 357b68e1f86Smrg esac && 358b68e1f86Smrg $mkdirprog -m$different_mode -p -- "$tmpdir" && { 359b68e1f86Smrg ls_ld_tmpdir_1=`ls -ld "$tmpdir"` 360b68e1f86Smrg test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1" 361b68e1f86Smrg } 362b68e1f86Smrg } 363b68e1f86Smrg then posix_mkdir=: 364b68e1f86Smrg fi 365b68e1f86Smrg rmdir "$tmpdir/d" "$tmpdir" 366b68e1f86Smrg else 367b68e1f86Smrg # Remove any dirs left behind by ancient mkdir implementations. 368b68e1f86Smrg rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null 369b68e1f86Smrg fi 370b68e1f86Smrg trap '' 0;; 371b68e1f86Smrg esac;; 372b68e1f86Smrg esac 3736df26cacSmrg 374b68e1f86Smrg if 375b68e1f86Smrg $posix_mkdir && ( 376b68e1f86Smrg umask $mkdir_umask && 377b68e1f86Smrg $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir" 378b68e1f86Smrg ) 379b68e1f86Smrg then : 380b68e1f86Smrg else 38116fd1166Smrg 382b68e1f86Smrg # The umask is ridiculous, or mkdir does not conform to POSIX, 383b68e1f86Smrg # or it failed possibly due to a race condition. Create the 384b68e1f86Smrg # directory the slow way, step by step, checking for races as we go. 3856df26cacSmrg 386b68e1f86Smrg case $dstdir in 387b68e1f86Smrg /*) prefix='/';; 388b68e1f86Smrg -*) prefix='./';; 389b68e1f86Smrg *) prefix='';; 390b68e1f86Smrg esac 39116fd1166Smrg 392b68e1f86Smrg eval "$initialize_posix_glob" 39316fd1166Smrg 394b68e1f86Smrg oIFS=$IFS 395b68e1f86Smrg IFS=/ 396b68e1f86Smrg $posix_glob set -f 397b68e1f86Smrg set fnord $dstdir 3986df26cacSmrg shift 399b68e1f86Smrg $posix_glob set +f 400b68e1f86Smrg IFS=$oIFS 401b68e1f86Smrg 402b68e1f86Smrg prefixes= 403b68e1f86Smrg 404b68e1f86Smrg for d 405b68e1f86Smrg do 406b68e1f86Smrg test -z "$d" && continue 407b68e1f86Smrg 408b68e1f86Smrg prefix=$prefix$d 409b68e1f86Smrg if test -d "$prefix"; then 410b68e1f86Smrg prefixes= 411b68e1f86Smrg else 412b68e1f86Smrg if $posix_mkdir; then 413b68e1f86Smrg (umask=$mkdir_umask && 414b68e1f86Smrg $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break 415b68e1f86Smrg # Don't fail if two instances are running concurrently. 416b68e1f86Smrg test -d "$prefix" || exit 1 417b68e1f86Smrg else 418b68e1f86Smrg case $prefix in 419b68e1f86Smrg *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;; 420b68e1f86Smrg *) qprefix=$prefix;; 421b68e1f86Smrg esac 422b68e1f86Smrg prefixes="$prefixes '$qprefix'" 423b68e1f86Smrg fi 424b68e1f86Smrg fi 425b68e1f86Smrg prefix=$prefix/ 426b68e1f86Smrg done 427b68e1f86Smrg 428b68e1f86Smrg if test -n "$prefixes"; then 429b68e1f86Smrg # Don't fail if two instances are running concurrently. 430b68e1f86Smrg (umask $mkdir_umask && 431b68e1f86Smrg eval "\$doit_exec \$mkdirprog $prefixes") || 432b68e1f86Smrg test -d "$dstdir" || exit 1 433b68e1f86Smrg obsolete_mkdir_used=true 4346df26cacSmrg fi 435b68e1f86Smrg fi 4366df26cacSmrg fi 4376df26cacSmrg 4386df26cacSmrg if test -n "$dir_arg"; then 439b68e1f86Smrg { test -z "$chowncmd" || $doit $chowncmd "$dst"; } && 440b68e1f86Smrg { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } && 441b68e1f86Smrg { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false || 442b68e1f86Smrg test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1 4436df26cacSmrg else 4446df26cacSmrg 4456df26cacSmrg # Make a couple of temp file names in the proper directory. 4466df26cacSmrg dsttmp=$dstdir/_inst.$$_ 4476df26cacSmrg rmtmp=$dstdir/_rm.$$_ 4486df26cacSmrg 4496df26cacSmrg # Trap to clean up those temp files at exit. 4506df26cacSmrg trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0 4516df26cacSmrg 4526df26cacSmrg # Copy the file name to the temp name. 453b68e1f86Smrg (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") && 4546df26cacSmrg 4556df26cacSmrg # and set any options; do chmod last to preserve setuid bits. 4566df26cacSmrg # 4576df26cacSmrg # If any of these fail, we abort the whole thing. If we want to 4586df26cacSmrg # ignore errors from any of these, just make sure not to ignore 4596df26cacSmrg # errors from the above "$doit $cpprog $src $dsttmp" command. 4606df26cacSmrg # 461b68e1f86Smrg { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } && 462b68e1f86Smrg { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } && 463b68e1f86Smrg { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } && 464b68e1f86Smrg { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } && 465b68e1f86Smrg 466b68e1f86Smrg # If -C, don't bother to copy if it wouldn't change the file. 467b68e1f86Smrg if $copy_on_change && 468b68e1f86Smrg old=`LC_ALL=C ls -dlL "$dst" 2>/dev/null` && 469b68e1f86Smrg new=`LC_ALL=C ls -dlL "$dsttmp" 2>/dev/null` && 470b68e1f86Smrg 471b68e1f86Smrg eval "$initialize_posix_glob" && 472b68e1f86Smrg $posix_glob set -f && 473b68e1f86Smrg set X $old && old=:$2:$4:$5:$6 && 474b68e1f86Smrg set X $new && new=:$2:$4:$5:$6 && 475b68e1f86Smrg $posix_glob set +f && 476b68e1f86Smrg 477b68e1f86Smrg test "$old" = "$new" && 478b68e1f86Smrg $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1 479b68e1f86Smrg then 480b68e1f86Smrg rm -f "$dsttmp" 481b68e1f86Smrg else 482b68e1f86Smrg # Rename the file to the real destination. 483b68e1f86Smrg $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null || 484b68e1f86Smrg 485b68e1f86Smrg # The rename failed, perhaps because mv can't rename something else 486b68e1f86Smrg # to itself, or perhaps because mv is so ancient that it does not 487b68e1f86Smrg # support -f. 488b68e1f86Smrg { 489b68e1f86Smrg # Now remove or move aside any old file at destination location. 490b68e1f86Smrg # We try this two ways since rm can't unlink itself on some 491b68e1f86Smrg # systems and the destination file might be busy for other 492b68e1f86Smrg # reasons. In this case, the final cleanup might fail but the new 493b68e1f86Smrg # file should still install successfully. 494b68e1f86Smrg { 495b68e1f86Smrg test ! -f "$dst" || 496b68e1f86Smrg $doit $rmcmd -f "$dst" 2>/dev/null || 497b68e1f86Smrg { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null && 498b68e1f86Smrg { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; } 499b68e1f86Smrg } || 500b68e1f86Smrg { echo "$0: cannot unlink or rename $dst" >&2 501b68e1f86Smrg (exit 1); exit 1 502b68e1f86Smrg } 503b68e1f86Smrg } && 504b68e1f86Smrg 505b68e1f86Smrg # Now rename the file to the real destination. 506b68e1f86Smrg $doit $mvcmd "$dsttmp" "$dst" 507b68e1f86Smrg } 508b68e1f86Smrg fi || exit 1 509b68e1f86Smrg 510b68e1f86Smrg trap '' 0 511b68e1f86Smrg fi 5126df26cacSmrgdone 5136df26cacSmrg 5146df26cacSmrg# Local variables: 5156df26cacSmrg# eval: (add-hook 'write-file-hooks 'time-stamp) 5166df26cacSmrg# time-stamp-start: "scriptversion=" 5176df26cacSmrg# time-stamp-format: "%:y-%02m-%02d.%02H" 5186df26cacSmrg# time-stamp-end: "$" 5196df26cacSmrg# End: 520