• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/sh
2#
3# This is an actually-safe install command which installs the new
4# file atomically in the new location, rather than overwriting
5# existing files.
6#
7
8usage() {
9printf "usage: %s [-D] [-l] [-r] [-m mode] src dest\n" "$0" 1>&2
10exit 1
11}
12
13mkdirp=
14symlink=
15symlinkflags="-s"
16mode=755
17
18while getopts Dlrm: name ; do
19case "$name" in
20D) mkdirp=yes ;;
21l) symlink=yes ;;
22r) symlink=yes; symlinkflags="$symlinkflags -r" ;;
23m) mode=$OPTARG ;;
24?) usage ;;
25esac
26done
27shift $(($OPTIND - 1))
28
29test "$#" -eq 2 || usage
30src=$1
31dst=$2
32tmp="$dst.tmp.$$"
33
34case "$dst" in
35*/) printf "%s: %s ends in /\n", "$0" "$dst" 1>&2 ; exit 1 ;;
36esac
37
38set -C
39set -e
40
41if test "$mkdirp" ; then
42umask 022
43case "$2" in
44*/*) mkdir -p "${dst%/*}" ;;
45esac
46fi
47
48trap 'rm -f "$tmp"' EXIT INT QUIT TERM HUP
49
50umask 077
51
52if test "$symlink" ; then
53ln $symlinkflags "$1" "$tmp"
54else
55cat < "$1" > "$tmp"
56chmod "$mode" "$tmp"
57fi
58
59mv -f "$tmp" "$2"
60test -d "$2" && {
61rm -f "$2/$tmp"
62printf "%s: %s is a directory\n" "$0" "$dst" 1>&2
63exit 1
64}
65
66exit 0
67