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