zsh-workers
 help / color / mirror / code / Atom feed
* no_clobber and error conditions
       [not found] <87r7324zyh.fsf@asfast.com>
@ 2006-05-10 15:59 ` Bart Schaefer
  2006-05-10 19:12   ` Peter Stephenson
  0 siblings, 1 reply; 2+ messages in thread
From: Bart Schaefer @ 2006-05-10 15:59 UTC (permalink / raw)
  To: zsh-workers

On May 9,  6:04pm, Lloyd Zusman wrote:
} Subject: File locking within zsh?
}
} Do any of you know of any functions, primitives, tricks, hacks, or even
} outright abominations which will allow me to do cooperative file locking
} from within zsh?

I was going to suggest this:

    function lock {
      setopt localoptions noclobber
      while true
      do
	{
	  : > $1
	  return 0
	} always {
	  (( TRY_BLOCK_ERROR=0 ))
	} 2>/dev/null
        sleep 1
      done
      return 1
    }

However, it appears that the error from noclobber does not "throw an
exception," it merely causes the command to have exit status 1.

That's not consistent with csh behavior (tcsh in this sample):

[schaefer@toltec /tmp]$ ( set noclobber; echo > lockfile; echo oops )
lockfile: File exists.
[schaefer@toltec /tmp]$ 
schaefer[537] ( setopt noclobber; echo > lockfile; echo oops )
zsh: file exists: lockfile
oops
schaefer[538]


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: no_clobber and error conditions
  2006-05-10 15:59 ` no_clobber and error conditions Bart Schaefer
@ 2006-05-10 19:12   ` Peter Stephenson
  0 siblings, 0 replies; 2+ messages in thread
From: Peter Stephenson @ 2006-05-10 19:12 UTC (permalink / raw)
  To: Zsh hackers list

Bart Schaefer wrote:
> However, it appears that the error from noclobber does not "throw an
> exception," it merely causes the command to have exit status 1.
> 
> That's not consistent with csh behavior (tcsh in this sample):
> 
> [schaefer@toltec /tmp]$ ( set noclobber; echo > lockfile; echo oops )
> lockfile: File exists.
> [schaefer@toltec /tmp]$ 
> schaefer[537] ( setopt noclobber; echo > lockfile; echo oops )
> zsh: file exists: lockfile
> oops
> schaefer[538]

It is consistent with bash.  The standard just says it "shall fail".  I
couldn't see a formal definition of "fail", but it seems just to
indicate returning a non-zero status, as in

  If a command fails during word expansion or redirection, its exit status
  shall be greater than zero.

But a failure doesn't need to be atomically handled, does it?  How about
revisiting:

  echo >foo || { readonly THROW; THROW= 2>/dev/null; };

(where "readonly THROW" should be at the top of the function).  Or, of
course, use throw and catch functions:

function lock() {
  emulate -L zsh
  setopt noclobber xtrace
  integer ret=-1

  autoload -U throw catch
  while true; do
    {
       : >$1  ||  throw Exists
    } always {
      if ! catch Exists; then
        if catch ''; then
          ret=1
        else
          ret=0
        fi
      fi
    }
    (( ret >= 0 )) && return $ret
    sleep 1
  done
}

("return" in the middle of the always block doesn't work.  I can't quite
make up my mind whether to be embarrassed or not.  It depends how
seriously you take "Execution [after the always block] continues from
the result of the execution of try-list".)

-- 
Peter Stephenson <p.w.stephenson@ntlworld.com>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2006-05-10 19:12 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <87r7324zyh.fsf@asfast.com>
2006-05-10 15:59 ` no_clobber and error conditions Bart Schaefer
2006-05-10 19:12   ` Peter Stephenson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).