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