Date: Wed, 29 Aug 2012 09:41:15 +0200 From: Gary Jennejohn <gljennjohn@googlemail.com> To: John Baldwin <jhb@freebsd.org> Cc: current@freebsd.org Subject: Re: [PATCH] Add a "-h" flag to mv Message-ID: <20120829094115.3a40d35f@ernst.jennejohn.org> In-Reply-To: <201208281058.10029.jhb@freebsd.org> References: <201208281058.10029.jhb@freebsd.org>
next in thread | previous in thread | raw e-mail | index | archive | help
On Tue, 28 Aug 2012 10:58:09 -0400 John Baldwin <jhb@freebsd.org> 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? > [snip patch] Nope, seems like a reasonable extension to me (gj@). -- Gary Jennejohn
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20120829094115.3a40d35f>