9 #include <rpm/rpmlog.h>
10 #include <rpm/rpmstring.h>
15 /* just to put a marker in librpm.a */
16 const char * const RPMVERSION = VERSION;
18 unsigned int hashFunctionString(const char * string)
20 /* Jenkins One-at-a-time hash */
21 unsigned int hash = 0xe4721b68;
23 while (*string != '\0') {
35 /* unameToUid(), uidTouname() and the group variants are really poorly
36 implemented. They really ought to use hash tables. I just made the
37 guess that most files would be owned by root or the same person/group
38 who owned the last file. Those two values are cached, everything else
39 is looked up via getpw() and getgr() functions. If this performs
40 too poorly I'll have to implement it properly :-( */
42 int unameToUid(const char * thisUname, uid_t * uid)
44 static char * lastUname = NULL;
45 static size_t lastUnameLen = 0;
46 static size_t lastUnameAlloced;
48 struct passwd * pwent;
54 } else if (rstreq(thisUname, "root")) {
59 thisUnameLen = strlen(thisUname);
60 if (lastUname == NULL || thisUnameLen != lastUnameLen ||
61 !rstreq(thisUname, lastUname))
63 if (lastUnameAlloced < thisUnameLen + 1) {
64 lastUnameAlloced = thisUnameLen + 10;
65 lastUname = xrealloc(lastUname, lastUnameAlloced); /* XXX memory leak */
67 strcpy(lastUname, thisUname);
69 pwent = getpwnam(thisUname);
73 pwent = getpwnam(thisUname);
74 if (pwent == NULL) return -1;
77 lastUid = pwent->pw_uid;
85 int gnameToGid(const char * thisGname, gid_t * gid)
87 static char * lastGname = NULL;
88 static size_t lastGnameLen = 0;
89 static size_t lastGnameAlloced;
94 if (thisGname == NULL) {
97 } else if (rstreq(thisGname, "root")) {
102 thisGnameLen = strlen(thisGname);
103 if (lastGname == NULL || thisGnameLen != lastGnameLen ||
104 !rstreq(thisGname, lastGname))
106 if (lastGnameAlloced < thisGnameLen + 1) {
107 lastGnameAlloced = thisGnameLen + 10;
108 lastGname = xrealloc(lastGname, lastGnameAlloced); /* XXX memory leak */
110 strcpy(lastGname, thisGname);
112 grent = getgrnam(thisGname);
116 grent = getgrnam(thisGname);
118 /* XXX The filesystem package needs group/lock w/o getgrnam. */
119 if (rstreq(thisGname, "lock")) {
123 if (rstreq(thisGname, "mail")) {
130 lastGid = grent->gr_gid;
138 const char * uidToUname(uid_t uid)
140 static uid_t lastUid = (uid_t) -1;
141 static char * lastUname = NULL;
142 static size_t lastUnameLen = 0;
144 if (uid == (uid_t) -1) {
145 lastUid = (uid_t) -1;
147 } else if (uid == (uid_t) 0) {
149 } else if (uid == lastUid) {
152 struct passwd * pwent = getpwuid(uid);
155 if (pwent == NULL) return NULL;
158 len = strlen(pwent->pw_name);
159 if (lastUnameLen < len + 1) {
160 lastUnameLen = len + 20;
161 lastUname = xrealloc(lastUname, lastUnameLen);
163 strcpy(lastUname, pwent->pw_name);
169 const char * gidToGname(gid_t gid)
171 static gid_t lastGid = (gid_t) -1;
172 static char * lastGname = NULL;
173 static size_t lastGnameLen = 0;
175 if (gid == (gid_t) -1) {
176 lastGid = (gid_t) -1;
178 } else if (gid == (gid_t) 0) {
180 } else if (gid == lastGid) {
183 struct group * grent = getgrgid(gid);
186 if (grent == NULL) return NULL;
189 len = strlen(grent->gr_name);
190 if (lastGnameLen < len + 1) {
191 lastGnameLen = len + 20;
192 lastGname = xrealloc(lastGname, lastGnameLen);
194 strcpy(lastGname, grent->gr_name);