csomh / source-git / rpm

Forked from source-git/rpm 4 years ago
Clone
2ff057
#!/bin/sh
2ff057
# install - install a program, script, or datafile
2ff057
2ff057
scriptversion=2013-12-25.23; # UTC
2ff057
2ff057
# This originates from X11R5 (mit/util/scripts/install.sh), which was
2ff057
# later released in X11R6 (xc/config/util/install.sh) with the
2ff057
# following copyright and license.
2ff057
#
2ff057
# Copyright (C) 1994 X Consortium
2ff057
#
2ff057
# Permission is hereby granted, free of charge, to any person obtaining a copy
2ff057
# of this software and associated documentation files (the "Software"), to
2ff057
# deal in the Software without restriction, including without limitation the
2ff057
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
2ff057
# sell copies of the Software, and to permit persons to whom the Software is
2ff057
# furnished to do so, subject to the following conditions:
2ff057
#
2ff057
# The above copyright notice and this permission notice shall be included in
2ff057
# all copies or substantial portions of the Software.
2ff057
#
2ff057
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2ff057
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2ff057
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2ff057
# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2ff057
# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2ff057
# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2ff057
#
2ff057
# Except as contained in this notice, the name of the X Consortium shall not
2ff057
# be used in advertising or otherwise to promote the sale, use or other deal-
2ff057
# ings in this Software without prior written authorization from the X Consor-
2ff057
# tium.
2ff057
#
2ff057
#
2ff057
# FSF changes to this file are in the public domain.
2ff057
#
2ff057
# Calling this script install-sh is preferred over install.sh, to prevent
2ff057
# 'make' implicit rules from creating a file called install from it
2ff057
# when there is no Makefile.
2ff057
#
2ff057
# This script is compatible with the BSD install script, but was written
2ff057
# from scratch.
2ff057
2ff057
tab='	'
2ff057
nl='
2ff057
'
2ff057
IFS=" $tab$nl"
2ff057
2ff057
# Set DOITPROG to "echo" to test this script.
2ff057
2ff057
doit=${DOITPROG-}
2ff057
doit_exec=${doit:-exec}
2ff057
2ff057
# Put in absolute file names if you don't have them in your path;
2ff057
# or use environment vars.
2ff057
2ff057
chgrpprog=${CHGRPPROG-chgrp}
2ff057
chmodprog=${CHMODPROG-chmod}
2ff057
chownprog=${CHOWNPROG-chown}
2ff057
cmpprog=${CMPPROG-cmp}
2ff057
cpprog=${CPPROG-cp}
2ff057
mkdirprog=${MKDIRPROG-mkdir}
2ff057
mvprog=${MVPROG-mv}
2ff057
rmprog=${RMPROG-rm}
2ff057
stripprog=${STRIPPROG-strip}
2ff057
2ff057
posix_mkdir=
2ff057
2ff057
# Desired mode of installed file.
2ff057
mode=0755
2ff057
2ff057
chgrpcmd=
2ff057
chmodcmd=$chmodprog
2ff057
chowncmd=
2ff057
mvcmd=$mvprog
2ff057
rmcmd="$rmprog -f"
2ff057
stripcmd=
2ff057
2ff057
src=
2ff057
dst=
2ff057
dir_arg=
2ff057
dst_arg=
2ff057
2ff057
copy_on_change=false
2ff057
is_target_a_directory=possibly
2ff057
2ff057
usage="\
2ff057
Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
2ff057
   or: $0 [OPTION]... SRCFILES... DIRECTORY
2ff057
   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
2ff057
   or: $0 [OPTION]... -d DIRECTORIES...
2ff057
2ff057
In the 1st form, copy SRCFILE to DSTFILE.
2ff057
In the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
2ff057
In the 4th, create DIRECTORIES.
2ff057
2ff057
Options:
2ff057
     --help     display this help and exit.
2ff057
     --version  display version info and exit.
2ff057
2ff057
  -c            (ignored)
2ff057
  -C            install only if different (preserve the last data modification time)
2ff057
  -d            create directories instead of installing files.
2ff057
  -g GROUP      $chgrpprog installed files to GROUP.
2ff057
  -m MODE       $chmodprog installed files to MODE.
2ff057
  -o USER       $chownprog installed files to USER.
2ff057
  -s            $stripprog installed files.
2ff057
  -t DIRECTORY  install into DIRECTORY.
2ff057
  -T            report an error if DSTFILE is a directory.
2ff057
2ff057
Environment variables override the default commands:
2ff057
  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
2ff057
  RMPROG STRIPPROG
2ff057
"
2ff057
2ff057
while test $# -ne 0; do
2ff057
  case $1 in
2ff057
    -c) ;;
2ff057
2ff057
    -C) copy_on_change=true;;
