blob: 662eff584b4bff3aedd9a3fa0590c5e881305754 [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** encode.c - libtar code to encode 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>
16#include <pwd.h>
17#include <grp.h>
18#include <sys/types.h>
19
20#ifdef STDC_HEADERS
21# include <string.h>
22# include <stdlib.h>
23#endif
24
25
26/* magic, version, and checksum */
27void
28th_finish(TAR *t)
29{
30 if (t->options & TAR_GNU)
31 {
32 /* we're aiming for this result, but must do it in
33 * two calls to avoid FORTIFY segfaults on some Linux
34 * systems:
35 * strncpy(t->th_buf.magic, "ustar ", 8);
36 */
37 strncpy(t->th_buf.magic, "ustar ", 6);
38 strncpy(t->th_buf.version, " ", 2);
39 }
40 else
41 {
42 strncpy(t->th_buf.version, TVERSION, TVERSLEN);
43 strncpy(t->th_buf.magic, TMAGIC, TMAGLEN);
44 }
45
46 int_to_oct(th_crc_calc(t), t->th_buf.chksum, 8);
47}
48
49
50/* map a file mode to a typeflag */
51void
52th_set_type(TAR *t, mode_t mode)
53{
54 if (S_ISLNK(mode))
55 t->th_buf.typeflag = SYMTYPE;
56 if (S_ISREG(mode))
57 t->th_buf.typeflag = REGTYPE;
58 if (S_ISDIR(mode))
59 t->th_buf.typeflag = DIRTYPE;
60 if (S_ISCHR(mode))
61 t->th_buf.typeflag = CHRTYPE;
62 if (S_ISBLK(mode))
63 t->th_buf.typeflag = BLKTYPE;
64 if (S_ISFIFO(mode) || S_ISSOCK(mode))
65 t->th_buf.typeflag = FIFOTYPE;
66}
67
68
69/* encode file path */
70void
71th_set_path(TAR *t, char *pathname)
72{
73 char suffix[2] = "";
74 char *tmp;
75
76#ifdef DEBUG
77 printf("in th_set_path(th, pathname=\"%s\")\n", pathname);
78#endif
79
80 if (t->th_buf.gnu_longname != NULL)
81 free(t->th_buf.gnu_longname);
82 t->th_buf.gnu_longname = NULL;
83
84 if (pathname[strlen(pathname) - 1] != '/' && TH_ISDIR(t))
85 strcpy(suffix, "/");
86
87 if (strlen(pathname) > T_NAMELEN-1 && (t->options & TAR_GNU))
88 {
89 /* GNU-style long name */
90 t->th_buf.gnu_longname = strdup(pathname);
91 strncpy(t->th_buf.name, t->th_buf.gnu_longname, T_NAMELEN);
92 }
93 else if (strlen(pathname) > T_NAMELEN)
94 {
95 /* POSIX-style prefix field */
96 tmp = strchr(&(pathname[strlen(pathname) - T_NAMELEN - 1]), '/');
97 if (tmp == NULL)
98 {
99 printf("!!! '/' not found in \"%s\"\n", pathname);
100 return;
101 }
102 snprintf(t->th_buf.name, 100, "%s%s", &(tmp[1]), suffix);
103 snprintf(t->th_buf.prefix,
104 ((tmp - pathname + 1) <
105 155 ? (tmp - pathname + 1) : 155), "%s", pathname);
106 }
107 else
108 /* classic tar format */
109 snprintf(t->th_buf.name, 100, "%s%s", pathname, suffix);
110
111#ifdef DEBUG
112 puts("returning from th_set_path()...");
113#endif
114}
115
116
117/* encode link path */
118void
119th_set_link(TAR *t, char *linkname)
120{
121#ifdef DEBUG
122 printf("==> th_set_link(th, linkname=\"%s\")\n", linkname);
123#endif
124
125 if (strlen(linkname) > T_NAMELEN-1 && (t->options & TAR_GNU))
126 {
127 /* GNU longlink format */
128 t->th_buf.gnu_longlink = strdup(linkname);
129 strcpy(t->th_buf.linkname, "././@LongLink");
130 }
131 else
132 {
133 /* classic tar format */
134 strlcpy(t->th_buf.linkname, linkname,
135 sizeof(t->th_buf.linkname));
136 if (t->th_buf.gnu_longlink != NULL)
137 free(t->th_buf.gnu_longlink);
138 t->th_buf.gnu_longlink = NULL;
139 }
140}
141
142
143/* encode device info */
144void
145th_set_device(TAR *t, dev_t device)
146{
147#ifdef DEBUG
148 printf("th_set_device(): major = %d, minor = %d\n",
149 major(device), minor(device));
150#endif
151 int_to_oct(major(device), t->th_buf.devmajor, 8);
152 int_to_oct(minor(device), t->th_buf.devminor, 8);
153}
154
155
156/* encode user info */
157void
158th_set_user(TAR *t, uid_t uid)
159{
160 struct passwd *pw;
161
162 pw = getpwuid(uid);
163 if (pw != NULL)
164 strlcpy(t->th_buf.uname, pw->pw_name, sizeof(t->th_buf.uname));
165
166 int_to_oct(uid, t->th_buf.uid, 8);
167}
168
169
170/* encode group info */
171void
172th_set_group(TAR *t, gid_t gid)
173{
174 struct group *gr;
175
176 gr = getgrgid(gid);
177 if (gr != NULL)
178 strlcpy(t->th_buf.gname, gr->gr_name, sizeof(t->th_buf.gname));
179
180 int_to_oct(gid, t->th_buf.gid, 8);
181}
182
183
184/* encode file mode */
185void
186th_set_mode(TAR *t, mode_t fmode)
187{
188 if (S_ISSOCK(fmode))
189 {
190 fmode &= ~S_IFSOCK;
191 fmode |= S_IFIFO;
192 }
193 int_to_oct(fmode, (t)->th_buf.mode, 8);
194}
195
196
197void
198th_set_from_stat(TAR *t, struct stat *s)
199{
200 th_set_type(t, s->st_mode);
201 if (S_ISCHR(s->st_mode) || S_ISBLK(s->st_mode))
202 th_set_device(t, s->st_rdev);
203 th_set_user(t, s->st_uid);
204 th_set_group(t, s->st_gid);
205 th_set_mode(t, s->st_mode);
206 th_set_mtime(t, s->st_mtime);
207 if (S_ISREG(s->st_mode))
208 th_set_size(t, s->st_size);
209 else
210 th_set_size(t, 0);
211}
212
213