'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)&) ; )

Tzafrir Cohen tzafrir.cohen at xorcom.com
Sat Jul 8 13:47:54 MST 2006


On Sat, Jul 08, 2006 at 09:39:36PM +0200, Robert Michel wrote:
> 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.

When not on the same filesystem, mv is essencially 'cp; rm'
rename(2) only works in the same filesystem.

> 
> 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')

use 'basename' instead?

> 
> # 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

You should exit with an error if you did not deliver a call file.

exit 1

Also, to make this a little less bashist:

if [ "$sf" = '' ]; then

> 
> 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

-- 
Tzafrir Cohen      sip:tzafrir at local.xorcom.com
icq#16849755       iax:tzafrir at local.xorcom.com
+972-50-7952406           
tzafrir.cohen at xorcom.com  http://www.xorcom.com



More information about the asterisk-users mailing list