blob: c9371527bf4cd19a6b2168926f6111f4cf567d70 [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
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050071th_set_path(TAR *t, const char *pathname)
bigbiff bigbiff9c754052013-01-09 09:09:08 -050072{
73 char suffix[2] = "";
74 char *tmp;
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050075 size_t pathname_len = strlen(pathname);
bigbiff bigbiff9c754052013-01-09 09:09:08 -050076
77#ifdef DEBUG
78 printf("in th_set_path(th, pathname=\"%s\")\n", pathname);
79#endif
80
81 if (t->th_buf.gnu_longname != NULL)
82 free(t->th_buf.gnu_longname);
83 t->th_buf.gnu_longname = NULL;
84
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050085 /* old archive compatibility (not needed for gnu): add trailing / to directories */
86 if (pathname[pathname_len - 1] != '/' && TH_ISDIR(t))
bigbiff bigbiff9c754052013-01-09 09:09:08 -050087 strcpy(suffix, "/");
88
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050089 if (pathname_len >= T_NAMELEN && (t->options & TAR_GNU))
bigbiff bigbiff9c754052013-01-09 09:09:08 -050090 {
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050091 /* GNU-style long name (no file name length limit) */
bigbiff bigbiff9c754052013-01-09 09:09:08 -050092 t->th_buf.gnu_longname = strdup(pathname);
93 strncpy(t->th_buf.name, t->th_buf.gnu_longname, T_NAMELEN);
94 }
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050095 else if (pathname_len >= T_NAMELEN)
bigbiff bigbiff9c754052013-01-09 09:09:08 -050096 {
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -050097 /* POSIX-style prefix field:
98 * The maximum length of a file name is limited to 256 characters,
99 * provided that the file name can be split at a directory separator
100 * in two parts. The first part being at most 155 bytes long and
101 * the second part being at most 100 bytes long. So, in most cases
102 * the maximum file name length will be shorter than 256 characters.
103 */
104 char tail_path[T_NAMELEN + 1];
105 tmp = strchr(&(pathname[pathname_len - T_NAMELEN]), '/');
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500106 if (tmp == NULL)
107 {
108 printf("!!! '/' not found in \"%s\"\n", pathname);
109 return;
110 }
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500111 snprintf(tail_path, T_NAMELEN + 1, "%s%s", &tmp[1], suffix);
112 strncpy(t->th_buf.name, tail_path, T_NAMELEN);
113
114 /*
115 * first part, max = 155 == sizeof(t->th_buf.prefix) , include NULL if it fits
116 * trailing '/' is added during decode: decode.c/th_get_pathname()
117 */
118 if (tmp - pathname >= 155) {
119 strncpy(t->th_buf.prefix, pathname, 155);
120 } else {
121 snprintf(t->th_buf.prefix, (tmp - pathname + 1), "%s", pathname);
122 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500123 }
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500124 else {
125 /* any short name for all formats, or classic tar format (99 chars max) */
126 snprintf(t->th_buf.name, T_NAMELEN, "%s%s", pathname, suffix);
127 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500128
129#ifdef DEBUG
130 puts("returning from th_set_path()...");
131#endif
132}
133
134
135/* encode link path */
136void
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500137th_set_link(TAR *t, const char *linkname)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500138{
139#ifdef DEBUG
140 printf("==> th_set_link(th, linkname=\"%s\")\n", linkname);
141#endif
142
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500143 if (strlen(linkname) >= T_NAMELEN && (t->options & TAR_GNU))
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500144 {
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500145 /* --format=gnu: GNU-style long name (no file name length limit) */
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500146 t->th_buf.gnu_longlink = strdup(linkname);
147 strcpy(t->th_buf.linkname, "././@LongLink");
148 }
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500149 else if (strlen(linkname) >= T_NAMELEN)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500150 {
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500151 /* --format=ustar: 100 chars max limit for symbolic links */
152 strncpy(t->th_buf.linkname, linkname, T_NAMELEN);
153 if (t->th_buf.gnu_longlink != NULL)
154 free(t->th_buf.gnu_longlink);
155 t->th_buf.gnu_longlink = NULL;
156 } else {
157 /* all short links or v7 tar format: The maximum length of a symbolic link name is limited to 99 characters */
158 snprintf(t->th_buf.linkname, T_NAMELEN, "%s", linkname);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500159 if (t->th_buf.gnu_longlink != NULL)
160 free(t->th_buf.gnu_longlink);
161 t->th_buf.gnu_longlink = NULL;
162 }
163}
164
165
166/* encode device info */
167void
168th_set_device(TAR *t, dev_t device)
169{
170#ifdef DEBUG
171 printf("th_set_device(): major = %d, minor = %d\n",
172 major(device), minor(device));
173#endif
174 int_to_oct(major(device), t->th_buf.devmajor, 8);
175 int_to_oct(minor(device), t->th_buf.devminor, 8);
176}
177
178
179/* encode user info */
180void
181th_set_user(TAR *t, uid_t uid)
182{
183 struct passwd *pw;
184
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500185 if (!(t->options & TAR_USE_NUMERIC_ID)) {
186 pw = getpwuid(uid);
187 if (pw != NULL)
188 strlcpy(t->th_buf.uname, pw->pw_name, sizeof(t->th_buf.uname));
189 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500190
191 int_to_oct(uid, t->th_buf.uid, 8);
192}
193
194
195/* encode group info */
196void
197th_set_group(TAR *t, gid_t gid)
198{
199 struct group *gr;
200
James Christopher Adduono6f57f7c2016-03-01 16:01:53 -0500201 if (!(t->options & TAR_USE_NUMERIC_ID)) {
202 gr = getgrgid(gid);
203 if (gr != NULL)
204 strlcpy(t->th_buf.gname, gr->gr_name, sizeof(t->th_buf.gname));
205 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500206
207 int_to_oct(gid, t->th_buf.gid, 8);
208}
209
210
211/* encode file mode */
212void
213th_set_mode(TAR *t, mode_t fmode)
214{
215 if (S_ISSOCK(fmode))
216 {
217 fmode &= ~S_IFSOCK;
218 fmode |= S_IFIFO;
219 }
220 int_to_oct(fmode, (t)->th_buf.mode, 8);
221}
222
223
224void
225th_set_from_stat(TAR *t, struct stat *s)
226{
227 th_set_type(t, s->st_mode);
228 if (S_ISCHR(s->st_mode) || S_ISBLK(s->st_mode))
229 th_set_device(t, s->st_rdev);
230 th_set_user(t, s->st_uid);
231 th_set_group(t, s->st_gid);
232 th_set_mode(t, s->st_mode);
233 th_set_mtime(t, s->st_mtime);
234 if (S_ISREG(s->st_mode))
235 th_set_size(t, s->st_size);
236 else
237 th_set_size(t, 0);
238}