On Thu, 13 Sep 2018 10:47:35 -0600, "Todd C. Miller" wrote:
> Use the passwd/group cache functions in mtree(1) to avoid repeatedly
> looking up the same user/group. The passwd and group files are
> kept open too.
I sent out and older version of the diff that was missing some
includes in mtree.c. Below is the corrected version.
- todd
Index: usr.sbin/mtree/create.c
===================================================================
RCS file: /cvs/src/usr.sbin/mtree/create.c,v
retrieving revision 1.32
diff -u -p -u -r1.32 create.c
--- usr.sbin/mtree/create.c 16 Aug 2016 16:41:46 -0000 1.32
+++ usr.sbin/mtree/create.c 13 Sep 2018 19:41:59 -0000
@@ -127,11 +127,10 @@ cwalk(void)
static void
statf(int indent, FTSENT *p)
{
- struct group *gr;
- struct passwd *pw;
u_int32_t len, val;
int fd, offset;
- char *name, *escaped_name;
+ const char *name;
+ char *escaped_name;
size_t esc_len;
esc_len = p->fts_namelen * 4 + 1;
@@ -157,8 +156,9 @@ statf(int indent, FTSENT *p)
output(indent, &offset, "type=%s",
inotype(p->fts_statp->st_mode));
if (p->fts_statp->st_uid != uid) {
if (keys & F_UNAME) {
- if ((pw = getpwuid(p->fts_statp->st_uid)) != NULL) {
- output(indent, &offset, "uname=%s",
pw->pw_name);
+ name = user_from_uid(p->fts_statp->st_uid, 1);
+ if (name != NULL) {
+ output(indent, &offset, "uname=%s", name);
} else {
error("could not get uname for uid=%u",
p->fts_statp->st_uid);
@@ -169,8 +169,9 @@ statf(int indent, FTSENT *p)
}
if (p->fts_statp->st_gid != gid) {
if (keys & F_GNAME) {
- if ((gr = getgrgid(p->fts_statp->st_gid)) != NULL) {
- output(indent, &offset, "gname=%s",
gr->gr_name);
+ name = group_from_gid(p->fts_statp->st_gid, 1);
+ if (name != NULL) {
+ output(indent, &offset, "gname=%s", name);
} else {
error("could not get gname for gid=%u",
p->fts_statp->st_gid);
@@ -270,8 +271,6 @@ statd(FTS *t, FTSENT *parent, uid_t *pui
gid_t sgid;
uid_t suid;
mode_t smode;
- struct group *gr;
- struct passwd *pw;
gid_t savegid = *pgid;
uid_t saveuid = *puid;
mode_t savemode = *pmode;
@@ -281,6 +280,7 @@ statd(FTS *t, FTSENT *parent, uid_t *pui
gid_t g[MAXGID];
uid_t u[MAXUID];
mode_t m[MAXMODE];
+ const char *name;
static int first = 1;
if ((p = fts_children(t, 0)) == NULL) {
@@ -327,16 +327,16 @@ statd(FTS *t, FTSENT *parent, uid_t *pui
else
(void)printf("/set type=file");
if (keys & F_UNAME) {
- if ((pw = getpwuid(saveuid)) != NULL)
- (void)printf(" uname=%s", pw->pw_name);
+ if ((name = user_from_uid(saveuid, 1)) != NULL)
+ (void)printf(" uname=%s", name);
else
error("could not get uname for uid=%u",
saveuid);
}
if (keys & F_UID)
(void)printf(" uid=%u", saveuid);
if (keys & F_GNAME) {
- if ((gr = getgrgid(savegid)) != NULL)
- (void)printf(" gname=%s", gr->gr_name);
+ if ((name = group_from_gid(savegid, 1)) != NULL)
+ (void)printf(" gname=%s", name);
else
error("could not get gname for gid=%u",
savegid);
}
Index: usr.sbin/mtree/mtree.c
===================================================================
RCS file: /cvs/src/usr.sbin/mtree/mtree.c,v
retrieving revision 1.24
diff -u -p -u -r1.24 mtree.c
--- usr.sbin/mtree/mtree.c 20 Dec 2015 19:53:24 -0000 1.24
+++ usr.sbin/mtree/mtree.c 13 Sep 2018 20:13:21 -0000
@@ -37,6 +37,8 @@
#include <stdio.h>
#include <limits.h>
#include <fts.h>
+#include <grp.h>
+#include <pwd.h>
#include "mtree.h"
#include "extern.h"
@@ -154,6 +156,10 @@ main(int argc, char *argv[])
err(1, "pledge");
}
}
+
+ /* Keep passwd and group files open for faster lookups. */
+ setpassent(1);
+ setgroupent(1);
if (dir && chdir(dir))
error("%s: %s", dir, strerror(errno));
Index: usr.sbin/mtree/spec.c
===================================================================
RCS file: /cvs/src/usr.sbin/mtree/spec.c,v
retrieving revision 1.28
diff -u -p -u -r1.28 spec.c
--- usr.sbin/mtree/spec.c 16 Aug 2016 16:41:46 -0000 1.28
+++ usr.sbin/mtree/spec.c 13 Sep 2018 19:41:59 -0000
@@ -169,8 +169,6 @@ set(char *t, NODE *ip)
{
int type;
char *kw, *val = NULL;
- struct group *gr;
- struct passwd *pw;
void *m;
int value;
u_int32_t fset, fclr;
@@ -207,9 +205,8 @@ set(char *t, NODE *ip)
error("invalid gid %s", val);
break;
case F_GNAME:
- if ((gr = getgrnam(val)) == NULL)
+ if (gid_from_group(val, &ip->st_gid) == -1)
error("unknown group %s", val);
- ip->st_gid = gr->gr_gid;
break;
case F_IGN:
/* just set flag bit */
@@ -302,9 +299,8 @@ set(char *t, NODE *ip)
error("invalid uid %s", val);
break;
case F_UNAME:
- if ((pw = getpwnam(val)) == NULL)
+ if (uid_from_user(val, &ip->st_uid) == -1)
error("unknown user %s", val);
- ip->st_uid = pw->pw_uid;
break;
}
}