Get users' and groups' names via a cache to avoid calling

2006-02-26  Zbigniew Chyla  <mail@zbigniew.chyla.pl>

	Get users' and groups' names via a cache to avoid calling
	getpwuid/getgrgid too often (i.e. many times during single redraw).

	* libnautilus-private/Makefile.am: (libnautilus_private_la_SOURCES):
	Added nautilus-users-groups-cache.[ch]
	* libnautilus-private/nautilus-users-groups-cache.[ch]: New,
	implementation of cache for getpwuid/getgrgid results.
	* libnautilus-private/nautilus-file.c:
	(get_user_name_from_id): removed
	(get_real_name): accept "name + gecos" pair instead of "struct passwd".
	(get_user_and_real_name_from_id): get user name and gecos using
	nautilus_users_cache_* functions instead of using getpwuid directly.
	(nautilus_get_user_names): adjusted to changed signature of get_real_name.
	(nautilus_file_get_group_name): get group name using
	nautilus_groups_cache_get_name instead of using getgrgid directly.
	(nautilus_file_get_owner_as_string): get user name using
	nautilus_users_cache_get_name.
This commit is contained in:
Zbigniew Chyla 2006-02-27 15:36:40 +00:00 committed by Alexander Larsson
parent e02fe835e5
commit 69facef412
5 changed files with 367 additions and 52 deletions

View file

@ -1,3 +1,23 @@
2006-02-26 Zbigniew Chyla <mail@zbigniew.chyla.pl>
Get users' and groups' names via a cache to avoid calling
getpwuid/getgrgid too often (i.e. many times during single redraw).
* libnautilus-private/Makefile.am: (libnautilus_private_la_SOURCES):
Added nautilus-users-groups-cache.[ch]
* libnautilus-private/nautilus-users-groups-cache.[ch]: New,
implementation of cache for getpwuid/getgrgid results.
* libnautilus-private/nautilus-file.c:
(get_user_name_from_id): removed
(get_real_name): accept "name + gecos" pair instead of "struct passwd".
(get_user_and_real_name_from_id): get user name and gecos using
nautilus_users_cache_* functions instead of using getpwuid directly.
(nautilus_get_user_names): adjusted to changed signature of get_real_name.
(nautilus_file_get_group_name): get group name using
nautilus_groups_cache_get_name instead of using getgrgid directly.
(nautilus_file_get_owner_as_string): get user name using
nautilus_users_cache_get_name.
2006-02-26 Zbigniew Chyla <mail@zbigniew.chyla.pl>
Don't add all possible columns to GtkTreeView when initializing

View file

@ -193,6 +193,8 @@ libnautilus_private_la_SOURCES = \
nautilus-undo-transaction.h \
nautilus-undo.c \
nautilus-undo.h \
nautilus-users-groups-cache.c \
nautilus-users-groups-cache.h \
nautilus-vfs-directory.c \
nautilus-vfs-directory.h \
nautilus-vfs-file.c \

View file

