tar: support for tar --numeric-owner. By Natanael Copa.
authorDenis Vlasenko <vda.linux@googlemail.com>
Tue, 21 Apr 2009 00:52:21 +0000 (00:52 -0000)
committerDenis Vlasenko <vda.linux@googlemail.com>
Tue, 21 Apr 2009 00:52:21 +0000 (00:52 -0000)
function                                             old     new   delta
tar_longopts                                         221     237     +16
data_extract_all                                     692     705     +13
tar_main                                             690     702     +12

archival/libunarchive/data_extract_all.c
archival/tar.c
include/unarchive.h

index 8b1ee2a6ea29f0e37a3cdbcd00fc7934d556d286..a2dfcb9e1829e10dccd775ae57261c3781699df7 100644 (file)
@@ -114,22 +114,24 @@ void FAST_FUNC data_extract_all(archive_handle_t *archive_handle)
        }
 
        if (!(archive_handle->ah_flags & ARCHIVE_NOPRESERVE_OWN)) {
-#if ENABLE_FEATURE_TAR_UNAME_GNAME
-               uid_t uid = file_header->uid;
-               gid_t gid = file_header->gid;
+               if (ENABLE_FEATURE_TAR_UNAME_GNAME
+                && !(archive_handle->ah_flags & ARCHIVE_NUMERIC_OWNER)
+               ) {
+                       uid_t uid = file_header->uid;
+                       gid_t gid = file_header->gid;
 
-               if (file_header->uname) {
-                       struct passwd *pwd = getpwnam(file_header->uname);
-                       if (pwd) uid = pwd->pw_uid;
-               }
-               if (file_header->gname) {
-                       struct group *grp = getgrnam(file_header->gname);
-                       if (grp) gid = grp->gr_gid;
+                       if (file_header->uname) {
+                               struct passwd *pwd = getpwnam(file_header->uname);
+                               if (pwd) uid = pwd->pw_uid;
+                       }
+                       if (file_header->gname) {
+                               struct group *grp = getgrnam(file_header->gname);
+                               if (grp) gid = grp->gr_gid;
+                       }
+                       lchown(file_header->name, uid, gid);
+               } else {
+                       lchown(file_header->name, file_header->uid, file_header->gid);
                }
-               lchown(file_header->name, uid, gid);
-#else
-               lchown(file_header->name, file_header->uid, file_header->gid);
-#endif
        }
        if ((file_header->mode & S_IFMT) != S_IFLNK) {
                /* uclibc has no lchmod, glibc is even stranger -
index eeaf3586bf7ca0f1c5c42b33aa9176d559d18cf8..03d66a6929e9e8b2dc8ecca730c6039c0c162d11 100644 (file)
@@ -738,6 +738,7 @@ enum {
        USE_FEATURE_SEAMLESS_Z(   OPTBIT_COMPRESS    ,)
        OPTBIT_NOPRESERVE_OWN,
        OPTBIT_NOPRESERVE_PERM,
+       OPTBIT_NUMERIC_OWNER,
        OPT_TEST         = 1 << 0, // t
        OPT_EXTRACT      = 1 << 1, // x
        OPT_BASEDIR      = 1 << 2, // C
@@ -756,6 +757,7 @@ enum {
        OPT_COMPRESS     = USE_FEATURE_SEAMLESS_Z(   (1 << OPTBIT_COMPRESS    )) + 0, // Z
        OPT_NOPRESERVE_OWN  = 1 << OPTBIT_NOPRESERVE_OWN , // no-same-owner
        OPT_NOPRESERVE_PERM = 1 << OPTBIT_NOPRESERVE_PERM, // no-same-permissions
+       OPT_NUMERIC_OWNER = 1 << OPTBIT_NUMERIC_OWNER,
 };
 #if ENABLE_FEATURE_TAR_LONG_OPTIONS
 static const char tar_longopts[] ALIGN1 =
@@ -787,6 +789,7 @@ static const char tar_longopts[] ALIGN1 =
 # if ENABLE_FEATURE_SEAMLESS_Z
        "compress\0"            No_argument       "Z"
 # endif
+       "numeric-owner\0"       No_argument       "\xfc"
        "no-same-owner\0"       No_argument       "\xfd"
        "no-same-permissions\0" No_argument       "\xfe"
        /* --exclude takes next bit position in option mask, */
@@ -873,6 +876,9 @@ int tar_main(int argc UNUSED_PARAM, char **argv)
        if (opt & OPT_NOPRESERVE_PERM)
                tar_handle->ah_flags |= ARCHIVE_NOPRESERVE_PERM;
 
+       if (opt & OPT_NUMERIC_OWNER)
+               tar_handle->ah_flags |= ARCHIVE_NUMERIC_OWNER;
+
        if (opt & OPT_GZIP)
                get_header_ptr = get_header_tar_gz;
 
index beb962c8ff12cfca57a0277602d8beda493b2e49..9d4f1fa403d04831d6ac5320a3f644b4b7439e97 100644 (file)
@@ -11,6 +11,7 @@ PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN
 #define ARCHIVE_EXTRACT_NEWER           16
 #define ARCHIVE_NOPRESERVE_OWN          32
 #define ARCHIVE_NOPRESERVE_PERM         64
+#define ARCHIVE_NUMERIC_OWNER           128
 
 typedef struct file_header_t {
        char *name;