From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 18567 invoked from network); 10 May 2006 21:47:35 -0000 X-Spam-Checker-Version: SpamAssassin 3.1.1 (2006-03-10) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.5 required=5.0 tests=BAYES_00,FORGED_RCVD_HELO autolearn=ham version=3.1.1 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by ns1.primenet.com.au with SMTP; 10 May 2006 21:47:35 -0000 Received: (qmail 57201 invoked from network); 10 May 2006 21:47:28 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 10 May 2006 21:47:28 -0000 Received: (qmail 742 invoked by alias); 10 May 2006 21:47:20 -0000 Mailing-List: contact zsh-users-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 10241 Received: (qmail 733 invoked from network); 10 May 2006 21:47:19 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by sunsite.dk with SMTP; 10 May 2006 21:47:19 -0000 Received: (qmail 56220 invoked from network); 10 May 2006 21:47:19 -0000 Received: from mail.starnix.com (24.215.7.100) by a.mx.sunsite.dk with SMTP; 10 May 2006 21:47:17 -0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by mail.starnix.com (Postfix) with ESMTP id 4D4CD4C2D3; Wed, 10 May 2006 17:47:15 -0400 (EDT) Received: from newsol.starnix.com (unknown [209.250.156.214]) by mail.starnix.com (Postfix) with ESMTP id 0A33F4C2CE; Wed, 10 May 2006 17:47:11 -0400 (EDT) Received: by newsol.starnix.com (Postfix, from userid 510) id 18D6848DF4; Wed, 10 May 2006 17:53:34 -0400 (EDT) Sender: tim@starnix.com From: Tim Writer To: "Brian K. White" Cc: Subject: Re: File locking within zsh? References: <87r7324zyh.fsf@asfast.com> <009101c673f7$06b3f090$6500000a@venti> Date: 10 May 2006 17:53:33 -0400 In-Reply-To: <009101c673f7$06b3f090$6500000a@venti> Message-ID: User-Agent: Gnus/5.0808 (Gnus v5.8.8) XEmacs/21.4 (Common Lisp) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: by starnix.com "Brian K. White" writes: > ----- Original Message ----- > From: "Tim Writer" > To: "Lloyd Zusman" > Cc: > Sent: Tuesday, May 09, 2006 11:31 PM > Subject: Re: File locking within zsh? > > > > Lloyd Zusman writes: > > > >> 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 know that I can do this with a number of compiled executables, but I'm > >> looking for a zsh-only solution. > >> > >> Assuming some sort of zsh locking operator called "lock", consider > >> this example (within a zsh script): > >> > >> lock -x -t 0 file # for this example of a hypothetical operator, '-x' > >> # means to wait until I get an exclusive lock, and > >> # '-t 0' means no time out > >> print foo bar baz >>file > >> # do a whole lot of other stuff to "file" > >> unlock file # release the lock > >> > >> In this example, any other zsh script which asks for an exclusive lock > >> on "file" using this hypothetical "lock" operator will block until the > >> "unlock" operator has been invoked. > >> > >> Can this be done somehow in zsh, or do I have to rely on a compiled > >> executable to accomplish this? > > > > The usual way to lock within shell scripts is to use ln. This works on all > > UNIX like systems because creating a hard link (with ln) is an atomic > > operation which fails if the target already exists. Your example above canbe > > > written like this: > > > > while ! ln file file.lock 2>/dev/null > > do > > sleep 1 > > done > > # Lock obtained > > > > print foo bar baz >>file > > # do a whole lot of other stuff to "file" > > > > rm -f file.lock > > # Lock released > > > > Wrapping this idiom into lock/unlock functions is left as an exercise for the > > > reader. :-) > > > Is that better or worse, and why, than umask 222 ; >file ? > > http://www.unix.org.ua/orelly/unix/upt/ch45_36.htm I think the ln solution is better because it's guaranteed to be atomic. Granted, a sane implementation of ">file" will use open(2) with O_CREAT what guarantees do you have that your shell (and I'm not talking about zsh here) is sane? I have often seen code like this pseudocode: if (file exists) open file else { creat file open file } I don't know that any shells contain crap like this but I don't know they don't either. -- tim writer starnix inc. 647.722.5301 toronto, ontario, canada http://www.starnix.com professional linux services & products