2ff057
2ff057
    -d) dir_arg=true;;
2ff057
2ff057
    -g) chgrpcmd="$chgrpprog $2"
2ff057
        shift;;
2ff057
2ff057
    --help) echo "$usage"; exit $?;;
2ff057
2ff057
    -m) mode=$2
2ff057
        case $mode in
2ff057
          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
2ff057
            echo "$0: invalid mode: $mode" >&2
2ff057
            exit 1;;
2ff057
        esac
2ff057
        shift;;
2ff057
2ff057
    -o) chowncmd="$chownprog $2"
2ff057
        shift;;
2ff057
2ff057
    -s) stripcmd=$stripprog;;
2ff057
2ff057
    -t)
2ff057
        is_target_a_directory=always
2ff057
        dst_arg=$2
2ff057
        # Protect names problematic for 'test' and other utilities.
2ff057
        case $dst_arg in
2ff057
          -* | [=\(\)!]) dst_arg=./$dst_arg;;
2ff057
        esac
2ff057
        shift;;
2ff057
2ff057
    -T) is_target_a_directory=never;;
2ff057
2ff057
    --version) echo "$0 $scriptversion"; exit $?;;
2ff057
2ff057
    --) shift
2ff057
        break;;
2ff057
2ff057
    -*) echo "$0: invalid option: $1" >&2
2ff057
        exit 1;;
2ff057
2ff057
    *)  break;;
2ff057
  esac
2ff057
  shift
2ff057
done
2ff057
2ff057
# We allow the use of options -d and -T together, by making -d
2ff057
# take the precedence; this is for compatibility with GNU install.
2ff057
2ff057
if test -n "$dir_arg"; then
2ff057
  if test -n "$dst_arg"; then
2ff057
    echo "$0: target directory not allowed when installing a directory." >&2
2ff057
    exit 1
2ff057
  fi
2ff057
fi
2ff057
2ff057
if test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
2ff057
  # When -d is used, all remaining arguments are directories to create.
2ff057
  # When -t is used, the destination is already specified.
2ff057
  # Otherwise, the last argument is the destination.  Remove it from $@.
2ff057
  for arg
2ff057
  do
2ff057
    if test -n "$dst_arg"; then
2ff057
      # $@ is not empty: it contains at least $arg.
2ff057
      set fnord "$@" "$dst_arg"
2ff057
      shift # fnord
2ff057
    fi
2ff057
    shift # arg
2ff057
    dst_arg=$arg
2ff057
    # Protect names problematic for 'test' and other utilities.
2ff057
    case $dst_arg in
2ff057
      -* | [=\(\)!]) dst_arg=./$dst_arg;;
2ff057
    esac
2ff057
  done
2ff057
fi
2ff057
2ff057
if test $# -eq 0; then
2ff057
  if test -z "$dir_arg"; then
2ff057
    echo "$0: no input file specified." >&2
2ff057
    exit 1
2ff057
  fi
2ff057
  # It's OK to call 'install-sh -d' without argument.
2ff057
  # This can happen when creating conditional directories.
2ff057
  exit 0
2ff057
fi
2ff057
2ff057
if test -z "$dir_arg"; then
2ff057
  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2ff057
    if test ! -d "$dst_arg"; then
2ff057
      echo "$0: $dst_arg: Is not a directory." >&2
2ff057
      exit 1
2ff057
    fi
2ff057
  fi
2ff057
fi
2ff057
2ff057
if test -z "$dir_arg"; then
2ff057
  do_exit='(exit $ret); exit $ret'
2ff057
  trap "ret=129; $do_exit" 1
2ff057
  trap "ret=130; $do_exit" 2
2ff057
  trap "ret=141; $do_exit" 13
2ff057
  trap "ret=143; $do_exit" 15
2ff057
2ff057
  # Set umask so as not to create temps with too-generous modes.
2ff057
  # However, 'strip' requires both read and write access to temps.
2ff057
  case $mode in
2ff057
    # Optimize common cases.
2ff057
    *644) cp_umask=133;;
2ff057
    *755) cp_umask=22;;
2ff057
2ff057
    *[0-7])
2ff057
      if test -z "$stripcmd"; then
2ff057
        u_plus_rw=
2ff057
      else
2ff057
        u_plus_rw='% 200'
2ff057
      fi
2ff057
      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2ff057
    *)
2ff057
      if test -z "$stripcmd"; then
2ff057
        u_plus_rw=
2ff057
      else
2ff057
        u_plus_rw=,u+rw
2ff057
      fi
2ff057
      cp_umask=$mode$u_plus_rw;;
2ff057
  esac
2ff057
fi
2ff057
2ff057
for src
2ff057
do
2ff057
  # Protect names problematic for 'test' and other utilities.
2ff057
  case $src in
