Path: blob/main/contrib/bc/scripts/safe-install.sh
39534 views
#!/bin/sh1#2# Written by Rich Felker, originally as part of musl libc.3# Multi-licensed under MIT, 0BSD, and CC0.4#5# This is an actually-safe install command which installs the new6# file atomically in the new location, rather than overwriting7# existing files.8#910usage() {11printf "usage: %s [-D] [-l] [-m mode] src dest\n" "$0" 1>&212exit 113}1415mkdirp=16symlink=17mode=7551819while getopts Dlm: name ; do20case "$name" in21D) mkdirp=yes ;;22l) symlink=yes ;;23m) mode=$OPTARG ;;24?) usage ;;25esac26done27shift $(($OPTIND - 1))2829test "$#" -eq 2 || usage30src=$131dst=$232tmp="$dst.tmp.$$"3334case "$dst" in35*/) printf "%s: %s ends in /\n", "$0" "$dst" 1>&2 ; exit 1 ;;36esac3738set -C39set -e4041if test "$mkdirp" ; then42umask 02243case "$dst" in44*/*) mkdir -p "${dst%/*}" ;;45esac46fi4748trap 'rm -f "$tmp"' EXIT INT QUIT TERM HUP4950umask 0775152if test "$symlink" ; then53ln -s "$src" "$tmp"54else55cat < "$src" > "$tmp"56chmod "$mode" "$tmp"57fi5859mv -f "$tmp" "$dst"60test -d "$dst" && {61rm -f "$dst/$tmp"62printf "%s: %s is a directory\n" "$0" "$dst" 1>&263exit 164}6566exit 0676869