7 /* just to put a marker in librpm.a */
8 const char * const RPMVERSION = VERSION;
10 #include <rpm/rpmlog.h>
11 #include <rpm/rpmstring.h>
17 /* unameToUid(), uidTouname() and the group variants are really poorly
18 implemented. They really ought to use hash tables. I just made the
19 guess that most files would be owned by root or the same person/group
20 who owned the last file. Those two values are cached, everything else
21 is looked up via getpw() and getgr() functions. If this performs
22 too poorly I'll have to implement it properly :-( */
24 int unameToUid(const char * thisUname, uid_t * uid)
26 static char * lastUname = NULL;
27 static size_t lastUnameLen = 0;
28 static size_t lastUnameAlloced;
30 struct passwd * pwent;
36 } else if (rstreq(thisUname, "root")) {
41 thisUnameLen = strlen(thisUname);
42 if (lastUname == NULL || thisUnameLen != lastUnameLen ||
43 !rstreq(thisUname, lastUname))
45 if (lastUnameAlloced < thisUnameLen + 1) {
46 lastUnameAlloced = thisUnameLen + 10;
47 lastUname = xrealloc(lastUname, lastUnameAlloced); /* XXX memory leak */
49 strcpy(lastUname, thisUname);
51 pwent = getpwnam(thisUname);
55 pwent = getpwnam(thisUname);
56 if (pwent == NULL) return -1;
59 lastUid = pwent->pw_uid;
67 int gnameToGid(const char * thisGname, gid_t * gid)
69 static char * lastGname = NULL;
70 static size_t lastGnameLen = 0;
71 static size_t lastGnameAlloced;
76 if (thisGname == NULL) {
79 } else if (rstreq(thisGname, "root")) {
84 thisGnameLen = strlen(thisGname);
85 if (lastGname == NULL || thisGnameLen != lastGnameLen ||
86 !rstreq(thisGname, lastGname))
88 if (lastGnameAlloced < thisGnameLen + 1) {
89 lastGnameAlloced = thisGnameLen + 10;
90 lastGname = xrealloc(lastGname, lastGnameAlloced); /* XXX memory leak */
92 strcpy(lastGname, thisGname);
94 grent = getgrnam(thisGname);
98 grent = getgrnam(thisGname);
100 /* XXX The filesystem package needs group/lock w/o getgrnam. */
101 if (rstreq(thisGname, "lock")) {
105 if (rstreq(thisGname, "mail")) {
112 lastGid = grent->gr_gid;
120 const char * uidToUname(uid_t uid)
122 static uid_t lastUid = (uid_t) -1;
123 static char * lastUname = NULL;
124 static size_t lastUnameLen = 0;
126 if (uid == (uid_t) -1) {
127 lastUid = (uid_t) -1;
129 } else if (uid == (uid_t) 0) {
131 } else if (uid == lastUid) {
134 struct passwd * pwent = getpwuid(uid);
137 if (pwent == NULL) return NULL;
140 len = strlen(pwent->pw_name);
141 if (lastUnameLen < len + 1) {
142 lastUnameLen = len + 20;
143 lastUname = xrealloc(lastUname, lastUnameLen);
145 strcpy(lastUname, pwent->pw_name);
151 const char * gidToGname(gid_t gid)
153 static gid_t lastGid = (gid_t) -1;
154 static char * lastGname = NULL;
155 static size_t lastGnameLen = 0;
157 if (gid == (gid_t) -1) {
158 lastGid = (gid_t) -1;
160 } else if (gid == (gid_t) 0) {
162 } else if (gid == lastGid) {
165 struct group * grent = getgrgid(gid);
168 if (grent == NULL) return NULL;
171 len = strlen(grent->gr_name);
172 if (lastGnameLen < len + 1) {
173 lastGnameLen = len + 20;
174 lastGname = xrealloc(lastGname, lastGnameLen);
176 strcpy(lastGname, grent->gr_name);