2ff057
    -* | [=\(\)!]) src=./$src;;
2ff057
  esac
2ff057
2ff057
  if test -n "$dir_arg"; then
2ff057
    dst=$src
2ff057
    dstdir=$dst
2ff057
    test -d "$dstdir"
2ff057
    dstdir_status=$?
2ff057
  else
2ff057
2ff057
    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2ff057
    # might cause directories to be created, which would be especially bad
2ff057
    # if $src (and thus $dsttmp) contains '*'.
2ff057
    if test ! -f "$src" && test ! -d "$src"; then
2ff057
      echo "$0: $src does not exist." >&2
2ff057
      exit 1
2ff057
    fi
2ff057
2ff057
    if test -z "$dst_arg"; then
2ff057
      echo "$0: no destination specified." >&2
2ff057
      exit 1
2ff057
    fi
2ff057
    dst=$dst_arg
2ff057
2ff057
    # If destination is a directory, append the input filename; won't work
2ff057
    # if double slashes aren't ignored.
2ff057
    if test -d "$dst"; then
2ff057
      if test "$is_target_a_directory" = never; then
2ff057
        echo "$0: $dst_arg: Is a directory" >&2
2ff057
        exit 1
2ff057
      fi
2ff057
      dstdir=$dst
2ff057
      dst=$dstdir/`basename "$src"`
2ff057
      dstdir_status=0
2ff057
    else
2ff057
      dstdir=`dirname "$dst"`
2ff057
      test -d "$dstdir"
2ff057
      dstdir_status=$?
2ff057
    fi
2ff057
  fi
2ff057
2ff057
  obsolete_mkdir_used=false
2ff057
2ff057
  if test $dstdir_status != 0; then
2ff057
    case $posix_mkdir in
2ff057
      '')
2ff057
        # Create intermediate dirs using mode 755 as modified by the umask.
2ff057
        # This is like FreeBSD 'install' as of 1997-10-28.
2ff057
        umask=`umask`
2ff057
        case $stripcmd.$umask in
2ff057
          # Optimize common cases.
2ff057
          *[2367][2367]) mkdir_umask=$umask;;
2ff057
          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
2ff057
2ff057
          *[0-7])
2ff057
            mkdir_umask=`expr $umask + 22 \
2ff057
              - $umask % 100 % 40 + $umask % 20 \
2ff057
              - $umask % 10 % 4 + $umask % 2
2ff057
            `;;
2ff057
          *) mkdir_umask=$umask,go-w;;
2ff057
        esac
2ff057
2ff057
        # With -d, create the new directory with the user-specified mode.
2ff057
        # Otherwise, rely on $mkdir_umask.
2ff057
        if test -n "$dir_arg"; then
2ff057
          mkdir_mode=-m$mode
2ff057
        else
2ff057
          mkdir_mode=
2ff057
        fi
2ff057
2ff057
        posix_mkdir=false
2ff057
        case $umask in
2ff057
          *[123567][0-7][0-7])
2ff057
            # POSIX mkdir -p sets u+wx bits regardless of umask, which
2ff057
            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
2ff057
            ;;
2ff057
          *)
2ff057
            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
2ff057
            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
2ff057
2ff057
            if (umask $mkdir_umask &&
2ff057
                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
2ff057
            then
2ff057
              if test -z "$dir_arg" || {
2ff057
                   # Check for POSIX incompatibilities with -m.
2ff057
                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
2ff057
                   # other-writable bit of parent directory when it shouldn't.
2ff057
                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
2ff057
                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
2ff057
                   case $ls_ld_tmpdir in
2ff057
                     d????-?r-*) different_mode=700;;
2ff057
                     d????-?--*) different_mode=755;;
2ff057
                     *) false;;
2ff057
                   esac &&
2ff057
                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
2ff057
                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
2ff057
                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
2ff057
                   }
2ff057
                 }
2ff057
              then posix_mkdir=:
2ff057
              fi
2ff057
              rmdir "$tmpdir/d" "$tmpdir"
2ff057
            else
2ff057
              # Remove any dirs left behind by ancient mkdir implementations.
2ff057
              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
2ff057
            fi
2ff057
            trap '' 0;;
2ff057
        esac;;
2ff057
    esac
2ff057
2ff057
    if
2ff057
      $posix_mkdir && (
2ff057
        umask $mkdir_umask &&
2ff057
        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
2ff057
      )
2ff057
    then :
2ff057
    else
2ff057
2ff057
      # The umask is ridiculous, or mkdir does not conform to POSIX,
2ff057
      # or it failed possibly due to a race condition.  Create the
2ff057
      # directory the slow way, step by step, checking for races as we go.
2ff057
2ff057
      case $dstdir in
