*** ssh-method.c.orig	Wed Sep 17 17:16:54 2003
--- ssh-method.c	Sat Sep 20 14:38:11 2003
***************
*** 60,66 ****
  	GnomeVFSFileInfoOptions info_opts;
  } SshHandle;
  
! static GnomeVFSResult ssh_read_error	(int error_fd,
  					 gpointer buffer,
  					 GnomeVFSFileSize num_bytes,
  					 GnomeVFSFileSize *bytes_read);
--- 60,66 ----
  	GnomeVFSFileInfoOptions info_opts;
  } SshHandle;
  
! static GnomeVFSResult ssh_read		(gint fd,
  					 gpointer buffer,
  					 GnomeVFSFileSize num_bytes,
  					 GnomeVFSFileSize *bytes_read);
***************
*** 186,211 ****
  
  	/* We do support ssh:/// as ssh://localhost/ */
  	hostname = gnome_vfs_uri_get_host_name (uri);
- 	if (hostname == NULL) {
- 		hostname = "localhost";
- 	}
  
  	username = gnome_vfs_uri_get_user_name(uri);
- 	if (username == NULL) {
- 		username = g_get_user_name();
- 	}
  
! 	host_port = g_strdup_printf("%d", gnome_vfs_uri_get_host_port(uri) ?
! 			gnome_vfs_uri_get_host_port(uri) : 22);
  
! 	command_line  = g_strconcat ("ssh -oBatchMode=yes -x -l ", 
! 				     username,
! 				     " -p ", host_port,
  				     " ", hostname,
! 				     " ",
! 				     "\"LC_ALL=C; echo READY > /dev/stderr;",
! 				     command, "; echo DONE > /dev/stderr\"",
! 				     NULL);
  	g_free (host_port);
  
  	D(("ssh_connect () command: %s\n", command_line));
--- 186,202 ----
  
  	/* We do support ssh:/// as ssh://localhost/ */
  	hostname = gnome_vfs_uri_get_host_name (uri);
  
  	username = gnome_vfs_uri_get_user_name(uri);
  
! 	host_port = gnome_vfs_uri_get_host_port(uri) ?
! 		g_strdup_printf("%d",gnome_vfs_uri_get_host_port(uri)) : NULL;
  
! 	command_line  = g_strconcat ("ssh -oBatchMode=yes -x", 
! 				     (username ? " -l ", username : ""),
! 				     (host_port ? " -p ", host_port : ""),
  				     " ", hostname,
! 				     " ", command, NULL);
  	g_free (host_port);
  
  	D(("ssh_connect () command: %s\n", command_line));
***************
*** 224,231 ****
  	g_spawn_async_with_pipes (NULL, argv, NULL, 
  				  G_SPAWN_SEARCH_PATH,
  				  NULL, NULL,
! 				  (gint *)&handle->pid, &handle->write_fd, 
! 				  &handle->read_fd, &handle->error_fd, 
  				  &gerror);
  	g_strfreev (argv);
  
--- 215,224 ----
  	g_spawn_async_with_pipes (NULL, argv, NULL, 
  				  G_SPAWN_SEARCH_PATH,
  				  NULL, NULL,
! 				  (gint *)&handle->pid,
! 				  &handle->write_fd, 
! 				  &handle->read_fd,
! 				  &handle->error_fd, 
  				  &gerror);
  	g_strfreev (argv);
  
***************
*** 241,253 ****
  
  	/* You can add more error checking here */
  	memset (buffer, '\0', LINE_LENGTH);
