blob: 1a3d0ee56a5f7d0047f70ed99f634880cf2789d4 [file] [log] [blame]
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001/*
2** Copyright 1998-2003 University of Illinois Board of Trustees
3** Copyright 1998-2003 Mark D. Roth
4** All rights reserved.
5**
6** decode.c - libtar code to decode tar header blocks
7**
8** Mark D. Roth <roth@uiuc.edu>
9** Campus Information Technologies and Educational Services
10** University of Illinois at Urbana-Champaign
11*/
12
13#include <internal.h>
14
15#include <stdio.h>
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050016#include <stdlib.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050017#include <sys/param.h>
18#include <pwd.h>
19#include <grp.h>
20
21#ifdef STDC_HEADERS
22# include <string.h>
23#endif
24
25
26/* determine full path name */
27char *
28th_get_pathname(TAR *t)
29{
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050030 if (t->th_buf.gnu_longname)
bigbiff bigbiff9c754052013-01-09 09:09:08 -050031 return t->th_buf.gnu_longname;
bigbiff bigbiff9c754052013-01-09 09:09:08 -050032
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050033 /* allocate the th_pathname buffer if not already */
34 if (t->th_pathname == NULL)
bigbiff bigbiff9c754052013-01-09 09:09:08 -050035 {
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050036 t->th_pathname = malloc(MAXPATHLEN * sizeof(char));
37 if (t->th_pathname == NULL)
38 /* out of memory */
39 return NULL;
bigbiff bigbiff9c754052013-01-09 09:09:08 -050040 }
41
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050042 if (t->th_buf.prefix[0] == '\0')
43 {
44 snprintf(t->th_pathname, MAXPATHLEN, "%.100s", t->th_buf.name);
45 }
46 else
47 {
48 snprintf(t->th_pathname, MAXPATHLEN, "%.155s/%.100s",
49 t->th_buf.prefix, t->th_buf.name);
50 }
51
52 /* will be deallocated in tar_close() */
53 return t->th_pathname;
bigbiff bigbiff9c754052013-01-09 09:09:08 -050054}
55
56
57uid_t
58th_get_uid(TAR *t)
59{
60 int uid;
61 struct passwd *pw;
62
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050063 if (!(t->options & TAR_USE_NUMERIC_ID)) {
64 pw = getpwnam(t->th_buf.uname);
65 if (pw != NULL)
66 return pw->pw_uid;
67 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -050068
69 /* if the password entry doesn't exist */
70 sscanf(t->th_buf.uid, "%o", &uid);
71 return uid;
72}
73
74
75gid_t
76th_get_gid(TAR *t)
77{
78 int gid;
79 struct group *gr;
80
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050081 if (!(t->options & TAR_USE_NUMERIC_ID)) {
82 gr = getgrnam(t->th_buf.gname);
83 if (gr != NULL)
84 return gr->gr_gid;
85 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -050086
87 /* if the group entry doesn't exist */
88 sscanf(t->th_buf.gid, "%o", &gid);
89 return gid;
90}
91
92
93mode_t
94th_get_mode(TAR *t)
95{
96 mode_t mode;
97
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050098 mode = (mode_t)oct_to_int(t->th_buf.mode, sizeof(t->th_buf.mode));
bigbiff bigbiff9c754052013-01-09 09:09:08 -050099 if (! (mode & S_IFMT))
100 {
101 switch (t->th_buf.typeflag)
102 {
103 case SYMTYPE:
104 mode |= S_IFLNK;
105 break;
106 case CHRTYPE:
107 mode |= S_IFCHR;
108 break;
109 case BLKTYPE:
110 mode |= S_IFBLK;
111 break;
112 case DIRTYPE:
113 mode |= S_IFDIR;
114 break;
115 case FIFOTYPE:
116 mode |= S_IFIFO;
117 break;
118 case AREGTYPE:
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500119 if (t->th_buf.name[strnlen(t->th_buf.name, T_NAMELEN) - 1] == '/')
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500120 {
121 mode |= S_IFDIR;
122 break;
123 }
124 /* FALLTHROUGH */
125 case LNKTYPE:
126 case REGTYPE:
127 default:
128 mode |= S_IFREG;
129 }
130 }
131
132 return mode;
133}
134
135