@ -45,6 +45,7 @@
#include "nautilus-thumbnails.h"
#include "nautilus-trash-directory.h"
#include "nautilus-trash-file.h"
#include "nautilus-users-groups-cache.h"
#include "nautilus-vfs-file.h"
#include "nautilus-saved-search-file.h"
#include <eel/eel-debug.h>
@ -3537,30 +3538,15 @@ nautilus_file_set_permissions (NautilusFile *file,
}
static char *
get_user_name_from_id (uid_t uid)
{
struct passwd *password_info;
/* No need to free result of getpwuid */
password_info = getpwuid (uid);
if (password_info == NULL) {
return NULL;
}
return g_strdup (password_info->pw_name);
}
static char *
get_real_name (struct passwd *user)
get_real_name (const char *name, const char *gecos)
{
char *locale_string, *part_before_comma, *capitalized_login_name, *real_name;
if (user->pw_gecos == NULL) {
if (gecos == NULL) {
return NULL;
}
locale_string = eel_str_strip_substring_and_after (user->pw_gecos, ",");
locale_string = eel_str_strip_substring_and_after (gecos, ",");
if (!g_utf8_validate (locale_string, -1, NULL)) {
part_before_comma = g_locale_to_utf8 (locale_string, -1, NULL, NULL, NULL);
g_free (locale_string);
@ -3568,10 +3554,10 @@ get_real_name (struct passwd *user)
part_before_comma = locale_string;
}
if (!g_utf8_validate (user->pw_name, -1, NULL)) {
locale_string = g_locale_to_utf8 (user->pw_name, -1, NULL, NULL, NULL);
if (!g_utf8_validate (name, -1, NULL)) {
locale_string = g_locale_to_utf8 (name, -1, NULL, NULL, NULL);
} else {
locale_string = g_strdup (user->pw_name);
locale_string = g_strdup (name);
}
capitalized_login_name = eel_str_capitalize (locale_string);
@ -3587,7 +3573,7 @@ get_real_name (struct passwd *user)
if (eel_str_is_empty (real_name)
|| eel_strcmp (user->pw_name, real_name) == 0
|| eel_strcmp (name, real_name) == 0
|| eel_strcmp (capitalized_login_name, real_name) == 0) {
g_free (real_name);
real_name = NULL;
@ -3601,25 +3587,23 @@ get_real_name (struct passwd *user)
static char *
get_user_and_real_name_from_id (uid_t uid)
{
char *name, *gecos;
char *real_name, *user_and_real_name;
struct passwd *password_info;
/* No need to free result of getpwuid */
password_info = getpwuid (uid);
name = nautilus_users_cache_get_name (uid);
gecos = nautilus_users_cache_get_gecos (uid);
if (password_info == NULL) {
return NULL;
}
real_name = get_real_name (password_info);
real_name = get_real_name (name, gecos);
if (real_name != NULL) {
user_and_real_name = g_strdup_printf
("%s - %s", password_info->pw_name, real_name);
user_and_real_name = g_strdup_printf ("%s - %s", name, real_name);
} else {
user_and_real_name = g_strdup (password_info->pw_name);
user_and_real_name = g_strdup (name);
}
g_free (real_name);
g_free (name);
g_free (gecos);
return user_and_real_name;
}
@ -3893,7 +3877,7 @@ nautilus_get_user_names (void)
setpwent ();
while ((user = getpwent ()) != NULL) {
real_name = get_real_name (user);
real_name = get_real_name (user->pw_name, user->pw_gecos);
if (real_name != NULL) {
name = g_strconcat (user->pw_name, "\n", real_name, NULL);
} else {
@ -3939,24 +3923,22 @@ nautilus_file_can_get_group (NautilusFile *file)
char *
nautilus_file_get_group_name (NautilusFile *file)
{
struct group *group_info;
char *group_name;
/* Before we have info on a file, the owner is unknown. */
if (nautilus_file_info_missing (file, GNOME_VFS_FILE_INFO_FIELDS_IDS)) {
return NULL;
}
/* No need to free result of getgrgid */
group_info = getgrgid ((gid_t) file->details->info->gid);
if (group_info != NULL) {
return g_strdup (group_info->gr_name);
group_name = nautilus_groups_cache_get_name ((gid_t) file->details->info->gid);
if (group_name == NULL) {
/* In the oddball case that the group name has been set to an id for which
* there is no defined group, return the id in string form.
*/
group_name = g_strdup_printf ("%d", file->details->info->gid);
}
/* In the oddball case that the group name has been set to an id for which
* there is no defined group, return the id in string form.
*/
return g_strdup_printf ("%d", file->details->info->gid);
return group_name;
}
/**
@ -4252,17 +4234,17 @@ nautilus_file_get_owner_as_string (NautilusFile *file, gboolean include_real_nam
if (include_real_name) {
user_name = get_user_and_real_name_from_id (file->details->info->uid);
} else {
user_name = get_user_name_from_id (file->details->info->uid);
user_name = nautilus_users_cache_get_name (file->details->info->uid);
}
if (user_name != NULL) {
return user_name;
if (user_name == NULL) {
/* In the oddball case that the user name has been set to an id for which
* there is no defined user, return the id in string form.
*/
user_name = g_strdup_printf ("%d", file->details->info->uid);
}
/* In the oddball case that the user name has been set to an id for which
* there is no defined user, return the id in string form.
*/
return g_strdup_printf ("%d", file->details->info->uid);
return user_name;
}
static char *

View file

@ -0,0 +1,277 @@
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-
nautilus-users-groups-cache.c: cache of users' and groups' names.
Copyright (C) 2006 Zbigniew Chyla
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public
License along with this program; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
Author: Zbigniew Chyla <mail@zbigniew.chyla.pl>
*/
#include <config.h>
#include "nautilus-users-groups-cache.h"
#include <glib.h>
#include <grp.h>
#include <pwd.h>
typedef struct _ExpiringCache ExpiringCache;
/* times in milliseconds */
#define USERS_CACHE_EXPIRE_TIME (60 * 1000)
#define GROUPS_CACHE_EXPIRE_TIME (60 * 1000)
/* cache of users' names */
static ExpiringCache *users_cache = NULL;
/* cache of groups' names */
static ExpiringCache *groups_cache = NULL;
/**
* Generic implementation of cache with guint keys and values which expire
* after specified amount of time.
*/
typedef gpointer (*ExpiringCacheGetValFunc) (guint key);
struct _ExpiringCache
{
/* Expiration time of cached value */
time_t expire_time;
/* Called to obtain a value by a key */
ExpiringCacheGetValFunc get_value_func;
/* Called to destroy a value */
GDestroyNotify value_destroy_func;
/* Stores cached values */
GHashTable *cached_values;
};
typedef struct _ExpiringCacheEntry ExpiringCacheEntry;
struct _ExpiringCacheEntry
{
ExpiringCache *cache;
guint key;
gpointer value;
};
static ExpiringCache *
expiring_cache_new (time_t expire_time, ExpiringCacheGetValFunc get_value_func,
GDestroyNotify value_destroy_func)
{
ExpiringCache *cache;
g_return_val_if_fail (get_value_func != NULL, NULL);
cache = g_new (ExpiringCache, 1);
cache->expire_time = expire_time;
cache->get_value_func = get_value_func;
cache->value_destroy_func = value_destroy_func;
cache->cached_values = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, NULL);
return cache;
}
static ExpiringCacheEntry *
expiring_cache_entry_new (ExpiringCache *cache, guint key, gpointer value)
{
ExpiringCacheEntry *entry;
entry = g_slice_new (ExpiringCacheEntry);
entry->cache = cache;
entry->key = key;
entry->value = value;
return entry;
}
static void
expiring_cache_entry_destroy (ExpiringCacheEntry *entry)
{
if (entry->cache->value_destroy_func != NULL) {
entry->cache->value_destroy_func (entry->value);
}
g_slice_free (ExpiringCacheEntry, entry);
}
static gboolean
cb_cache_entry_expired (ExpiringCacheEntry *entry)
{
g_hash_table_remove (entry->cache->cached_values, GSIZE_TO_POINTER (entry->key));
expiring_cache_entry_destroy (entry);
return FALSE;
}
static gpointer
expiring_cache_get_value (ExpiringCache *cache, guint key)
{
ExpiringCacheEntry *entry;
g_return_val_if_fail (cache != NULL, NULL);
if (!g_hash_table_lookup_extended (cache->cached_values, GSIZE_TO_POINTER (key),
NULL, (gpointer) &entry)) {
entry = expiring_cache_entry_new (cache, key, cache->get_value_func (key));
g_hash_table_insert (cache->cached_values, GSIZE_TO_POINTER (key), entry);
g_timeout_add (cache->expire_time, (GSourceFunc) cb_cache_entry_expired, entry);
}
return entry->value;
}
/*
* Cache of users' names based on ExpiringCache.
*/
typedef struct _UserInfo UserInfo;
struct _UserInfo {
char *name;
char *gecos;
};
static UserInfo *
user_info_new (struct passwd *password_info)
{
UserInfo *uinfo;
if (password_info != NULL) {
uinfo = g_slice_new (UserInfo);
uinfo->name = g_strdup (password_info->pw_name);
uinfo->gecos = g_strdup (password_info->pw_gecos);
} else {
uinfo = NULL;
}
return uinfo;
}
static void
user_info_free (UserInfo *uinfo)
{
if (uinfo != NULL) {
g_free (uinfo->name);
g_free (uinfo->gecos);
g_slice_free (UserInfo, uinfo);
}
}
static gpointer
users_cache_get_value (guint key)
{
return user_info_new (getpwuid (key));
}
static UserInfo *
get_cached_user_info(guint uid)
{
if (users_cache == NULL) {
users_cache = expiring_cache_new (USERS_CACHE_EXPIRE_TIME, users_cache_get_value,
(GDestroyNotify) user_info_free);
}
return expiring_cache_get_value (users_cache, uid);
}
/**
* nautilus_users_cache_get_name:
*
* Returns name of user with given uid (using cached data if possible) or
* NULL in case a user with given uid can't be found.
*
* Returns: Newly allocated string or NULL.
*/
char *
nautilus_users_cache_get_name (uid_t uid)
{
UserInfo *uinfo;
uinfo = get_cached_user_info (uid);
if (uinfo != NULL) {
return g_strdup (uinfo->name);
} else {
return NULL;
}
}
/**
* nautilus_users_cache_get_gecos:
*
* Returns gecos of user with given uid (using cached data if possible) or
* NULL in case a user with given uid can't be found.
*
* Returns: Newly allocated string or NULL.
*/
char *
nautilus_users_cache_get_gecos (uid_t uid)
{
UserInfo *uinfo;
uinfo = get_cached_user_info (uid);
if (uinfo != NULL) {
return g_strdup (uinfo->gecos);
} else {
return NULL;
}
}
/*
* Cache of groups' names based on ExpiringCache.
*/
static gpointer
groups_cache_get_value (guint key)
{
struct group *group_info;
group_info = getgrgid (GPOINTER_TO_SIZE (key));
if (group_info != NULL) {
return g_strdup (group_info->gr_name);
} else {
return NULL;
}
}
/**
* nautilus_groups_cache_get_name:
*
* Returns name of group with given gid (using cached data if possible) or
* NULL in case a group with given gid can't be found.
*
* Returns: Newly allocated string or NULL.
*/
char *
nautilus_groups_cache_get_name (gid_t gid)
{
if (groups_cache == NULL) {
groups_cache = expiring_cache_new (GROUPS_CACHE_EXPIRE_TIME, groups_cache_get_value, g_free);
}
return g_strdup (expiring_cache_get_value (groups_cache, gid));
}

View file

@ -0,0 +1,34 @@
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-
nautilus-users-groups-cache.h: cache of users' and groups' names.
Copyright (C) 2006 Zbigniew Chyla
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public
License along with this program; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
Author: Zbigniew Chyla <mail@zbigniew.chyla.pl>
*/
#ifndef NAUTILUS_USERS_GROUPS_CACHE_H
#define NAUTILUS_USERS_GROUPS_CACHE_H
#include <sys/types.h>
char *nautilus_users_cache_get_name (uid_t uid);
char *nautilus_users_cache_get_gecos (uid_t uid);
char *nautilus_groups_cache_get_name (gid_t gid);
#endif /* NAUTILUS_USERS_GROUPS_CACHE_H */