firefox-home-profile/checkpath: Use checkpath variant (instead of mktmp) with mktemp compatible swicthes
parent
ca94eccbb9
commit
f3081c8a40
@ -1,86 +1,108 @@
|
|||||||
#
|
#
|
||||||
# Provide a cheap helper for temporary directory/file creation
|
# $Header: firefox-home-profile/functions/checkpath Exp $
|
||||||
#
|
# $Aythor: (c) 2012-015 -tclover <tokiclover@gmail.com> Exp $
|
||||||
# $Header: firefox-profile/functions/fhp Exp $
|
|
||||||
# $Aythor: (c) 2011-2014 -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.2 2014/09/09 21:09:26 Exp $
|
# $Version: 0.7 2015/05/05 21:09:26 Exp $
|
||||||
#
|
#
|
||||||
|
|
||||||
function die {
|
function die {
|
||||||
local ret=$?
|
local ret=$?
|
||||||
print -P " %F{red}%1x: %F{yellow}%U%I%u:%f $argv" >&2
|
print -P " %F{red}*%f ${(%):-%1x}: $argv" >&2
|
||||||
return $ret
|
return $ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function checkpath {
|
||||||
function usage {
|
function usage {
|
||||||
cat <<-EOH
|
cat <<-EOH
|
||||||
usage: mktmp [-d|-f] [-m <mode>] [-o <owner[:group]>] [-g <group>] TEMPLATE
|
usage: ${(%):-%1x} [-p] [-d|-f] [-m mode] [-o owner[:group]] TEMPLATE|DIR|FILE
|
||||||
-d, --dir create a directory
|
-d, --dir (Create a) directory
|
||||||
-f, --file create a file
|
-f, --file (Create a) file
|
||||||
-o, --owner <name> owner naame
|
-P, --pipe (Create a) pipe (FIFO)
|
||||||
-g, --group <name> group name
|
-o, --owner <name> Use owner name
|
||||||
-m, --mode <1700> octal mode
|
-g, --group <name> Use group name
|
||||||
-h, --help help/exit
|
-m, --mode <1700> Use octal mode
|
||||||
|
-c, --checkpath Enable check mode
|
||||||
|
-p, --tmpdir[=DIR] Enable mktmp mode
|
||||||
|
-q, --quiet Enable quiet mode
|
||||||
|
-h, --help Help/Exit
|
||||||
EOH
|
EOH
|
||||||
return
|
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=
|
||||||
|
|
||||||
test $# -ge 1 -a -n "$1" || return
|
local group mode owner task tmp quiet
|
||||||
|
for (( ; $# > 1; )) {
|
||||||
local type temp=XXXXXX tmpdir=${TMPDIR:-/tmp}
|
|
||||||
for (( ; $# > 1; ))
|
|
||||||
case $1 {
|
case $1 {
|
||||||
(-d|--dir)
|
(-c|--chec*) task=chk ;;
|
||||||
type=dir
|
(-p|--tmpd*) task=tmp tmpdir=${2:-$TMPDIR}; shift;;
|
||||||
shift;;
|
(-d|--dir) args=-d type=dir;;
|
||||||
(-f|--file)
|
(-f|--file) type=file ;;
|
||||||
type=file
|
(-P|--pipe) type=pipe ;;
|
||||||
shift;;
|
(-h|--help) usage ;;
|
||||||
(-h|--help)
|
(-m|--mode) mode=$2 ; shift;;
|
||||||
usage;;
|
(-o|--owner) owner=$2 ; shift;;
|
||||||
(-m|--mode)
|
(-g|-group) group=$2 ; shift;;
|
||||||
local mode=$2
|
(-q|--quiet) quiet=true;;
|
||||||
shift 2;;
|
(*) shift; break ;;
|
||||||
(-o|--owner)
|
|
||||||
local owner="$2"
|
|
||||||
shift 2;;
|
|
||||||
(-g|-group)
|
|
||||||
local group="$2"
|
|
||||||
shift 2;;
|
|
||||||
(*)
|
|
||||||
die "invalid argument $@"
|
|
||||||
return;;
|
|
||||||
}
|
}
|
||||||
|
shift
|
||||||
test -n "$1" -a "${1%-$temp}" != "$1"
|
|
||||||
if (( $? )) {
|
|
||||||
die "invalid/null TEMPLATE"
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
(( $+commands[uuidgen] )) && temp=$($commands[uuidgen] --random)
|
if ! [ $# -eq 1 -a -n "$1" ]; then
|
||||||
local tmp="$tmpdir/${1%$temp}$temp[1,6]"
|
die "Invalid argument(s)/TEMPLATE"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
if [[ $type == "dir" ]] {
|
case $task {
|
||||||
mkdir -p ${mode:+-m$mode} "$tmp"
|
(tmp)
|
||||||
if (( $? )) {
|
quiet= tmpdir=${tmpdir:-/tmp}
|
||||||
die "failed to make $tmp"
|
if [[ ${1%$temp} = $1 ]] {
|
||||||
|
die "Invalid TEMPLATE"
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if (( $+commands[mktemp] )) {
|
||||||
|
tmp=$($commands[mktemp] -p $tmpdir $args $1)
|
||||||
} else {
|
} else {
|
||||||
mkdir -p "$tmp:h" && touch "$tmp"
|
(( $+commands[uuidgen] )) && temp=$($commands[uuidgen] --random)
|
||||||
if (( $? )) {
|
tmp=$tmpdir/${1%-*}-$temp[1,6]
|
||||||
die "failed to make $tmp"
|
}
|
||||||
return
|
;;
|
||||||
|
(*)
|
||||||
|
tmp=$1
|
||||||
}
|
}
|
||||||
(( $+mode )) && chmod $mode "$tmp"
|
case $type {
|
||||||
|
(dir)
|
||||||
|
[[ -d $tmp ]] || mkdir -p $tmp
|
||||||
|
;;
|
||||||
|
(*)
|
||||||
|
[[ -e $tmp ]] || mkdir -p $tmp:h && break
|
||||||
|
case $type {
|
||||||
|
(pipe) mkfifo $tmp;;
|
||||||
|
(file) touch $tmp;;
|
||||||
|
}
|
||||||
|
;;
|
||||||
}
|
}
|
||||||
|
|
||||||
(( $+owner )) && chown $owner "$tmp"
|
(( $? == 0 )) || { die "Failed to create ${tmp}"; return; }
|
||||||
(( $+group )) && chgrp $group "$tmp"
|
[[ -h $tmp ]] && return
|
||||||
|
[[ -n $owner ]] && chown $owner $tmp
|
||||||
|
[[ -n $group ]] && chgrp $group $tmp
|
||||||
|
[[ -n $mode ]] && chmod $mode $tmp
|
||||||
|
[[ -n $quiet ]] || print $tmp
|
||||||
|
}
|
||||||
|
|
||||||
print "$tmp"
|
if [[ ${(%):-%1x} == (checkpath|mktmp)(|.zsh) ]] {
|
||||||
|
checkpath "$@"
|
||||||
|
}
|
||||||
|
|
||||||
#
|
#
|
||||||
# vim:fenc=utf-8:ft=zsh:ci:pi:sts=2:sw=2:ts=2:
|
# vim:fenc=utf-8:ft=zsh:ci:pi:sts=0:sw=2:ts=2:
|
||||||
#
|
#
|
||||||
|
Loading…
Reference in new issue