>From 5362f8ac33f13908b6e3656f3a7e946da83481b8 Mon Sep 17 00:00:00 2001 From: Collin Funk Date: Tue, 6 Aug 2024 21:16:15 -0700 Subject: [PATCH 4/5] maint: Use idcache from Gnulib. * gnulib.modules: Add idcache. * src/Makefile.am (cpio_SOURCES): Remove idcache.c * src/copyin.c: Include idcache.h (long_format): Adjust code to Gnulib's version which may return a null pointer instead of uid/gid as a numeric string. * src/tar.c (write_out_tar_header): Likewise. * src/extern.h (getgroup, getuser, getuidbyname, getgidbyname): Remove declarations. * src/idcache.c: Remove file. --- gnulib.modules | 1 + src/Makefile.am | 1 - src/copyin.c | 17 +++-- src/extern.h | 6 -- src/idcache.c | 197 ------------------------------------------------ src/tar.c | 13 +++- 6 files changed, 21 insertions(+), 214 deletions(-) delete mode 100644 src/idcache.c diff --git a/gnulib.modules b/gnulib.modules index 40e8087..ee54d2f 100644 --- a/gnulib.modules +++ b/gnulib.modules @@ -17,6 +17,7 @@ gettext-h gettime gitlog-to-changelog hash +idcache inttostr inttypes lchown diff --git a/src/Makefile.am b/src/Makefile.am index b469aa5..5ee0e79 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -36,7 +36,6 @@ cpio_SOURCES = \ main.c\ tar.c\ util.c\ - idcache.c\ makepath.c\ userspec.c diff --git a/src/copyin.c b/src/copyin.c index 467d86d..3725cab 100644 --- a/src/copyin.c +++ b/src/copyin.c @@ -32,6 +32,7 @@ #endif #include #include +#include #ifndef HAVE_LCHOWN # define lchown(f,u,g) 0 @@ -875,6 +876,8 @@ void long_format (struct cpio_file_stat *file_hdr, char const *link_name) { char mbuf[12]; + char *user = !numeric_uid ? getuser (file_hdr->c_uid) : NULL; + char *group = !numeric_uid ? getgroup (file_hdr->c_gid) : NULL; time_t when; char *tbuf; struct timespec when_timespec; @@ -890,13 +893,15 @@ long_format (struct cpio_file_stat *file_hdr, char const *link_name) printf ("%s%3ju ", mbuf, (uintmax_t) file_hdr->c_nlink); - if (numeric_uid) - printf ("%-8ju %-8ju ", - (uintmax_t) file_hdr->c_uid, - (uintmax_t) file_hdr->c_gid); + if (user != NULL) + printf ("%-8.8s ", user); else - printf ("%-8.8s %-8.8s ", getuser (file_hdr->c_uid), - getgroup (file_hdr->c_gid)); + printf ("%-8ju ", (uintmax_t) file_hdr->c_uid); + + if (group != NULL) + printf ("%-8.8s ", group); + else + printf ("%-8ju ", (uintmax_t) file_hdr->c_gid); if ((file_hdr->c_mode & CP_IFMT) == CP_IFCHR || (file_hdr->c_mode & CP_IFMT) == CP_IFBLK) diff --git a/src/extern.h b/src/extern.h index 1cb99fc..0b7fb83 100644 --- a/src/extern.h +++ b/src/extern.h @@ -135,12 +135,6 @@ int link_to_name (char const *link_name, char const *link_target); /* dirname.c */ char *dirname (char *path); -/* idcache.c */ -char *getgroup (gid_t gid); -char *getuser (uid_t uid); -uid_t *getuidbyname (char *user); -gid_t *getgidbyname (char *group); - /* main.c */ void process_args (int argc, char *argv[]); void initialize_buffers (void); diff --git a/src/idcache.c b/src/idcache.c deleted file mode 100644 index 7e18799..0000000 --- a/src/idcache.c +++ /dev/null @@ -1,197 +0,0 @@ -/* idcache.c -- map user and group IDs, cached for speed - Copyright (C) 1985-2024 Free Software Foundation, Inc. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3, or (at your option) - any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public - License along with this program; if not, write to the Free - Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - Boston, MA 02110-1301 USA. */ - -#ifdef HAVE_CONFIG_H -#include -#endif - -#include -#include -#include - -#include - -#if defined(STDC_HEADERS) || defined(HAVE_STRING_H) -#include -#else -#include -#endif - -#include -#include "cpiohdr.h" -#include "extern.h" - -struct userid -{ - union - { - uid_t u; - gid_t g; - } id; - char *name; - struct userid *next; -}; - -static struct userid *user_alist; - -/* The members of this list have names not in the local passwd file. */ -static struct userid *nouser_alist; - -/* Translate UID to a login name or a stringified number, - with cache. */ - -char * -getuser (uid_t uid) -{ - register struct userid *tail; - struct passwd *pwent; - - for (tail = user_alist; tail; tail = tail->next) - if (tail->id.u == uid) - return tail->name; - - pwent = getpwuid (uid); - tail = (struct userid *) xmalloc (sizeof (struct userid)); - tail->id.u = uid; - if (pwent == 0) - { - char nbuf[UINTMAX_STRSIZE_BOUND]; - tail->name = xstrdup (umaxtostr (uid, nbuf)); - } - else - tail->name = xstrdup (pwent->pw_name); - - /* Add to the head of the list, so most recently used is first. */ - tail->next = user_alist; - user_alist = tail; - return tail->name; -} - -/* Translate USER to a UID, with cache. - Return NULL if there is no such user. - (We also cache which user names have no passwd entry, - so we don't keep looking them up.) */ - -uid_t * -getuidbyname (char *user) -{ - register struct userid *tail; - struct passwd *pwent; - - for (tail = user_alist; tail; tail = tail->next) - /* Avoid a function call for the most common case. */ - if (*tail->name == *user && !strcmp (tail->name, user)) - return &tail->id.u; - - for (tail = nouser_alist; tail; tail = tail->next) - /* Avoid a function call for the most common case. */ - if (*tail->name == *user && !strcmp (tail->name, user)) - return 0; - - pwent = getpwnam (user); - - tail = (struct userid *) xmalloc (sizeof (struct userid)); - tail->name = xstrdup (user); - - /* Add to the head of the list, so most recently used is first. */ - if (pwent) - { - tail->id.u = pwent->pw_uid; - tail->next = user_alist; - user_alist = tail; - return &tail->id.u; - } - - tail->next = nouser_alist; - nouser_alist = tail; - return 0; -} - -/* Use the same struct as for userids. */ -static struct userid *group_alist; -static struct userid *nogroup_alist; - -/* Translate GID to a group name or a stringified number, - with cache. */ - -char * -getgroup (gid_t gid) -{ - register struct userid *tail; - struct group *grent; - - for (tail = group_alist; tail; tail = tail->next) - if (tail->id.g == gid) - return tail->name; - - grent = getgrgid (gid); - tail = (struct userid *) xmalloc (sizeof (struct userid)); - tail->id.g = gid; - if (grent == 0) - { - char nbuf[UINTMAX_STRSIZE_BOUND]; - tail->name = xstrdup (umaxtostr (gid, nbuf)); - } - else - tail->name = xstrdup (grent->gr_name); - - /* Add to the head of the list, so most recently used is first. */ - tail->next = group_alist; - group_alist = tail; - return tail->name; -} - -/* Translate GROUP to a UID, with cache. - Return NULL if there is no such group. - (We also cache which group names have no group entry, - so we don't keep looking them up.) */ - -gid_t * -getgidbyname (char *group) -{ - register struct userid *tail; - struct group *grent; - - for (tail = group_alist; tail; tail = tail->next) - /* Avoid a function call for the most common case. */ - if (*tail->name == *group && !strcmp (tail->name, group)) - return &tail->id.g; - - for (tail = nogroup_alist; tail; tail = tail->next) - /* Avoid a function call for the most common case. */ - if (*tail->name == *group && !strcmp (tail->name, group)) - return 0; - - grent = getgrnam (group); - - tail = (struct userid *) xmalloc (sizeof (struct userid)); - tail->name = xstrdup (group); - - /* Add to the head of the list, so most recently used is first. */ - if (grent) - { - tail->id.g = grent->gr_gid; - tail->next = group_alist; - group_alist = tail; - return &tail->id.g; - } - - tail->next = nogroup_alist; - nogroup_alist = tail; - return 0; -} diff --git a/src/tar.c b/src/tar.c index 318cbbe..dc0672d 100644 --- a/src/tar.c +++ b/src/tar.c @@ -26,6 +26,7 @@ #include "dstring.h" #include "extern.h" #include +#include #include "tarhdr.h" /* Stash the tar linkname in static storage. */ @@ -208,16 +209,20 @@ write_out_tar_header (struct cpio_file_stat *file_hdr, int out_des) if (archive_format == arf_ustar) { char *name; + char id_buffer[UINTMAX_STRSIZE_BOUND]; strncpy (tar_hdr->magic, TMAGIC, TMAGLEN); strncpy (tar_hdr->version, TVERSION, TVERSLEN); name = getuser (file_hdr->c_uid); - if (name) - strcpy (tar_hdr->uname, name); + if (name == NULL) + name = umaxtostr (file_hdr->c_uid, id_buffer); + strcpy (tar_hdr->uname, name); + name = getgroup (file_hdr->c_gid); - if (name) - strcpy (tar_hdr->gname, name); + if (name == NULL) + name = umaxtostr (file_hdr->c_gid, id_buffer); + strcpy (tar_hdr->gname, name); TO_OCT (file_hdr, c_rdev_maj, 8, tar_hdr, devmajor); TO_OCT (file_hdr, c_rdev_min, 8, tar_hdr, devminor); -- 2.45.2