=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/ssh/sftp-server.c,v retrieving revision 1.38.2.1 retrieving revision 1.38.2.2 diff -u -r1.38.2.1 -r1.38.2.2 --- src/usr.bin/ssh/sftp-server.c 2003/04/01 00:12:14 1.38.2.1 +++ src/usr.bin/ssh/sftp-server.c 2003/04/03 23:27:12 1.38.2.2 @@ -22,7 +22,7 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "includes.h" -RCSID("$OpenBSD: sftp-server.c,v 1.38.2.1 2003/04/01 00:12:14 margarida Exp $"); +RCSID("$OpenBSD: sftp-server.c,v 1.38.2.2 2003/04/03 23:27:12 margarida Exp $"); #include "buffer.h" #include "bufaux.h" @@ -816,20 +816,31 @@ u_int32_t id; char *oldpath, *newpath; int status; + struct stat sb; id = get_int(); oldpath = get_string(NULL); newpath = get_string(NULL); TRACE("rename id %u old %s new %s", id, oldpath, newpath); - /* fail if 'newpath' exists */ - if (link(oldpath, newpath) == -1) + status = SSH2_FX_FAILURE; + if (lstat(oldpath, &sb) == -1) status = errno_to_portable(errno); - else if (unlink(oldpath) == -1) { - status = errno_to_portable(errno); - /* clean spare link */ - unlink(newpath); - } else - status = SSH2_FX_OK; + else if (S_ISREG(sb.st_mode)) { + /* Race-free rename of regular files */ + if (link(oldpath, newpath) == -1) + status = errno_to_portable(errno); + else if (unlink(oldpath) == -1) { + status = errno_to_portable(errno); + /* clean spare link */ + unlink(newpath); + } else + status = SSH2_FX_OK; + } else if (stat(newpath, &sb) == -1) { + if (rename(oldpath, newpath) == -1) + status = errno_to_portable(errno); + else + status = SSH2_FX_OK; + } send_status(id, status); xfree(oldpath); xfree(newpath);