'cpmv' instead of 'cp' Re: [asterisk-users] [tip]semicolon trouble: System($(sleep 4; cp 1.call out)&) not working, but System($( sleep 4 && cp 1.call out)&) ; )

Robert Michel news at robertmichel.de
Sat Jul 8 12:39:36 MST 2006


Salve Tzafrir!

On Sat, 08 Jul 2006, Tzafrir Cohen wrote:
> To simplify error handling:
> set -e

> tmpfile=`mktemp`
> Note that the temp file has to be in the sme filesystem as the asterisk
> spool
It would be more performant when it is the same filesystem,
but it should also work when not. AFAIK will mv internaly
copy it first to the target filesytem and change then the
file node.

Thanks again,
rob


The version with Tzafrir's tips:

#!/bin/bash 
set -e
# cpmv by news at robertmichel.de 2006-08-07
# Thanks to Tzafrir Cohen for tips ;)
# Like with BSD-licence, do what you like.
#
# cp is not atomic and not reliable
# for fast reading process e.g. inside
# asterisk to store a copy to 
# /var/spool/asterisk/outgoing
# 
# cpmv will copy a file to /tmp first 
# and move it then to your target.
#
# Store it e.g. as /usr/bin/cpmv
# use it "mv $1 $2"
# $1 : source path/file
# $2 : target path/file
# This solution does not work with
# wildcards like '?', '*'.
# "." and  ../name is also not working
######################################

# s : source  f : file
# t : target  p : path
# test if a path is given 
sslash=$(echo $1 | sed 's/[^/]//g')
tslash=$(echo $2 | sed 's/[^/]//g')

# extract file/path name from $1 $2
sf=${1##*/}
tf=${2##*/}
sp=${1%/*}
tp=${2%/*}

if [[ $sf = "" ]]; then
  exit
fi
if [[ $sp$sf = $tp$tf ]]; then
  exit
fi

if [[ $sslash != "" ]]; then
   sp=$(echo $sp/)
else
   sp=$(echo $(pwd)/)
fi
if [[ $tslash != "" ]]; then
   tp=$(echo $tp/)
else
   tp=$sp
fi

tmp=$(mktemp /tmp/$sf.cpmv.XXXXXXXXXX)
if [[ $tf != "" ]]; then
   cp -p $sp$sf $tmp; mv $tmp $tp$tf
      else
   cp -p $sp$sf $tmp; mv $tmp $tp$sf
fi
# EOF


 



More information about the asterisk-users mailing list