! 	res = ssh_read_error (handle->error_fd, &buffer,
! 			LINE_LENGTH, &bytes_read);
  
  	if (bytes_read != 0 && res == GNOME_VFS_OK) {
! 		if (strncmp ("READY", buffer, strlen ("READY")) == 0) {
! 			res = GNOME_VFS_OK;
! 		} else if (strncmp ("Permission denied", buffer,
  					strlen("Permission denied")) == 0) {
  			res = GNOME_VFS_ERROR_LOGIN_FAILED;
  		} else if (strncmp ("Host key verification failed", buffer,
--- 234,243 ----
  
  	/* You can add more error checking here */
  	memset (buffer, '\0', LINE_LENGTH);
! 	res = ssh_read (handle->error_fd, &buffer, LINE_LENGTH, &bytes_read);
  
  	if (bytes_read != 0 && res == GNOME_VFS_OK) {
! 		if (strncmp ("Permission denied", buffer,
  					strlen("Permission denied")) == 0) {
  			res = GNOME_VFS_ERROR_LOGIN_FAILED;
  		} else if (strncmp ("Host key verification failed", buffer,
***************
*** 276,338 ****
  }
  
  static GnomeVFSResult
! ssh_read (SshHandle *handle,
! 	   gpointer buffer,
! 	   GnomeVFSFileSize num_bytes,
! 	   GnomeVFSFileSize *bytes_read)
! {
  	GnomeVFSFileSize my_read;
  
! 	my_read = (GnomeVFSFileSize) read (handle->read_fd, buffer, 
! 					   (size_t) num_bytes);
  
! 	if (my_read == -1) {
! 		return gnome_vfs_result_from_errno ();
! 	}
  
! 	*bytes_read = my_read;
! 
! 	return GNOME_VFS_OK;
! }
! 
! static GnomeVFSResult
! ssh_read_error (int error_fd,
! 		gpointer buffer,
! 		GnomeVFSFileSize num_bytes,
! 		GnomeVFSFileSize *bytes_read)
! {
! 	GnomeVFSFileSize my_read;
! 
! 	my_read = (GnomeVFSFileSize) read (error_fd, buffer,
! 			(size_t) num_bytes);
! 
! 	if (my_read == -1) {
  		return gnome_vfs_result_from_errno ();
- 	}
- 
- 	*bytes_read = my_read;
- 
- 	return GNOME_VFS_OK;
- }
- 
- static GnomeVFSResult
- ssh_check_for_done (SshHandle *handle)
- {
- 	char buffer[LINE_LENGTH];
- 	GnomeVFSResult res;
- 	GnomeVFSFileSize bytes_read;
  
! 	memset (buffer, '\0', LINE_LENGTH);
! 	res = ssh_read_error (handle->error_fd, &buffer,
! 			LINE_LENGTH, &bytes_read);
! 
! 	if (bytes_read != 0 && res == GNOME_VFS_OK) {
! 		if (strncmp ("DONE", buffer, strlen ("DONE")) == 0) {
! 			return res;
  		}
  	}
  
! 	return GNOME_VFS_ERROR_GENERIC;
  }
  
  static GnomeVFSResult
--- 266,300 ----
  }
  
  static GnomeVFSResult
! ssh_read (gint fd,
! 	      gpointer buffer,
! 	      GnomeVFSFileSize num_bytes,
! 	      GnomeVFSFileSize *bytes_read)
! {
! 	int retval;
! 	fd_set fds;
! 	struct timeval tv = {5, 0};
  	GnomeVFSFileSize my_read;
  
! 	FD_ZERO(&fds);
! 	FD_SET(fd, &fds);
! 	retval = select(fd + 1, &fds, NULL, NULL, &tv);
  
! 	*bytes_read = 0;
  
! 	if (retval < 0)
  		return gnome_vfs_result_from_errno ();
  
! 	if (retval) {
! 		my_read = (GnomeVFSFileSize) read (fd, buffer,
! 						   (size_t) num_bytes);
! 		if (my_read == -1) {
! 			return gnome_vfs_result_from_errno ();
  		}
+ 		*bytes_read = my_read;
  	}
  
! 	return GNOME_VFS_OK;
  }
  
  static GnomeVFSResult
***************
*** 407,413 ****
  	 GnomeVFSOpenMode mode,
  	 GnomeVFSContext *context)
  {
! 	GnomeVFSResult result = GNOME_VFS_OK;
  	char *cmd;
  	SshHandle *handle = NULL;
  
--- 369,375 ----
  	 GnomeVFSOpenMode mode,
  	 GnomeVFSContext *context)
  {
! 	GnomeVFSResult result;
  	char *cmd;
  	SshHandle *handle = NULL;
  
***************
*** 502,521 ****
  {
  	GnomeVFSResult result, res_secondary;
  
! 	result =  ssh_read ((SshHandle *)method_handle, buffer, num_bytes,
! 			bytes_read);
! 	if (result != GNOME_VFS_OK) {
! 		return result;
! 	}
! 
! 	if (*bytes_read == 0) {	
! 		res_secondary = ssh_check_for_done ((SshHandle *)method_handle);
! 		if (res_secondary != GNOME_VFS_OK) {
! 			result = res_secondary;
! 		} else {
! 			result = GNOME_VFS_ERROR_EOF;
! 		}
! 	}
  	return result;
  }
  
--- 464,471 ----
  {
  	GnomeVFSResult result, res_secondary;
  
! 	result =  ssh_read (((SshHandle *)method_handle)->read_fd, buffer,
! 			    num_bytes, bytes_read);
  	return result;
  }
  
***************
*** 626,632 ****
                       return;
               }               
               
!              result = ssh_read (handle, &c, 1, &bytes_read);
               if ((bytes_read > 0) && (c == '0')) {
                       file_info->permissions |= params[i].perm;
               } else {
--- 576,582 ----
                       return;
               }               
               
!              result = ssh_read (handle->read_fd, &c, 1, &bytes_read);
               if ((bytes_read > 0) && (c == '0')) {
                       file_info->permissions |= params[i].perm;
               } else {
***************
*** 669,689 ****
  		bytes_read = 0;
  
  		while (i<LINE_LENGTH) {
! 			result = ssh_read (handle, &c,
! 					   sizeof(char), &bytes_read);
  			if (bytes_read == 0 || c == '\r' || c == '\n') {
  				break;
  			}
  
- 			if (result != GNOME_VFS_OK) {
- 				res_secondary = ssh_check_for_done (handle);
- 				if (res_secondary != GNOME_VFS_OK) {
- 					result = res_secondary;
- 				} else {
- 					return result;
- 				}
- 			}
- 
  			line[i] = c;
  			i++;
  		}
--- 619,630 ----
  		bytes_read = 0;
  
  		while (i<LINE_LENGTH) {
! 			result = ssh_read (handle->read_fd, &c,
! 					   1, &bytes_read);
  			if (bytes_read == 0 || c == '\r' || c == '\n') {
  				break;
  			}
  
  			line[i] = c;
  			i++;
  		}
***************
*** 774,780 ****
  		*/
  		break;
  	}
- 
  	return result;
  }
  
--- 715,720 ----
***************
*** 799,807 ****
  	quoted_name = g_shell_quote (name);
  
  	if (*name != '\0') {
! 		cmd = g_strdup_printf ("ls -ld %s 2>&1", quoted_name);
  	} else {
! 		cmd = g_strdup_printf ("ls -ld '/' 2>&1");
  	}
  
  	result = ssh_connect (&handle, uri, cmd);
--- 739,747 ----
  	quoted_name = g_shell_quote (name);
  
  	if (*name != '\0') {
! 		cmd = g_strdup_printf ("ls -ld %s", quoted_name);
  	} else {
! 		cmd = g_strdup_printf ("ls -ld '/'");
  	}
  
  	result = ssh_connect (&handle, uri, cmd);
***************
*** 1065,1071 ****
  			char c;
  			GnomeVFSResult res;
  			GnomeVFSFileSize bytes_read;
! 			res = ssh_read (handle, &c, 1, &bytes_read);
  			if (bytes_read == 0 || res != GNOME_VFS_OK)
  				break;
  		}
--- 1005,1011 ----
  			char c;
  			GnomeVFSResult res;
  			GnomeVFSFileSize bytes_read;
! 			res = ssh_read (handle->read_fd, &c, 1, &bytes_read);
  			if (bytes_read == 0 || res != GNOME_VFS_OK)
  				break;
  		}
