install-sh revision 01395c31
192bb16f8Smrg#!/bin/sh
292bb16f8Smrg# install - install a program, script, or datafile
392bb16f8Smrg
401395c31Smrgscriptversion=2018-03-11.20; # UTC
592bb16f8Smrg
692bb16f8Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
792bb16f8Smrg# later released in X11R6 (xc/config/util/install.sh) with the
892bb16f8Smrg# following copyright and license.
992bb16f8Smrg#
1092bb16f8Smrg# Copyright (C) 1994 X Consortium
1192bb16f8Smrg#
1292bb16f8Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1392bb16f8Smrg# of this software and associated documentation files (the "Software"), to
1492bb16f8Smrg# deal in the Software without restriction, including without limitation the
1592bb16f8Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1692bb16f8Smrg# sell copies of the Software, and to permit persons to whom the Software is
1792bb16f8Smrg# furnished to do so, subject to the following conditions:
1892bb16f8Smrg#
1992bb16f8Smrg# The above copyright notice and this permission notice shall be included in
2092bb16f8Smrg# all copies or substantial portions of the Software.
2192bb16f8Smrg#
2292bb16f8Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2392bb16f8Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2492bb16f8Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2592bb16f8Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2692bb16f8Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2792bb16f8Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2892bb16f8Smrg#
2992bb16f8Smrg# Except as contained in this notice, the name of the X Consortium shall not
3092bb16f8Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3192bb16f8Smrg# ings in this Software without prior written authorization from the X Consor-
3292bb16f8Smrg# tium.
3392bb16f8Smrg#
3492bb16f8Smrg#
3592bb16f8Smrg# FSF changes to this file are in the public domain.
3692bb16f8Smrg#
3792bb16f8Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3801395c31Smrg# 'make' implicit rules from creating a file called install from it
3992bb16f8Smrg# when there is no Makefile.
4092bb16f8Smrg#
4192bb16f8Smrg# This script is compatible with the BSD install script, but was written
428255a313Smrg# from scratch.
438255a313Smrg
4401395c31Smrgtab='	'
458255a313Smrgnl='
468255a313Smrg'
4701395c31SmrgIFS=" $tab$nl"
4892bb16f8Smrg
4901395c31Smrg# Set DOITPROG to "echo" to test this script.
5092bb16f8Smrg
518255a313Smrgdoit=${DOITPROG-}
5201395c31Smrgdoit_exec=${doit:-exec}
5392bb16f8Smrg
548255a313Smrg# Put in absolute file names if you don't have them in your path;
558255a313Smrg# or use environment vars.
568255a313Smrg
578255a313Smrgchgrpprog=${CHGRPPROG-chgrp}
588255a313Smrgchmodprog=${CHMODPROG-chmod}
598255a313Smrgchownprog=${CHOWNPROG-chown}
608255a313Smrgcmpprog=${CMPPROG-cmp}
618255a313Smrgcpprog=${CPPROG-cp}
628255a313Smrgmkdirprog=${MKDIRPROG-mkdir}
638255a313Smrgmvprog=${MVPROG-mv}
648255a313Smrgrmprog=${RMPROG-rm}
658255a313Smrgstripprog=${STRIPPROG-strip}
668255a313Smrg
678255a313Smrgposix_mkdir=
688255a313Smrg
698255a313Smrg# Desired mode of installed file.
708255a313Smrgmode=0755
7192bb16f8Smrg
7292bb16f8Smrgchgrpcmd=
738255a313Smrgchmodcmd=$chmodprog
748255a313Smrgchowncmd=
758255a313Smrgmvcmd=$mvprog
7692bb16f8Smrgrmcmd="$rmprog -f"
778255a313Smrgstripcmd=
788255a313Smrg
7992bb16f8Smrgsrc=
8092bb16f8Smrgdst=
8192bb16f8Smrgdir_arg=
828255a313Smrgdst_arg=
838255a313Smrg
848255a313Smrgcopy_on_change=false
8501395c31Smrgis_target_a_directory=possibly
8692bb16f8Smrg
878255a313Smrgusage="\
888255a313SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
8992bb16f8Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
9092bb16f8Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
9192bb16f8Smrg   or: $0 [OPTION]... -d DIRECTORIES...
9292bb16f8Smrg
9392bb16f8SmrgIn the 1st form, copy SRCFILE to DSTFILE.
9492bb16f8SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
9592bb16f8SmrgIn the 4th, create DIRECTORIES.
9692bb16f8Smrg
9792bb16f8SmrgOptions:
988255a313Smrg     --help     display this help and exit.
998255a313Smrg     --version  display version info and exit.
1008255a313Smrg
1018255a313Smrg  -c            (ignored)
1028255a313Smrg  -C            install only if different (preserve the last data modification time)
1038255a313Smrg  -d            create directories instead of installing files.
1048255a313Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1058255a313Smrg  -m MODE       $chmodprog installed files to MODE.
1068255a313Smrg  -o USER       $chownprog installed files to USER.
1078255a313Smrg  -s            $stripprog installed files.
1088255a313Smrg  -t DIRECTORY  install into DIRECTORY.
1098255a313Smrg  -T            report an error if DSTFILE is a directory.
11092bb16f8Smrg
11192bb16f8SmrgEnvironment variables override the default commands:
1128255a313Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1138255a313Smrg  RMPROG STRIPPROG
11492bb16f8Smrg"
11592bb16f8Smrg
1168255a313Smrgwhile test $# -ne 0; do
11792bb16f8Smrg  case $1 in
1188255a313Smrg    -c) ;;
1198255a313Smrg
1208255a313Smrg    -C) copy_on_change=true;;
12192bb16f8Smrg
1228255a313Smrg    -d) dir_arg=true;;
12392bb16f8Smrg
12492bb16f8Smrg    -g) chgrpcmd="$chgrpprog $2"
12501395c31Smrg        shift;;
12692bb16f8Smrg
12792bb16f8Smrg    --help) echo "$usage"; exit $?;;
12892bb16f8Smrg
1298255a313Smrg    -m) mode=$2
13001395c31Smrg        case $mode in
13101395c31Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
13201395c31Smrg            echo "$0: invalid mode: $mode" >&2
13301395c31Smrg            exit 1;;
13401395c31Smrg        esac
13501395c31Smrg        shift;;
13692bb16f8Smrg
13792bb16f8Smrg    -o) chowncmd="$chownprog $2"
13801395c31Smrg        shift;;
13992bb16f8Smrg
1408255a313Smrg    -s) stripcmd=$stripprog;;
14192bb16f8Smrg
14201395c31Smrg    -t)
14301395c31Smrg        is_target_a_directory=always
14401395c31Smrg        dst_arg=$2
14501395c31Smrg        # Protect names problematic for 'test' and other utilities.
14601395c31Smrg        case $dst_arg in
14701395c31Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
14801395c31Smrg        esac
14901395c31Smrg        shift;;
15092bb16f8Smrg
15101395c31Smrg    -T) is_target_a_directory=never;;
15292bb16f8Smrg
15392bb16f8Smrg    --version) echo "$0 $scriptversion"; exit $?;;
15492bb16f8Smrg
15501395c31Smrg    --) shift
15601395c31Smrg        break;;
1578255a313Smrg
15801395c31Smrg    -*) echo "$0: invalid option: $1" >&2
15901395c31Smrg        exit 1;;
1608255a313Smrg
1618255a313Smrg    *)  break;;
16292bb16f8Smrg  esac
1638255a313Smrg  shift
16492bb16f8Smrgdone
16592bb16f8Smrg
16601395c31Smrg# We allow the use of options -d and -T together, by making -d
16701395c31Smrg# take the precedence; this is for compatibility with GNU install.
16801395c31Smrg
16901395c31Smrgif test -n "$dir_arg"; then
17001395c31Smrg  if test -n "$dst_arg"; then
17101395c31Smrg    echo "$0: target directory not allowed when installing a directory." >&2
17201395c31Smrg    exit 1
17301395c31Smrg  fi
17401395c31Smrgfi
17501395c31Smrg
1768255a313Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1778255a313Smrg  # When -d is used, all remaining arguments are directories to create.
1788255a313Smrg  # When -t is used, the destination is already specified.
1798255a313Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1808255a313Smrg  for arg
1818255a313Smrg  do
1828255a313Smrg    if test -n "$dst_arg"; then
1838255a313Smrg      # $@ is not empty: it contains at least $arg.
1848255a313Smrg      set fnord "$@" "$dst_arg"
1858255a313Smrg      shift # fnord
1868255a313Smrg    fi
1878255a313Smrg    shift # arg
1888255a313Smrg    dst_arg=$arg
18901395c31Smrg    # Protect names problematic for 'test' and other utilities.
19001395c31Smrg    case $dst_arg in
19101395c31Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
19201395c31Smrg    esac
1938255a313Smrg  done
1948255a313Smrgfi
1958255a313Smrg
1968255a313Smrgif test $# -eq 0; then
19792bb16f8Smrg  if test -z "$dir_arg"; then
19892bb16f8Smrg    echo "$0: no input file specified." >&2
19992bb16f8Smrg    exit 1
20092bb16f8Smrg  fi
20101395c31Smrg  # It's OK to call 'install-sh -d' without argument.
20292bb16f8Smrg  # This can happen when creating conditional directories.
20392bb16f8Smrg  exit 0
20492bb16f8Smrgfi
20592bb16f8Smrg
2068255a313Smrgif test -z "$dir_arg"; then
20701395c31Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
20801395c31Smrg    if test ! -d "$dst_arg"; then
20901395c31Smrg      echo "$0: $dst_arg: Is not a directory." >&2
21001395c31Smrg      exit 1
21101395c31Smrg    fi
21201395c31Smrg  fi
21301395c31Smrgfi
21401395c31Smrg
21501395c31Smrgif test -z "$dir_arg"; then
21601395c31Smrg  do_exit='(exit $ret); exit $ret'
21701395c31Smrg  trap "ret=129; $do_exit" 1
21801395c31Smrg  trap "ret=130; $do_exit" 2
21901395c31Smrg  trap "ret=141; $do_exit" 13
22001395c31Smrg  trap "ret=143; $do_exit" 15
2218255a313Smrg
2228255a313Smrg  # Set umask so as not to create temps with too-generous modes.
2238255a313Smrg  # However, 'strip' requires both read and write access to temps.
2248255a313Smrg  case $mode in
2258255a313Smrg    # Optimize common cases.
2268255a313Smrg    *644) cp_umask=133;;
2278255a313Smrg    *755) cp_umask=22;;
2288255a313Smrg
2298255a313Smrg    *[0-7])
2308255a313Smrg      if test -z "$stripcmd"; then
23101395c31Smrg        u_plus_rw=
2328255a313Smrg      else
23301395c31Smrg        u_plus_rw='% 200'
2348255a313Smrg      fi
2358255a313Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2368255a313Smrg    *)
2378255a313Smrg      if test -z "$stripcmd"; then
23801395c31Smrg        u_plus_rw=
2398255a313Smrg      else
24001395c31Smrg        u_plus_rw=,u+rw
2418255a313Smrg      fi
2428255a313Smrg      cp_umask=$mode$u_plus_rw;;
2438255a313Smrg  esac
2448255a313Smrgfi
2458255a313Smrg
24692bb16f8Smrgfor src
24792bb16f8Smrgdo
24801395c31Smrg  # Protect names problematic for 'test' and other utilities.
24992bb16f8Smrg  case $src in
25001395c31Smrg    -* | [=\(\)!]) src=./$src;;
25192bb16f8Smrg  esac
25292bb16f8Smrg
25392bb16f8Smrg  if test -n "$dir_arg"; then
25492bb16f8Smrg    dst=$src
2558255a313Smrg    dstdir=$dst
2568255a313Smrg    test -d "$dstdir"
2578255a313Smrg    dstdir_status=$?
25892bb16f8Smrg  else
2598255a313Smrg
26092bb16f8Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
26192bb16f8Smrg    # might cause directories to be created, which would be especially bad
26292bb16f8Smrg    # if $src (and thus $dsttmp) contains '*'.
26392bb16f8Smrg    if test ! -f "$src" && test ! -d "$src"; then
26492bb16f8Smrg      echo "$0: $src does not exist." >&2
26592bb16f8Smrg      exit 1
26692bb16f8Smrg    fi
26792bb16f8Smrg
2688255a313Smrg    if test -z "$dst_arg"; then
26992bb16f8Smrg      echo "$0: no destination specified." >&2
27092bb16f8Smrg      exit 1
27192bb16f8Smrg    fi
2728255a313Smrg    dst=$dst_arg
27392bb16f8Smrg
27401395c31Smrg    # If destination is a directory, append the input filename.
27592bb16f8Smrg    if test -d "$dst"; then
27601395c31Smrg      if test "$is_target_a_directory" = never; then
27701395c31Smrg        echo "$0: $dst_arg: Is a directory" >&2
27801395c31Smrg        exit 1
27992bb16f8Smrg      fi
2808255a313Smrg      dstdir=$dst
28101395c31Smrg      dstbase=`basename "$src"`
28201395c31Smrg      case $dst in
28301395c31Smrg	*/) dst=$dst$dstbase;;
28401395c31Smrg	*)  dst=$dst/$dstbase;;
28501395c31Smrg      esac
2868255a313Smrg      dstdir_status=0
2878255a313Smrg    else
28801395c31Smrg      dstdir=`dirname "$dst"`
2898255a313Smrg      test -d "$dstdir"
2908255a313Smrg      dstdir_status=$?
29192bb16f8Smrg    fi
29292bb16f8Smrg  fi
29392bb16f8Smrg
29401395c31Smrg  case $dstdir in
29501395c31Smrg    */) dstdirslash=$dstdir;;
29601395c31Smrg    *)  dstdirslash=$dstdir/;;
29701395c31Smrg  esac
29801395c31Smrg
2998255a313Smrg  obsolete_mkdir_used=false
3008255a313Smrg
3018255a313Smrg  if test $dstdir_status != 0; then
3028255a313Smrg    case $posix_mkdir in
3038255a313Smrg      '')
30401395c31Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
30501395c31Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
30601395c31Smrg        umask=`umask`
30701395c31Smrg        case $stripcmd.$umask in
30801395c31Smrg          # Optimize common cases.
30901395c31Smrg          *[2367][2367]) mkdir_umask=$umask;;
31001395c31Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
31101395c31Smrg
31201395c31Smrg          *[0-7])
31301395c31Smrg            mkdir_umask=`expr $umask + 22 \
31401395c31Smrg              - $umask % 100 % 40 + $umask % 20 \
31501395c31Smrg              - $umask % 10 % 4 + $umask % 2
31601395c31Smrg            `;;
31701395c31Smrg          *) mkdir_umask=$umask,go-w;;
31801395c31Smrg        esac
31901395c31Smrg
32001395c31Smrg        # With -d, create the new directory with the user-specified mode.
32101395c31Smrg        # Otherwise, rely on $mkdir_umask.
32201395c31Smrg        if test -n "$dir_arg"; then
32301395c31Smrg          mkdir_mode=-m$mode
32401395c31Smrg        else
32501395c31Smrg          mkdir_mode=
32601395c31Smrg        fi
32701395c31Smrg
32801395c31Smrg        posix_mkdir=false
32901395c31Smrg        case $umask in
33001395c31Smrg          *[123567][0-7][0-7])
33101395c31Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
33201395c31Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
33301395c31Smrg            ;;
33401395c31Smrg          *)
33501395c31Smrg            # Note that $RANDOM variable is not portable (e.g. dash);  Use it
33601395c31Smrg            # here however when possible just to lower collision chance.
33701395c31Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
33801395c31Smrg
33901395c31Smrg            trap 'ret=$?; rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null; exit $ret' 0
34001395c31Smrg
34101395c31Smrg            # Because "mkdir -p" follows existing symlinks and we likely work
34201395c31Smrg            # directly in world-writeable /tmp, make sure that the '$tmpdir'
34301395c31Smrg            # directory is successfully created first before we actually test
34401395c31Smrg            # 'mkdir -p' feature.
34501395c31Smrg            if (umask $mkdir_umask &&
34601395c31Smrg                $mkdirprog $mkdir_mode "$tmpdir" &&
34701395c31Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
34801395c31Smrg            then
34901395c31Smrg              if test -z "$dir_arg" || {
35001395c31Smrg                   # Check for POSIX incompatibilities with -m.
35101395c31Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35201395c31Smrg                   # other-writable bit of parent directory when it shouldn't.
35301395c31Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
35401395c31Smrg                   test_tmpdir="$tmpdir/a"
35501395c31Smrg                   ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
35601395c31Smrg                   case $ls_ld_tmpdir in
35701395c31Smrg                     d????-?r-*) different_mode=700;;
35801395c31Smrg                     d????-?--*) different_mode=755;;
35901395c31Smrg                     *) false;;
36001395c31Smrg                   esac &&
36101395c31Smrg                   $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
36201395c31Smrg                     ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
36301395c31Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
36401395c31Smrg                   }
36501395c31Smrg                 }
36601395c31Smrg              then posix_mkdir=:
36701395c31Smrg              fi
36801395c31Smrg              rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
36901395c31Smrg            else
37001395c31Smrg              # Remove any dirs left behind by ancient mkdir implementations.
37101395c31Smrg              rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
37201395c31Smrg            fi
37301395c31Smrg            trap '' 0;;
37401395c31Smrg        esac;;
3758255a313Smrg    esac
37692bb16f8Smrg
3778255a313Smrg    if
3788255a313Smrg      $posix_mkdir && (
37901395c31Smrg        umask $mkdir_umask &&
38001395c31Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3818255a313Smrg      )
3828255a313Smrg    then :
3838255a313Smrg    else
38492bb16f8Smrg
3858255a313Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3868255a313Smrg      # or it failed possibly due to a race condition.  Create the
3878255a313Smrg      # directory the slow way, step by step, checking for races as we go.
38892bb16f8Smrg
3898255a313Smrg      case $dstdir in
39001395c31Smrg        /*) prefix='/';;
39101395c31Smrg        [-=\(\)!]*) prefix='./';;
39201395c31Smrg        *)  prefix='';;
3938255a313Smrg      esac
39492bb16f8Smrg
3958255a313Smrg      oIFS=$IFS
3968255a313Smrg      IFS=/
39701395c31Smrg      set -f
3988255a313Smrg      set fnord $dstdir
39992bb16f8Smrg      shift
40001395c31Smrg      set +f
4018255a313Smrg      IFS=$oIFS
4028255a313Smrg
4038255a313Smrg      prefixes=
4048255a313Smrg
4058255a313Smrg      for d
4068255a313Smrg      do
40701395c31Smrg        test X"$d" = X && continue
40801395c31Smrg
40901395c31Smrg        prefix=$prefix$d
41001395c31Smrg        if test -d "$prefix"; then
41101395c31Smrg          prefixes=
41201395c31Smrg        else
41301395c31Smrg          if $posix_mkdir; then
41401395c31Smrg            (umask=$mkdir_umask &&
41501395c31Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
41601395c31Smrg            # Don't fail if two instances are running concurrently.
41701395c31Smrg            test -d "$prefix" || exit 1
41801395c31Smrg          else
41901395c31Smrg            case $prefix in
42001395c31Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42101395c31Smrg              *) qprefix=$prefix;;
42201395c31Smrg            esac
42301395c31Smrg            prefixes="$prefixes '$qprefix'"
42401395c31Smrg          fi
42501395c31Smrg        fi
42601395c31Smrg        prefix=$prefix/
4278255a313Smrg      done
4288255a313Smrg
4298255a313Smrg      if test -n "$prefixes"; then
43001395c31Smrg        # Don't fail if two instances are running concurrently.
43101395c31Smrg        (umask $mkdir_umask &&
43201395c31Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
43301395c31Smrg          test -d "$dstdir" || exit 1
43401395c31Smrg        obsolete_mkdir_used=true
43592bb16f8Smrg      fi
4368255a313Smrg    fi
43792bb16f8Smrg  fi
43892bb16f8Smrg
43992bb16f8Smrg  if test -n "$dir_arg"; then
4408255a313Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4418255a313Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4428255a313Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4438255a313Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44492bb16f8Smrg  else
44592bb16f8Smrg
44692bb16f8Smrg    # Make a couple of temp file names in the proper directory.
44701395c31Smrg    dsttmp=${dstdirslash}_inst.$$_
44801395c31Smrg    rmtmp=${dstdirslash}_rm.$$_
44992bb16f8Smrg
45092bb16f8Smrg    # Trap to clean up those temp files at exit.
45192bb16f8Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45292bb16f8Smrg
45392bb16f8Smrg    # Copy the file name to the temp name.
4548255a313Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
45592bb16f8Smrg
45692bb16f8Smrg    # and set any options; do chmod last to preserve setuid bits.
45792bb16f8Smrg    #
45892bb16f8Smrg    # If any of these fail, we abort the whole thing.  If we want to
45992bb16f8Smrg    # ignore errors from any of these, just make sure not to ignore
46092bb16f8Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
46192bb16f8Smrg    #
4628255a313Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4638255a313Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4648255a313Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4658255a313Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4668255a313Smrg
4678255a313Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4688255a313Smrg    if $copy_on_change &&
46901395c31Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
47001395c31Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
47101395c31Smrg       set -f &&
4728255a313Smrg       set X $old && old=:$2:$4:$5:$6 &&
4738255a313Smrg       set X $new && new=:$2:$4:$5:$6 &&
47401395c31Smrg       set +f &&
4758255a313Smrg       test "$old" = "$new" &&
4768255a313Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4778255a313Smrg    then
4788255a313Smrg      rm -f "$dsttmp"
4798255a313Smrg    else
4808255a313Smrg      # Rename the file to the real destination.
4818255a313Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4828255a313Smrg
4838255a313Smrg      # The rename failed, perhaps because mv can't rename something else
4848255a313Smrg      # to itself, or perhaps because mv is so ancient that it does not
4858255a313Smrg      # support -f.
4868255a313Smrg      {
48701395c31Smrg        # Now remove or move aside any old file at destination location.
48801395c31Smrg        # We try this two ways since rm can't unlink itself on some
48901395c31Smrg        # systems and the destination file might be busy for other
49001395c31Smrg        # reasons.  In this case, the final cleanup might fail but the new
49101395c31Smrg        # file should still install successfully.
49201395c31Smrg        {
49301395c31Smrg          test ! -f "$dst" ||
49401395c31Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
49501395c31Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
49601395c31Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
49701395c31Smrg          } ||
49801395c31Smrg          { echo "$0: cannot unlink or rename $dst" >&2
49901395c31Smrg            (exit 1); exit 1
50001395c31Smrg          }
50101395c31Smrg        } &&
50201395c31Smrg
50301395c31Smrg        # Now rename the file to the real destination.
50401395c31Smrg        $doit $mvcmd "$dsttmp" "$dst"
5058255a313Smrg      }
5068255a313Smrg    fi || exit 1
5078255a313Smrg
5088255a313Smrg    trap '' 0
5098255a313Smrg  fi
51092bb16f8Smrgdone
51192bb16f8Smrg
51292bb16f8Smrg# Local variables:
51301395c31Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
51492bb16f8Smrg# time-stamp-start: "scriptversion="
51592bb16f8Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
51601395c31Smrg# time-stamp-time-zone: "UTC0"
5178255a313Smrg# time-stamp-end: "; # UTC"
51892bb16f8Smrg# End:
519