2
0

install.sh 944 B

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