Re: adding new info to GnomeVFSFileInfo



> So yes, the right patch will be accepted :) It seems like you've been
> thinking about the problem the same way I have.
> 

So the patch in this email makes the changes to GnomeVFSFileInfo that I
described in a previous mail. It also modifies the file: and ssh:
methods so that they correctly fill the new fields. (it may not apply
correctly with the other patches I posted on this list)

Comments are welcome, in particular comments about whether it is the
right approach or not.

Christophe
diff -ur /usr/src/gnome-vfs/libgnomevfs/gnome-vfs-file-info.h ./libgnomevfs/gnome-vfs-file-info.h
--- /usr/src/gnome-vfs/libgnomevfs/gnome-vfs-file-info.h	2002-06-21 19:39:25.000000000 +0200
+++ ./libgnomevfs/gnome-vfs-file-info.h	2002-09-13 23:12:59.000000000 +0200
@@ -77,7 +77,8 @@
 	GNOME_VFS_FILE_INFO_FIELDS_MTIME = 1 << 10,
 	GNOME_VFS_FILE_INFO_FIELDS_CTIME = 1 << 11,
 	GNOME_VFS_FILE_INFO_FIELDS_SYMLINK_NAME = 1 << 12,
-	GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE = 1 << 13
+	GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE = 1 << 13,
+	GNOME_VFS_FILE_INFO_FIELDS_ACCESS = 1 << 14
 } GnomeVFSFileInfoFields;
 
 /* File permissions.  These are the same as the Unix ones, but we wrap them
@@ -105,15 +106,22 @@
 	GNOME_VFS_PERM_OTHER_READ = S_IROTH,
 	GNOME_VFS_PERM_OTHER_WRITE = S_IWOTH,
 	GNOME_VFS_PERM_OTHER_EXEC = S_IXOTH,
-	GNOME_VFS_PERM_OTHER_ALL = S_IROTH | S_IWOTH | S_IXOTH
+	GNOME_VFS_PERM_OTHER_ALL = S_IROTH | S_IWOTH | S_IXOTH,
+	/* FIXME: I put this stuff there to avoid using one of the 
+	 * padding pointers, but nothing proves that any of this value is
+	 * not already used by one of the S_ constants.
+	 * Too bad these constants weren't modified before GNOME2 :(
+	 */
+	GNOME_VFS_PERM_ACCESS_READABLE   = 1 << 16,
+	GNOME_VFS_PERM_ACCESS_WRITEABLE  = 1 << 17,
+	GNOME_VFS_PERM_ACCESS_EXECUTABLE = 1 << 18
 } GnomeVFSFilePermissions;
 
-
 typedef struct {
 	/* Base name of the file (no path).  */
 	char *name;
 
-	/* Fields which are actually valid in this strcture. */
+	/* Fields which are actually valid in this structure. */
 	GnomeVFSFileInfoFields valid_fields;
 
 	/* File type (i.e. regular, directory, block device...).  */
@@ -248,3 +256,4 @@
 G_END_DECLS
 
 #endif /* GNOME_VFS_FILE_INFO_H */
+
diff -ur /usr/src/gnome-vfs/modules/file-method.c ./modules/file-method.c
--- /usr/src/gnome-vfs/modules/file-method.c	2002-07-13 01:33:30.000000000 +0200
+++ ./modules/file-method.c	2002-09-16 23:06:39.000000000 +0200
@@ -594,6 +594,25 @@
 }
 
 static GnomeVFSResult
+get_access_info (GnomeVFSFileInfo *file_info,
+		 const gchar *full_name)
+{
+	if (access(full_name, R_OK) == 0) {
+		file_info->permissions |= GNOME_VFS_PERM_ACCESS_READABLE;
+	}
+
+	if (access(full_name, W_OK) == 0) {
+		file_info->permissions |= GNOME_VFS_PERM_ACCESS_WRITEABLE;
+	}
+
+	if (access(full_name, X_OK) == 0) {
+		file_info->permissions |= GNOME_VFS_PERM_ACCESS_EXECUTABLE;
+	}
+	file_info->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_ACCESS;
+	return GNOME_VFS_OK;
+}
+
+static GnomeVFSResult
 get_stat_info (GnomeVFSFileInfo *file_info,
 	       const gchar *full_name,
 	       GnomeVFSFileInfoOptions options,
@@ -822,6 +841,12 @@
 		g_free (full_name);
 		return result;
 	}
+	
+	result = get_access_info (file_info, full_name);
+	if (result != GNOME_VFS_OK) {
+		g_free(full_name);
+		return result;
+	}
 
 	if (options & GNOME_VFS_FILE_INFO_GET_MIME_TYPE) {
 		get_mime_type (file_info, full_name, options, &statbuf);
diff -ur /usr/src/gnome-vfs/modules/ssh-method.c ./modules/ssh-method.c
--- /usr/src/gnome-vfs/modules/ssh-method.c	2002-07-19 09:57:11.000000000 +0200
+++ ./modules/ssh-method.c	2002-09-16 23:01:04.000000000 +0200
@@ -47,6 +47,7 @@
 	GnomeVFSOpenMode open_mode;
 	int read_fd;
 	int write_fd;
+	int error_fd;
 	pid_t pid;
 } SshHandle;
 
@@ -153,17 +154,20 @@
 	char ** argv;
 	SshHandle *handle;
 	char *command_line;
+	const gchar *username;
 	int argc;
 	GError *gerror = NULL;
 
