blob: 1e679d81e4e4bd94fce50e6d71b07038b7862848 [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>
Ethan Yonkerfefe5912017-09-30 22:22:13 -050019#include <sys/sysmacros.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050020
21#ifdef STDC_HEADERS
22# include <string.h>
23# include <stdlib.h>
24#endif
25
26
27/* magic, version, and checksum */
28void
29th_finish(TAR *t)
30{
31 if (t->options & TAR_GNU)
32 {
33 /* we're aiming for this result, but must do it in
34 * two calls to avoid FORTIFY segfaults on some Linux
35 * systems:
36 * strncpy(t->th_buf.magic, "ustar ", 8);
37 */
38 strncpy(t->th_buf.magic, "ustar ", 6);
39 strncpy(t->th_buf.version, " ", 2);
40 }
41 else
42 {
43 strncpy(t->th_buf.version, TVERSION, TVERSLEN);
44 strncpy(t->th_buf.magic, TMAGIC, TMAGLEN);
45 }
46
47 int_to_oct(th_crc_calc(t), t->th_buf.chksum, 8);
48}
49
50
51/* map a file mode to a typeflag */
52void
53th_set_type(TAR *t, mode_t mode)
54{
55 if (S_ISLNK(mode))
56 t->th_buf.typeflag = SYMTYPE;
57 if (S_ISREG(mode))
58 t->th_buf.typeflag = REGTYPE;
59 if (S_ISDIR(mode))
60 t->th_buf.typeflag = DIRTYPE;
61 if (S_ISCHR(mode))
62 t->th_buf.typeflag = CHRTYPE;
63 if (S_ISBLK(mode))
64 t->th_buf.typeflag = BLKTYPE;
65 if (S_ISFIFO(mode) || S_ISSOCK(mode))
66 t->th_buf.typeflag = FIFOTYPE;
67}
68
69
70/* encode file path */
71void
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050072th_set_path(TAR *t, const char *pathname)
bigbiff bigbiff9c754052013-01-09 09:09:08 -050073{
74 char suffix[2] = "";
75 char *tmp;
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050076 size_t pathname_len = strlen(pathname);
bigbiff bigbiff9c754052013-01-09 09:09:08 -050077
78#ifdef DEBUG
79 printf("in th_set_path(th, pathname=\"%s\")\n", pathname);
80#endif
81
82 if (t->th_buf.gnu_longname != NULL)
83 free(t->th_buf.gnu_longname);
84 t->th_buf.gnu_longname = NULL;
85
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050086 /* old archive compatibility (not needed for gnu): add trailing / to directories */
87 if (pathname[pathname_len - 1] != '/' && TH_ISDIR(t))
bigbiff bigbiff9c754052013-01-09 09:09:08 -050088 strcpy(suffix, "/");
89
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050090 if (pathname_len >= T_NAMELEN && (t->options & TAR_GNU))
bigbiff bigbiff9c754052013-01-09 09:09:08 -050091 {
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050092 /* GNU-style long name (no file name length limit) */
bigbiff bigbiff9c754052013-01-09 09:09:08 -050093 t->th_buf.gnu_longname = strdup(pathname);
94 strncpy(t->th_buf.name, t->th_buf.gnu_longname, T_NAMELEN);
95 }
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050096 else if (pathname_len >= T_NAMELEN)
bigbiff bigbiff9c754052013-01-09 09:09:08 -050097 {
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050098 /* POSIX-style prefix field:
99 * The maximum length of a file name is limited to 256 characters,
100 * provided that the file name can be split at a directory separator
101 * in two parts. The first part being at most 155 bytes long and
102 * the second part being at most 100 bytes long. So, in most cases
103 * the maximum file name length will be shorter than 256 characters.
104 */
105 char tail_path[T_NAMELEN + 1];
106 tmp = strchr(&(pathname[pathname_len - T_NAMELEN]), '/');
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500107 if (tmp == NULL)
108 {
109 printf("!!! '/' not found in \"%s\"\n", pathname);
110 return;
111 }
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500112 snprintf(tail_path, T_NAMELEN + 1, "%s%s", &tmp[1], suffix);
113 strncpy(t->th_buf.name, tail_path, T_NAMELEN);
114
115 /*
116 * first part, max = 155 == sizeof(t->th_buf.prefix) , include NULL if it fits
117 * trailing '/' is added during decode: decode.c/th_get_pathname()
118 */
119 if (tmp - pathname >= 155) {
120 strncpy(t->th_buf.prefix, pathname, 155);
121 } else {
122 snprintf(t->th_buf.prefix, (tmp - pathname + 1), "%s", pathname);
123 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500124 }
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500125 else {
126 /* any short name for all formats, or classic tar format (99 chars max) */
127 snprintf(t->th_buf.name, T_NAMELEN, "%s%s", pathname, suffix);
128 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500129
130#ifdef DEBUG
131 puts("returning from th_set_path()...");
132#endif
133}
134
135
136/* encode link path */
137void
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500138th_set_link(TAR *t, const char *linkname)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500139{
140#ifdef DEBUG
141 printf("==> th_set_link(th, linkname=\"%s\")\n", linkname);
142#endif
143
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500144 if (strlen(linkname) >= T_NAMELEN && (t->options & TAR_GNU))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500145 {
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500146 /* --format=gnu: GNU-style long name (no file name length limit) */
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500147 t->th_buf.gnu_longlink = strdup(linkname);
148 strcpy(t->th_buf.linkname, "././@LongLink");
149 }
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500150 else if (strlen(linkname) >= T_NAMELEN)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500151 {
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500152 /* --format=ustar: 100 chars max limit for symbolic links */
153 strncpy(t->th_buf.linkname, linkname, T_NAMELEN);
154 if (t->th_buf.gnu_longlink != NULL)
155 free(t->th_buf.gnu_longlink);
156 t->th_buf.gnu_longlink = NULL;
157 } else {
158 /* all short links or v7 tar format: The maximum length of a symbolic link name is limited to 99 characters */
159 snprintf(t->th_buf.linkname, T_NAMELEN, "%s", linkname);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500160 if (t->th_buf.gnu_longlink != NULL)
161 free(t->th_buf.gnu_longlink);
162 t->th_buf.gnu_longlink = NULL;
163 }
164}
165
166
167/* encode device info */
168void
169th_set_device(TAR *t, dev_t device)
170{
171#ifdef DEBUG
172 printf("th_set_device(): major = %d, minor = %d\n",
173 major(device), minor(device));
174#endif
175 int_to_oct(major(device), t->th_buf.devmajor, 8);
176 int_to_oct(minor(device), t->th_buf.devminor, 8);
177}
178
179
180/* encode user info */
181void
182th_set_user(TAR *t, uid_t uid)
183{
184 struct passwd *pw;
185
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500186 if (!(t->options & TAR_USE_NUMERIC_ID)) {
187 pw = getpwuid(uid);
188 if (pw != NULL)
189 strlcpy(t->th_buf.uname, pw->pw_name, sizeof(t->th_buf.uname));
190 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500191
192 int_to_oct(uid, t->th_buf.uid, 8);
193}
194
195
196/* encode group info */
197void
198th_set_group(TAR *t, gid_t gid)
199{
200 struct group *gr;
201
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500202 if (!(t->options & TAR_USE_NUMERIC_ID)) {
203 gr = getgrgid(gid);
204 if (gr != NULL)
205 strlcpy(t->th_buf.gname, gr->gr_name, sizeof(t->th_buf.gname));
206 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500207
208 int_to_oct(gid, t->th_buf.gid, 8);
209}
210
211
212/* encode file mode */
213void
214th_set_mode(TAR *t, mode_t fmode)
215{
216 if (S_ISSOCK(fmode))
217 {
218 fmode &= ~S_IFSOCK;
219 fmode |= S_IFIFO;
220 }
221 int_to_oct(fmode, (t)->th_buf.mode, 8);
222}
223
224
225void
226th_set_from_stat(TAR *t, struct stat *s)
227{
228 th_set_type(t, s->st_mode);
229 if (S_ISCHR(s->st_mode) || S_ISBLK(s->st_mode))
230 th_set_device(t, s->st_rdev);
231 th_set_user(t, s->st_uid);
232 th_set_group(t, s->st_gid);
233 th_set_mode(t, s->st_mode);
234 th_set_mtime(t, s->st_mtime);
235 if (S_ISREG(s->st_mode))
236 th_set_size(t, s->st_size);
237 else
238 th_set_size(t, 0);
239}