2 Bacula® - The Network Backup Solution
4 Copyright (C) 2007-2011 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 three of the GNU Affero 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 Affero 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 Kern Sibbald.
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.
39 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
52 guid_list *new_guid_list()
56 list = (guid_list *)malloc(sizeof(guid_list));
57 list->uid_list = New(dlist(item, &item->link));
58 list->gid_list = New(dlist(item, &item->link));
62 void free_guid_list(guid_list *list)
65 foreach_dlist(item, list->uid_list) {
68 foreach_dlist(item, list->gid_list) {
71 delete list->uid_list;
72 delete list->gid_list;
76 static int uid_compare(void *item1, void *item2)
78 guitem *i1 = (guitem *)item1;
79 guitem *i2 = (guitem *)item2;
80 if (i1->uid < i2->uid) {
82 } else if (i1->uid > i2->uid) {
89 static int gid_compare(void *item1, void *item2)
91 guitem *i1 = (guitem *)item1;
92 guitem *i2 = (guitem *)item2;
93 if (i1->gid < i2->gid) {
95 } else if (i1->gid > i2->gid) {
103 static void get_uidname(uid_t uid, guitem *item)
106 struct passwd *pwbuf;
108 pwbuf = getpwuid(uid);
109 if (pwbuf != NULL && strcmp(pwbuf->pw_name, "????????") != 0) {
110 item->name = bstrdup(pwbuf->pw_name);
116 static void get_gidname(gid_t gid, guitem *item)
121 grbuf = getgrgid(gid);
122 if (grbuf != NULL && strcmp(grbuf->gr_name, "????????") != 0) {
123 item->name = bstrdup(grbuf->gr_name);
130 char *guid_list::uid_to_name(uid_t uid, char *name, int maxlen)
132 guitem sitem, *item, *fitem;
136 item = (guitem *)uid_list->binary_search(&sitem, uid_compare);
137 Dmsg2(900, "uid=%d item=%p\n", uid, item);
139 item = (guitem *)malloc(sizeof(guitem));
142 get_uidname(uid, item);
144 item->name = bstrdup(edit_int64(uid, buf));
145 Dmsg2(900, "set uid=%d name=%s\n", uid, item->name);
147 fitem = (guitem *)uid_list->binary_insert(item, uid_compare);
148 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 */
181 bstrncpy(name, item->name, maxlen);
191 char ed1[50], ed2[50];
192 list = new_guid_list();
193 for (i=0; i<1001; i++) {
194 printf("uid=%d name=%s gid=%d name=%s\n", i, list->uid_to_name(i, ed1, sizeof(ed1)),
195 i, list->gid_to_name(i, ed2, sizeof(ed2)));
196 printf("uid=%d name=%s gid=%d name=%s\n", i, list->uid_to_name(i, ed1, sizeof(ed1)),
197 i, list->gid_to_name(i, ed2, sizeof(ed2)));
200 free_guid_list(list);
201 sm_dump(false); /* unit test */