2 Bacula® - The Network Backup Solution
4 Copyright (C) 2007 Kern Sibbald
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version two of the GNU General Public
10 License as published by the Free Software Foundation, which is
11 listed in the file LICENSE.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of John Walker.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
29 * Written by Kern Sibbald, July 2007 to replace idcache.c
31 * Program to convert uid and gid into names, and cache the results
32 * for preformance reasons.
34 * Version $Id: guid_to_name.c 5502 2007-09-09 08:00:09Z kerns $
40 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
53 guid_list *new_guid_list()
57 list = (guid_list *)malloc(sizeof(guid_list));
58 list->uid_list = New(dlist(item, &item->link));
59 list->gid_list = New(dlist(item, &item->link));
63 void free_guid_list(guid_list *list)
66 foreach_dlist(item, list->uid_list) {
69 foreach_dlist(item, list->gid_list) {
72 delete list->uid_list;
73 delete list->gid_list;
77 static int uid_compare(void *item1, void *item2)
79 guitem *i1 = (guitem *)item1;
80 guitem *i2 = (guitem *)item2;
81 if (i1->uid < i2->uid) {
83 } else if (i1->uid > i2->uid) {
90 static int gid_compare(void *item1, void *item2)
92 guitem *i1 = (guitem *)item1;
93 guitem *i2 = (guitem *)item2;
94 if (i1->gid < i2->gid) {
96 } else if (i1->gid > i2->gid) {
104 static void get_uidname(uid_t uid, guitem *item)
107 struct passwd *pwbuf;
109 pwbuf = getpwuid(uid);
110 if (pwbuf != NULL && strcmp(pwbuf->pw_name, "????????") != 0) {
111 item->name = bstrdup(pwbuf->pw_name);
117 static void get_gidname(gid_t gid, guitem *item)
122 grbuf = getgrgid(gid);
123 if (grbuf != NULL && strcmp(grbuf->gr_name, "????????") != 0) {
124 item->name = bstrdup(grbuf->gr_name);
131 char *guid_list::uid_to_name(uid_t uid, char *name, int maxlen)
133 guitem sitem, *item, *fitem;
137 item = (guitem *)uid_list->binary_search(&sitem, uid_compare);
138 Dmsg2(900, "uid=%d item=%p\n", uid, item);
140 item = (guitem *)malloc(sizeof(guitem));
143 get_uidname(uid, item);
145 item->name = bstrdup(edit_int64(uid, buf));
146 Dmsg2(900, "set uid=%d name=%s\n", uid, item->name);
148 fitem = (guitem *)uid_list->binary_insert(item, uid_compare);
149 if (fitem != item) { /* item already there this shouldn't happen */
154 bstrncpy(name, item->name, maxlen);
158 char *guid_list::gid_to_name(gid_t gid, char *name, int maxlen)
160 guitem sitem, *item, *fitem;
164 item = (guitem *)gid_list->binary_search(&sitem, gid_compare);
166 item = (guitem *)malloc(sizeof(guitem));
169 get_gidname(gid, item);
171 item->name = bstrdup(edit_int64(gid, buf));
173 fitem = (guitem *)gid_list->binary_insert(item, gid_compare);
174 if (fitem != item) { /* item already there this shouldn't happen */
180 bstrncpy(name, item->name, maxlen);
190 char ed1[50], ed2[50];
191 list = new_guid_list();
192 for (i=0; i<1001; i++) {
193 printf("uid=%d name=%s gid=%d name=%s\n", i, list->uid_to_name(i, ed1, sizeof(ed1)),
194 i, list->gid_to_name(i, ed2, sizeof(ed2)));
195 printf("uid=%d name=%s gid=%d name=%s\n", i, list->uid_to_name(i, ed1, sizeof(ed1)),
196 i, list->gid_to_name(i, ed2, sizeof(ed2)));
199 free_guid_list(list);