Skip to content

Commit

Permalink
Support "posix-rename@openssh.com" extension...
Browse files Browse the repository at this point in the history
  • Loading branch information
szmi committed Mar 31, 2008
1 parent 4802b14 commit 4d6e5a0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
8 changes: 8 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
2008-03-31 Miklos Szeredi <miklos@szeredi.hu>

* Support "posix-rename@openssh.com" extension available in
OpenSSH 4.9. This allows rename to be atomic even when target
file or directory exists. If available, the extension will be
used instead of the rename operation in the original protocol and
the "-oworkaround=rename" option will be ignored.

2008-03-28 Miklos Szeredi <miklos@szeredi.hu>

* Add support for password authentication with pam_mount.
Expand Down
44 changes: 41 additions & 3 deletions sshfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@
#define SSH_FXF_TRUNC 0x00000010
#define SSH_FXF_EXCL 0x00000020

#define SFTP_EXT_POSIX_RENAME "posix-rename@openssh.com"

#define PROTO_VERSION 3

#define MY_EOF 1
Expand Down Expand Up @@ -199,6 +201,7 @@ struct sshfs {
pthread_cond_t outstanding_cond;
int password_stdin;
char *password;
int ext_posix_rename;
};

static struct sshfs sshfs;
Expand Down Expand Up @@ -1213,10 +1216,30 @@ static int sftp_init_reply_ok(struct buffer *buf, uint32_t *version)
if (buf_get_uint32(buf, version) == -1)
return -1;

DEBUG("Server version: %u\n", *version);

if (len > 5) {
struct buffer buf2;

buf_init(&buf2, len - 5);
return do_read(&buf2);
if (do_read(&buf2) == -1)
return -1;

do {
char *ext;
char *extdata;

if (buf_get_string(&buf2, &ext) == -1 ||
buf_get_string(&buf2, &extdata) == -1)
return -1;

DEBUG("Extension: %s <%s>\n", ext, extdata);

if (strcmp(ext, SFTP_EXT_POSIX_RENAME) == 0) {
sshfs.ext_posix_rename = 1;
sshfs.rename_workaround = 0;
}
} while (buf2.len < buf2.size);
}
return 0;
}
Expand Down Expand Up @@ -1263,7 +1286,6 @@ static int sftp_init()
goto out;

sshfs.server_version = version;
DEBUG("Server version: %i\n", sshfs.server_version);
if (version > PROTO_VERSION) {
fprintf(stderr,
"Warning: server uses version: %i, we support: %i\n",
Expand Down Expand Up @@ -1856,6 +1878,19 @@ static int sshfs_do_rename(const char *from, const char *to)
return err;
}

static int sshfs_ext_posix_rename(const char *from, const char *to)
{
int err;
struct buffer buf;
buf_init(&buf, 0);
buf_add_string(&buf, SFTP_EXT_POSIX_RENAME);
buf_add_path(&buf, from);
buf_add_path(&buf, to);
err = sftp_request(SSH_FXP_EXTENDED, &buf, SSH_FXP_STATUS, NULL);
buf_free(&buf);
return err;
}

static void random_string(char *str, int length)
{
int i;
Expand All @@ -1867,7 +1902,10 @@ static void random_string(char *str, int length)
static int sshfs_rename(const char *from, const char *to)
{
int err;
err = sshfs_do_rename(from, to);
if (sshfs.ext_posix_rename)
err = sshfs_ext_posix_rename(from, to);
else
err = sshfs_do_rename(from, to);
if (err == -EPERM && sshfs.rename_workaround) {
size_t tolen = strlen(to);
if (tolen + RENAME_TEMP_CHARS < PATH_MAX) {
Expand Down

0 comments on commit 4d6e5a0

Please sign in to comment.