coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [RFC PATCH] mv: add --swap option


From: Dominique Martinet
Subject: Re: [RFC PATCH] mv: add --swap option
Date: Fri, 28 May 2021 10:02:20 +0900

Hi,

Dominique Martinet wrote on Mon, Apr 12, 2021 at 02:23:41PM +0900:
> gnulib supports RENAME_EXCHANGE in renameatu, but there is currently
> no way of using it.
> Add a new switch to allow users to swap files atomically.

I'm not going to insist if the feature isn't wanted; but an answer would
be appreciated :)

I think it'd be very handy in shell scripts, if this isn't considered
for inclusion I'll try to package a tiny command (hardest part is always
figuring out a good name for it...), but I personally think it'd make
sense here.

Anyway, there is no strong feeling either way, but not knowing is
annoying so please tell.


Leaving the original comment if anyone skipped reading the first time:
> Note: I'm not suggesting this as a final version of the patch; it's just
> something I hacked in a few minutes just now and appears to work.
> A real patch would need at least adding to help string, proper
> documentation, possibly some test cases... The code probably could be
> made prettier too. Anyway, some rationale:
> 
> Linux and BSDs ave had ways of swapping two files atomically for a while
> (linux has renameat2 with RENAME_EXCHANGE flag since 3.15 (2014), with
> most filesystems implementing it since around 2015, some BSDs have had
> renamex_np and renameatx_np with RENAME_SWAP for at least 5 years as
> well (didn't check as much))
> 
> Yet I do not see any tool making use of it; if you try to search online
> the best hits seem to be people suggesting to use tcc in a shell
> function to JIT code that calls renameat2[1] or to use a gist on
> github[2]... Not exactly a good UX.
> 
> [1] https://unix.stackexchange.com/a/625900
> [2] https://gist.github.com/eatnumber1/f97ac7dad7b1f5a9721f
> 
> 
> 
> The gnulib's renameatu helper that mv uses supports the flag, so there
> is no technical reason not to expose it through a new e.g. --swap
> command line switch if we so wish to do -- it doesn't have fallback for
> the case the flag is not supported, but I personally think it's a good
> thing: this will warn user the swap is not atomic, so they need to
> handle some sort of recovery in case of hard crash between the renames.
> If mv would just emulate the operation there would be no way of
> detecting that.
> 
> 
> Looking at the list archives, one person offered to implement such a
> flag and never got any reply in 2018[3], but the subject never came up
> otherwise that I can see.
> 
> [3] https://lists.gnu.org/archive/html/coreutils/2018-12/msg00004.html
> 
> 
> Since there were no reply I took it a step further with a simple proof
> of concept, but my request is basically the same: if I were to polish
> this up a bit, clear up documentation etc would such a patch be accepted?
> 
> I'm really surprised the topic didn't come up at least once, so perhaps
> I have missed something; please just tell me if you're not interested or
> if there is a reason not to do this.

-- 
Dominique Martinet



reply via email to

[Prev in Thread] Current Thread [Next in Thread]