Rich Felker | e678fc6 | 2013-08-17 22:21:11 -0400 | [diff] [blame] | 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 | |
| 8 | usage() { |
| 9 | printf "usage: %s [-D] [-l] [-m mode] src dest\n" "$0" 1>&2 |
| 10 | exit 1 |
| 11 | } |
| 12 | |
| 13 | mkdirp= |
| 14 | symlink= |
| 15 | mode=755 |
| 16 | |
| 17 | while getopts Dlm: name ; do |
| 18 | case "$name" in |
| 19 | D) mkdirp=yes ;; |
| 20 | l) symlink=yes ;; |
| 21 | m) mode=$OPTARG ;; |
| 22 | ?) usage ;; |
| 23 | esac |
| 24 | done |
| 25 | shift $(($OPTIND - 1)) |
| 26 | |
| 27 | test "$#" -eq 2 || usage |
| 28 | src=$1 |
| 29 | dst=$2 |
| 30 | tmp="$dst.tmp.$$" |
| 31 | |
| 32 | case "$dst" in |
| 33 | */) printf "%s: %s ends in /\n", "$0" "$dst" 1>&2 ; exit 1 ;; |
| 34 | esac |
| 35 | |
| 36 | set -C |
| 37 | set -e |
| 38 | |
| 39 | if test "$mkdirp" ; then |
| 40 | umask 022 |
| 41 | case "$2" in |
| 42 | */*) mkdir -p "${dst%/*}" ;; |
| 43 | esac |
| 44 | fi |
| 45 | |
| 46 | trap 'rm -f "$tmp"' EXIT INT QUIT TERM HUP |
| 47 | |
| 48 | umask 077 |
| 49 | |
| 50 | if test "$symlink" ; then |
| 51 | ln -s "$1" "$tmp" |
| 52 | else |
| 53 | cat < "$1" > "$tmp" |
Rich Felker | 7cbb6f7 | 2014-01-15 22:29:13 -0500 | [diff] [blame] | 54 | chmod "$mode" "$tmp" |
Rich Felker | e678fc6 | 2013-08-17 22:21:11 -0400 | [diff] [blame] | 55 | fi |
| 56 | |
Rich Felker | ceacb6a | 2013-12-03 19:01:40 -0500 | [diff] [blame] | 57 | mv -f "$tmp" "$2" |
Rich Felker | e678fc6 | 2013-08-17 22:21:11 -0400 | [diff] [blame] | 58 | test -d "$2" && { |
| 59 | rm -f "$2/$tmp" |
| 60 | printf "%s: %s is a directory\n" "$0" "$dst" 1>&2 |
| 61 | exit 1 |
| 62 | } |
| 63 | |
Rich Felker | e678fc6 | 2013-08-17 22:21:11 -0400 | [diff] [blame] | 64 | exit 0 |