1
0
Fork 0

browser-home-profile/functions/mktmp: Use only a mktemp variant function

pull/890/head
tokiclover 10 years ago
parent 5ce054262a
commit 2069e408fb

@ -1,106 +1,84 @@
# #
# $Header: firefox-home-profile/functions/checkpath Exp $ # $Header: browser-home-profile/functions/mktmp Exp $
# $Aythor: (c) 2012-015 -tclover <tokiclover@gmail.com> Exp $ # $Aythor: (c) 2012-015 -tclover <tokiclover@gmail.com> Exp $
# $License: MIT (or 2-clause/new/simplified BSD) Exp $ # $License: MIT (or 2-clause/new/simplified BSD) Exp $
# $Version: 0.7 2015/05/05 21:09:26 Exp $ # $Version: 0.7 2015/05/14 21:09:26 Exp $
# #
function die { function mktmp {
local ret=$? function mktmp-help {
print -P " %F{red}*%f ${(%):-%1x}: $argv" >&2 cat <<-EOH
return $ret usage: mktmp [-p] [-d|-f] [-m mode] [-o owner[:group] TEMPLATE-XXXXXX
} -d, --dir (Create a) directory
function checkpath {
function usage {
cat <<-EOH
usage: ${(%):-%1x} [-p] [-d|-f] [-m mode] [-o owner[:group]] TEMPLATE|DIR|FILE
-d, --dir (Create a) directory
-f, --file (Create a) file -f, --file (Create a) file
-P, --pipe (Create a) pipe (FIFO)
-o, --owner <name> Use owner name -o, --owner <name> Use owner name
-g, --group <name> Use group name -g, --group <name> Use group name
-m, --mode <1700> Use octal mode -m, --mode 1700 Use octal mode
-c, --checkpath Enable check mode
-p, --tmpdir[=DIR] Enable mktmp mode -p, --tmpdir[=DIR] Enable mktmp mode
-q, --quiet Enable quiet mode
-h, --help Help/Exit -h, --help Help/Exit
EOH EOH
return }
}
(( $# == 0 )) && usage
local args temp=-XXXXXX type
args="$(getopt \
-o Pcdfg:hm:o:p::q \
-l checkpath,dir,file,group:,tmpdir:: \
-l help,mode:,owner:,pipe,quiet \
-s sh -n checkpath -- "$@")"
(( $? == 0 )) || usage
eval set -- $args
args=
local group mode owner task tmp quiet if (( ${#} == 0 )) {
for (( ; $# > 1; )) { mktmp-help
case $1 { return 1
(-c|--chec*) task=chk ;; }
(-p|--tmpd*) task=tmp tmpdir=${2:-$TMPDIR}; shift;;
(-d|--dir) args=-d type=dir;; local ARGS name=mktmp
(-f|--file) type=file ;; ARGS="$(getopt \
(-P|--pipe) type=pipe ;; -o dfg:hm:o:p:: \
(-h|--help) usage ;; -l dir,file,group:,tmpdir:: -l help,mode:owner: \
(-m|--mode) mode=$2 ; shift;; -s sh -n mktmp -- "${@}")"
(-o|--owner) owner=$2 ; shift;; if (( ${?} != 0 )) { mktmp-help; return 2; }
(-g|-group) group=$2 ; shift;; eval set -- ${ARGS}
(-q|--quiet) quiet=true;; ARGS=
(*) shift; break ;;
local group mode owner temp=-XXXXXX tmp type
while true; do
case ${1} {
(-p|--tmpd*) tmpdir=${2:-${TMPDIR:-/tmp}}; shift;;
(-h|--help) mktmp_help; return;;
(-m|--mode) mode=${2} ; shift;;
(-o|--owner) owner=${2}; shift;;
(-g|--group) group=${2}; shift;;
(-d|--dir) ARGS=-d type=dir;;
(-f|--file) type=file;;
(*) shift; break;;
} }
shift shift
} done
if ! [ $# -eq 1 -a -n "$1" ]; then
die "Invalid argument(s)/TEMPLATE" if ! ([[ ${#} == 1 ]] && [[ -n ${1} ]]); then
return pr_error "Invalid argument(s)"
return 3
fi fi
case ${1} {
(*${temp}) ;;
(*) pr_error "Invalid TEMPLATE"; return 4;;
}
case $task { if (( ${+commands[mktemp]} )); then
(tmp) tmp="$(mktemp ${tmpdir:+-p} ${tmpdir} ${ARGS} ${1})"
quiet= tmpdir=${tmpdir:-/tmp} else
if [[ ${1%$temp} = $1 ]] { (( ${+commands[uuidgen]} )) && temp=$(uuidgen --random)
die "Invalid TEMPLATE" tmp="${tmpdir}/${1%-*}-${temp:1:6}"
return fi
} case ${type} {
if (( $+commands[mktemp] )) { (dir) [[ -d ${tmp} ]] || mkdir -p ${tmp};;
tmp=$($commands[mktemp] -p $tmpdir $args $1) (*) [[ -e ${tmp} ]] || { mkdir -p ${tmp:h}; touch ${tmp}; };;
} else {
(( $+commands[uuidgen] )) && temp=$($commands[uuidgen] --random)
tmp=$tmpdir/${1%-*}-$temp[1,6]
}
;;
(*)
tmp=$1
} }
case $type { if (( ${?} != 0 )) {
(dir) pr_error "Failed to create ${tmp}"
[[ -d $tmp ]] || mkdir -p $tmp return 5
;;
(*)
[[ -e $tmp ]] || mkdir -p $tmp:h && break
case $type {
(pipe) mkfifo $tmp;;
(file) touch $tmp;;
}
;;
} }
[[ -h ${tmp} ]] && return
(( $? == 0 )) || { die "Failed to create ${tmp}"; return; } (( -n ${owner} )) && chown ${owner} ${tmp}
[[ -h $tmp ]] && return (( -n ${group} )) && chgrp ${group} ${tmp}
[[ -n $owner ]] && chown $owner $tmp (( -n ${mode} )) && chmod ${mode} ${tmp}
[[ -n $group ]] && chgrp $group $tmp
[[ -n $mode ]] && chmod $mode $tmp
[[ -n $quiet ]] || print $tmp
} }
if [[ ${(%):-%1x} == (checkpath|mktmp)(|.zsh) ]] { case ${(%):-%1x} {
checkpath "$@" (mktmp*) mktmp "${@}";;
} }
# #

Loading…
Cancel
Save