1
0
Fork 0
mirror of git://git.code.sf.net/p/zsh/code synced 2025-01-01 05:16:05 +01:00

34067: safe tempfile creation, part 1

This commit is contained in:
Barton E. Schaefer 2014-12-27 23:59:29 -08:00
parent 1cd8023570
commit 200accac63
15 changed files with 75 additions and 65 deletions

View file

@ -1,5 +1,14 @@
2014-12-27 Barton E. Schaefer <schaefer@zsh.org>
* 34067: Completion/Base/Widget/_complete_debug,
Completion/compinstall, Functions/Calendar/calendar,
Functions/Zftp/zfcd_match, Functions/Zftp/zfcget,
Functions/Zftp/zfcput, Functions/Zftp/zffcache,
Functions/Zftp/zfget_match, Functions/Zftp/zfrglob,
Functions/Zftp/zftransfer, Functions/Zftp/zftype,
Functions/Zftp/zfuget, Functions/Zftp/zfuput,
Functions/Zle/edit-command-line: safe tempfile creation
* unposted: Test/A05execution.ztst: regressions for 34065
* 34065: Src/exec.c, Src/loop.c: following an "if" condition, do

View file

@ -9,6 +9,7 @@ local pager w="${(qq)words}"
integer debug_fd=-1
{
if [[ -t 2 ]]; then
mv -f =(:) $tmp &&
exec {debug_fd}>&2 2>| $tmp
fi

View file

@ -1958,11 +1958,14 @@ if [[ -z $ifile || -d $ifile ]] ||
fi
local tmpout=${TMPPREFIX:-/tmp/zsh}compinstall$$
mv -f =(:) $tmpout && # safe tempfile creation
mv -f =(:) ${tmpout}x || return 1
#
# Assemble the complete set of lines to
# insert.
#
__ci_output >$tmpout
__ci_output >>$tmpout
if [[ -n $ifile ]]; then
if [[ $ifile != *(zshrc|zlogin|zshenv) ]]; then
@ -1984,15 +1987,15 @@ future use."
fi
if { { grep "$endline" $ifile >/dev/null 2>&1 &&
sed -e "/^[ ]*$endline/r $tmpout
/^[ ]*$startline/,/^[ ]*$endline/d" $ifile >${tmpout}2 } ||
{ cp $ifile ${tmpout}2 && cat $tmpout >>${tmpout}2 } } &&
cp ${tmpout}2 $ifile && rm -f ${tmpout}2; then
/^[ ]*$startline/,/^[ ]*$endline/d" $ifile >>${tmpout}x } ||
{ cp $ifile ${tmpout}x && cat $tmpout >>${tmpout}x } } &&
cp ${tmpout}x $ifile && rm -f ${tmpout}x; then
print "\nSuccessfully added compinstall lines to $ifile."
rm -f $tmpout
else
print "\nFailure adding lines to $ifile. Lines left in \`$tmpout'"
fi
rm -f ${tmpout}2
rm -f ${tmpout}x
elif read -q key'?Print them to stdout instead ([y]es, [n]o)? '; then
cat $tmpout
rm -f $tmpout

View file

@ -254,8 +254,7 @@ if (( verbose )); then
fi
local mycmds="${TMPPREFIX:-/tmp/zsh}.calendar_cmds.$$"
touch $mycmds
chmod 600 $mycmds
mv -f =(:) $mycmds
# start of subshell for OS file locking
(

View file

@ -12,7 +12,6 @@ fi
local ZFTP_VERBOSE=45
# should we redirect 2>/dev/null or let the user see it?
local tmpf=${TMPPREFIX}zfcm$$
local -a match mbegin mend
if [[ $ZFTP_SYSTEM = UNIX* ]]; then
@ -27,9 +26,10 @@ if [[ $ZFTP_SYSTEM = UNIX* ]]; then
# If we're using -F, we get away with using a directory
# to list, but not a glob. Don't ask me why.
reply=(${${(M)${(f)"$(zftp ls -lF $dir)"}:#d*}/(#b)*[[:space:]](*)\//$match[1]})
# zftp ls -LF $dir >$tmpf
# reply=($(awk '/\/$/ { print substr($1, 1, length($1)-1) }' $tmpf))
# rm -f $tmpf
# () {
# zftp ls -LF $dir >|$1
# reply=($(awk '/\/$/ { print substr($1, 1, length($1)-1) }' $1))
# } =(:)
[[ -n $dir && $dir != */ ]] && dir="$dir/"
if [[ -n $WIDGET ]]; then
_wanted directories expl 'remote directory' \

View file

@ -14,7 +14,7 @@ emulate -L zsh
[[ $curcontext = :zf* ]] || local curcontext=:zfcget
local loc rem stat=0 opt opt_G opt_t remlist locst remst
local tmpfile=${TMPPREFIX}zfcget$$ rstat tsize
local rstat tsize
while getopts :Gt opt; do
[[ $opt = '?' ]] && print "zfcget: bad option: -$OPTARG" && return 1
@ -39,10 +39,11 @@ for remlist in $*; do
else
# Compare the sizes.
locst=($(zftp local $loc))
zftp remote $rem >$tmpfile
rstat=$?
remst=($(<$tmpfile))
rm -f $tmpfile
() {
zftp remote $rem >|$1
rstat=$?
remst=($(<$1))
} =(: temporary file)
if [[ $rstat = 2 ]]; then
print "Server does not support SIZE command.\n" \
"Assuming you know what you're doing..." 2>&1

View file

@ -14,7 +14,6 @@ emulate -L zsh
[[ $curcontext = :zf* ]] || local curcontext=:zfcput
local loc rem stat=0 locst remst offs tailtype
local tmpfile=${TMPPREFIX}zfcget$$ rstat
# find how tail works. this is intensely annoying, since it's completely
# standard in C. od's no use, since we can only skip whole blocks.
@ -40,10 +39,11 @@ for loc in $*; do
else
# Compare the sizes.
locst=($(zftp local $loc))
zftp remote $rem >$tmpfile
rstat=$?
remst=($(<$tmpfile))
rm -f $tmpfile
() {
zftp remote $rem >|$1
rstat=$?
remst=($(<$1))
} =(: temporary file)
if [[ $rstat = 2 ]]; then
print "Server does not support remote status commands.\n" \
"You will have to find out the size by hand and use zftp append." 2>&1

View file

@ -19,8 +19,5 @@ fi
if [[ $1 = -d ]]; then
unset $fcache_name
elif (( ${(P)#fcache_name} == 0 )); then
local tmpf=${TMPPREFIX}zffcache$$
zftp ls >$tmpf
eval "$fcache_name=(\${(f)\"\$(<\$tmpf)\"})"
rm -f $tmpf
eval "$fcache_name=(\${(f)\"\$(zftp ls)\"})"
fi

View file

@ -7,22 +7,22 @@ if [[ $1 == $HOME || $1 == $HOME/* ]]; then
1="~${1#$HOME}"
fi
local tmpf=${TMPPREFIX}zfgm$$
if [[ $ZFTP_SYSTEM == UNIX* && $1 == */* ]]; then
setopt localoptions clobber
local tmpf=${TMPPREFIX}zfgm$$
mv -f =(:) $tmpf
if [[ -n $WIDGET ]]; then
local dir=${1:h}
[[ $dir = */ ]] || dir="$dir/"
zftp ls -LF $dir >$tmpf
local reply
reply=(${${${(f)"$(<$tmpf)"}##$dir}%\*})
rm -f $tmpf
_wanted files expl 'remote file' compadd -P $dir - $reply
else
# On the first argument to ls, we usually get away with a glob.
zftp ls "$1*$2" >$tmpf
reply=($(<$tmpf))
rm -f $tmpf
fi
else
local fcache_name

View file

@ -33,12 +33,12 @@ if [[ $pat != *[][*?]* &&
( -n $zfrglob || $pat != *[(|)#^]* ) ]]; then
return 0
fi
local tmpf=${TMPPREFIX}zfrglob$$
if [[ $zfrglob != '' ]]; then
zftp ls "$pat" >$tmpf 2>/dev/null
eval "$1=(\$(<\$tmpf))"
rm -f $tmpf
() {
zftp ls "$pat" >|$1 2>/dev/null
eval "$1=(\$(<\$1))"
} =(: temporary file)
else
if [[ $ZFTP_SYSTEM = UNIX* && $pat = */* ]]; then
# not the current directory and we know how to handle paths
@ -49,10 +49,11 @@ else
dir=/
fi
nondir=${pat##*/}
zftp ls "$dir" 2>/dev/null >$tmpf
files=($(<$tmpf))
() {
zftp ls "$dir" 2>/dev/null >|$1
files=($(<$1))
} =(: temporary file)
files=(${files:t})
rm -f $tmpf
else
# we just have to do an ls and hope that's right
local fcache_name

View file

@ -43,10 +43,11 @@ zfautocheck || return 1
local style
zstyle -s ':zftp:zftransfer' progress style
if [[ -n $style && $style != none ]]; then
local ZFTP_TSIZE array tmpfile=${TMPPREFIX}zft$$
zftp remote $file1 >$tmpfile 2>/dev/null
array=($(<$tmpfile))
rm -f $tmpfile
local ZFTP_TSIZE array
() {
zftp remote $file1 >|$1 2>/dev/null
array=($(<$1))
} =(: temporary file)
[[ $#array -eq 2 ]] && ZFTP_TSIZE=$array[1]
fi

View file

@ -1,13 +1,11 @@
# function zftype {
local type zftmp=${TMPPREFIX}zftype$$
local type
[[ $curcontext = :zf* ]] || local curcontext=:zftype
zfautocheck -d
if (( $# == 0 )); then
zftp type >$zftmp
type=$(<$zftmp)
rm -f $zftmp
type=$(zftp type)
if [[ $type = I ]]; then
print "Current type is image (binary)"
return 0

View file

@ -26,7 +26,7 @@
emulate -L zsh
[[ $curcontext = :zf* ]] || local curcontext=:zfuget
local loc rem locstats remstats doit tmpfile=${TMPPREFIX}zfuget$$
local loc rem locstats remstats doit
local rstat remlist opt opt_v opt_s opt_G opt_t
integer stat do_close
@ -66,12 +66,13 @@ for remlist in $*; do
doit=y
remstats=()
if [[ -f $loc ]]; then
zftp local $loc >$tmpfile
locstats=($(<$tmpfile))
zftp remote $rem >$tmpfile
rstat=$?
remstats=($(<$tmpfile))
rm -f $tmpfile
() {
zftp local $loc >|$1
locstats=($(<$1))
zftp remote $rem >|$1
rstat=$?
remstats=($(<$1))
} =(: temporary file)
if [[ $rstat = 2 ]]; then
print "Server does not implement full command set required." 1>&2
return 1

View file

@ -12,7 +12,7 @@
emulate -L zsh
[[ $curcontext = :zf* ]] || local curcontext=:zfuput
local loc rem locstats remstats doit tmpfile=${TMPPREFIX}zfuput$$
local loc rem locstats remstats doit
local rstat opt opt_v opt_s
integer stat do_close
@ -52,12 +52,13 @@ for rem in $*; do
stat=1
continue
fi
zftp local $loc >$tmpfile
locstats=($(<$tmpfile))
zftp remote $rem >$tmpfile
rstat=$?
remstats=($(<$tmpfile))
rm -f $tmpfile
() {
zftp local $loc >|$1
locstats=($(<$1))
zftp remote $rem >|$1
rstat=$?
remstats=($(<$1))
} =(: temporary file)
if [[ $rstat = 2 ]]; then
print "Server does not implement full command set required." 1>&2
return 1

View file

@ -6,12 +6,10 @@
# will give ksh-like behaviour for that key,
# except that it will handle multi-line buffers properly.
local tmpfile=${TMPPREFIX:-/tmp/zsh}ecl$$
() {
exec </dev/tty
${=${VISUAL:-${EDITOR:-vi}}} $1
print -Rz - "$(<$1)"
} =(<<<"$PREBUFFER$BUFFER")
print -R - "$PREBUFFER$BUFFER" >$tmpfile
exec </dev/tty
${=${VISUAL:-${EDITOR:-vi}}} $tmpfile
print -Rz - "$(<$tmpfile)"
command rm -f $tmpfile
zle send-break # Force reload from the buffer stack