]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/guid_to_name.c
Add debug times, reduce size of two-pool-changer test
[bacula/bacula] / bacula / src / lib / guid_to_name.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2007 Kern Sibbald
5
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.
12
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.
17
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
21    02110-1301, USA.
22
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.
27 */
28 /*
29  * Written by Kern Sibbald, July 2007 to replace idcache.c
30  * 
31  *  Program to convert uid and gid into names, and cache the results
32  *   for preformance reasons.
33  *
34  *  Version $Id$
35  */
36
37 #include "bacula.h"
38
39 #ifndef WIN32
40 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
41 #endif
42
43 struct guitem {
44    dlink link;
45    char *name;
46    union {
47       uid_t uid;
48       gid_t gid;
49    };
50 };
51
52
53 guid_list *new_guid_list()
54 {
55    guid_list *list;
56    guitem *item = NULL;
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));
60    return list;
61 }
62
63 void free_guid_list(guid_list *list)
64 {
65    guitem *item;
66    foreach_dlist(item, list->uid_list) {
67       free(item->name);
68    }
69    foreach_dlist(item, list->gid_list) {
70       free(item->name);
71    }
72    delete list->uid_list;
73    delete list->gid_list;
74    free(list);
75 }
76
77 static int uid_compare(void *item1, void *item2)
78 {
79    guitem *i1 = (guitem *)item1;
80    guitem *i2 = (guitem *)item2;
81    if (i1->uid < i2->uid) {
82       return -1;
83    } else if (i1->uid > i2->uid) {
84       return 1;
85    } else {
86       return 0;
87    }
88 }
89
90 static int gid_compare(void *item1, void *item2)
91 {
92    guitem *i1 = (guitem *)item1;
93    guitem *i2 = (guitem *)item2;
94    if (i1->gid < i2->gid) {
95       return -1;
96    } else if (i1->gid > i2->gid) {
97       return 1;
98    } else {
99       return 0;
100    }
101 }
102
103
104 static void get_uidname(uid_t uid, guitem *item)
105 {
106 #ifndef HAVE_WIN32
107    struct passwd *pwbuf;
108    P(mutex);
109    pwbuf = getpwuid(uid);
110    if (pwbuf != NULL && strcmp(pwbuf->pw_name, "????????") != 0) {
111       item->name = bstrdup(pwbuf->pw_name);
112    }
113    V(mutex);
114 #endif
115 }
116
117 static void get_gidname(gid_t gid, guitem *item)
118 {
119 #ifndef HAVE_WIN32
120    struct group *grbuf;
121    P(mutex);
122    grbuf = getgrgid(gid);
123    if (grbuf != NULL && strcmp(grbuf->gr_name, "????????") != 0) {
124       item->name = bstrdup(grbuf->gr_name);
125    }
126    V(mutex);
127 #endif
128 }
129
130
131 char *guid_list::uid_to_name(uid_t uid, char *name, int maxlen)
132 {
133    guitem sitem, *item, *fitem;
134    sitem.uid = uid;
135    char buf[50];
136
137    item = (guitem *)uid_list->binary_search(&sitem, uid_compare);
138    Dmsg2(900, "uid=%d item=%p\n", uid, item);
139    if (!item) {
140       item = (guitem *)malloc(sizeof(guitem));
141       item->uid = uid;
142       item->name = NULL;
143       get_uidname(uid, item);
144       if (!item->name) {
145          item->name = bstrdup(edit_int64(uid, buf));
146          Dmsg2(900, "set uid=%d name=%s\n", uid, item->name);
147       }
148       fitem = (guitem *)uid_list->binary_insert(item, uid_compare);
149       if (fitem != item) {               /* item already there this shouldn't happen */
150          free(item);
151          item = fitem;   
152       }
153    }
154    bstrncpy(name, item->name, maxlen);
155    return name;
156 }
157
158 char *guid_list::gid_to_name(gid_t gid, char *name, int maxlen)
159 {
160    guitem sitem, *item, *fitem;
161    sitem.gid = gid;
162    char buf[50];
163
164    item = (guitem *)gid_list->binary_search(&sitem, gid_compare);
165    if (!item) {
166       item = (guitem *)malloc(sizeof(guitem));
167       item->gid = gid;
168       item->name = NULL;
169       get_gidname(gid, item);
170       if (!item->name) {
171          item->name = bstrdup(edit_int64(gid, buf));
172       }
173       fitem = (guitem *)gid_list->binary_insert(item, gid_compare);
174       if (fitem != item) {               /* item already there this shouldn't happen */
175          free(item);
176          item = fitem;   
177       }
178    }
179
180    bstrncpy(name, item->name, maxlen);
181    return name;
182 }
183
184 #ifdef TEST_PROGRAM
185
186 int main()
187 {
188    int i;
189    guid_list *list;
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)));
197    }
198     
199    free_guid_list(list);
200    sm_dump(false);
201
202    return 0;
203 }
204  
205 #endif