[PATCH] add statfs extension to sftp-server

Miklos Szeredi miklos at szeredi.hu
Fri Dec 7 22:50:30 EST 2007


And while we are at it, can you please comment on these patches as
well, originally submitted around one year ago.

Oh, and I think we can agree, that the secsh-filexfer standardization
is dead, so there's not much point in trying to support newer protocol
versions, which don't have statfs anyway.

Thanks,
Miklos
-----

This is needed to be able to support statfs operation on an SSH
filesystem (http://fuse.sourceforge.net/sshfs.html).

It uses and sends only those fields of struct statfs which are common
with struct statvfs as defined by POSIX, and which are shared among
most UNIX-like systems.

The SSH_FXP_EXTENDED message type is used, so this change is backward
compatible and conforms to the protocol specification.

Index: ssh/sftp-server.c
===================================================================
--- ssh.orig/sftp-server.c	2006-08-19 16:24:17.000000000 +0200
+++ ssh/sftp-server.c	2006-08-19 16:49:03.000000000 +0200
@@ -19,6 +19,7 @@
 #include <sys/stat.h>
 #include <sys/time.h>
 #include <sys/param.h>
+#include <sys/mount.h>
 
 #include <dirent.h>
 #include <errno.h>
@@ -462,6 +463,24 @@ send_attrib(u_int32_t id, const Attrib *
 	buffer_free(&msg);
 }
 
+static void
+send_statfs(u_int32_t id, struct statfs *st)
+{
+	Buffer msg;
+
+	buffer_init(&msg);
+	buffer_put_char(&msg, SSH2_FXP_EXTENDED_REPLY);
+	buffer_put_int(&msg, id);
+	buffer_put_int(&msg, st->f_bsize);
+	buffer_put_int64(&msg, st->f_blocks);
+	buffer_put_int64(&msg, st->f_bfree);
+	buffer_put_int64(&msg, st->f_bavail);
+	buffer_put_int64(&msg, st->f_files);
+	buffer_put_int64(&msg, st->f_ffree);
+	send_msg(&msg);
+	buffer_free(&msg);
+}
+
 /* parse incoming */
 
 static void
@@ -1049,6 +1068,25 @@ process_symlink(void)
 }
 
 static void
+process_extended_statfs(u_int32_t id)
+{
+	char *path;
+	struct statfs st;
+	int ret;
+
+	path = get_string(NULL);
+	debug3("request %u: statfs", id);
+	logit("statfs \"%s\"", path);
+
+	ret = statfs(path, &st);
+	if (ret == -1)
+		send_status(id, errno_to_portable(errno));
+	else
+		send_statfs(id, &st);
+        xfree(path);
+}
+
+static void
 process_extended(void)
 {
 	u_int32_t id;
@@ -1056,7 +1094,10 @@ process_extended(void)
 
 	id = get_int();
 	request = get_string(NULL);
-	send_status(id, SSH2_FX_OP_UNSUPPORTED);		/* MUST */
+	if (strcmp(request, "statfs at openssh.org") == 0)
+		process_extended_statfs(id);
+	else
+		send_status(id, SSH2_FX_OP_UNSUPPORTED);	/* MUST */
 	xfree(request);
 }
 



More information about the openssh-unix-dev mailing list