[Nautilus-list] Cache getpwuid results



Here is a patch that caches the getpwuid results in 
nautilus_file_denies_access_permission() which seems to be called for 
every file in a scanned directory.

/ Alex

Index: nautilus-file.c
===================================================================
RCS file: /cvs/gnome/nautilus/libnautilus-private/nautilus-file.c,v
retrieving revision 1.249.2.4
diff -u -p -r1.249.2.4 nautilus-file.c
--- nautilus-file.c	2001/07/31 21:59:28	1.249.2.4
+++ nautilus-file.c	2001/08/15 01:11:30
@@ -60,7 +60,11 @@
 #include <pwd.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <sys/time.h>
 
+/* Time in seconds to cache getpwuid results */
+#define GETPWUID_CACHE_TIME (5*60)
+
 #undef NAUTILUS_FILE_DEBUG_REF
 
 #ifdef NAUTILUS_FILE_DEBUG_REF
@@ -582,10 +586,13 @@ nautilus_file_denies_access_permission (
 				        GnomeVFSFilePermissions group_permission,
 				        GnomeVFSFilePermissions other_permission)
 {
-	uid_t user_id;
-	struct passwd *password_info;
-	gid_t supplementary_groups[NGROUPS_MAX];
-	int num_supplementary_groups;
+	struct timeval now;
+	static struct timeval cached_time = {0,0};
+	static struct passwd *password_info;
+	static uid_t user_id;
+	static gid_t primary_group;
+	static gid_t supplementary_groups[NGROUPS_MAX];
+	static int num_supplementary_groups;
 	int i;
 
 	g_assert (NAUTILUS_IS_FILE (file));
@@ -602,9 +609,23 @@ nautilus_file_denies_access_permission (
 		return FALSE;
 	}
 
-	/* Check the user. */
-	user_id = geteuid ();
+	/* This is called often. Cache the user information for five minutes */
+	
+	gettimeofday (&now, NULL);
+	
+	if (cached_time.tv_sec == 0 ||
+	    (now.tv_sec - cached_time.tv_sec) > GETPWUID_CACHE_TIME) {
+		cached_time = now;
+		user_id = geteuid ();
+		/* No need to free result of getpwuid. */
+		password_info = getpwuid (user_id);
+		if (password_info)
+			primary_group = password_info->pw_gid;
+		num_supplementary_groups = getgroups (NGROUPS_MAX, supplementary_groups);
+	}
 
+	/* Check the user. */
+	
 	/* Root is not forbidden to do anything. */
 	if (user_id == 0) {
 		return FALSE;
@@ -619,8 +640,6 @@ nautilus_file_denies_access_permission (
 		return (file->details->info->permissions & owner_permission) == 0;
 	}
 
-	/* No need to free result of getpwuid. */
-	password_info = getpwuid (user_id);
 
 	/* Group member's access is governed by the group bits. */
 	/* FIXME bugzilla.eazel.com 644: 
@@ -628,11 +647,10 @@ nautilus_file_denies_access_permission (
 	 * there be garbage there? What will it do for non-local files?
 	 */
 	if (password_info != NULL
-	    && password_info->pw_gid == (gid_t) file->details->info->gid) {
+	    && primary_group == (gid_t) file->details->info->gid) {
 		return (file->details->info->permissions & group_permission) == 0;
 	}
 	/* Check supplementary groups */
-	num_supplementary_groups = getgroups (NGROUPS_MAX, supplementary_groups);
 	for (i = 0; i < num_supplementary_groups; i++) {
 		if ((gid_t) file->details->info->gid == supplementary_groups[i]) {
 			return (file->details->info->permissions & group_permission) == 0;





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