link(2) to rename files in sftp

Carson Gaspar carson at taltos.org
Wed Apr 7 09:49:18 EST 2004



--On Tuesday, April 06, 2004 12:00:57 +1000 Darren Tucker 
<dtucker at zip.com.au> wrote:

> The original code for rename looked like the following:
>      if (stat(newpath, &st) == -1) {
>          ret = rename(oldpath, newpath);
>          status = (ret == -1) ? errno_to_portable(errno) : SSH2_FX_OK;
>      }
>
> The idea is obviously to not clobber existing files, but the
> implementation is racy (hence the change, I guess).
>
> I have no idea how to implement that portably for filesystems without
> Unix semantics without the race.

Well, if they are reasonably sane, you can do:

if (fd = open(newpath, O_CREAT|O_EXCL, 0)) {
	ret = rename(oldpath, newpath);
	close(fd);
	...
}

Unless I'm missing something...

-- 
Carson




More information about the openssh-unix-dev mailing list