From ad4be7781d2a254cb4be4919ec7a6eed0782ab7a Mon Sep 17 00:00:00 2001 From: Kern Sibbald Date: Wed, 5 Sep 2007 11:13:57 +0000 Subject: [PATCH] kes Remove idcache.c kes Add guid_to_name.c/h which replace idcache. kes Remove enh_fnmatch.c. Make code that references it use fnmatch.c git-svn-id: https://bacula.svn.sourceforge.net/svnroot/bacula/trunk@5460 91ce42f0-d328-0410-95d8-f526ca767f89 --- bacula/src/dird/ua_tree.c | 23 ++- bacula/src/jcr.h | 1 + bacula/src/lib/Makefile.in | 6 +- bacula/src/lib/attr.c | 27 ++-- .../lib/{uid_gid_name.c => guid_to_name.c} | 14 +- bacula/src/lib/guid_to_name.h | 48 ++++++ bacula/src/lib/idcache.c | 152 ------------------ bacula/src/lib/jcr.c | 6 +- bacula/src/lib/lib.h | 1 + bacula/src/lib/protos.h | 6 - bacula/technotes-2.3 | 2 + 11 files changed, 92 insertions(+), 194 deletions(-) rename bacula/src/lib/{uid_gid_name.c => guid_to_name.c} (95%) create mode 100644 bacula/src/lib/guid_to_name.h delete mode 100644 bacula/src/lib/idcache.c diff --git a/bacula/src/dird/ua_tree.c b/bacula/src/dird/ua_tree.c index 576049f023..032c164768 100644 --- a/bacula/src/dird/ua_tree.c +++ b/bacula/src/dird/ua_tree.c @@ -490,17 +490,11 @@ static int lsmarkcmd(UAContext *ua, TREE_CTX *tree) return 1; } - - -extern char *getuser(uid_t uid, char *name, int len); -extern char *getgroup(gid_t gid, char *name, int len); - /* * This is actually the long form used for "dir" */ -static void ls_output(char *buf, const char *fname, const char *tag, +static void ls_output(guid_list *guid, char *buf, const char *fname, const char *tag, struct stat *statp, bool dot_cmd) - { char *p; const char *f; @@ -514,8 +508,9 @@ static void ls_output(char *buf, const char *fname, const char *tag, *p++ = ','; n = sprintf(p, "%d,", (uint32_t)statp->st_nlink); p += n; - n = sprintf(p, "%s,%s,", getuser(statp->st_uid, en1, sizeof(en1)), - getgroup(statp->st_gid, en2, sizeof(en2))); + n = sprintf(p, "%s,%s,", + guid->uid_to_name(statp->st_uid, en1, sizeof(en1)), + guid->gid_to_name(statp->st_gid, en2, sizeof(en2))); p += n; n = sprintf(p, "%s,", edit_uint64(statp->st_size, ec1)); p += n; @@ -526,8 +521,9 @@ static void ls_output(char *buf, const char *fname, const char *tag, } else { n = sprintf(p, " %2d ", (uint32_t)statp->st_nlink); p += n; - n = sprintf(p, "%-8.8s %-8.8s", getuser(statp->st_uid, en1, sizeof(en1)), - getgroup(statp->st_gid, en2, sizeof(en2))); + n = sprintf(p, "%-8.8s %-8.8s", + guid->uid_to_name(statp->st_uid, en1, sizeof(en1)), + guid->gid_to_name(statp->st_gid, en2, sizeof(en2))); p += n; n = sprintf(p, "%10.10s ", edit_uint64(statp->st_size, ec1)); p += n; @@ -557,12 +553,14 @@ static int do_dircmd(UAContext *ua, TREE_CTX *tree, bool dot_cmd) struct stat statp; char buf[1100]; char cwd[1100], *pcwd; + guid_list *guid; if (!tree_node_has_child(tree->node)) { ua->send_msg(_("Node %s has no children.\n"), tree->node->fname); return 1; } + guid = new_guid_list(); foreach_child(node, tree->node) { const char *tag; if (ua->argc == 1 || fnmatch(ua->argk[1], node->fname, 0) == 0) { @@ -600,10 +598,11 @@ static int do_dircmd(UAContext *ua, TREE_CTX *tree, bool dot_cmd) /* Something went wrong getting attributes -- print name */ memset(&statp, 0, sizeof(statp)); } - ls_output(buf, cwd, tag, &statp, dot_cmd); + ls_output(guid, buf, cwd, tag, &statp, dot_cmd); ua->send_msg("%s\n", buf); } } + free_guid_list(guid); return 1; } diff --git a/bacula/src/jcr.h b/bacula/src/jcr.h index 89e2095cdb..e54a97837a 100644 --- a/bacula/src/jcr.h +++ b/bacula/src/jcr.h @@ -188,6 +188,7 @@ public: B_DB *db; /* database pointer */ B_DB *db_batch; /* database pointer for batch insert */ ATTR_DBR *ar; /* DB attribute record */ + guid_list *id_list; /* User/group id to name list */ /* Daemon specific part of JCR */ /* This should be empty in the library */ diff --git a/bacula/src/lib/Makefile.in b/bacula/src/lib/Makefile.in index b9ca9acaa6..c32085f460 100644 --- a/bacula/src/lib/Makefile.in +++ b/bacula/src/lib/Makefile.in @@ -23,7 +23,7 @@ LIBSRCS = attr.c base64.c berrno.c bsys.c bget_msg.c \ bnet.c bnet_server.c runscript.c \ bsock.c bpipe.c bsnprintf.c btime.c \ cram-md5.c crc32.c crypto.c daemon.c edit.c fnmatch.c \ - hmac.c idcache.c jcr.c lex.c alist.c dlist.c \ + guid_to_name.c hmac.c jcr.c lex.c alist.c dlist.c \ md5.c message.c mem_pool.c openssl.c parse_conf.c \ queue.c bregex.c \ res.c rwlock.c scan.c serial.c sha1.c \ @@ -35,8 +35,8 @@ LIBSRCS = attr.c base64.c berrno.c bsys.c bget_msg.c \ LIBOBJS = attr.o base64.o berrno.o bsys.o bget_msg.o \ bnet.o bnet_server.o runscript.o \ bsock.o bpipe.o bsnprintf.o btime.o \ - cram-md5.o crc32.o crypto.o daemon.o edit.o enh_fnmatch.o fnmatch.o \ - hmac.o idcache.o jcr.o lex.o alist.o dlist.o \ + cram-md5.o crc32.o crypto.o daemon.o edit.o fnmatch.o \ + guid_to_name.o hmac.o jcr.o lex.o alist.o dlist.o \ md5.o message.o mem_pool.o openssl.o parse_conf.o \ queue.o bregex.o \ res.o rwlock.o scan.o serial.o sha1.o \ diff --git a/bacula/src/lib/attr.c b/bacula/src/lib/attr.c index d3f59bffb0..cbe673f1cb 100644 --- a/bacula/src/lib/attr.c +++ b/bacula/src/lib/attr.c @@ -1,14 +1,7 @@ -/* - * attr.c Unpack an Attribute record returned from the tape - * - * Kern Sibbald, June MMIII (code pulled from filed/restore.c and updated) - * - * Version $Id$ - */ /* Bacula® - The Network Backup Solution - Copyright (C) 2003-2006 Free Software Foundation Europe e.V. + Copyright (C) 2003-2007 Free Software Foundation Europe e.V. The main author of Bacula is Kern Sibbald, with contributions from many others, a complete list can be found in the file AUTHORS. @@ -32,6 +25,14 @@ (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich, Switzerland, email:ftf@fsfeurope.org. */ +/* + * attr.c Unpack an Attribute record returned from the tape + * + * Kern Sibbald, June MMIII (code pulled from filed/restore.c and updated) + * + * Version $Id$ + */ + #include "bacula.h" #include "jcr.h" @@ -237,11 +238,17 @@ void print_ls_output(JCR *jcr, ATTR *attr) char ec1[30]; char en1[30], en2[30]; char *p, *f; + guid_list *guid; + if (!jcr->id_list) { + jcr->id_list = new_guid_list(); + } + guid = jcr->id_list; p = encode_mode(attr->statp.st_mode, buf); p += sprintf(p, " %2d ", (uint32_t)attr->statp.st_nlink); - p += sprintf(p, "%-8.8s %-8.8s", getuser(attr->statp.st_uid, en1, sizeof(en1)), - getgroup(attr->statp.st_gid, en2, sizeof(en2))); + p += sprintf(p, "%-8.8s %-8.8s", + guid->uid_to_name(attr->statp.st_uid, en1, sizeof(en1)), + guid->gid_to_name(attr->statp.st_gid, en2, sizeof(en2))); p += sprintf(p, "%10.10s ", edit_uint64(attr->statp.st_size, ec1)); p = encode_time(attr->statp.st_ctime, p); *p++ = ' '; diff --git a/bacula/src/lib/uid_gid_name.c b/bacula/src/lib/guid_to_name.c similarity index 95% rename from bacula/src/lib/uid_gid_name.c rename to bacula/src/lib/guid_to_name.c index 7f7d665678..f5415f3f02 100644 --- a/bacula/src/lib/uid_gid_name.c +++ b/bacula/src/lib/guid_to_name.c @@ -26,15 +26,19 @@ Switzerland, email:ftf@fsfeurope.org. */ /* - * Written by Kern Sibbald, July 2005 to replace idcache.c + * Written by Kern Sibbald, July 2007 to replace idcache.c * * Program to convert uid and gid into names, and cache the results * for preformance reasons. + * + * Version $Id$ */ #include "bacula.h" +#ifndef WIN32 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; +#endif struct guitem { dlink link; @@ -45,14 +49,6 @@ struct guitem { }; }; -class guid_list { -public: - dlist *uid_list; - dlist *gid_list; - - char *uid_to_name(uid_t uid, char *name, int maxlen); - char *gid_to_name(gid_t gid, char *name, int maxlen); -}; guid_list *new_guid_list() { diff --git a/bacula/src/lib/guid_to_name.h b/bacula/src/lib/guid_to_name.h new file mode 100644 index 0000000000..0fb1acc919 --- /dev/null +++ b/bacula/src/lib/guid_to_name.h @@ -0,0 +1,48 @@ +/* + Bacula® - The Network Backup Solution + + Copyright (C) 2007 Kern Sibbald + + The main author of Bacula is Kern Sibbald, with contributions from + many others, a complete list can be found in the file AUTHORS. + This program is Free Software; you can redistribute it and/or + modify it under the terms of version two of the GNU General Public + License as published by the Free Software Foundation, which is + listed in the file LICENSE. + + 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., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. + + Bacula® is a registered trademark of John Walker. + The licensor of Bacula is the Free Software Foundation Europe + (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich, + Switzerland, email:ftf@fsfeurope.org. +*/ +/* + * Written by Kern Sibbald, July 2007 to replace idcache.c + * + * Program to convert uid and gid into names, and cache the results + * for preformance reasons. + * + * Version $Id$ + */ + +class guid_list { +public: + dlist *uid_list; + dlist *gid_list; + + char *uid_to_name(uid_t uid, char *name, int maxlen); + char *gid_to_name(gid_t gid, char *name, int maxlen); +}; + +guid_list *new_guid_list(); +void free_guid_list(guid_list *list); + diff --git a/bacula/src/lib/idcache.c b/bacula/src/lib/idcache.c deleted file mode 100644 index d3e78f8762..0000000000 --- a/bacula/src/lib/idcache.c +++ /dev/null @@ -1,152 +0,0 @@ -/* idcache.c -- map user and group IDs, cached for speed - Copyright (C) 1985, 1988, 1989, 1990 Free Software Foundation, Inc. - - 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, 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., 675 Mass Ave, Cambridge, MA 02139, USA. */ - -#include "bacula.h" - -struct userid { - union { - uid_t u; - gid_t g; - } id; - char *name; - struct userid *next; -}; - -static struct userid *user_alist = NULL; -/* Use the same struct as for userids. */ -static struct userid *group_alist = NULL; - -static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; - -/* Translate UID to a login name or a stringified number, - with cache. */ - -char *getuser(uid_t uid, char *name, int len) -{ - register struct userid *tail; - char usernum_string[20]; - - P(mutex); - for (tail = user_alist; tail; tail = tail->next) { - if (tail->id.u == uid) { - goto uid_done; - } - } - - tail = (struct userid *)malloc(sizeof (struct userid)); - tail->id.u = uid; - tail->name = NULL; - -#if !defined(HAVE_WIN32) - { - struct passwd *pwent = getpwuid(uid); - - if (pwent != NULL && strcmp(pwent->pw_name, "????????") != 0) { - tail->name = bstrdup(pwent->pw_name); - } - } -#endif - - if (tail->name == NULL) { - sprintf(usernum_string, "%u", (uint32_t)uid); - tail->name = bstrdup(usernum_string); - } - - /* Add to the head of the list, so most recently used is first. */ - tail->next = user_alist; - user_alist = tail; - -uid_done: - bstrncpy(name, tail->name, len); - V(mutex); - return name; -} - -void free_getuser_cache() -{ - register struct userid *tail; - - P(mutex); - for (tail = user_alist; tail; ) { - struct userid *otail = tail; - free(tail->name); - tail = tail->next; - free(otail); - } - user_alist = NULL; - V(mutex); -} - - - -/* Translate GID to a group name or a stringified number, - with cache. */ -char *getgroup(gid_t gid, char *name, int len) -{ - register struct userid *tail; - char groupnum_string[20]; - - P(mutex); - for (tail = group_alist; tail; tail = tail->next) { - if (tail->id.g == gid) { - goto gid_done; - } - } - - tail = (struct userid *)malloc(sizeof (struct userid)); - tail->id.g = gid; - tail->name = NULL; - -#if !defined(HAVE_WIN32) - { - struct group *grent = getgrgid(gid); - - if (grent != NULL && strcmp(grent->gr_name, "????????") != 0) { - tail->name = bstrdup(grent->gr_name); - } - } -#endif - - if (tail->name == NULL) { - sprintf (groupnum_string, "%u", (uint32_t)gid); - tail->name = bstrdup(groupnum_string); - } - - /* Add to the head of the list, so most recently used is first. */ - tail->next = group_alist; - group_alist = tail; - -gid_done: - bstrncpy(name, tail->name, len); - V(mutex); - return name; -} - -void free_getgroup_cache() -{ - register struct userid *tail; - - P(mutex); - for (tail = group_alist; tail; ) { - struct userid *otail = tail; - free(tail->name); - tail = tail->next; - free(otail); - } - group_alist = NULL; - V(mutex); -} diff --git a/bacula/src/lib/jcr.c b/bacula/src/lib/jcr.c index 0b5e01fdc8..ef542cc09e 100644 --- a/bacula/src/lib/jcr.c +++ b/bacula/src/lib/jcr.c @@ -401,8 +401,10 @@ static void free_common_jcr(JCR *jcr) jcr->cached_path = NULL; jcr->cached_pnl = 0; } - free_getuser_cache(); - free_getgroup_cache(); + if (jcr->id_list) { + free_guid_list(jcr->id_list); + jcr->id_list = NULL; + } free(jcr); } diff --git a/bacula/src/lib/lib.h b/bacula/src/lib/lib.h index 0397bd9521..7ecc0858a6 100644 --- a/bacula/src/lib/lib.h +++ b/bacula/src/lib/lib.h @@ -67,5 +67,6 @@ #include "attr.h" #include "var.h" #include "address_conf.h" +#include "guid_to_name.h" #include "protos.h" diff --git a/bacula/src/lib/protos.h b/bacula/src/lib/protos.h index e3b9340bdc..ef09d9f05a 100644 --- a/bacula/src/lib/protos.h +++ b/bacula/src/lib/protos.h @@ -240,12 +240,6 @@ int net_connect (int port); BSOCK * bnet_bind (int port); BSOCK * bnet_accept (BSOCK *bsock, char *who); -/* idcache.c */ -char *getuser(uid_t uid); -void free_getuser_cache(); -char *getgroup (gid_t gid); -void free_getgroup_cache(); - /* python.c */ typedef int (EVENT_HANDLER)(JCR *jcr, const char *event); void init_python_interpreter(const char *progname, const char *scripts, diff --git a/bacula/technotes-2.3 b/bacula/technotes-2.3 index 008575c414..bb08001bd6 100644 --- a/bacula/technotes-2.3 +++ b/bacula/technotes-2.3 @@ -2,6 +2,8 @@ General: 05Sep07 +kes Remove idcache.c +kes Add guid_to_name.c/h which replace idcache. kes Remove enh_fnmatch.c. Make code that references it use fnmatch.c 04Sep07 ebl Detect if new PosgreSQL batch insert API is present. -- 2.39.5