From owner-freebsd-current@FreeBSD.ORG Wed Aug 29 10:02:48 2012 Return-Path: Delivered-To: current@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id E421F1065674; Wed, 29 Aug 2012 10:02:48 +0000 (UTC) (envelope-from jilles@stack.nl) Received: from mx1.stack.nl (relay04.stack.nl [IPv6:2001:610:1108:5010::107]) by mx1.freebsd.org (Postfix) with ESMTP id 7DC528FC1B; Wed, 29 Aug 2012 10:02:48 +0000 (UTC) Received: from snail.stack.nl (snail.stack.nl [IPv6:2001:610:1108:5010::131]) by mx1.stack.nl (Postfix) with ESMTP id D2B611DD413; Wed, 29 Aug 2012 12:02:47 +0200 (CEST) Received: by snail.stack.nl (Postfix, from userid 1677) id AC4ED2847B; Wed, 29 Aug 2012 12:02:47 +0200 (CEST) Date: Wed, 29 Aug 2012 12:02:47 +0200 From: Jilles Tjoelker To: John Baldwin Message-ID: <20120829100247.GA66677@stack.nl> References: <201208281058.10029.jhb@freebsd.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <201208281058.10029.jhb@freebsd.org> User-Agent: Mutt/1.5.21 (2010-09-15) Cc: current@freebsd.org Subject: Re: [PATCH] Add a "-h" flag to mv X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 29 Aug 2012 10:02:49 -0000 On Tue, Aug 28, 2012 at 10:58:09AM -0400, John Baldwin wrote: > I have a use case at work where I need to be able to update a symlink > that points to a directory atomically (so that it points to a new > directory). To give a conrete example, suppose I have two directories > 'foo' and 'bar', and a symlink 'a' that I wish to atomically flip from > 'foo' to 'bar'. > Using 'ln -shf bar a' is not atomic as it uses separate unlink() and > symlink() system calls, so there is a race where another thread may > encounter ENOENT while traversing 'a'. > The approach we used was to create a new symbolic link 'a.new' (e.g. > via 'ln -s bar a.new') and then use rename() to rename 'a.new' on top > of 'a'. Normally to do an atomic rename from userland one would use > 'mv', but 'mv a.new a' doesn't do that. Instead, it moves 'a.new' > into the directory referenced by the 'a' symlink. At work we have > resorted to invoking python's os.rename() in a one-liner to handle > this. > While rehashing this discussion today it occurred to me that a -h flag to > mv would allow it to work in this case (and is very similar to how ln treats > its -h flag). To that end, I have a patch to add a new -h flag to mv that > allows one to atomically update a symlink that points to a directory. I > could not find any other mv commands that have adopted a -h (or a different > flag that accomplishes the same task). Given that it functions identically > to the -h flag for ln, -h seemed the "logical" choice. Any objections? GNU coreutils mv (and also cp/install/ln) appears to use -T/--no-target-directory for a similar purpose: -T prevents the target being treated as a directory (whether it is a symlink or not). -- Jilles Tjoelker