help-bash
[Top][All Lists]
Advanced

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

Re: command for swapping of two files or two dirs?


From: Peng Yu
Subject: Re: command for swapping of two files or two dirs?
Date: Fri, 26 Mar 2021 16:21:29 -0500

The problem is that the swap based on multiple commands is not atomic.

With multiple commands, wouldn't it cause complications for error
recovery? Also, is it safe? If there is any malicious program delete
some files during the process?

On 3/26/21, Alex fxmbsw7 Ratchev <fxmbsw7@gmail.com> wrote:
> i agree to all excepts the srandom thingy
> a [[ -e loop passthru maybe.. like always
>
> On Fri, Mar 26, 2021 at 9:58 PM Greg Wooledge <greg@wooledge.org> wrote:
>>
>> On Fri, Mar 26, 2021 at 09:18:10PM +0100, Alex fxmbsw7 Ratchev wrote:
>> > swap() { declare t= tt= ; [[ $# != 2 ]] && return 1
>> >  mv "$1" "${t:=/tmp/$SRANDOM}"
>> >  mv "$2" "${tt:=/tmp/$SRANDOM}"
>> >  mv "$t" "$2"
>> >  mv "$tt" "$1"
>> > }
>>
>> 1) Use mktemp(1) or tempfile(1) or whatever.  Your $SRANDOM hack has
>>    security problems.
>>
>> 2) Don't move the file to /tmp.  That might be on a different file
>> system.
>>    You want to keep the file within its current directory, as that's the
>>    only place you can be sure isn't on another file system.
>>
>> 3) There should only need to be three moves.
>>
>> 4) You probably want && between the moves, to abort if something goes
>>    wrong.
>>
>
>


-- 
Regards,
Peng



reply via email to

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