avoid creating symlinks with restricted permissions
[rofl0r-install.sh.git] / install.sh
blob2888b1eba7b6798dcb7e11b3f71d61993605ebb6
1 #!/bin/sh
3 # Written by Rich Felker, originally as part of musl libc.
4 # Multi-licensed under MIT, 0BSD, and CC0.
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.
11 usage() {
12 printf "usage: %s [-D] [-l] [-m mode] src dest\n" "$0" 1>&2
13 exit 1
16 mkdirp=
17 symlink=
18 mode=755
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))
30 test "$#" -eq 2 || usage
31 src=$1
32 dst=$2
33 tmp="$dst.tmp.$$"
35 case "$dst" in
36 */) printf "%s: %s ends in /\n", "$0" "$dst" 1>&2 ; exit 1 ;;
37 esac
39 set -C
40 set -e
42 if test "$mkdirp" ; then
43 umask 022
44 case "$2" in
45 */*) mkdir -p "${dst%/*}" ;;
46 esac
49 trap 'rm -f "$tmp"' EXIT INT QUIT TERM HUP
51 umask 077
53 if test "$symlink" ; then
54 umask 000
55 ln -s "$1" "$tmp"
56 umask 077
57 else
58 cat < "$1" > "$tmp"
59 chmod "$mode" "$tmp"
62 mv -f "$tmp" "$2"
63 test -d "$2" && {
64 rm -f "$2/$tmp"
65 printf "%s: %s is a directory\n" "$0" "$dst" 1>&2
66 exit 1
69 exit 0