-	
+	username = gnome_vfs_uri_get_user_name(uri);
+	if (username == NULL) {
+		username = g_get_user_name();
+	}
 	command_line  = g_strconcat ("ssh -oBatchmode=yes -x -l ", 
-				     gnome_vfs_uri_get_user_name (uri),
+				     username,
 				     " ", gnome_vfs_uri_get_host_name (uri),
-				     " ", command,
+				     " ", "\"LC_ALL=C;", command,"\"",
 				     NULL);
 
-
 	g_shell_parse_argv (command_line, &argc, &argv, &gerror);
 	g_free (command_line);
 	if (gerror) {
@@ -177,10 +181,11 @@
 	handle->uri = uri;
 
 	g_spawn_async_with_pipes (NULL, argv, NULL, 
-				  G_SPAWN_SEARCH_PATH | G_SPAWN_STDERR_TO_DEV_NULL,
+				  G_SPAWN_SEARCH_PATH,
 				  NULL, NULL,
-				  &handle->pid, &handle->write_fd, &handle->read_fd,
-				  NULL, &gerror);
+				  &handle->pid, &handle->write_fd, 
+				  &handle->read_fd, &handle->error_fd,
+				  &gerror);
 	g_strfreev (argv);
 
 	if (gerror) {
@@ -195,6 +200,7 @@
 	return GNOME_VFS_OK;
 }
 
+
 static GnomeVFSResult
 ssh_destroy (SshHandle *handle)
 {
@@ -450,7 +456,6 @@
 	*method_handle = (GnomeVFSMethodHandle *)handle;
 
 	return result;
-	return GNOME_VFS_OK;
 }
 
 static GnomeVFSResult 
@@ -463,6 +468,52 @@
 
 #define LINE_LENGTH 4096 /* max line length we'll grok */
 
+static void get_access_info (GnomeVFSURI *uri, GnomeVFSFileInfo *file_info)
+{
+	gint i;
+	gchar *name;
+	struct param {
+		char c;
+		GnomeVFSFilePermissions perm;
+	};
+	struct param params[3] = {{'r', GNOME_VFS_PERM_ACCESS_READABLE},
+				  {'w', GNOME_VFS_PERM_ACCESS_WRITEABLE},
+				  {'x', GNOME_VFS_PERM_ACCESS_EXECUTABLE}};
+
+	/* FIXME: escape for shell */
+	name = gnome_vfs_unescape_string (uri->text, G_DIR_SEPARATOR_S);
+
+	if ( *name == '\0' ) {
+		g_free(name);
+		name = g_strdup ("'/'");
+	}
+	
+	for (i = 0; i<3; i++) {
+		gchar c;
+		gchar *cmd;
+		SshHandle *handle;
+		GnomeVFSFileSize bytes_read;
+		GnomeVFSResult result;
+
+		cmd = g_strdup_printf("access -%c %s && echo $?", 
+				      params[i].c, name);
+		result = ssh_connect (&handle, uri, cmd);
+		g_free (cmd);
+		
+		if (result != GNOME_VFS_OK) {
+			return;
+		}		
+		
+		result = ssh_read (handle, &c, 1, &bytes_read);
+		if ((bytes_read > 0) && (c == '0')) {
+			g_print("%c\n", params[i].c);
+			file_info->permissions |= params[i].perm;
+		}
+		ssh_destroy (handle);
+	}
+	file_info->valid_fields |= GNOME_VFS_FILE_INFO_FIELDS_ACCESS;
+}
+
 static GnomeVFSResult 
 do_read_directory (GnomeVFSMethod *method,
 		   GnomeVFSMethodHandle *method_handle,
@@ -535,6 +586,8 @@
 		file_info->valid_fields &= 
 			~GNOME_VFS_FILE_INFO_FIELDS_IO_BLOCK_SIZE;
 
+		get_access_info(((SshHandle*)method_handle)->uri, file_info);
+		
 		/* Break out.
 		   We are in a loop so we get the first 'ls' line;
 		   often it starts with 'total 2213' etc.
@@ -557,6 +610,7 @@
 	GnomeVFSResult result;
 	char *name;
 
+
 	/* FIXME: escape for shell */
 	name = gnome_vfs_unescape_string (uri->text, G_DIR_SEPARATOR_S);
 
diff -ur /usr/src/gnome-vfs/test/test-info.c ./test/test-info.c
--- /usr/src/gnome-vfs/test/test-info.c	2002-05-02 04:43:32.000000000 +0200
+++ ./test/test-info.c	2002-09-13 23:24:46.000000000 +0200
@@ -115,7 +115,16 @@
 
 	if(info->valid_fields&GNOME_VFS_FILE_INFO_FIELDS_INODE)
 		printf ("Inode #           : %ld\n", (gulong) info->inode);
-
+		
+	if(info->valid_fields&GNOME_VFS_FILE_INFO_FIELDS_ACCESS) {
+		printf ("Readable          : %s\n", 
+			(info->permissions&GNOME_VFS_PERM_ACCESS_READABLE?"YES":"NO"));
+		printf ("Writeable         : %s\n", 
+			(info->permissions&GNOME_VFS_PERM_ACCESS_WRITEABLE?"YES":"NO"));
+		printf ("Executable        : %s\n", 
+			(info->permissions&GNOME_VFS_PERM_ACCESS_EXECUTABLE?"YES":"NO"));
+	}
+	
 
 #undef FLAG_STRING
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]