2ff057
        /*) prefix='/';;
2ff057
        [-=\(\)!]*) prefix='./';;
2ff057
        *)  prefix='';;
2ff057
      esac
2ff057
2ff057
      oIFS=$IFS
2ff057
      IFS=/
2ff057
      set -f
2ff057
      set fnord $dstdir
2ff057
      shift
2ff057
      set +f
2ff057
      IFS=$oIFS
2ff057
2ff057
      prefixes=
2ff057
2ff057
      for d
2ff057
      do
2ff057
        test X"$d" = X && continue
2ff057
2ff057
        prefix=$prefix$d
2ff057
        if test -d "$prefix"; then
2ff057
          prefixes=
2ff057
        else
2ff057
          if $posix_mkdir; then
2ff057
            (umask=$mkdir_umask &&
2ff057
             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
2ff057
            # Don't fail if two instances are running concurrently.
2ff057
            test -d "$prefix" || exit 1
2ff057
          else
2ff057
            case $prefix in
2ff057
              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
2ff057
              *) qprefix=$prefix;;
2ff057
            esac
2ff057
            prefixes="$prefixes '$qprefix'"
2ff057
          fi
2ff057
        fi
2ff057
        prefix=$prefix/
2ff057
      done
2ff057
2ff057
      if test -n "$prefixes"; then
2ff057
        # Don't fail if two instances are running concurrently.
2ff057
        (umask $mkdir_umask &&
2ff057
         eval "\$doit_exec \$mkdirprog $prefixes") ||
2ff057
          test -d "$dstdir" || exit 1
2ff057
        obsolete_mkdir_used=true
2ff057
      fi
2ff057
    fi
2ff057
  fi
2ff057
2ff057
  if test -n "$dir_arg"; then
2ff057
    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
2ff057
    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
2ff057
    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
2ff057
      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
2ff057
  else
2ff057
2ff057
    # Make a couple of temp file names in the proper directory.
2ff057
    dsttmp=$dstdir/_inst.$$_
2ff057
    rmtmp=$dstdir/_rm.$$_
2ff057
2ff057
    # Trap to clean up those temp files at exit.
2ff057
    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
2ff057
2ff057
    # Copy the file name to the temp name.
2ff057
    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
2ff057
2ff057
    # and set any options; do chmod last to preserve setuid bits.
2ff057
    #
2ff057
    # If any of these fail, we abort the whole thing.  If we want to
2ff057
    # ignore errors from any of these, just make sure not to ignore
2ff057
    # errors from the above "$doit $cpprog $src $dsttmp" command.
2ff057
    #
2ff057
    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
2ff057
    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
2ff057
    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
2ff057
    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
2ff057
2ff057
    # If -C, don't bother to copy if it wouldn't change the file.
2ff057
    if $copy_on_change &&
2ff057
       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
2ff057
       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
2ff057
       set -f &&
2ff057
       set X $old && old=:$2:$4:$5:$6 &&
2ff057
       set X $new && new=:$2:$4:$5:$6 &&
2ff057
       set +f &&
2ff057
       test "$old" = "$new" &&
2ff057
       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
2ff057
    then
2ff057
      rm -f "$dsttmp"
2ff057
    else
2ff057
      # Rename the file to the real destination.
2ff057
      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
2ff057
2ff057
      # The rename failed, perhaps because mv can't rename something else
2ff057
      # to itself, or perhaps because mv is so ancient that it does not
2ff057
      # support -f.
2ff057
      {
2ff057
        # Now remove or move aside any old file at destination location.
2ff057
        # We try this two ways since rm can't unlink itself on some
2ff057
        # systems and the destination file might be busy for other
2ff057
        # reasons.  In this case, the final cleanup might fail but the new
2ff057
        # file should still install successfully.
2ff057
        {
2ff057
          test ! -f "$dst" ||
2ff057
          $doit $rmcmd -f "$dst" 2>/dev/null ||
2ff057
          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
2ff057
            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
2ff057
          } ||
2ff057
          { echo "$0: cannot unlink or rename $dst" >&2
2ff057
            (exit 1); exit 1
2ff057
          }
2ff057
        } &&
2ff057
2ff057
        # Now rename the file to the real destination.
2ff057
        $doit $mvcmd "$dsttmp" "$dst"
2ff057
      }
2ff057
    fi || exit 1
2ff057
2ff057
    trap '' 0
2ff057
  fi
2ff057
done
2ff057
2ff057
# Local variables:
2ff057
# eval: (add-hook 'write-file-hooks 'time-stamp)
2ff057
# time-stamp-start: "scriptversion="
2ff057
# time-stamp-format: "%:y-%02m-%02d.%02H"
2ff057
# time-stamp-time-zone: "UTC"
2ff057
# time-stamp-end: "; # UTC"
2ff057
# End: