Print this page
5218 posix definition of NULL
correct unistd.h and iso/stddef_iso.h
update gate source affected
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/tar/tar.c
+++ new/usr/src/cmd/tar/tar.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
23 23 * Copyright 2012 Milan Jurik. All rights reserved.
24 24 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
25 25 */
26 26
27 27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 28 /* All Rights Reserved */
29 29
30 30 /* Copyright (c) 1987, 1988 Microsoft Corporation */
31 31 /* All Rights Reserved */
32 32
33 33 /*
34 34 * Portions of this source code were derived from Berkeley 4.3 BSD
35 35 * under license from the Regents of the University of California.
36 36 */
37 37
38 38 #include <unistd.h>
39 39 #include <sys/types.h>
40 40 #include <sys/param.h>
41 41 #include <sys/stat.h>
42 42 #include <sys/mkdev.h>
43 43 #include <sys/wait.h>
44 44 #include <dirent.h>
45 45 #include <errno.h>
46 46 #include <stdio.h>
47 47 #include <signal.h>
48 48 #include <ctype.h>
49 49 #include <locale.h>
50 50 #include <nl_types.h>
51 51 #include <langinfo.h>
52 52 #include <pwd.h>
53 53 #include <grp.h>
54 54 #include <fcntl.h>
55 55 #include <string.h>
56 56 #include <malloc.h>
57 57 #include <time.h>
58 58 #include <utime.h>
59 59 #include <stdlib.h>
60 60 #include <stdarg.h>
61 61 #include <widec.h>
62 62 #include <sys/mtio.h>
63 63 #include <sys/acl.h>
64 64 #include <strings.h>
65 65 #include <deflt.h>
66 66 #include <limits.h>
67 67 #include <iconv.h>
68 68 #include <assert.h>
69 69 #include <libgen.h>
70 70 #include <libintl.h>
71 71 #include <aclutils.h>
72 72 #include <libnvpair.h>
73 73 #include <archives.h>
74 74
75 75 #if defined(__SunOS_5_6) || defined(__SunOS_5_7)
76 76 extern int defcntl();
77 77 #endif
78 78 #if defined(_PC_SATTR_ENABLED)
79 79 #include <attr.h>
80 80 #include <libcmdutils.h>
81 81 #endif
82 82
83 83 /* Trusted Extensions */
84 84 #include <zone.h>
85 85 #include <tsol/label.h>
86 86 #include <sys/tsol/label_macro.h>
87 87
88 88 #include "getresponse.h"
89 89 /*
90 90 * Source compatibility
91 91 */
92 92
93 93 /*
94 94 * These constants come from archives.h and sys/fcntl.h
95 95 * and were introduced by the extended attributes project
96 96 * in Solaris 9.
97 97 */
98 98 #if !defined(O_XATTR)
99 99 #define AT_SYMLINK_NOFOLLOW 0x1000
100 100 #define AT_REMOVEDIR 0x1
101 101 #define AT_FDCWD 0xffd19553
102 102 #define _XATTR_HDRTYPE 'E'
103 103 static int attropen();
104 104 static int fstatat();
105 105 static int renameat();
106 106 static int unlinkat();
107 107 static int openat();
108 108 static int fchownat();
109 109 static int futimesat();
110 110 #endif
111 111
112 112 /*
113 113 * Compiling with -D_XPG4_2 gets this but produces other problems, so
114 114 * instead of including sys/time.h and compiling with -D_XPG4_2, I'm
115 115 * explicitly doing the declaration here.
116 116 */
117 117 int utimes(const char *path, const struct timeval timeval_ptr[]);
118 118
119 119 #ifndef MINSIZE
120 120 #define MINSIZE 250
121 121 #endif
122 122 #define DEF_FILE "/etc/default/tar"
123 123
124 124 #define min(a, b) ((a) < (b) ? (a) : (b))
125 125 #define max(a, b) ((a) > (b) ? (a) : (b))
126 126
127 127 /* -DDEBUG ONLY for debugging */
128 128 #ifdef DEBUG
129 129 #undef DEBUG
130 130 #define DEBUG(a, b, c)\
131 131 (void) fprintf(stderr, "DEBUG - "), (void) fprintf(stderr, a, b, c)
132 132 #endif
133 133
134 134 #define TBLOCK 512 /* tape block size--should be universal */
135 135
136 136 #ifdef BSIZE
137 137 #define SYS_BLOCK BSIZE /* from sys/param.h: secondary block size */
138 138 #else /* BSIZE */
139 139 #define SYS_BLOCK 512 /* default if no BSIZE in param.h */
140 140 #endif /* BSIZE */
141 141
142 142 #define NBLOCK 20
143 143 #define NAMSIZ 100
144 144 #define PRESIZ 155
145 145 #define MAXNAM 256
146 146 #define MODEMASK 0777777 /* file creation mode mask */
147 147 #define POSIXMODES 07777 /* mask for POSIX mode bits */
148 148 #define MAXEXT 9 /* reasonable max # extents for a file */
149 149 #define EXTMIN 50 /* min blks left on floppy to split a file */
150 150
151 151 /* max value dblock.dbuf.efsize can store */
152 152 #define TAR_EFSIZE_MAX 0777777777
153 153
154 154 /*
155 155 * Symbols which specify the values at which the use of the 'E' function
156 156 * modifier is required to properly store a file.
157 157 *
158 158 * TAR_OFFSET_MAX - the largest file size we can archive
159 159 * OCTAL7CHAR - the limit for ustar gid, uid, dev
160 160 */
161 161
162 162 #ifdef XHDR_DEBUG
163 163 /* tiny values which force the creation of extended header entries */
164 164 #define TAR_OFFSET_MAX 9
165 165 #define OCTAL7CHAR 2
166 166 #else
167 167 /* normal values */
168 168 #define TAR_OFFSET_MAX 077777777777ULL
169 169 #define OCTAL7CHAR 07777777
170 170 #endif
171 171
172 172 #define TBLOCKS(bytes) (((bytes) + TBLOCK - 1) / TBLOCK)
173 173 #define K(tblocks) ((tblocks+1)/2) /* tblocks to Kbytes for printing */
174 174
175 175 #define MAXLEV (PATH_MAX / 2)
176 176 #define LEV0 1
177 177 #define SYMLINK_LEV0 0
178 178
179 179 #define TRUE 1
180 180 #define FALSE 0
181 181
182 182 #define XATTR_FILE 1
183 183 #define NORMAL_FILE 0
184 184
185 185 #define PUT_AS_LINK 1
186 186 #define PUT_NOTAS_LINK 0
187 187
188 188 #ifndef VIEW_READONLY
189 189 #define VIEW_READONLY "SUNWattr_ro"
190 190 #endif
191 191
192 192 #ifndef VIEW_READWRITE
193 193 #define VIEW_READWRITE "SUNWattr_rw"
194 194 #endif
195 195
196 196 #if _FILE_OFFSET_BITS == 64
197 197 #define FMT_off_t "lld"
198 198 #define FMT_off_t_o "llo"
199 199 #define FMT_blkcnt_t "lld"
200 200 #else
201 201 #define FMT_off_t "ld"
202 202 #define FMT_off_t_o "lo"
203 203 #define FMT_blkcnt_t "ld"
204 204 #endif
205 205
206 206 /* ACL support */
207 207
208 208 static
209 209 struct sec_attr {
210 210 char attr_type;
211 211 char attr_len[7];
212 212 char attr_info[1];
213 213 } *attr;
214 214
215 215 #if defined(O_XATTR)
216 216 typedef enum {
217 217 ATTR_OK,
218 218 ATTR_SKIP,
219 219 ATTR_CHDIR_ERR,
220 220 ATTR_OPEN_ERR,
221 221 ATTR_XATTR_ERR,
222 222 ATTR_SATTR_ERR
223 223 } attr_status_t;
224 224 #endif
225 225
226 226 #if defined(O_XATTR)
227 227 typedef enum {
228 228 ARC_CREATE,
229 229 ARC_RESTORE
230 230 } arc_action_t;
231 231 #endif
232 232
233 233 typedef struct attr_data {
234 234 char *attr_parent;
235 235 char *attr_path;
236 236 int attr_parentfd;
237 237 int attr_rw_sysattr;
238 238 } attr_data_t;
239 239
240 240 /*
241 241 *
242 242 * Tar has been changed to support extended attributes.
243 243 *
244 244 * As part of this change tar now uses the new *at() syscalls
245 245 * such as openat, fchownat(), unlinkat()...
246 246 *
247 247 * This was done so that attributes can be handled with as few code changes
248 248 * as possible.
249 249 *
250 250 * What this means is that tar now opens the directory that a file or directory
251 251 * resides in and then performs *at() functions to manipulate the entry.
252 252 *
253 253 * For example a new file is now created like this:
254 254 *
255 255 * dfd = open(<some dir path>)
256 256 * fd = openat(dfd, <name>,....);
257 257 *
258 258 * or in the case of an extended attribute
259 259 *
260 260 * dfd = attropen(<pathname>, ".", ....)
261 261 *
262 262 * Once we have a directory file descriptor all of the *at() functions can
263 263 * be applied to it.
264 264 *
265 265 * unlinkat(dfd, <component name>,...)
266 266 * fchownat(dfd, <component name>,..)
267 267 *
268 268 * This works for both normal namespace files and extended attribute file
269 269 *
270 270 */
271 271
272 272 /*
273 273 *
274 274 * Extended attribute Format
275 275 *
276 276 * Extended attributes are stored in two pieces.
277 277 * 1. An attribute header which has information about
278 278 * what file the attribute is for and what the attribute
279 279 * is named.
280 280 * 2. The attribute record itself. Stored as a normal file type
281 281 * of entry.
282 282 * Both the header and attribute record have special modes/typeflags
283 283 * associated with them.
284 284 *
285 285 * The names of the header in the archive look like:
286 286 * /dev/null/attr.hdr
287 287 *
288 288 * The name of the attribute looks like:
289 289 * /dev/null/attr
290 290 *
291 291 * This is done so that an archiver that doesn't understand these formats
292 292 * can just dispose of the attribute records.
293 293 *
294 294 * The format is composed of a fixed size header followed
295 295 * by a variable sized xattr_buf. If the attribute is a hard link
296 296 * to another attribute then another xattr_buf section is included
297 297 * for the link.
298 298 *
299 299 * The xattr_buf is used to define the necessary "pathing" steps
300 300 * to get to the extended attribute. This is necessary to support
301 301 * a fully recursive attribute model where an attribute may itself
302 302 * have an attribute.
303 303 *
304 304 * The basic layout looks like this.
305 305 *
306 306 * --------------------------------
307 307 * | |
308 308 * | xattr_hdr |
309 309 * | |
310 310 * --------------------------------
311 311 * --------------------------------
312 312 * | |
313 313 * | xattr_buf |
314 314 * | |
315 315 * --------------------------------
316 316 * --------------------------------
317 317 * | |
318 318 * | (optional link info) |
319 319 * | |
320 320 * --------------------------------
321 321 * --------------------------------
322 322 * | |
323 323 * | attribute itself |
324 324 * | stored as normal tar |
325 325 * | or cpio data with |
326 326 * | special mode or |
327 327 * | typeflag |
328 328 * | |
329 329 * --------------------------------
330 330 *
331 331 */
332 332
333 333 /*
334 334 * xattrhead is a pointer to the xattr_hdr
335 335 *
336 336 * xattrp is a pointer to the xattr_buf structure
337 337 * which contains the "pathing" steps to get to attributes
338 338 *
339 339 * xattr_linkp is a pointer to another xattr_buf structure that is
340 340 * only used when an attribute is actually linked to another attribute
341 341 *
342 342 */
343 343
344 344 static struct xattr_hdr *xattrhead;
345 345 static struct xattr_buf *xattrp;
346 346 static struct xattr_buf *xattr_linkp; /* pointer to link info, if any */
347 347 static char *xattrapath; /* attribute name */
348 348 static char *xattr_linkaname; /* attribute attribute is linked to */
349 349 static char Hiddendir; /* are we processing hidden xattr dir */
350 350 static char xattrbadhead;
351 351
352 352 /* Was statically allocated tbuf[NBLOCK] */
353 353 static
354 354 union hblock {
355 355 char dummy[TBLOCK];
356 356 struct header {
357 357 char name[NAMSIZ]; /* If non-null prefix, path is */
358 358 /* <prefix>/<name>; otherwise */
359 359 /* <name> */
360 360 char mode[8];
361 361 char uid[8];
362 362 char gid[8];
363 363 char size[12]; /* size of this extent if file split */
364 364 char mtime[12];
365 365 char chksum[8];
366 366 char typeflag;
367 367 char linkname[NAMSIZ];
368 368 char magic[6];
369 369 char version[2];
370 370 char uname[32];
371 371 char gname[32];
372 372 char devmajor[8];
373 373 char devminor[8];
374 374 char prefix[PRESIZ]; /* Together with "name", the path of */
375 375 /* the file: <prefix>/<name> */
376 376 char extno; /* extent #, null if not split */
377 377 char extotal; /* total extents */
378 378 char efsize[10]; /* size of entire file */
379 379 } dbuf;
380 380 } dblock, *tbuf, xhdr_buf;
381 381
382 382 static
383 383 struct xtar_hdr {
384 384 uid_t x_uid, /* Uid of file */
385 385 x_gid; /* Gid of file */
386 386 major_t x_devmajor; /* Device major node */
387 387 minor_t x_devminor; /* Device minor node */
388 388 off_t x_filesz; /* Length of file */
389 389 char *x_uname, /* Pointer to name of user */
390 390 *x_gname, /* Pointer to gid of user */
391 391 *x_linkpath, /* Path for a hard/symbolic link */
392 392 *x_path; /* Path of file */
393 393 timestruc_t x_mtime; /* Seconds and nanoseconds */
394 394 } Xtarhdr;
395 395
396 396 static
397 397 struct gen_hdr {
398 398 ulong_t g_mode; /* Mode of file */
399 399 uid_t g_uid, /* Uid of file */
400 400 g_gid; /* Gid of file */
401 401 off_t g_filesz; /* Length of file */
402 402 time_t g_mtime; /* Modification time */
403 403 uint_t g_cksum; /* Checksum of file */
404 404 ulong_t g_devmajor, /* File system of file */
405 405 g_devminor; /* Major/minor of special files */
406 406 } Gen;
407 407
408 408 static
409 409 struct linkbuf {
410 410 ino_t inum;
411 411 dev_t devnum;
412 412 int count;
413 413 char pathname[MAXNAM+1]; /* added 1 for last NULL */
414 414 char attrname[MAXNAM+1];
415 415 struct linkbuf *nextp;
416 416 } *ihead;
417 417
418 418 /* see comments before build_table() */
419 419 #define TABLE_SIZE 512
420 420 typedef struct file_list {
421 421 char *name; /* Name of file to {in,ex}clude */
422 422 struct file_list *next; /* Linked list */
423 423 } file_list_t;
424 424 static file_list_t *exclude_tbl[TABLE_SIZE],
425 425 *include_tbl[TABLE_SIZE];
426 426
427 427 static int append_secattr(char **, int *, int, char *, char);
428 428 static void write_ancillary(union hblock *, char *, int, char);
429 429
430 430 static void add_file_to_table(file_list_t *table[], char *str);
431 431 static void assert_string(char *s, char *msg);
432 432 static int istape(int fd, int type);
433 433 static void backtape(void);
434 434 static void build_table(file_list_t *table[], char *file);
435 435 static int check_prefix(char **namep, char **dirp, char **compp);
436 436 static void closevol(void);
437 437 static void copy(void *dst, void *src);
438 438 static int convtoreg(off_t);
439 439 static void delete_target(int fd, char *comp, char *namep);
440 440 static void doDirTimes(char *name, timestruc_t modTime);
441 441 static void done(int n);
442 442 static void dorep(char *argv[]);
443 443 static void dotable(char *argv[]);
444 444 static void doxtract(char *argv[]);
445 445 static int tar_chdir(const char *path);
446 446 static int is_directory(char *name);
447 447 static int has_dot_dot(char *name);
448 448 static int is_absolute(char *name);
449 449 static char *make_relative_name(char *name, char **stripped_prefix);
450 450 static void fatal(char *format, ...);
451 451 static void vperror(int exit_status, char *fmt, ...);
452 452 static void flushtape(void);
453 453 static void getdir(void);
454 454 static void *getmem(size_t);
455 455 static void longt(struct stat *st, char aclchar);
456 456 static void load_info_from_xtarhdr(u_longlong_t flag, struct xtar_hdr *xhdrp);
457 457 static int makeDir(char *name);
458 458 static void mterr(char *operation, int i, int exitcode);
459 459 static void newvol(void);
460 460 static void passtape(void);
461 461 static void putempty(blkcnt_t n);
462 462 static int putfile(char *longname, char *shortname, char *parent,
463 463 attr_data_t *attrinfo, int filetype, int lev, int symlink_lev);
464 464 static void readtape(char *buffer);
465 465 static void seekdisk(blkcnt_t blocks);
466 466 static void setPathTimes(int dirfd, char *path, timestruc_t modTime);
467 467 static void setbytes_to_skip(struct stat *st, int err);
468 468 static void splitfile(char *longname, int ifd, char *name,
469 469 char *prefix, int filetype);
470 470 static void tomodes(struct stat *sp);
471 471 static void usage(void);
472 472 static int xblocks(int issysattr, off_t bytes, int ofile);
473 473 static int xsfile(int issysattr, int ofd);
474 474 static void resugname(int dirfd, char *name, int symflag);
475 475 static int bcheck(char *bstr);
476 476 static int checkdir(char *name);
477 477 static int checksum(union hblock *dblockp);
478 478 #ifdef EUC
479 479 static int checksum_signed(union hblock *dblockp);
480 480 #endif /* EUC */
481 481 static int checkupdate(char *arg);
482 482 static int checkw(char c, char *name);
483 483 static int cmp(char *b, char *s, int n);
484 484 static int defset(char *arch);
485 485 static int endtape(void);
486 486 static int is_in_table(file_list_t *table[], char *str);
487 487 static int notsame(void);
488 488 static int is_prefix(char *s1, char *s2);
489 489 static int response(void);
490 490 static int build_dblock(const char *, const char *, const char,
491 491 const int filetype, const struct stat *, const dev_t, const char *);
492 492 static unsigned int hash(char *str);
493 493
494 494 static blkcnt_t kcheck(char *kstr);
495 495 static off_t bsrch(char *s, int n, off_t l, off_t h);
496 496 static void onintr(int sig);
497 497 static void onquit(int sig);
498 498 static void onhup(int sig);
499 499 static uid_t getuidbyname(char *);
500 500 static gid_t getgidbyname(char *);
501 501 static char *getname(gid_t);
502 502 static char *getgroup(gid_t);
503 503 static int checkf(char *name, int mode, int howmuch);
504 504 static int writetbuf(char *buffer, int n);
505 505 static int wantit(char *argv[], char **namep, char **dirp, char **comp,
506 506 attr_data_t **attrinfo);
507 507 static void append_ext_attr(char *shortname, char **secinfo, int *len);
508 508 static int get_xdata(void);
509 509 static void gen_num(const char *keyword, const u_longlong_t number);
510 510 static void gen_date(const char *keyword, const timestruc_t time_value);
511 511 static void gen_string(const char *keyword, const char *value);
512 512 static void get_xtime(char *value, timestruc_t *xtime);
513 513 static int chk_path_build(char *name, char *longname, char *linkname,
514 514 char *prefix, char type, int filetype);
515 515 static int gen_utf8_names(const char *filename);
516 516 static int utf8_local(char *option, char **Xhdr_ptrptr, char *target,
517 517 const char *src, int max_val);
518 518 static int local_utf8(char **Xhdr_ptrptr, char *target, const char *src,
519 519 iconv_t iconv_cd, int xhdrflg, int max_val);
520 520 static int c_utf8(char *target, const char *source);
521 521 static int getstat(int dirfd, char *longname, char *shortname,
522 522 char *attrparent);
523 523 static void xattrs_put(char *, char *, char *, char *);
524 524 static void prepare_xattr(char **, char *, char *,
525 525 char, struct linkbuf *, int *);
526 526 static int put_link(char *name, char *longname, char *component,
527 527 char *longattrname, char *prefix, int filetype, char typeflag);
528 528 static int put_extra_attributes(char *longname, char *shortname,
529 529 char *longattrname, char *prefix, int filetype, char typeflag);
530 530 static int put_xattr_hdr(char *longname, char *shortname, char *longattrname,
531 531 char *prefix, int typeflag, int filetype, struct linkbuf *lp);
532 532 static int read_xattr_hdr(attr_data_t **attrinfo);
533 533
534 534 /* Trusted Extensions */
535 535 #define AUTO_ZONE "/zone"
536 536
537 537 static void extract_attr(char **file_ptr, struct sec_attr *);
538 538 static int check_ext_attr(char *filename);
539 539 static void rebuild_comp_path(char *str, char **namep);
540 540 static int rebuild_lk_comp_path(char *str, char **namep);
541 541
542 542 static void get_parent(char *path, char *dir);
543 543 static char *get_component(char *path);
544 544 static int retry_open_attr(int pdirfd, int cwd, char *dirp, char *pattr,
545 545 char *name, int oflag, mode_t mode);
546 546 static char *skipslashes(char *string, char *start);
547 547 static void chop_endslashes(char *path);
548 548 static pid_t compress_file(void);
549 549 static void compress_back(void);
550 550 static void decompress_file(void);
551 551 static pid_t uncompress_file(void);
552 552 static void *compress_malloc(size_t);
553 553 static void check_compression(void);
554 554 static char *bz_suffix(void);
555 555 static char *gz_suffix(void);
556 556 static char *xz_suffix(void);
557 557 static char *add_suffix();
558 558 static void wait_pid(pid_t);
559 559 static void verify_compress_opt(const char *t);
560 560 static void detect_compress(void);
561 561
562 562 static struct stat stbuf;
563 563
564 564 static char *myname;
565 565 static char *xtract_chdir = NULL;
566 566 static int checkflag = 0;
567 567 static int Xflag, Fflag, iflag, hflag, Bflag, Iflag;
568 568 static int rflag, xflag, vflag, tflag, mt, svmt, cflag, mflag, pflag;
569 569 static int uflag;
570 570 static int errflag;
571 571 static int oflag;
572 572 static int bflag, Aflag;
573 573 static int Pflag; /* POSIX conformant archive */
574 574 static int Eflag; /* Allow files greater than 8GB */
575 575 static int atflag; /* traverse extended attributes */
576 576 static int saflag; /* traverse extended sys attributes */
577 577 static int Dflag; /* Data change flag */
578 578 static int jflag; /* flag to use 'bzip2' */
579 579 static int zflag; /* flag to use 'gzip' */
580 580 static int Zflag; /* flag to use 'compress' */
581 581 static int Jflag; /* flag to use 'xz' */
582 582 static int aflag; /* flag to use autocompression */
583 583
584 584 /* Trusted Extensions */
585 585 static int Tflag; /* Trusted Extensions attr flags */
586 586 static int dir_flag; /* for attribute extract */
587 587 static int mld_flag; /* for attribute extract */
588 588 static char *orig_namep; /* original namep - unadorned */
589 589 static int rpath_flag; /* MLD real path is rebuilt */
590 590 static char real_path[MAXPATHLEN]; /* MLD real path */
591 591 static int lk_rpath_flag; /* linked to real path is rebuilt */
592 592 static char lk_real_path[MAXPATHLEN]; /* linked real path */
593 593 static bslabel_t bs_label; /* for attribute extract */
594 594 static bslabel_t admin_low;
595 595 static bslabel_t admin_high;
596 596 static int ignored_aprivs = 0;
597 597 static int ignored_fprivs = 0;
598 598 static int ignored_fattrs = 0;
599 599
600 600 static int term, chksum, wflag,
601 601 first = TRUE, defaults_used = FALSE, linkerrok;
602 602 static blkcnt_t recno;
603 603 static int freemem = 1;
604 604 static int nblock = NBLOCK;
605 605 static int Errflg = 0;
606 606 static int exitflag = 0;
607 607
608 608 static dev_t mt_dev; /* device containing output file */
609 609 static ino_t mt_ino; /* inode number of output file */
610 610 static int mt_devtype; /* dev type of archive, from stat structure */
611 611
612 612 static int update = 1; /* for `open' call */
613 613
614 614 static off_t low;
615 615 static off_t high;
616 616
617 617 static FILE *tfile;
618 618 static FILE *vfile = stdout;
619 619 static char *tmpdir;
620 620 static char *tmp_suffix = "/tarXXXXXX";
621 621 static char *tname;
622 622 static char archive[] = "archive0=";
623 623 static char *Xfile;
624 624 static char *usefile;
625 625 static char tfname[1024];
626 626
627 627 static int mulvol; /* multi-volume option selected */
628 628 static blkcnt_t blocklim; /* number of blocks to accept per volume */
629 629 static blkcnt_t tapepos; /* current block number to be written */
630 630 static int NotTape; /* true if tape is a disk */
631 631 static int dumping; /* true if writing a tape or other archive */
632 632 static int extno; /* number of extent: starts at 1 */
633 633 static int extotal; /* total extents in this file */
634 634 static off_t extsize; /* size of current extent during extraction */
635 635 static ushort_t Oumask = 0; /* old umask value */
636 636 static int is_posix; /* true if archive we're reading is POSIX-conformant */
637 637 static const char *magic_type = "ustar";
638 638 static size_t xrec_size = 8 * PATH_MAX; /* extended rec initial size */
639 639 static char *xrec_ptr;
640 640 static off_t xrec_offset = 0;
641 641 static int Xhdrflag;
642 642 static int charset_type = 0;
643 643
644 644 static u_longlong_t xhdr_flgs; /* Bits set determine which items */
645 645 /* need to be in extended header. */
646 646 #define _X_DEVMAJOR 0x1
647 647 #define _X_DEVMINOR 0x2
648 648 #define _X_GID 0x4
649 649 #define _X_GNAME 0x8
650 650 #define _X_LINKPATH 0x10
651 651 #define _X_PATH 0x20
652 652 #define _X_SIZE 0x40
653 653 #define _X_UID 0x80
654 654 #define _X_UNAME 0x100
655 655 #define _X_ATIME 0x200
656 656 #define _X_CTIME 0x400
657 657 #define _X_MTIME 0x800
658 658 #define _X_XHDR 0x1000 /* Bit flag that determines whether 'X' */
659 659 /* typeflag was followed by 'A' or non 'A' */
660 660 /* typeflag. */
661 661 #define _X_LAST 0x40000000
662 662
663 663 #define PID_MAX_DIGITS (10 * sizeof (pid_t) / 4)
664 664 #define TIME_MAX_DIGITS (10 * sizeof (time_t) / 4)
665 665 #define LONG_MAX_DIGITS (10 * sizeof (long) / 4)
666 666 #define ULONGLONG_MAX_DIGITS (10 * sizeof (u_longlong_t) / 4)
667 667 /*
668 668 * UTF_8 encoding requires more space than the current codeset equivalent.
669 669 * Currently a factor of 2-3 would suffice, but it is possible for a factor
670 670 * of 6 to be needed in the future, so for saftey, we use that here.
671 671 */
672 672 #define UTF_8_FACTOR 6
673 673
674 674 static u_longlong_t xhdr_count = 0;
675 675 static char xhdr_dirname[PRESIZ + 1];
676 676 static char pidchars[PID_MAX_DIGITS + 1];
677 677 static char *tchar = ""; /* null linkpath */
678 678
679 679 static char local_path[UTF_8_FACTOR * PATH_MAX + 1];
680 680 static char local_linkpath[UTF_8_FACTOR * PATH_MAX + 1];
681 681 static char local_gname[UTF_8_FACTOR * _POSIX_NAME_MAX + 1];
682 682 static char local_uname[UTF_8_FACTOR * _POSIX_NAME_MAX + 1];
683 683
684 684 /*
685 685 * The following mechanism is provided to allow us to debug tar in complicated
686 686 * situations, like when it is part of a pipe. The idea is that you compile
687 687 * with -DWAITAROUND defined, and then add the 'D' function modifier to the
688 688 * target tar invocation, eg. "tar cDf tarfile file". If stderr is available,
689 689 * it will tell you to which pid to attach the debugger; otherwise, use ps to
690 690 * find it. Attach to the process from the debugger, and, *PRESTO*, you are
691 691 * there!
692 692 *
693 693 * Simply assign "waitaround = 0" once you attach to the process, and then
694 694 * proceed from there as usual.
695 695 */
696 696
697 697 #ifdef WAITAROUND
698 698 int waitaround = 0; /* wait for rendezvous with the debugger */
699 699 #endif
700 700
701 701 #define BZIP "/usr/bin/bzip2"
702 702 #define GZIP "/usr/bin/gzip"
703 703 #define COMPRESS "/usr/bin/compress"
704 704 #define XZ "/usr/bin/xz"
705 705 #define BZCAT "/usr/bin/bzcat"
706 706 #define GZCAT "/usr/bin/gzcat"
707 707 #define ZCAT "/usr/bin/zcat"
708 708 #define XZCAT "/usr/bin/xzcat"
709 709 #define GSUF 8 /* number of valid 'gzip' sufixes */
710 710 #define BSUF 4 /* number of valid 'bzip2' sufixes */
711 711 #define XSUF 1 /* number of valid 'xz' suffixes */
712 712
713 713 static char *compress_opt; /* compression type */
714 714
715 715 static char *gsuffix[] = {".gz", "-gz", ".z", "-z", "_z", ".Z",
716 716 ".tgz", ".taz"};
717 717 static char *bsuffix[] = {".bz2", ".bz", ".tbz2", ".tbz"};
718 718 static char *xsuffix[] = {".xz"};
719 719 static char *suffix;
720 720
721 721
722 722 int
723 723 main(int argc, char *argv[])
724 724 {
725 725 char *cp;
726 726 char *tmpdirp;
727 727 pid_t thispid;
728 728 pid_t pid;
729 729 int wstat;
730 730
731 731 (void) setlocale(LC_ALL, "");
732 732 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
733 733 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
734 734 #endif
735 735 (void) textdomain(TEXT_DOMAIN);
736 736 if (argc < 2)
737 737 usage();
738 738
739 739 tfile = NULL;
740 740 if ((myname = strdup(argv[0])) == NULL) {
741 741 (void) fprintf(stderr, gettext(
742 742 "tar: cannot allocate program name\n"));
743 743 exit(1);
744 744 }
745 745
746 746 if (init_yes() < 0) {
747 747 (void) fprintf(stderr, gettext(ERR_MSG_INIT_YES),
748 748 strerror(errno));
749 749 exit(2);
750 750 }
751 751
752 752 /*
753 753 * For XPG4 compatibility, we must be able to accept the "--"
754 754 * argument normally recognized by getopt; it is used to delimit
755 755 * the end opt the options section, and so can only appear in
756 756 * the position of the first argument. We simply skip it.
757 757 */
758 758
759 759 if (strcmp(argv[1], "--") == 0) {
760 760 argv++;
761 761 argc--;
762 762 if (argc < 3)
763 763 usage();
764 764 }
765 765
766 766 argv[argc] = NULL;
767 767 argv++;
768 768
769 769 /*
770 770 * Set up default values.
771 771 * Search the operand string looking for the first digit or an 'f'.
772 772 * If you find a digit, use the 'archive#' entry in DEF_FILE.
773 773 * If 'f' is given, bypass looking in DEF_FILE altogether.
774 774 * If no digit or 'f' is given, still look in DEF_FILE but use '0'.
775 775 */
776 776 if ((usefile = getenv("TAPE")) == (char *)NULL) {
777 777 for (cp = *argv; *cp; ++cp)
778 778 if (isdigit(*cp) || *cp == 'f')
779 779 break;
780 780 if (*cp != 'f') {
781 781 archive[7] = (*cp)? *cp: '0';
782 782 if (!(defaults_used = defset(archive))) {
783 783 usefile = NULL;
784 784 nblock = 1;
785 785 blocklim = 0;
786 786 NotTape = 0;
787 787 }
788 788 }
789 789 }
790 790
791 791 for (cp = *argv++; *cp; cp++)
792 792 switch (*cp) {
793 793 #ifdef WAITAROUND
794 794 case 'D':
795 795 /* rendezvous with the debugger */
796 796 waitaround = 1;
797 797 break;
798 798 #endif
799 799 case 'f':
800 800 assert_string(*argv, gettext(
801 801 "tar: tarfile must be specified with 'f' "
802 802 "function modifier\n"));
803 803 usefile = *argv++;
804 804 break;
805 805 case 'F':
806 806 Fflag++;
807 807 break;
808 808 case 'c':
809 809 cflag++;
810 810 rflag++;
811 811 update = 1;
812 812 break;
813 813 #if defined(O_XATTR)
814 814 case '@':
815 815 atflag++;
816 816 break;
817 817 #endif /* O_XATTR */
818 818 #if defined(_PC_SATTR_ENABLED)
819 819 case '/':
820 820 saflag++;
821 821 break;
822 822 #endif /* _PC_SATTR_ENABLED */
823 823 case 'u':
824 824 uflag++; /* moved code after signals caught */
825 825 rflag++;
826 826 update = 2;
827 827 break;
828 828 case 'r':
829 829 rflag++;
830 830 update = 2;
831 831 break;
832 832 case 'v':
833 833 vflag++;
834 834 break;
835 835 case 'w':
836 836 wflag++;
837 837 break;
838 838 case 'x':
839 839 xflag++;
840 840 break;
841 841 case 'X':
842 842 assert_string(*argv, gettext(
843 843 "tar: exclude file must be specified with 'X' "
844 844 "function modifier\n"));
845 845 Xflag = 1;
846 846 Xfile = *argv++;
847 847 build_table(exclude_tbl, Xfile);
848 848 break;
849 849 case 't':
850 850 tflag++;
851 851 break;
852 852 case 'm':
853 853 mflag++;
854 854 break;
855 855 case 'p':
856 856 pflag++;
857 857 break;
858 858 case 'D':
859 859 Dflag++;
860 860 break;
861 861 case '-':
862 862 /* ignore this silently */
863 863 break;
864 864 case '0': /* numeric entries used only for defaults */
865 865 case '1':
866 866 case '2':
867 867 case '3':
868 868 case '4':
869 869 case '5':
870 870 case '6':
871 871 case '7':
872 872 break;
873 873 case 'b':
874 874 assert_string(*argv, gettext(
875 875 "tar: blocking factor must be specified "
876 876 "with 'b' function modifier\n"));
877 877 bflag++;
878 878 nblock = bcheck(*argv++);
879 879 break;
880 880 case 'n': /* not a magtape (instead of 'k') */
881 881 NotTape++; /* assume non-magtape */
882 882 break;
883 883 case 'l':
884 884 linkerrok++;
885 885 break;
886 886 case 'e':
887 887 errflag++;
888 888 case 'o':
889 889 oflag++;
890 890 break;
891 891 case 'h':
892 892 hflag++;
893 893 break;
894 894 case 'i':
895 895 iflag++;
896 896 break;
897 897 case 'B':
898 898 Bflag++;
899 899 break;
900 900 case 'P':
901 901 Pflag++;
902 902 break;
903 903 case 'E':
904 904 Eflag++;
905 905 Pflag++; /* Only POSIX archive made */
906 906 break;
907 907 case 'T':
908 908 Tflag++; /* Handle Trusted Extensions attrs */
909 909 pflag++; /* also set flag for ACL */
910 910 break;
911 911 case 'j': /* compession "bzip2" */
912 912 jflag = 1;
913 913 break;
914 914 case 'z': /* compression "gzip" */
915 915 zflag = 1;
916 916 break;
917 917 case 'Z': /* compression "compress" */
918 918 Zflag = 1;
919 919 break;
920 920 case 'J': /* compression "xz" */
921 921 Jflag = 1;
922 922 break;
923 923 case 'a':
924 924 aflag = 1; /* autocompression */
925 925 break;
926 926 default:
927 927 (void) fprintf(stderr, gettext(
928 928 "tar: %c: unknown function modifier\n"), *cp);
929 929 usage();
930 930 }
931 931
932 932 if (!rflag && !xflag && !tflag)
933 933 usage();
934 934 if ((rflag && xflag) || (xflag && tflag) || (rflag && tflag)) {
935 935 (void) fprintf(stderr, gettext(
936 936 "tar: specify only one of [ctxru].\n"));
937 937 usage();
938 938 }
939 939 if (cflag) {
940 940 if ((jflag + zflag + Zflag + Jflag + aflag) > 1) {
941 941 (void) fprintf(stderr, gettext(
942 942 "tar: specify only one of [ajJzZ] to "
943 943 "create a compressed file.\n"));
944 944 usage();
945 945 }
946 946 }
947 947 /* Trusted Extensions attribute handling */
948 948 if (Tflag && ((getzoneid() != GLOBAL_ZONEID) ||
949 949 !is_system_labeled())) {
950 950 (void) fprintf(stderr, gettext(
951 951 "tar: the 'T' option is only available with "
952 952 "Trusted Extensions\nand must be run from "
953 953 "the global zone.\n"));
954 954 usage();
955 955 }
956 956 if (cflag && *argv == NULL)
957 957 fatal(gettext("Missing filenames"));
958 958 if (usefile == NULL)
959 959 fatal(gettext("device argument required"));
960 960
961 961 /* alloc a buffer of the right size */
962 962 if ((tbuf = (union hblock *)
963 963 calloc(sizeof (union hblock) * nblock, sizeof (char))) ==
964 964 (union hblock *)NULL) {
965 965 (void) fprintf(stderr, gettext(
966 966 "tar: cannot allocate physio buffer\n"));
967 967 exit(1);
968 968 }
969 969
970 970 if ((xrec_ptr = malloc(xrec_size)) == NULL) {
971 971 (void) fprintf(stderr, gettext(
972 972 "tar: cannot allocate extended header buffer\n"));
973 973 exit(1);
974 974 }
975 975
976 976 #ifdef WAITAROUND
977 977 if (waitaround) {
978 978 (void) fprintf(stderr, gettext("Rendezvous with tar on pid"
979 979 " %d\n"), getpid());
980 980
981 981 while (waitaround) {
982 982 (void) sleep(10);
983 983 }
984 984 }
985 985 #endif
986 986
987 987 thispid = getpid();
988 988 (void) sprintf(pidchars, "%ld", thispid);
989 989 thispid = strlen(pidchars);
990 990
991 991 if ((tmpdirp = getenv("TMPDIR")) == (char *)NULL)
992 992 (void) strcpy(xhdr_dirname, "/tmp");
993 993 else {
994 994 /*
995 995 * Make sure that dir is no longer than what can
996 996 * fit in the prefix part of the header.
997 997 */
998 998 if (strlen(tmpdirp) > (size_t)(PRESIZ - thispid - 12)) {
999 999 (void) strcpy(xhdr_dirname, "/tmp");
1000 1000 if ((vflag > 0) && (Eflag > 0))
1001 1001 (void) fprintf(stderr, gettext(
1002 1002 "Ignoring TMPDIR\n"));
1003 1003 } else
1004 1004 (void) strcpy(xhdr_dirname, tmpdirp);
1005 1005 }
1006 1006 (void) strcat(xhdr_dirname, "/PaxHeaders.");
1007 1007 (void) strcat(xhdr_dirname, pidchars);
1008 1008
1009 1009 if (rflag) {
1010 1010 if (cflag && usefile != NULL) {
1011 1011 /* Set the compression type */
1012 1012 if (aflag)
1013 1013 detect_compress();
1014 1014
1015 1015 if (jflag) {
1016 1016 compress_opt = compress_malloc(strlen(BZIP)
1017 1017 + 1);
1018 1018 (void) strcpy(compress_opt, BZIP);
1019 1019 } else if (zflag) {
1020 1020 compress_opt = compress_malloc(strlen(GZIP)
1021 1021 + 1);
1022 1022 (void) strcpy(compress_opt, GZIP);
1023 1023 } else if (Zflag) {
1024 1024 compress_opt =
1025 1025 compress_malloc(strlen(COMPRESS) + 1);
1026 1026 (void) strcpy(compress_opt, COMPRESS);
1027 1027 } else if (Jflag) {
1028 1028 compress_opt = compress_malloc(strlen(XZ) + 1);
1029 1029 (void) strcpy(compress_opt, XZ);
1030 1030 }
1031 1031 } else {
1032 1032 /*
1033 1033 * Decompress if the file is compressed for
1034 1034 * an update or replace.
1035 1035 */
1036 1036 if (strcmp(usefile, "-") != 0) {
1037 1037 check_compression();
1038 1038 if (compress_opt != NULL) {
1039 1039 decompress_file();
1040 1040 }
1041 1041 }
1042 1042 }
1043 1043
1044 1044 if (cflag && tfile != NULL)
1045 1045 usage();
1046 1046 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1047 1047 (void) signal(SIGINT, onintr);
1048 1048 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1049 1049 (void) signal(SIGHUP, onhup);
1050 1050 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1051 1051 (void) signal(SIGQUIT, onquit);
1052 1052 if (uflag) {
1053 1053 int tnum;
1054 1054 struct stat sbuf;
1055 1055
1056 1056 tmpdir = getenv("TMPDIR");
1057 1057 /*
1058 1058 * If the name is invalid or this isn't a directory,
1059 1059 * or the directory is not writable, then reset to
1060 1060 * a default temporary directory.
1061 1061 */
1062 1062 if (tmpdir == NULL || *tmpdir == '\0' ||
1063 1063 (strlen(tmpdir) + strlen(tmp_suffix)) > PATH_MAX) {
1064 1064 tmpdir = "/tmp";
1065 1065 } else if (stat(tmpdir, &sbuf) < 0 ||
1066 1066 (sbuf.st_mode & S_IFMT) != S_IFDIR ||
1067 1067 (sbuf.st_mode & S_IWRITE) == 0) {
1068 1068 tmpdir = "/tmp";
1069 1069 }
1070 1070
1071 1071 if ((tname = calloc(1, strlen(tmpdir) +
1072 1072 strlen(tmp_suffix) + 1)) == NULL) {
1073 1073 vperror(1, gettext("tar: out of memory, "
1074 1074 "cannot create temporary file\n"));
1075 1075 }
1076 1076 (void) strcpy(tname, tmpdir);
1077 1077 (void) strcat(tname, tmp_suffix);
1078 1078
1079 1079 if ((tnum = mkstemp(tname)) == -1)
1080 1080 vperror(1, "%s", tname);
1081 1081 if ((tfile = fdopen(tnum, "w")) == NULL)
1082 1082 vperror(1, "%s", tname);
1083 1083 }
1084 1084 if (strcmp(usefile, "-") == 0) {
1085 1085 if (cflag == 0)
1086 1086 fatal(gettext(
1087 1087 "can only create standard output archives."));
1088 1088 vfile = stderr;
1089 1089 mt = dup(1);
1090 1090 ++bflag;
1091 1091 } else {
1092 1092 if (cflag)
1093 1093 mt = open(usefile,
1094 1094 O_RDWR|O_CREAT|O_TRUNC, 0666);
1095 1095 else
1096 1096 mt = open(usefile, O_RDWR);
1097 1097
1098 1098 if (mt < 0) {
1099 1099 if (cflag == 0 || (mt = creat(usefile, 0666))
1100 1100 < 0)
1101 1101 vperror(1, "%s", usefile);
1102 1102 }
1103 1103 }
1104 1104 /* Get inode and device number of output file */
1105 1105 (void) fstat(mt, &stbuf);
1106 1106 mt_ino = stbuf.st_ino;
1107 1107 mt_dev = stbuf.st_dev;
1108 1108 mt_devtype = stbuf.st_mode & S_IFMT;
1109 1109 NotTape = !istape(mt, mt_devtype);
1110 1110
1111 1111 if (rflag && !cflag && (mt_devtype == S_IFIFO))
1112 1112 fatal(gettext("cannot append to pipe or FIFO."));
1113 1113
1114 1114 if (Aflag && vflag)
1115 1115 (void) printf(
1116 1116 gettext("Suppressing absolute pathnames\n"));
1117 1117 if (cflag && compress_opt != NULL) {
1118 1118 pid = compress_file();
1119 1119 wait_pid(pid);
1120 1120 }
1121 1121 dorep(argv);
1122 1122 if (rflag && !cflag && (compress_opt != NULL))
1123 1123 compress_back();
1124 1124 } else if (xflag || tflag) {
1125 1125 /*
1126 1126 * for each argument, check to see if there is a "-I file" pair.
1127 1127 * if so, move the 3rd argument into "-I"'s place, build_table()
1128 1128 * using "file"'s name and increment argc one (the second
1129 1129 * increment appears in the for loop) which removes the two
1130 1130 * args "-I" and "file" from the argument vector.
1131 1131 */
1132 1132 for (argc = 0; argv[argc]; argc++) {
1133 1133 if (strcmp(argv[argc], "-I") == 0) {
1134 1134 if (!argv[argc+1]) {
1135 1135 (void) fprintf(stderr, gettext(
1136 1136 "tar: missing argument for -I flag\n"));
1137 1137 done(2);
1138 1138 } else {
1139 1139 Iflag = 1;
1140 1140 argv[argc] = argv[argc+2];
1141 1141 build_table(include_tbl, argv[++argc]);
1142 1142 }
1143 1143 } else if (strcmp(argv[argc], "-C") == 0) {
1144 1144 if (!argv[argc+1]) {
1145 1145 (void) fprintf(stderr, gettext("tar: "
1146 1146 "missing argument for -C flag\n"));
1147 1147 done(2);
1148 1148 } else if (xtract_chdir != NULL) {
1149 1149 (void) fprintf(stderr, gettext("tar: "
1150 1150 "extract should have only one -C "
1151 1151 "flag\n"));
1152 1152 done(2);
1153 1153 } else {
1154 1154 argv[argc] = argv[argc+2];
1155 1155 xtract_chdir = argv[++argc];
1156 1156 }
1157 1157 }
1158 1158 }
1159 1159 if (strcmp(usefile, "-") == 0) {
1160 1160 mt = dup(0);
1161 1161 ++bflag;
1162 1162 /* try to recover from short reads when reading stdin */
1163 1163 ++Bflag;
1164 1164 } else if ((mt = open(usefile, 0)) < 0)
1165 1165 vperror(1, "%s", usefile);
1166 1166
1167 1167 /* Decompress if the file is compressed */
1168 1168
1169 1169 if (strcmp(usefile, "-") != 0) {
1170 1170 check_compression();
1171 1171 if (compress_opt != NULL) {
1172 1172 pid = uncompress_file();
1173 1173 wait_pid(pid);
1174 1174 }
1175 1175 }
1176 1176 if (xflag) {
1177 1177 if (xtract_chdir != NULL) {
1178 1178 if (tar_chdir(xtract_chdir) < 0) {
1179 1179 vperror(1, gettext("can't change "
1180 1180 "directories to %s"), xtract_chdir);
1181 1181 }
1182 1182 }
1183 1183 if (Aflag && vflag)
1184 1184 (void) printf(gettext(
1185 1185 "Suppressing absolute pathnames.\n"));
1186 1186
1187 1187 doxtract(argv);
1188 1188 } else if (tflag)
1189 1189 dotable(argv);
1190 1190 }
1191 1191 else
1192 1192 usage();
1193 1193
1194 1194 done(Errflg);
1195 1195
1196 1196 /* Not reached: keep compiler quiet */
1197 1197 return (1);
1198 1198 }
1199 1199
1200 1200 static void
1201 1201 usage(void)
1202 1202 {
1203 1203 (void) fprintf(stderr, gettext(
1204 1204 #if defined(O_XATTR)
1205 1205 #if defined(_PC_SATTR_ENABLED)
1206 1206 "Usage: tar {c|r|t|u|x}[BDeEFhilmnopPTvw@/[0-7]][bf][X...] "
1207 1207 #else
1208 1208 "Usage: tar {c|r|t|u|x}[BDeEFhilmnopPTvw@[0-7]][bf][X...] "
1209 1209 #endif /* _PC_SATTR_ENABLED */
1210 1210 #else
1211 1211 "Usage: tar {c|r|t|u|x}[BDeEFhilmnopPTvw[0-7]][bf][X...] "
1212 1212 #endif /* O_XATTR */
1213 1213 "[j|J|z|Z] "
1214 1214 "[blocksize] [tarfile] [size] [exclude-file...] "
1215 1215 "{file | -I include-file | -C directory file}...\n"));
1216 1216 done(1);
1217 1217 }
1218 1218
1219 1219 /*
1220 1220 * dorep - do "replacements"
1221 1221 *
1222 1222 * Dorep is responsible for creating ('c'), appending ('r')
1223 1223 * and updating ('u');
1224 1224 */
1225 1225
1226 1226 static void
1227 1227 dorep(char *argv[])
1228 1228 {
1229 1229 char *cp, *cp2, *p;
1230 1230 char wdir[PATH_MAX+2], tempdir[PATH_MAX+2], *parent;
1231 1231 char file[PATH_MAX*2], origdir[PATH_MAX+1];
1232 1232 FILE *fp = (FILE *)NULL;
1233 1233 int archtype;
1234 1234 int ret;
1235 1235
1236 1236
1237 1237 if (!cflag) {
1238 1238 xhdr_flgs = 0;
1239 1239 getdir(); /* read header for next file */
1240 1240 if (Xhdrflag > 0) {
1241 1241 if (!Eflag)
1242 1242 fatal(gettext("Archive contains extended"
1243 1243 " header. -E flag required.\n"));
1244 1244 ret = get_xdata(); /* Get extended header items */
1245 1245 /* and regular header */
1246 1246 } else {
1247 1247 if (Eflag)
1248 1248 fatal(gettext("Archive contains no extended"
1249 1249 " header. -E flag not allowed.\n"));
1250 1250 }
1251 1251 while (!endtape()) { /* changed from a do while */
1252 1252 setbytes_to_skip(&stbuf, ret);
1253 1253 passtape(); /* skip the file data */
1254 1254 if (term)
1255 1255 done(Errflg); /* received signal to stop */
1256 1256 xhdr_flgs = 0;
1257 1257 getdir();
1258 1258 if (Xhdrflag > 0)
1259 1259 ret = get_xdata();
1260 1260 }
1261 1261 if (ret == 0) {
1262 1262 if ((dblock.dbuf.typeflag != 'A') &&
1263 1263 (xhdr_flgs != 0)) {
1264 1264 load_info_from_xtarhdr(xhdr_flgs,
1265 1265 &Xtarhdr);
1266 1266 xhdr_flgs |= _X_XHDR;
1267 1267 }
1268 1268 }
1269 1269 backtape(); /* was called by endtape */
1270 1270 if (tfile != NULL) {
1271 1271 /*
1272 1272 * Buffer size is calculated to be the size of the
1273 1273 * tmpdir string, plus 6 times the size of the tname
1274 1274 * string, plus a value that is known to be greater
1275 1275 * than the command pipeline string.
1276 1276 */
1277 1277 int buflen = strlen(tmpdir) + (6 * strlen(tname)) + 100;
1278 1278 char *buf;
1279 1279
1280 1280 if ((buf = (char *)calloc(1, buflen)) == NULL) {
1281 1281 vperror(1, gettext("tar: out of memory, "
1282 1282 "cannot create sort command file\n"));
1283 1283 }
1284 1284
1285 1285 (void) snprintf(buf, buflen, "env 'TMPDIR=%s' "
1286 1286 "sort +0 -1 +1nr %s -o %s; awk '$1 "
1287 1287 "!= prev {print; prev=$1}' %s >%sX;mv %sX %s",
1288 1288 tmpdir, tname, tname, tname, tname, tname, tname);
1289 1289 (void) fflush(tfile);
1290 1290 (void) system(buf);
1291 1291 free(buf);
1292 1292 (void) freopen(tname, "r", tfile);
1293 1293 (void) fstat(fileno(tfile), &stbuf);
1294 1294 high = stbuf.st_size;
1295 1295 }
1296 1296 }
1297 1297
1298 1298 dumping = 1;
1299 1299 if (mulvol) { /* SP-1 */
1300 1300 if (nblock && (blocklim%nblock) != 0)
1301 1301 fatal(gettext(
1302 1302 "Volume size not a multiple of block size."));
1303 1303 blocklim -= 2; /* for trailer records */
1304 1304 if (vflag)
1305 1305 (void) fprintf(vfile, gettext("Volume ends at %"
1306 1306 FMT_blkcnt_t "K, blocking factor = %dK\n"),
1307 1307 K((blocklim - 1)), K(nblock));
1308 1308 }
1309 1309
1310 1310 /*
1311 1311 * Save the original directory before it gets
1312 1312 * changed.
1313 1313 */
1314 1314 if (getcwd(origdir, (PATH_MAX+1)) == NULL) {
1315 1315 vperror(0, gettext("A parent directory cannot be read"));
1316 1316 exit(1);
1317 1317 }
1318 1318
1319 1319 (void) strcpy(wdir, origdir);
1320 1320
1321 1321 while ((*argv || fp) && !term) {
1322 1322 if (fp || (strcmp(*argv, "-I") == 0)) {
1323 1323 if (fp == NULL) {
1324 1324 if (*++argv == NULL)
1325 1325 fatal(gettext(
1326 1326 "missing file name for -I flag."));
1327 1327 else if ((fp = fopen(*argv++, "r")) == NULL)
1328 1328 vperror(0, "%s", argv[-1]);
1329 1329 continue;
1330 1330 } else if ((fgets(file, PATH_MAX-1, fp)) == NULL) {
1331 1331 (void) fclose(fp);
1332 1332 fp = NULL;
1333 1333 continue;
1334 1334 } else {
1335 1335 cp = cp2 = file;
1336 1336 if ((p = strchr(cp2, '\n')))
1337 1337 *p = 0;
1338 1338 }
1339 1339 } else if ((strcmp(*argv, "-C") == 0) && argv[1]) {
1340 1340 if (tar_chdir(*++argv) < 0)
1341 1341 vperror(0, gettext(
1342 1342 "can't change directories to %s"), *argv);
1343 1343 else
1344 1344 (void) getcwd(wdir, (sizeof (wdir)));
1345 1345 argv++;
1346 1346 continue;
1347 1347 } else
1348 1348 cp = cp2 = strcpy(file, *argv++);
1349 1349
1350 1350 /*
1351 1351 * point cp2 to the last '/' in file, but not
1352 1352 * to a trailing '/'
1353 1353 */
1354 1354 for (; *cp; cp++) {
1355 1355 if (*cp == '/') {
1356 1356 while (*(cp+1) == '/') {
1357 1357 ++cp;
1358 1358 }
1359 1359 if (*(cp+1) != '\0') {
1360 1360 /* not trailing slash */
1361 1361 cp2 = cp;
1362 1362 }
1363 1363 }
1364 1364 }
1365 1365 if (cp2 != file) {
1366 1366 *cp2 = '\0';
1367 1367 if (tar_chdir(file) < 0) {
1368 1368 vperror(0, gettext(
1369 1369 "can't change directories to %s"), file);
1370 1370 continue;
1371 1371 }
1372 1372 *cp2 = '/';
1373 1373 cp2++;
1374 1374 }
1375 1375
1376 1376 parent = getcwd(tempdir, (sizeof (tempdir)));
1377 1377
1378 1378 archtype = putfile(file, cp2, parent, NULL, NORMAL_FILE,
1379 1379 LEV0, SYMLINK_LEV0);
1380 1380
1381 1381 #if defined(O_XATTR)
1382 1382 if (!exitflag) {
1383 1383 if ((atflag || saflag) &&
1384 1384 (archtype == PUT_NOTAS_LINK)) {
1385 1385 xattrs_put(file, cp2, parent, NULL);
1386 1386 }
1387 1387 }
1388 1388 #endif
1389 1389
1390 1390 if (tar_chdir(origdir) < 0)
1391 1391 vperror(0, gettext("cannot change back?: %s"), origdir);
1392 1392
1393 1393 if (exitflag) {
1394 1394 /*
1395 1395 * If e function modifier has been specified
1396 1396 * write the files (that are listed before the
1397 1397 * file causing the error) to tape. exitflag is
1398 1398 * used because only some of the error conditions
1399 1399 * in putfile() recognize the e function modifier.
1400 1400 */
1401 1401 break;
1402 1402 }
1403 1403 }
1404 1404
1405 1405 putempty((blkcnt_t)2);
1406 1406 flushtape();
1407 1407 closevol(); /* SP-1 */
1408 1408 if (linkerrok == 1)
1409 1409 for (; ihead != NULL; ihead = ihead->nextp) {
1410 1410 if (ihead->count == 0)
1411 1411 continue;
1412 1412 (void) fprintf(stderr, gettext(
1413 1413 "tar: missing links to %s\n"), ihead->pathname);
1414 1414 if (errflag)
1415 1415 done(1);
1416 1416 else
1417 1417 Errflg = 1;
1418 1418 }
1419 1419 }
1420 1420
1421 1421
1422 1422 /*
1423 1423 * endtape - check for tape at end
1424 1424 *
1425 1425 * endtape checks the entry in dblock.dbuf to see if its the
1426 1426 * special EOT entry. Endtape is usually called after getdir().
1427 1427 *
1428 1428 * endtape used to call backtape; it no longer does, he who
1429 1429 * wants it backed up must call backtape himself
1430 1430 * RETURNS: 0 if not EOT, tape position unaffected
1431 1431 * 1 if EOT, tape position unaffected
1432 1432 */
1433 1433
1434 1434 static int
1435 1435 endtape(void)
1436 1436 {
1437 1437 if (dblock.dbuf.name[0] == '\0') { /* null header = EOT */
1438 1438 return (1);
1439 1439 } else
1440 1440 return (0);
1441 1441 }
1442 1442
1443 1443 /*
1444 1444 * getdir - get directory entry from tar tape
1445 1445 *
1446 1446 * getdir reads the next tarblock off the tape and cracks
1447 1447 * it as a directory. The checksum must match properly.
1448 1448 *
1449 1449 * If tfile is non-null getdir writes the file name and mod date
1450 1450 * to tfile.
1451 1451 */
1452 1452
1453 1453 static void
1454 1454 getdir(void)
1455 1455 {
1456 1456 struct stat *sp;
1457 1457 #ifdef EUC
1458 1458 static int warn_chksum_sign = 0;
1459 1459 #endif /* EUC */
1460 1460
1461 1461 top:
1462 1462 readtape((char *)&dblock);
1463 1463 if (dblock.dbuf.name[0] == '\0')
1464 1464 return;
1465 1465 sp = &stbuf;
1466 1466 (void) sscanf(dblock.dbuf.mode, "%8lo", &Gen.g_mode);
1467 1467 (void) sscanf(dblock.dbuf.uid, "%8lo", (ulong_t *)&Gen.g_uid);
1468 1468 (void) sscanf(dblock.dbuf.gid, "%8lo", (ulong_t *)&Gen.g_gid);
1469 1469 (void) sscanf(dblock.dbuf.size, "%12" FMT_off_t_o, &Gen.g_filesz);
1470 1470 (void) sscanf(dblock.dbuf.mtime, "%12lo", (ulong_t *)&Gen.g_mtime);
1471 1471 (void) sscanf(dblock.dbuf.chksum, "%8o", &Gen.g_cksum);
1472 1472 (void) sscanf(dblock.dbuf.devmajor, "%8lo", &Gen.g_devmajor);
1473 1473 (void) sscanf(dblock.dbuf.devminor, "%8lo", &Gen.g_devminor);
1474 1474
1475 1475 is_posix = (strcmp(dblock.dbuf.magic, magic_type) == 0);
1476 1476
1477 1477 sp->st_mode = Gen.g_mode;
1478 1478 if (is_posix && (sp->st_mode & S_IFMT) == 0)
1479 1479 switch (dblock.dbuf.typeflag) {
1480 1480 case '0': case 0: case _XATTR_HDRTYPE:
1481 1481 sp->st_mode |= S_IFREG;
1482 1482 break;
1483 1483 case '1': /* hard link */
1484 1484 break;
1485 1485 case '2':
1486 1486 sp->st_mode |= S_IFLNK;
1487 1487 break;
1488 1488 case '3':
1489 1489 sp->st_mode |= S_IFCHR;
1490 1490 break;
1491 1491 case '4':
1492 1492 sp->st_mode |= S_IFBLK;
1493 1493 break;
1494 1494 case '5':
1495 1495 sp->st_mode |= S_IFDIR;
1496 1496 break;
1497 1497 case '6':
1498 1498 sp->st_mode |= S_IFIFO;
1499 1499 break;
1500 1500 default:
1501 1501 if (convtoreg(Gen.g_filesz))
1502 1502 sp->st_mode |= S_IFREG;
1503 1503 break;
1504 1504 }
1505 1505
1506 1506 if ((dblock.dbuf.typeflag == 'X') || (dblock.dbuf.typeflag == 'L')) {
1507 1507 Xhdrflag = 1; /* Currently processing extended header */
1508 1508 } else {
1509 1509 Xhdrflag = 0;
1510 1510 }
1511 1511
1512 1512 sp->st_uid = Gen.g_uid;
1513 1513 sp->st_gid = Gen.g_gid;
1514 1514 sp->st_size = Gen.g_filesz;
1515 1515 sp->st_mtime = Gen.g_mtime;
1516 1516 chksum = Gen.g_cksum;
1517 1517
1518 1518 if (dblock.dbuf.extno != '\0') { /* split file? */
1519 1519 extno = dblock.dbuf.extno;
1520 1520 extsize = Gen.g_filesz;
1521 1521 extotal = dblock.dbuf.extotal;
1522 1522 } else {
1523 1523 extno = 0; /* tell others file not split */
1524 1524 extsize = 0;
1525 1525 extotal = 0;
1526 1526 }
1527 1527
1528 1528 #ifdef EUC
1529 1529 if (chksum != checksum(&dblock)) {
1530 1530 if (chksum != checksum_signed(&dblock)) {
1531 1531 (void) fprintf(stderr, gettext(
1532 1532 "tar: directory checksum error\n"));
1533 1533 if (iflag) {
1534 1534 Errflg = 2;
1535 1535 goto top;
1536 1536 }
1537 1537 done(2);
1538 1538 } else {
1539 1539 if (! warn_chksum_sign) {
1540 1540 warn_chksum_sign = 1;
1541 1541 (void) fprintf(stderr, gettext(
1542 1542 "tar: warning: tar file made with signed checksum\n"));
1543 1543 }
1544 1544 }
1545 1545 }
1546 1546 #else
1547 1547 if (chksum != checksum(&dblock)) {
1548 1548 (void) fprintf(stderr, gettext(
1549 1549 "tar: directory checksum error\n"));
1550 1550 if (iflag) {
1551 1551 Errflg = 2;
1552 1552 goto top;
1553 1553 }
1554 1554 done(2);
1555 1555 }
1556 1556 #endif /* EUC */
1557 1557 if (tfile != NULL && Xhdrflag == 0) {
1558 1558 /*
1559 1559 * If an extended header is present, then time is available
1560 1560 * in nanoseconds in the extended header data, so set it.
1561 1561 * Otherwise, give an invalid value so that checkupdate will
1562 1562 * not test beyond seconds.
1563 1563 */
1564 1564 if ((xhdr_flgs & _X_MTIME))
1565 1565 sp->st_mtim.tv_nsec = Xtarhdr.x_mtime.tv_nsec;
1566 1566 else
1567 1567 sp->st_mtim.tv_nsec = -1;
1568 1568
1569 1569 if (xhdr_flgs & _X_PATH)
1570 1570 (void) fprintf(tfile, "%s %10ld.%9.9ld\n",
1571 1571 Xtarhdr.x_path, sp->st_mtim.tv_sec,
1572 1572 sp->st_mtim.tv_nsec);
1573 1573 else
1574 1574 (void) fprintf(tfile, "%.*s %10ld.%9.9ld\n",
1575 1575 NAMSIZ, dblock.dbuf.name, sp->st_mtim.tv_sec,
1576 1576 sp->st_mtim.tv_nsec);
1577 1577 }
1578 1578
1579 1579 #if defined(O_XATTR)
1580 1580 Hiddendir = 0;
1581 1581 if (xattrp && dblock.dbuf.typeflag == _XATTR_HDRTYPE) {
1582 1582 if (xattrbadhead) {
1583 1583 free(xattrhead);
1584 1584 xattrp = NULL;
1585 1585 xattr_linkp = NULL;
1586 1586 xattrhead = NULL;
1587 1587 } else {
1588 1588 char *aname = basename(xattrapath);
1589 1589 size_t xindex = aname - xattrapath;
1590 1590
1591 1591 if (xattrapath[xindex] == '.' &&
1592 1592 xattrapath[xindex + 1] == '\0' &&
1593 1593 xattrp->h_typeflag == '5') {
1594 1594 Hiddendir = 1;
1595 1595 sp->st_mode =
1596 1596 (S_IFDIR | (sp->st_mode & POSIXMODES));
1597 1597 }
1598 1598 dblock.dbuf.typeflag = xattrp->h_typeflag;
1599 1599 }
1600 1600 }
1601 1601 #endif
1602 1602 }
1603 1603
1604 1604
1605 1605 /*
1606 1606 * passtape - skip over a file on the tape
1607 1607 *
1608 1608 * passtape skips over the next data file on the tape.
1609 1609 * The tape directory entry must be in dblock.dbuf. This
1610 1610 * routine just eats the number of blocks computed from the
1611 1611 * directory size entry; the tape must be (logically) positioned
1612 1612 * right after thee directory info.
1613 1613 */
1614 1614
1615 1615 static void
1616 1616 passtape(void)
1617 1617 {
1618 1618 blkcnt_t blocks;
1619 1619 char buf[TBLOCK];
1620 1620
1621 1621 /*
1622 1622 * Types link(1), sym-link(2), char special(3), blk special(4),
1623 1623 * directory(5), and FIFO(6) do not have data blocks associated
1624 1624 * with them so just skip reading the data block.
1625 1625 */
1626 1626 if (dblock.dbuf.typeflag == '1' || dblock.dbuf.typeflag == '2' ||
1627 1627 dblock.dbuf.typeflag == '3' || dblock.dbuf.typeflag == '4' ||
1628 1628 dblock.dbuf.typeflag == '5' || dblock.dbuf.typeflag == '6')
1629 1629 return;
1630 1630 blocks = TBLOCKS(stbuf.st_size);
1631 1631
1632 1632 /* if operating on disk, seek instead of reading */
1633 1633 if (NotTape)
1634 1634 seekdisk(blocks);
1635 1635 else
1636 1636 while (blocks-- > 0)
1637 1637 readtape(buf);
1638 1638 }
1639 1639
1640 1640 #if defined(O_XATTR)
1641 1641 static int
1642 1642 is_sysattr(char *name)
1643 1643 {
1644 1644 return ((strcmp(name, VIEW_READONLY) == 0) ||
1645 1645 (strcmp(name, VIEW_READWRITE) == 0));
1646 1646 }
1647 1647 #endif
1648 1648
1649 1649 #if defined(O_XATTR)
1650 1650 /*
1651 1651 * Verify the attribute, attrname, is an attribute we want to restore.
1652 1652 * Never restore read-only system attribute files. Only restore read-write
1653 1653 * system attributes files when -/ was specified, and only traverse into
1654 1654 * the 2nd level attribute directory containing only system attributes if
1655 1655 * -@ was specified. This keeps us from archiving
1656 1656 * <attribute name>/<read-write system attribute file>
1657 1657 * when -/ was specified without -@.
1658 1658 *
1659 1659 * attrname - attribute file name
1660 1660 * attrparent - attribute's parent name within the base file's attribute
1661 1661 * directory hierarchy
1662 1662 */
1663 1663 static attr_status_t
1664 1664 verify_attr(char *attrname, char *attrparent, int arc_rwsysattr,
1665 1665 int *rw_sysattr)
1666 1666 {
1667 1667 #if defined(_PC_SATTR_ENABLED)
1668 1668 int attr_supported;
1669 1669
1670 1670 /* Never restore read-only system attribute files */
1671 1671 if ((attr_supported = sysattr_type(attrname)) == _RO_SATTR) {
1672 1672 *rw_sysattr = 0;
1673 1673 return (ATTR_SKIP);
1674 1674 } else {
1675 1675 *rw_sysattr = (attr_supported == _RW_SATTR);
1676 1676 }
1677 1677 #else
1678 1678 /*
1679 1679 * Only need to check if this attribute is an extended system
1680 1680 * attribute.
1681 1681 */
1682 1682 if (*rw_sysattr = is_sysattr(attrname)) {
1683 1683 return (ATTR_SKIP);
1684 1684 } else {
1685 1685 return (ATTR_OK);
1686 1686 }
1687 1687 #endif /* _PC_SATTR_ENABLED */
1688 1688
1689 1689 /*
1690 1690 * If the extended system attribute file is specified with the
1691 1691 * arc_rwsysattr flag, as being transient (default extended
1692 1692 * attributes), then don't archive it.
1693 1693 */
1694 1694 if (*rw_sysattr && !arc_rwsysattr) {
1695 1695 return (ATTR_SKIP);
1696 1696 }
1697 1697
1698 1698 /*
1699 1699 * Only restore read-write system attribute files
1700 1700 * when -/ was specified. Only restore extended
1701 1701 * attributes when -@ was specified.
1702 1702 */
1703 1703 if (atflag) {
1704 1704 if (!saflag) {
1705 1705 /*
1706 1706 * Only archive/restore the hidden directory "." if
1707 1707 * we're processing the top level hidden attribute
1708 1708 * directory. We don't want to process the
1709 1709 * hidden attribute directory of the attribute
1710 1710 * directory that contains only extended system
1711 1711 * attributes.
1712 1712 */
1713 1713 if (*rw_sysattr || (Hiddendir &&
1714 1714 (attrparent != NULL))) {
1715 1715 return (ATTR_SKIP);
1716 1716 }
1717 1717 }
1718 1718 } else if (saflag) {
1719 1719 /*
1720 1720 * Only archive/restore read-write extended system attribute
1721 1721 * files of the base file.
1722 1722 */
1723 1723 if (!*rw_sysattr || (attrparent != NULL)) {
1724 1724 return (ATTR_SKIP);
1725 1725 }
1726 1726 } else {
1727 1727 return (ATTR_SKIP);
1728 1728 }
1729 1729
1730 1730 return (ATTR_OK);
1731 1731 }
1732 1732 #endif
1733 1733
1734 1734 static void
1735 1735 free_children(file_list_t *children)
1736 1736 {
1737 1737 file_list_t *child = children;
1738 1738 file_list_t *cptr;
1739 1739
1740 1740 while (child != NULL) {
1741 1741 cptr = child->next;
1742 1742 if (child->name != NULL) {
1743 1743 free(child->name);
1744 1744 }
1745 1745 child = cptr;
1746 1746 }
1747 1747 }
1748 1748
1749 1749 static int
1750 1750 putfile(char *longname, char *shortname, char *parent, attr_data_t *attrinfo,
1751 1751 int filetype, int lev, int symlink_lev)
1752 1752 {
1753 1753 int infile = -1; /* deliberately invalid */
1754 1754 blkcnt_t blocks;
1755 1755 char buf[PATH_MAX + 2]; /* Add trailing slash and null */
1756 1756 char *bigbuf;
1757 1757 int maxread;
1758 1758 int hint; /* amount to write to get "in sync" */
1759 1759 char filetmp[PATH_MAX + 1];
1760 1760 char *cp;
1761 1761 char *name;
1762 1762 char *attrparent = NULL;
1763 1763 char *longattrname = NULL;
1764 1764 file_list_t *child = NULL;
1765 1765 file_list_t *child_end = NULL;
1766 1766 file_list_t *cptr;
1767 1767 struct dirent *dp;
1768 1768 DIR *dirp;
1769 1769 int i;
1770 1770 int split;
1771 1771 int dirfd = -1;
1772 1772 int rc = PUT_NOTAS_LINK;
1773 1773 int archtype = 0;
1774 1774 int rw_sysattr = 0;
1775 1775 char newparent[PATH_MAX + MAXNAMLEN + 1];
1776 1776 char *prefix = "";
1777 1777 char *tmpbuf;
1778 1778 char goodbuf[PRESIZ + 2];
1779 1779 char junkbuf[MAXNAM+1];
1780 1780 char *lastslash;
1781 1781 int j;
1782 1782 struct stat sbuf;
1783 1783 int readlink_max;
1784 1784
1785 1785 (void) memset(goodbuf, '\0', sizeof (goodbuf));
1786 1786 (void) memset(junkbuf, '\0', sizeof (junkbuf));
1787 1787
1788 1788 xhdr_flgs = 0;
1789 1789
1790 1790 if (filetype == XATTR_FILE) {
1791 1791 attrparent = attrinfo->attr_parent;
1792 1792 longattrname = attrinfo->attr_path;
1793 1793 dirfd = attrinfo->attr_parentfd;
1794 1794 rw_sysattr = attrinfo->attr_rw_sysattr;
1795 1795 } else {
1796 1796 dirfd = open(".", O_RDONLY);
1797 1797 }
1798 1798
1799 1799 if (dirfd == -1) {
1800 1800 (void) fprintf(stderr, gettext(
1801 1801 "tar: unable to open%sdirectory %s%s%s%s\n"),
1802 1802 (filetype == XATTR_FILE) ? gettext(" attribute ") : " ",
1803 1803 (attrparent == NULL) ? "" : gettext("of attribute "),
1804 1804 (attrparent == NULL) ? "" : attrparent,
1805 1805 (attrparent == NULL) ? "" : gettext(" of "),
1806 1806 (filetype == XATTR_FILE) ? longname : parent);
1807 1807 goto out;
1808 1808 }
1809 1809
1810 1810 if (lev > MAXLEV) {
1811 1811 (void) fprintf(stderr,
1812 1812 gettext("tar: directory nesting too deep, %s not dumped\n"),
1813 1813 longname);
1814 1814 goto out;
1815 1815 }
1816 1816
1817 1817 if (getstat(dirfd, longname, shortname, attrparent))
1818 1818 goto out;
1819 1819
1820 1820 if (hflag) {
1821 1821 /*
1822 1822 * Catch nesting where a file is a symlink to its directory.
1823 1823 */
1824 1824 j = fstatat(dirfd, shortname, &sbuf, AT_SYMLINK_NOFOLLOW);
1825 1825 if (S_ISLNK(sbuf.st_mode)) {
1826 1826 if (symlink_lev++ >= MAXSYMLINKS) {
1827 1827 (void) fprintf(stderr, gettext(
1828 1828 "tar: %s: Number of symbolic links "
1829 1829 "encountered during path name traversal "
1830 1830 "exceeds MAXSYMLINKS\n"), longname);
1831 1831 Errflg = 1;
1832 1832 goto out;
1833 1833 }
1834 1834 }
1835 1835 }
1836 1836
1837 1837 /*
1838 1838 * Check if the input file is the same as the tar file we
1839 1839 * are creating
1840 1840 */
1841 1841 if ((mt_ino == stbuf.st_ino) && (mt_dev == stbuf.st_dev)) {
1842 1842 (void) fprintf(stderr, gettext(
1843 1843 "tar: %s%s%s%s%s same as archive file\n"),
1844 1844 rw_sysattr ? gettext("system ") : "",
1845 1845 (longattrname == NULL) ? "" : gettext("attribute "),
1846 1846 (longattrname == NULL) ? "" : longattrname,
1847 1847 (longattrname == NULL) ? "" : gettext(" of "),
1848 1848 longname);
1849 1849 Errflg = 1;
1850 1850 goto out;
1851 1851 }
1852 1852 /*
1853 1853 * Check size limit - we can't archive files that
1854 1854 * exceed TAR_OFFSET_MAX bytes because of header
1855 1855 * limitations. Exclude file types that set
1856 1856 * st_size to zero below because they take no
1857 1857 * archive space to represent contents.
1858 1858 */
1859 1859 if ((stbuf.st_size > (off_t)TAR_OFFSET_MAX) &&
1860 1860 !S_ISDIR(stbuf.st_mode) &&
1861 1861 !S_ISCHR(stbuf.st_mode) &&
1862 1862 !S_ISBLK(stbuf.st_mode) &&
1863 1863 (Eflag == 0)) {
1864 1864 (void) fprintf(stderr, gettext(
1865 1865 "tar: %s%s%s%s%s too large to archive. "
1866 1866 "Use E function modifier.\n"),
1867 1867 rw_sysattr ? gettext("system ") : "",
1868 1868 (longattrname == NULL) ? "" : gettext("attribute "),
1869 1869 (longattrname == NULL) ? "" : longattrname,
1870 1870 (longattrname == NULL) ? "" : gettext(" of "),
1871 1871 longname);
1872 1872 if (errflag)
1873 1873 exitflag = 1;
1874 1874 Errflg = 1;
1875 1875 goto out;
1876 1876 }
1877 1877
1878 1878 if (tfile != NULL && checkupdate(longname) == 0) {
1879 1879 goto out;
1880 1880 }
1881 1881 if (checkw('r', longname) == 0) {
1882 1882 goto out;
1883 1883 }
1884 1884
1885 1885 if (Fflag &&
1886 1886 checkf(longname, (stbuf.st_mode & S_IFMT) == S_IFDIR, Fflag) == 0)
1887 1887 goto out;
1888 1888
1889 1889 if (Xflag) {
1890 1890 if (is_in_table(exclude_tbl, longname)) {
1891 1891 if (vflag) {
1892 1892 (void) fprintf(vfile, gettext(
1893 1893 "a %s excluded\n"), longname);
1894 1894 }
1895 1895 goto out;
1896 1896 }
1897 1897 }
1898 1898
1899 1899 /*
1900 1900 * If the length of the fullname is greater than MAXNAM,
1901 1901 * print out a message and return (unless extended headers are used,
1902 1902 * in which case fullname is limited to PATH_MAX).
1903 1903 */
1904 1904
1905 1905 if ((((split = (int)strlen(longname)) > MAXNAM) && (Eflag == 0)) ||
1906 1906 (split > PATH_MAX)) {
1907 1907 (void) fprintf(stderr, gettext(
1908 1908 "tar: %s: file name too long\n"), longname);
1909 1909 if (errflag)
1910 1910 exitflag = 1;
1911 1911 Errflg = 1;
1912 1912 goto out;
1913 1913 }
1914 1914
1915 1915 /*
1916 1916 * We split the fullname into prefix and name components if any one
1917 1917 * of three conditions holds:
1918 1918 * -- the length of the fullname exceeds NAMSIZ,
1919 1919 * -- the length of the fullname equals NAMSIZ, and the shortname
1920 1920 * is less than NAMSIZ, (splitting in this case preserves
1921 1921 * compatibility with 5.6 and 5.5.1 tar), or
1922 1922 * -- the length of the fullname equals NAMSIZ, the file is a
1923 1923 * directory and we are not in POSIX-conformant mode (where
1924 1924 * trailing slashes are removed from directories).
1925 1925 */
1926 1926 if ((split > NAMSIZ) ||
1927 1927 (split == NAMSIZ && strlen(shortname) < NAMSIZ) ||
1928 1928 (split == NAMSIZ && S_ISDIR(stbuf.st_mode) && !Pflag)) {
1929 1929 /*
1930 1930 * Since path is limited to PRESIZ characters, look for the
1931 1931 * last slash within PRESIZ + 1 characters only.
1932 1932 */
1933 1933 (void) strncpy(&goodbuf[0], longname, min(split, PRESIZ + 1));
1934 1934 tmpbuf = goodbuf;
1935 1935 lastslash = strrchr(tmpbuf, '/');
1936 1936 if (lastslash == NULL) {
1937 1937 i = split; /* Length of name */
1938 1938 j = 0; /* Length of prefix */
1939 1939 goodbuf[0] = '\0';
1940 1940 } else {
1941 1941 *lastslash = '\0'; /* Terminate the prefix */
1942 1942 j = strlen(tmpbuf);
1943 1943 i = split - j - 1;
1944 1944 }
1945 1945 /*
1946 1946 * If the filename is greater than NAMSIZ we can't
1947 1947 * archive the file unless we are using extended headers.
1948 1948 */
1949 1949 if ((i > NAMSIZ) || (i == NAMSIZ && S_ISDIR(stbuf.st_mode) &&
1950 1950 !Pflag)) {
1951 1951 /* Determine which (filename or path) is too long. */
1952 1952 lastslash = strrchr(longname, '/');
1953 1953 if (lastslash != NULL)
1954 1954 i = strlen(lastslash + 1);
1955 1955 if (Eflag > 0) {
1956 1956 xhdr_flgs |= _X_PATH;
1957 1957 Xtarhdr.x_path = longname;
1958 1958 if (i <= NAMSIZ)
1959 1959 (void) strcpy(junkbuf, lastslash + 1);
1960 1960 else
1961 1961 (void) sprintf(junkbuf, "%llu",
1962 1962 xhdr_count + 1);
1963 1963 if (split - i - 1 > PRESIZ)
1964 1964 (void) strcpy(goodbuf, xhdr_dirname);
1965 1965 } else {
1966 1966 if ((i > NAMSIZ) || (i == NAMSIZ &&
1967 1967 S_ISDIR(stbuf.st_mode) && !Pflag))
1968 1968 (void) fprintf(stderr, gettext(
1969 1969 "tar: %s: filename is greater than "
1970 1970 "%d\n"), lastslash == NULL ?
1971 1971 longname : lastslash + 1, NAMSIZ);
1972 1972 else
1973 1973 (void) fprintf(stderr, gettext(
1974 1974 "tar: %s: prefix is greater than %d"
1975 1975 "\n"), longname, PRESIZ);
1976 1976 if (errflag)
1977 1977 exitflag = 1;
1978 1978 Errflg = 1;
1979 1979 goto out;
1980 1980 }
1981 1981 } else
1982 1982 (void) strncpy(&junkbuf[0], longname + j + 1,
1983 1983 strlen(longname + j + 1));
1984 1984 name = junkbuf;
1985 1985 prefix = goodbuf;
1986 1986 } else {
1987 1987 name = longname;
1988 1988 }
1989 1989 if (Aflag) {
1990 1990 if ((prefix != NULL) && (*prefix != '\0'))
1991 1991 while (*prefix == '/')
1992 1992 ++prefix;
1993 1993 else
1994 1994 while (*name == '/')
1995 1995 ++name;
1996 1996 }
1997 1997
1998 1998 switch (stbuf.st_mode & S_IFMT) {
1999 1999 case S_IFDIR:
2000 2000 stbuf.st_size = (off_t)0;
2001 2001 blocks = TBLOCKS(stbuf.st_size);
2002 2002
2003 2003 if (filetype != XATTR_FILE && Hiddendir == 0) {
2004 2004 i = 0;
2005 2005 cp = buf;
2006 2006 while ((*cp++ = longname[i++]))
2007 2007 ;
2008 2008 *--cp = '/';
2009 2009 *++cp = 0;
2010 2010 }
2011 2011 if (!oflag) {
2012 2012 tomodes(&stbuf);
2013 2013 if (build_dblock(name, tchar, '5', filetype,
2014 2014 &stbuf, stbuf.st_dev, prefix) != 0) {
2015 2015 goto out;
2016 2016 }
2017 2017 if (!Pflag) {
2018 2018 /*
2019 2019 * Old archives require a slash at the end
2020 2020 * of a directory name.
2021 2021 *
2022 2022 * XXX
2023 2023 * If directory name is too long, will
2024 2024 * slash overfill field?
2025 2025 */
2026 2026 if (strlen(name) > (unsigned)NAMSIZ-1) {
2027 2027 (void) fprintf(stderr, gettext(
2028 2028 "tar: %s: filename is greater "
2029 2029 "than %d\n"), name, NAMSIZ);
2030 2030 if (errflag)
2031 2031 exitflag = 1;
2032 2032 Errflg = 1;
2033 2033 goto out;
2034 2034 } else {
2035 2035 if (strlen(name) == (NAMSIZ - 1)) {
2036 2036 (void) memcpy(dblock.dbuf.name,
2037 2037 name, NAMSIZ);
2038 2038 dblock.dbuf.name[NAMSIZ-1]
2039 2039 = '/';
2040 2040 } else
2041 2041 (void) sprintf(dblock.dbuf.name,
2042 2042 "%s/", name);
2043 2043
2044 2044 /*
2045 2045 * need to recalculate checksum
2046 2046 * because the name changed.
2047 2047 */
2048 2048 (void) sprintf(dblock.dbuf.chksum,
2049 2049 "%07o", checksum(&dblock));
2050 2050 }
2051 2051 }
2052 2052
2053 2053 if (put_extra_attributes(longname, shortname,
2054 2054 longattrname, prefix, filetype, '5') != 0)
2055 2055 goto out;
2056 2056
2057 2057 #if defined(O_XATTR)
2058 2058 /*
2059 2059 * Reset header typeflag when archiving directory, since
2060 2060 * build_dblock changed it on us.
2061 2061 */
2062 2062 if (filetype == XATTR_FILE) {
2063 2063 dblock.dbuf.typeflag = _XATTR_HDRTYPE;
2064 2064 } else {
2065 2065 dblock.dbuf.typeflag = '5';
2066 2066 }
2067 2067 #else
2068 2068 dblock.dbuf.typeflag = '5';
2069 2069 #endif
2070 2070
2071 2071 (void) sprintf(dblock.dbuf.chksum, "%07o",
2072 2072 checksum(&dblock));
2073 2073
2074 2074 (void) writetbuf((char *)&dblock, 1);
2075 2075 }
2076 2076 if (vflag) {
2077 2077 #ifdef DEBUG
2078 2078 if (NotTape)
2079 2079 DEBUG("seek = %" FMT_blkcnt_t "K\t", K(tapepos),
2080 2080 0);
2081 2081 #endif
2082 2082 if (filetype == XATTR_FILE && Hiddendir) {
2083 2083 (void) fprintf(vfile,
2084 2084 gettext("a %s attribute %s "),
2085 2085 longname, longattrname);
2086 2086
2087 2087 } else {
2088 2088 (void) fprintf(vfile, "a %s/ ", longname);
2089 2089 }
2090 2090 if (NotTape)
2091 2091 (void) fprintf(vfile, "%" FMT_blkcnt_t "K\n",
2092 2092 K(blocks));
2093 2093 else
2094 2094 (void) fprintf(vfile, gettext("%" FMT_blkcnt_t
2095 2095 " tape blocks\n"), blocks);
2096 2096 }
2097 2097
2098 2098 /*
2099 2099 * If hidden dir then break now since xattrs_put() will do
2100 2100 * the iterating of the directory.
2101 2101 *
2102 2102 * At the moment, there can only be system attributes on
2103 2103 * attributes. There can be no attributes on attributes or
2104 2104 * directories within the attributes hidden directory hierarchy.
2105 2105 */
2106 2106 if (filetype == XATTR_FILE)
2107 2107 break;
2108 2108
2109 2109 if (*shortname != '/')
2110 2110 (void) sprintf(newparent, "%s/%s", parent, shortname);
2111 2111 else
2112 2112 (void) sprintf(newparent, "%s", shortname);
2113 2113
2114 2114 if (tar_chdir(shortname) < 0) {
2115 2115 vperror(0, "%s", newparent);
2116 2116 goto out;
2117 2117 }
2118 2118
2119 2119 if ((dirp = opendir(".")) == NULL) {
2120 2120 vperror(0, gettext(
2121 2121 "can't open directory %s"), longname);
2122 2122 if (tar_chdir(parent) < 0)
2123 2123 vperror(0, gettext("cannot change back?: %s"),
2124 2124 parent);
2125 2125 goto out;
2126 2126 }
2127 2127
2128 2128 /*
2129 2129 * Create a list of files (children) in this directory to avoid
2130 2130 * having to perform telldir()/seekdir().
2131 2131 */
2132 2132 while ((dp = readdir(dirp)) != NULL && !term) {
2133 2133 if ((strcmp(".", dp->d_name) == 0) ||
2134 2134 (strcmp("..", dp->d_name) == 0))
2135 2135 continue;
2136 2136 if (((cptr = (file_list_t *)calloc(sizeof (char),
2137 2137 sizeof (file_list_t))) == NULL) ||
2138 2138 ((cptr->name = strdup(dp->d_name)) == NULL)) {
2139 2139 vperror(1, gettext(
2140 2140 "Insufficient memory for directory "
2141 2141 "list entry %s/%s\n"),
2142 2142 newparent, dp->d_name);
2143 2143 }
2144 2144
2145 2145 /* Add the file to the list */
2146 2146 if (child == NULL) {
2147 2147 child = cptr;
2148 2148 } else {
2149 2149 child_end->next = cptr;
2150 2150 }
2151 2151 child_end = cptr;
2152 2152 }
2153 2153 (void) closedir(dirp);
2154 2154
2155 2155 /*
2156 2156 * Archive each of the files in the current directory.
2157 2157 * If a file is a directory, putfile() is called
2158 2158 * recursively to archive the file hierarchy of the
2159 2159 * directory before archiving the next file in the
2160 2160 * current directory.
2161 2161 */
2162 2162 while ((child != NULL) && !term) {
2163 2163 (void) strcpy(cp, child->name);
2164 2164 archtype = putfile(buf, cp, newparent, NULL,
2165 2165 NORMAL_FILE, lev + 1, symlink_lev);
2166 2166
2167 2167 if (!exitflag) {
2168 2168 if ((atflag || saflag) &&
2169 2169 (archtype == PUT_NOTAS_LINK)) {
2170 2170 xattrs_put(buf, cp, newparent, NULL);
2171 2171 }
2172 2172 }
2173 2173 if (exitflag)
2174 2174 break;
2175 2175
2176 2176 /* Free each child as we are done processing it. */
2177 2177 cptr = child;
2178 2178 child = child->next;
2179 2179 free(cptr->name);
2180 2180 free(cptr);
2181 2181 }
2182 2182 if ((child != NULL) && !term) {
2183 2183 free_children(child);
2184 2184 }
2185 2185
2186 2186 if (tar_chdir(parent) < 0) {
2187 2187 vperror(0, gettext("cannot change back?: %s"), parent);
2188 2188 }
2189 2189
2190 2190 break;
2191 2191
2192 2192 case S_IFLNK:
2193 2193 readlink_max = NAMSIZ;
2194 2194 if (stbuf.st_size > NAMSIZ) {
2195 2195 if (Eflag > 0) {
2196 2196 xhdr_flgs |= _X_LINKPATH;
2197 2197 readlink_max = PATH_MAX;
2198 2198 } else {
2199 2199 (void) fprintf(stderr, gettext(
2200 2200 "tar: %s: symbolic link too long\n"),
2201 2201 longname);
2202 2202 if (errflag)
2203 2203 exitflag = 1;
2204 2204 Errflg = 1;
2205 2205 goto out;
2206 2206 }
2207 2207 }
2208 2208 /*
2209 2209 * Sym-links need header size of zero since you
2210 2210 * don't store any data for this type.
2211 2211 */
2212 2212 stbuf.st_size = (off_t)0;
2213 2213 tomodes(&stbuf);
2214 2214 i = readlink(shortname, filetmp, readlink_max);
2215 2215 if (i < 0) {
2216 2216 vperror(0, gettext(
2217 2217 "can't read symbolic link %s"), longname);
2218 2218 goto out;
2219 2219 } else {
2220 2220 filetmp[i] = 0;
2221 2221 }
2222 2222 if (vflag)
2223 2223 (void) fprintf(vfile, gettext(
2224 2224 "a %s symbolic link to %s\n"),
2225 2225 longname, filetmp);
2226 2226 if (xhdr_flgs & _X_LINKPATH) {
2227 2227 Xtarhdr.x_linkpath = filetmp;
2228 2228 if (build_dblock(name, tchar, '2', filetype, &stbuf,
2229 2229 stbuf.st_dev, prefix) != 0)
2230 2230 goto out;
2231 2231 } else
2232 2232 if (build_dblock(name, filetmp, '2', filetype, &stbuf,
2233 2233 stbuf.st_dev, prefix) != 0)
2234 2234 goto out;
2235 2235 (void) writetbuf((char *)&dblock, 1);
2236 2236 /*
2237 2237 * No acls for symlinks: mode is always 777
2238 2238 * dont call write ancillary
2239 2239 */
2240 2240 rc = PUT_AS_LINK;
2241 2241 break;
2242 2242 case S_IFREG:
2243 2243 if ((infile = openat(dirfd, shortname, 0)) < 0) {
2244 2244 vperror(0, gettext("unable to open %s%s%s%s"), longname,
2245 2245 rw_sysattr ? gettext(" system") : "",
2246 2246 (filetype == XATTR_FILE) ?
2247 2247 gettext(" attribute ") : "",
2248 2248 (filetype == XATTR_FILE) ? (longattrname == NULL) ?
2249 2249 shortname : longattrname : "");
2250 2250 goto out;
2251 2251 }
2252 2252
2253 2253 blocks = TBLOCKS(stbuf.st_size);
2254 2254
2255 2255 if (put_link(name, longname, shortname, longattrname,
2256 2256 prefix, filetype, '1') == 0) {
2257 2257 (void) close(infile);
2258 2258 rc = PUT_AS_LINK;
2259 2259 goto out;
2260 2260 }
2261 2261
2262 2262 tomodes(&stbuf);
2263 2263
2264 2264 /* correctly handle end of volume */
2265 2265 while (mulvol && tapepos + blocks + 1 > blocklim) {
2266 2266 /* split if floppy has some room and file is large */
2267 2267 if (((blocklim - tapepos) >= EXTMIN) &&
2268 2268 ((blocks + 1) >= blocklim/10)) {
2269 2269 splitfile(longname, infile,
2270 2270 name, prefix, filetype);
2271 2271 (void) close(dirfd);
2272 2272 (void) close(infile);
2273 2273 goto out;
2274 2274 }
2275 2275 newvol(); /* not worth it--just get new volume */
2276 2276 }
2277 2277 #ifdef DEBUG
2278 2278 DEBUG("putfile: %s wants %" FMT_blkcnt_t " blocks\n", longname,
2279 2279 blocks);
2280 2280 #endif
2281 2281 if (build_dblock(name, tchar, '0', filetype,
2282 2282 &stbuf, stbuf.st_dev, prefix) != 0) {
2283 2283 goto out;
2284 2284 }
2285 2285 if (vflag) {
2286 2286 #ifdef DEBUG
2287 2287 if (NotTape)
2288 2288 DEBUG("seek = %" FMT_blkcnt_t "K\t", K(tapepos),
2289 2289 0);
2290 2290 #endif
2291 2291 (void) fprintf(vfile, "a %s%s%s%s ", longname,
2292 2292 rw_sysattr ? gettext(" system") : "",
2293 2293 (filetype == XATTR_FILE) ? gettext(
2294 2294 " attribute ") : "",
2295 2295 (filetype == XATTR_FILE) ?
2296 2296 longattrname : "");
2297 2297 if (NotTape)
2298 2298 (void) fprintf(vfile, "%" FMT_blkcnt_t "K\n",
2299 2299 K(blocks));
2300 2300 else
2301 2301 (void) fprintf(vfile,
2302 2302 gettext("%" FMT_blkcnt_t " tape blocks\n"),
2303 2303 blocks);
2304 2304 }
2305 2305
2306 2306 if (put_extra_attributes(longname, shortname, longattrname,
2307 2307 prefix, filetype, '0') != 0)
2308 2308 goto out;
2309 2309
2310 2310 /*
2311 2311 * No need to reset typeflag for extended attribute here, since
2312 2312 * put_extra_attributes already set it and we haven't called
2313 2313 * build_dblock().
2314 2314 */
2315 2315 (void) sprintf(dblock.dbuf.chksum, "%07o", checksum(&dblock));
2316 2316 hint = writetbuf((char *)&dblock, 1);
2317 2317 maxread = max(min(stbuf.st_blksize, stbuf.st_size),
2318 2318 (nblock * TBLOCK));
2319 2319 if ((bigbuf = calloc((unsigned)maxread, sizeof (char))) == 0) {
2320 2320 maxread = TBLOCK;
2321 2321 bigbuf = buf;
2322 2322 }
2323 2323
2324 2324 while (((i = (int)
2325 2325 read(infile, bigbuf, min((hint*TBLOCK), maxread))) > 0) &&
2326 2326 blocks) {
2327 2327 blkcnt_t nblks;
2328 2328
2329 2329 nblks = ((i-1)/TBLOCK)+1;
2330 2330 if (nblks > blocks)
2331 2331 nblks = blocks;
2332 2332 hint = writetbuf(bigbuf, nblks);
2333 2333 blocks -= nblks;
2334 2334 }
2335 2335 (void) close(infile);
2336 2336 if (bigbuf != buf)
2337 2337 free(bigbuf);
2338 2338 if (i < 0)
2339 2339 vperror(0, gettext("Read error on %s"), longname);
2340 2340 else if (blocks != 0 || i != 0) {
2341 2341 (void) fprintf(stderr, gettext(
2342 2342 "tar: %s: file changed size\n"), longname);
2343 2343 if (errflag) {
2344 2344 exitflag = 1;
2345 2345 Errflg = 1;
2346 2346 } else if (!Dflag) {
2347 2347 Errflg = 1;
2348 2348 }
2349 2349 }
2350 2350 putempty(blocks);
2351 2351 break;
2352 2352 case S_IFIFO:
2353 2353 blocks = TBLOCKS(stbuf.st_size);
2354 2354 stbuf.st_size = (off_t)0;
2355 2355
2356 2356 if (put_link(name, longname, shortname, longattrname,
2357 2357 prefix, filetype, '6') == 0) {
2358 2358 rc = PUT_AS_LINK;
2359 2359 goto out;
2360 2360 }
2361 2361 tomodes(&stbuf);
2362 2362
2363 2363 while (mulvol && tapepos + blocks + 1 > blocklim) {
2364 2364 if (((blocklim - tapepos) >= EXTMIN) &&
2365 2365 ((blocks + 1) >= blocklim/10)) {
2366 2366 splitfile(longname, infile, name,
2367 2367 prefix, filetype);
2368 2368 (void) close(dirfd);
2369 2369 (void) close(infile);
2370 2370 goto out;
2371 2371 }
2372 2372 newvol();
2373 2373 }
2374 2374 #ifdef DEBUG
2375 2375 DEBUG("putfile: %s wants %" FMT_blkcnt_t " blocks\n", longname,
2376 2376 blocks);
2377 2377 #endif
2378 2378 if (vflag) {
2379 2379 #ifdef DEBUG
2380 2380 if (NotTape)
2381 2381 DEBUG("seek = %" FMT_blkcnt_t "K\t", K(tapepos),
2382 2382 0);
2383 2383 #endif
2384 2384 if (NotTape)
2385 2385 (void) fprintf(vfile, gettext("a %s %"
2386 2386 FMT_blkcnt_t "K\n "), longname, K(blocks));
2387 2387 else
2388 2388 (void) fprintf(vfile, gettext(
2389 2389 "a %s %" FMT_blkcnt_t " tape blocks\n"),
2390 2390 longname, blocks);
2391 2391 }
2392 2392 if (build_dblock(name, tchar, '6', filetype,
2393 2393 &stbuf, stbuf.st_dev, prefix) != 0)
2394 2394 goto out;
2395 2395
2396 2396 if (put_extra_attributes(longname, shortname, longattrname,
2397 2397 prefix, filetype, '6') != 0)
2398 2398 goto out;
2399 2399
2400 2400 (void) sprintf(dblock.dbuf.chksum, "%07o", checksum(&dblock));
2401 2401 dblock.dbuf.typeflag = '6';
2402 2402
2403 2403 (void) writetbuf((char *)&dblock, 1);
2404 2404 break;
2405 2405 case S_IFCHR:
2406 2406 stbuf.st_size = (off_t)0;
2407 2407 blocks = TBLOCKS(stbuf.st_size);
2408 2408 if (put_link(name, longname, shortname, longattrname,
2409 2409 prefix, filetype, '3') == 0) {
2410 2410 rc = PUT_AS_LINK;
2411 2411 goto out;
2412 2412 }
2413 2413 tomodes(&stbuf);
2414 2414
2415 2415 while (mulvol && tapepos + blocks + 1 > blocklim) {
2416 2416 if (((blocklim - tapepos) >= EXTMIN) &&
2417 2417 ((blocks + 1) >= blocklim/10)) {
2418 2418 splitfile(longname, infile, name,
2419 2419 prefix, filetype);
2420 2420 (void) close(dirfd);
2421 2421 goto out;
2422 2422 }
2423 2423 newvol();
2424 2424 }
2425 2425 #ifdef DEBUG
2426 2426 DEBUG("putfile: %s wants %" FMT_blkcnt_t " blocks\n", longname,
2427 2427 blocks);
2428 2428 #endif
2429 2429 if (vflag) {
2430 2430 #ifdef DEBUG
2431 2431 if (NotTape)
2432 2432 DEBUG("seek = %" FMT_blkcnt_t "K\t", K(tapepos),
2433 2433 0);
2434 2434 #endif
2435 2435 if (NotTape)
2436 2436 (void) fprintf(vfile, gettext("a %s %"
2437 2437 FMT_blkcnt_t "K\n"), longname, K(blocks));
2438 2438 else
2439 2439 (void) fprintf(vfile, gettext("a %s %"
2440 2440 FMT_blkcnt_t " tape blocks\n"), longname,
2441 2441 blocks);
2442 2442 }
2443 2443 if (build_dblock(name, tchar, '3',
2444 2444 filetype, &stbuf, stbuf.st_rdev, prefix) != 0)
2445 2445 goto out;
2446 2446
2447 2447 if (put_extra_attributes(longname, shortname, longattrname,
2448 2448 prefix, filetype, '3') != 0)
2449 2449 goto out;
2450 2450
2451 2451 (void) sprintf(dblock.dbuf.chksum, "%07o", checksum(&dblock));
2452 2452 dblock.dbuf.typeflag = '3';
2453 2453
2454 2454 (void) writetbuf((char *)&dblock, 1);
2455 2455 break;
2456 2456 case S_IFBLK:
2457 2457 stbuf.st_size = (off_t)0;
2458 2458 blocks = TBLOCKS(stbuf.st_size);
2459 2459 if (put_link(name, longname, shortname, longattrname,
2460 2460 prefix, filetype, '4') == 0) {
2461 2461 rc = PUT_AS_LINK;
2462 2462 goto out;
2463 2463 }
2464 2464 tomodes(&stbuf);
2465 2465
2466 2466 while (mulvol && tapepos + blocks + 1 > blocklim) {
2467 2467 if (((blocklim - tapepos) >= EXTMIN) &&
2468 2468 ((blocks + 1) >= blocklim/10)) {
2469 2469 splitfile(longname, infile,
2470 2470 name, prefix, filetype);
2471 2471 (void) close(dirfd);
2472 2472 goto out;
2473 2473 }
2474 2474 newvol();
2475 2475 }
2476 2476 #ifdef DEBUG
2477 2477 DEBUG("putfile: %s wants %" FMT_blkcnt_t " blocks\n", longname,
2478 2478 blocks);
2479 2479 #endif
2480 2480 if (vflag) {
2481 2481 #ifdef DEBUG
2482 2482 if (NotTape)
2483 2483 DEBUG("seek = %" FMT_blkcnt_t "K\t", K(tapepos),
2484 2484 0);
2485 2485 #endif
2486 2486 (void) fprintf(vfile, "a %s ", longname);
2487 2487 if (NotTape)
2488 2488 (void) fprintf(vfile, "%" FMT_blkcnt_t "K\n",
2489 2489 K(blocks));
2490 2490 else
2491 2491 (void) fprintf(vfile, gettext("%"
2492 2492 FMT_blkcnt_t " tape blocks\n"), blocks);
2493 2493 }
2494 2494 if (build_dblock(name, tchar, '4',
2495 2495 filetype, &stbuf, stbuf.st_rdev, prefix) != 0)
2496 2496 goto out;
2497 2497
2498 2498 if (put_extra_attributes(longname, shortname, longattrname,
2499 2499 prefix, filetype, '4') != 0)
2500 2500 goto out;
2501 2501
2502 2502 (void) sprintf(dblock.dbuf.chksum, "%07o", checksum(&dblock));
2503 2503 dblock.dbuf.typeflag = '4';
2504 2504
2505 2505 (void) writetbuf((char *)&dblock, 1);
2506 2506 break;
2507 2507 default:
2508 2508 (void) fprintf(stderr, gettext(
2509 2509 "tar: %s is not a file. Not dumped\n"), longname);
2510 2510 if (errflag)
2511 2511 exitflag = 1;
2512 2512 Errflg = 1;
2513 2513 goto out;
2514 2514 }
2515 2515
2516 2516 out:
2517 2517 if ((dirfd != -1) && (filetype != XATTR_FILE)) {
2518 2518 (void) close(dirfd);
2519 2519 }
2520 2520 return (rc);
2521 2521 }
2522 2522
2523 2523
2524 2524 /*
2525 2525 * splitfile dump a large file across volumes
2526 2526 *
2527 2527 * splitfile(longname, fd);
2528 2528 * char *longname; full name of file
2529 2529 * int ifd; input file descriptor
2530 2530 *
2531 2531 * NOTE: only called by putfile() to dump a large file.
2532 2532 */
2533 2533
2534 2534 static void
2535 2535 splitfile(char *longname, int ifd, char *name, char *prefix, int filetype)
2536 2536 {
2537 2537 blkcnt_t blocks;
2538 2538 off_t bytes, s;
2539 2539 char buf[TBLOCK];
2540 2540 int i, extents;
2541 2541
2542 2542 blocks = TBLOCKS(stbuf.st_size); /* blocks file needs */
2543 2543
2544 2544 /*
2545 2545 * # extents =
2546 2546 * size of file after using up rest of this floppy
2547 2547 * blocks - (blocklim - tapepos) + 1 (for header)
2548 2548 * plus roundup value before divide by blocklim-1
2549 2549 * + (blocklim - 1) - 1
2550 2550 * all divided by blocklim-1 (one block for each header).
2551 2551 * this gives
2552 2552 * (blocks - blocklim + tapepos + 1 + blocklim - 2)/(blocklim-1)
2553 2553 * which reduces to the expression used.
2554 2554 * one is added to account for this first extent.
2555 2555 *
2556 2556 * When one is dealing with extremely large archives, one may want
2557 2557 * to allow for a large number of extents. This code should be
2558 2558 * revisited to determine if extents should be changed to something
2559 2559 * larger than an int.
2560 2560 */
2561 2561 extents = (int)((blocks + tapepos - 1ULL)/(blocklim - 1ULL) + 1);
2562 2562
2563 2563 if (extents < 2 || extents > MAXEXT) { /* let's be reasonable */
2564 2564 (void) fprintf(stderr, gettext(
2565 2565 "tar: %s needs unusual number of volumes to split\n"
2566 2566 "tar: %s not dumped\n"), longname, longname);
2567 2567 return;
2568 2568 }
2569 2569 if (build_dblock(name, tchar, '0', filetype,
2570 2570 &stbuf, stbuf.st_dev, prefix) != 0)
2571 2571 return;
2572 2572
2573 2573 dblock.dbuf.extotal = extents;
2574 2574 bytes = stbuf.st_size;
2575 2575
2576 2576 /*
2577 2577 * The value contained in dblock.dbuf.efsize was formerly used when the
2578 2578 * v flag was specified in conjunction with the t flag. Although it is
2579 2579 * no longer used, older versions of tar will expect the former
2580 2580 * behaviour, so we must continue to write it to the archive.
2581 2581 *
2582 2582 * Since dblock.dbuf.efsize is 10 chars in size, the maximum value it
2583 2583 * can store is TAR_EFSIZE_MAX. If bytes exceeds that value, simply
2584 2584 * store 0.
2585 2585 */
2586 2586 if (bytes <= TAR_EFSIZE_MAX)
2587 2587 (void) sprintf(dblock.dbuf.efsize, "%9" FMT_off_t_o, bytes);
2588 2588 else
2589 2589 (void) sprintf(dblock.dbuf.efsize, "%9" FMT_off_t_o, (off_t)0);
2590 2590
2591 2591 (void) fprintf(stderr, gettext(
2592 2592 "tar: large file %s needs %d extents.\n"
2593 2593 "tar: current device seek position = %" FMT_blkcnt_t "K\n"),
2594 2594 longname, extents, K(tapepos));
2595 2595
2596 2596 s = (off_t)(blocklim - tapepos - 1) * TBLOCK;
2597 2597 for (i = 1; i <= extents; i++) {
2598 2598 if (i > 1) {
2599 2599 newvol();
2600 2600 if (i == extents)
2601 2601 s = bytes; /* last ext. gets true bytes */
2602 2602 else
2603 2603 s = (off_t)(blocklim - 1)*TBLOCK; /* all */
2604 2604 }
2605 2605 bytes -= s;
2606 2606 blocks = TBLOCKS(s);
2607 2607
2608 2608 (void) sprintf(dblock.dbuf.size, "%011" FMT_off_t_o, s);
2609 2609 dblock.dbuf.extno = i;
2610 2610 (void) sprintf(dblock.dbuf.chksum, "%07o", checksum(&dblock));
2611 2611 (void) writetbuf((char *)&dblock, 1);
2612 2612
2613 2613 if (vflag)
2614 2614 (void) fprintf(vfile,
2615 2615 gettext("+++ a %s %" FMT_blkcnt_t
2616 2616 "K [extent #%d of %d]\n"),
2617 2617 longname, K(blocks), i, extents);
2618 2618 while (blocks && read(ifd, buf, TBLOCK) > 0) {
2619 2619 blocks--;
2620 2620 (void) writetbuf(buf, 1);
2621 2621 }
2622 2622 if (blocks != 0) {
2623 2623 (void) fprintf(stderr, gettext(
2624 2624 "tar: %s: file changed size\n"), longname);
2625 2625 (void) fprintf(stderr, gettext(
2626 2626 "tar: aborting split file %s\n"), longname);
2627 2627 (void) close(ifd);
2628 2628 return;
2629 2629 }
2630 2630 }
2631 2631 (void) close(ifd);
2632 2632 if (vflag)
2633 2633 (void) fprintf(vfile, gettext("a %s %" FMT_off_t "K (in %d "
2634 2634 "extents)\n"), longname, K(TBLOCKS(stbuf.st_size)),
2635 2635 extents);
2636 2636 }
2637 2637
2638 2638 /*
↓ open down ↓ |
2638 lines elided |
↑ open up ↑ |
2639 2639 * convtoreg - determines whether the file should be converted to a
2640 2640 * regular file when extracted
2641 2641 *
2642 2642 * Returns 1 when file size > 0 and typeflag is not recognized
2643 2643 * Otherwise returns 0
2644 2644 */
2645 2645 static int
2646 2646 convtoreg(off_t size)
2647 2647 {
2648 2648 if ((size > 0) && (dblock.dbuf.typeflag != '0') &&
2649 - (dblock.dbuf.typeflag != NULL) && (dblock.dbuf.typeflag != '1') &&
2649 + (dblock.dbuf.typeflag != '\0') && (dblock.dbuf.typeflag != '1') &&
2650 2650 (dblock.dbuf.typeflag != '2') && (dblock.dbuf.typeflag != '3') &&
2651 2651 (dblock.dbuf.typeflag != '4') && (dblock.dbuf.typeflag != '5') &&
2652 2652 (dblock.dbuf.typeflag != '6') && (dblock.dbuf.typeflag != 'A') &&
2653 2653 (dblock.dbuf.typeflag != 'L') &&
2654 2654 (dblock.dbuf.typeflag != _XATTR_HDRTYPE) &&
2655 2655 (dblock.dbuf.typeflag != 'X')) {
2656 2656 return (1);
2657 2657 }
2658 2658 return (0);
2659 2659 }
2660 2660
2661 2661 #if defined(O_XATTR)
2662 2662 static int
2663 2663 save_cwd(void)
2664 2664 {
2665 2665 return (open(".", O_RDONLY));
2666 2666 }
2667 2667 #endif
2668 2668
2669 2669 #if defined(O_XATTR)
2670 2670 static void
2671 2671 rest_cwd(int *cwd)
2672 2672 {
2673 2673 if (*cwd != -1) {
2674 2674 if (fchdir(*cwd) < 0) {
2675 2675 vperror(0, gettext(
2676 2676 "Cannot fchdir to attribute directory"));
2677 2677 exit(1);
2678 2678 }
2679 2679 (void) close(*cwd);
2680 2680 *cwd = -1;
2681 2681 }
2682 2682 }
2683 2683 #endif
2684 2684
2685 2685 /*
2686 2686 * Verify the underlying file system supports the attribute type.
2687 2687 * Only archive extended attribute files when '-@' was specified.
2688 2688 * Only archive system extended attribute files if '-/' was specified.
2689 2689 */
2690 2690 #if defined(O_XATTR)
2691 2691 static attr_status_t
2692 2692 verify_attr_support(char *filename, int attrflg, arc_action_t actflag,
2693 2693 int *ext_attrflg)
2694 2694 {
2695 2695 /*
2696 2696 * Verify extended attributes are supported/exist. We only
2697 2697 * need to check if we are processing a base file, not an
2698 2698 * extended attribute.
2699 2699 */
2700 2700 if (attrflg) {
2701 2701 *ext_attrflg = (pathconf(filename, (actflag == ARC_CREATE) ?
2702 2702 _PC_XATTR_EXISTS : _PC_XATTR_ENABLED) == 1);
2703 2703 }
2704 2704
2705 2705 if (atflag) {
2706 2706 if (!*ext_attrflg) {
2707 2707 #if defined(_PC_SATTR_ENABLED)
2708 2708 if (saflag) {
2709 2709 /* Verify system attributes are supported */
2710 2710 if (sysattr_support(filename,
2711 2711 (actflag == ARC_CREATE) ? _PC_SATTR_EXISTS :
2712 2712 _PC_SATTR_ENABLED) != 1) {
2713 2713 return (ATTR_SATTR_ERR);
2714 2714 }
2715 2715 } else
2716 2716 return (ATTR_XATTR_ERR);
2717 2717 #else
2718 2718 return (ATTR_XATTR_ERR);
2719 2719 #endif /* _PC_SATTR_ENABLED */
2720 2720 }
2721 2721
2722 2722 #if defined(_PC_SATTR_ENABLED)
2723 2723 } else if (saflag) {
2724 2724 /* Verify system attributes are supported */
2725 2725 if (sysattr_support(filename, (actflag == ARC_CREATE) ?
2726 2726 _PC_SATTR_EXISTS : _PC_SATTR_ENABLED) != 1) {
2727 2727 return (ATTR_SATTR_ERR);
2728 2728 }
2729 2729 #endif /* _PC_SATTR_ENABLED */
2730 2730 } else {
2731 2731 return (ATTR_SKIP);
2732 2732 }
2733 2733
2734 2734 return (ATTR_OK);
2735 2735 }
2736 2736 #endif
2737 2737
2738 2738 #if defined(O_XATTR)
2739 2739 /*
2740 2740 * Recursively open attribute directories until the attribute directory
2741 2741 * containing the specified attribute, attrname, is opened.
2742 2742 *
2743 2743 * Currently, only 2 directory levels of attributes are supported, (i.e.,
2744 2744 * extended system attributes on extended attributes). The following are
2745 2745 * the possible input combinations:
2746 2746 * 1. Open the attribute directory of the base file (don't change
2747 2747 * into it).
2748 2748 * attrinfo->parent = NULL
2749 2749 * attrname = '.'
2750 2750 * 2. Open the attribute directory of the base file and change into it.
2751 2751 * attrinfo->parent = NULL
2752 2752 * attrname = <attr> | <sys_attr>
2753 2753 * 3. Open the attribute directory of the base file, change into it,
2754 2754 * then recursively call open_attr_dir() to open the attribute's
2755 2755 * parent directory (don't change into it).
2756 2756 * attrinfo->parent = <attr>
2757 2757 * attrname = '.'
2758 2758 * 4. Open the attribute directory of the base file, change into it,
2759 2759 * then recursively call open_attr_dir() to open the attribute's
2760 2760 * parent directory and change into it.
2761 2761 * attrinfo->parent = <attr>
2762 2762 * attrname = <attr> | <sys_attr>
2763 2763 *
2764 2764 * An attribute directory will be opened only if the underlying file system
2765 2765 * supports the attribute type, and if the command line specifications (atflag
2766 2766 * and saflag) enable the processing of the attribute type.
2767 2767 *
2768 2768 * On succesful return, attrinfo->parentfd will be the file descriptor of the
2769 2769 * opened attribute directory. In addition, if the attribute is a read-write
2770 2770 * extended system attribute, attrinfo->rw_sysattr will be set to 1, otherwise
2771 2771 * it will be set to 0.
2772 2772 *
2773 2773 * Possible return values:
2774 2774 * ATTR_OK Successfully opened and, if needed, changed into the
2775 2775 * attribute directory containing attrname.
2776 2776 * ATTR_SKIP The command line specifications don't enable the
2777 2777 * processing of the attribute type.
2778 2778 * ATTR_CHDIR_ERR An error occurred while trying to change into an
2779 2779 * attribute directory.
2780 2780 * ATTR_OPEN_ERR An error occurred while trying to open an
2781 2781 * attribute directory.
2782 2782 * ATTR_XATTR_ERR The underlying file system doesn't support extended
2783 2783 * attributes.
2784 2784 * ATTR_SATTR_ERR The underlying file system doesn't support extended
2785 2785 * system attributes.
2786 2786 */
2787 2787 static int
2788 2788 open_attr_dir(char *attrname, char *dirp, int cwd, attr_data_t *attrinfo)
2789 2789 {
2790 2790 attr_status_t rc;
2791 2791 int firsttime = (attrinfo->attr_parentfd == -1);
2792 2792 int saveerrno;
2793 2793 int ext_attr;
2794 2794
2795 2795 /*
2796 2796 * open_attr_dir() was recursively called (input combination number 4),
2797 2797 * close the previously opened file descriptor as we've already changed
2798 2798 * into it.
2799 2799 */
2800 2800 if (!firsttime) {
2801 2801 (void) close(attrinfo->attr_parentfd);
2802 2802 attrinfo->attr_parentfd = -1;
2803 2803 }
2804 2804
2805 2805 /*
2806 2806 * Verify that the underlying file system supports the restoration
2807 2807 * of the attribute.
2808 2808 */
2809 2809 if ((rc = verify_attr_support(dirp, firsttime, ARC_RESTORE,
2810 2810 &ext_attr)) != ATTR_OK) {
2811 2811 return (rc);
2812 2812 }
2813 2813
2814 2814 /* Open the base file's attribute directory */
2815 2815 if ((attrinfo->attr_parentfd = attropen(dirp, ".", O_RDONLY)) == -1) {
2816 2816 /*
2817 2817 * Save the errno from the attropen so it can be reported
2818 2818 * if the retry of the attropen fails.
2819 2819 */
2820 2820 saveerrno = errno;
2821 2821 if ((attrinfo->attr_parentfd = retry_open_attr(-1, cwd, dirp,
2822 2822 NULL, ".", O_RDONLY, 0)) == -1) {
2823 2823 /*
2824 2824 * Reset typeflag back to real value so passtape
2825 2825 * will skip ahead correctly.
2826 2826 */
2827 2827 dblock.dbuf.typeflag = _XATTR_HDRTYPE;
2828 2828 (void) close(attrinfo->attr_parentfd);
2829 2829 attrinfo->attr_parentfd = -1;
2830 2830 errno = saveerrno;
2831 2831 return (ATTR_OPEN_ERR);
2832 2832 }
2833 2833 }
2834 2834
2835 2835 /*
2836 2836 * Change into the parent attribute's directory unless we are
2837 2837 * processing the hidden attribute directory of the base file itself.
2838 2838 */
2839 2839 if ((Hiddendir == 0) || (firsttime && attrinfo->attr_parent != NULL)) {
2840 2840 if (fchdir(attrinfo->attr_parentfd) != 0) {
2841 2841 saveerrno = errno;
2842 2842 (void) close(attrinfo->attr_parentfd);
2843 2843 attrinfo->attr_parentfd = -1;
2844 2844 errno = saveerrno;
2845 2845 return (ATTR_CHDIR_ERR);
2846 2846 }
2847 2847 }
2848 2848
2849 2849 /* Determine if the attribute should be processed */
2850 2850 if ((rc = verify_attr(attrname, attrinfo->attr_parent, 1,
2851 2851 &attrinfo->attr_rw_sysattr)) != ATTR_OK) {
2852 2852 saveerrno = errno;
2853 2853 (void) close(attrinfo->attr_parentfd);
2854 2854 attrinfo->attr_parentfd = -1;
2855 2855 errno = saveerrno;
2856 2856 return (rc);
2857 2857 }
2858 2858
2859 2859 /*
2860 2860 * If the attribute is an extended attribute, or extended system
2861 2861 * attribute, of an attribute (i.e., <attr>/<sys_attr>), then
2862 2862 * recursively call open_attr_dir() to open the attribute directory
2863 2863 * of the parent attribute.
2864 2864 */
2865 2865 if (firsttime && (attrinfo->attr_parent != NULL)) {
2866 2866 return (open_attr_dir(attrname, attrinfo->attr_parent,
2867 2867 attrinfo->attr_parentfd, attrinfo));
2868 2868 }
2869 2869
2870 2870 return (ATTR_OK);
2871 2871 }
2872 2872 #endif
2873 2873
2874 2874 static void
2875 2875 doxtract(char *argv[])
2876 2876 {
2877 2877 struct stat xtractbuf; /* stat on file after extracting */
2878 2878 blkcnt_t blocks;
2879 2879 off_t bytes;
2880 2880 int ofile;
2881 2881 int newfile; /* Does the file already exist */
2882 2882 int xcnt = 0; /* count # files extracted */
2883 2883 int fcnt = 0; /* count # files in argv list */
2884 2884 int dir;
2885 2885 int dirfd = -1;
2886 2886 int cwd = -1;
2887 2887 int rw_sysattr;
2888 2888 int saveerrno;
2889 2889 uid_t Uid;
2890 2890 char *namep, *dirp, *comp, *linkp; /* for removing absolute paths */
2891 2891 char dirname[PATH_MAX+1];
2892 2892 char templink[PATH_MAX+1]; /* temp link with terminating NULL */
2893 2893 int once = 1;
2894 2894 int error;
2895 2895 int symflag;
2896 2896 int want;
2897 2897 attr_data_t *attrinfo = NULL; /* attribute info */
2898 2898 acl_t *aclp = NULL; /* acl info */
2899 2899 char dot[] = "."; /* dirp for using realpath */
2900 2900 timestruc_t time_zero; /* used for call to doDirTimes */
2901 2901 int dircreate;
2902 2902 int convflag;
2903 2903 time_zero.tv_sec = 0;
2904 2904 time_zero.tv_nsec = 0;
2905 2905
2906 2906 /* reset Trusted Extensions variables */
2907 2907 rpath_flag = 0;
2908 2908 lk_rpath_flag = 0;
2909 2909 dir_flag = 0;
2910 2910 mld_flag = 0;
2911 2911 bslundef(&bs_label);
2912 2912 bsllow(&admin_low);
2913 2913 bslhigh(&admin_high);
2914 2914 orig_namep = 0;
2915 2915
2916 2916 dumping = 0; /* for newvol(), et al: we are not writing */
2917 2917
2918 2918 Uid = getuid();
2919 2919
2920 2920 for (;;) {
2921 2921 convflag = 0;
2922 2922 symflag = 0;
2923 2923 dir = 0;
2924 2924 Hiddendir = 0;
2925 2925 rw_sysattr = 0;
2926 2926 ofile = -1;
2927 2927
2928 2928 if (dirfd != -1) {
2929 2929 (void) close(dirfd);
2930 2930 dirfd = -1;
2931 2931 }
2932 2932 if (ofile != -1) {
2933 2933 if (close(ofile) != 0)
2934 2934 vperror(2, gettext("close error"));
2935 2935 }
2936 2936
2937 2937 #if defined(O_XATTR)
2938 2938 if (cwd != -1) {
2939 2939 rest_cwd(&cwd);
2940 2940 }
2941 2941 #endif
2942 2942
2943 2943 /* namep is set by wantit to point to the full name */
2944 2944 if ((want = wantit(argv, &namep, &dirp, &comp,
2945 2945 &attrinfo)) == 0) {
2946 2946 #if defined(O_XATTR)
2947 2947 if (xattrp != NULL) {
2948 2948 free(xattrhead);
2949 2949 xattrp = NULL;
2950 2950 xattr_linkp = NULL;
2951 2951 xattrhead = NULL;
2952 2952 }
2953 2953 #endif
2954 2954 continue;
2955 2955 }
2956 2956 if (want == -1)
2957 2957 break;
2958 2958
2959 2959 /* Trusted Extensions */
2960 2960 /*
2961 2961 * During tar extract (x):
2962 2962 * If the pathname of the restored file has been
2963 2963 * reconstructed from the ancillary file,
2964 2964 * use it to process the normal file.
2965 2965 */
2966 2966 if (mld_flag) { /* Skip over .MLD. directory */
2967 2967 mld_flag = 0;
2968 2968 passtape();
2969 2969 continue;
2970 2970 }
2971 2971 orig_namep = namep; /* save original */
2972 2972 if (rpath_flag) {
2973 2973 namep = real_path; /* use zone path */
2974 2974 comp = real_path; /* use zone path */
2975 2975 dirp = dot; /* work from the top */
2976 2976 rpath_flag = 0; /* reset */
2977 2977 }
2978 2978
2979 2979 if (dirfd != -1)
2980 2980 (void) close(dirfd);
2981 2981
2982 2982 (void) strcpy(&dirname[0], namep);
2983 2983 dircreate = checkdir(&dirname[0]);
2984 2984
2985 2985 #if defined(O_XATTR)
2986 2986 if (xattrp != NULL) {
2987 2987 int rc;
2988 2988
2989 2989 if (((cwd = save_cwd()) == -1) ||
2990 2990 ((rc = open_attr_dir(comp, dirp, cwd,
2991 2991 attrinfo)) != ATTR_OK)) {
2992 2992 if (cwd == -1) {
2993 2993 vperror(0, gettext(
2994 2994 "unable to save current working "
2995 2995 "directory while processing "
2996 2996 "attribute %s of %s"),
2997 2997 dirp, attrinfo->attr_path);
2998 2998 } else if (rc != ATTR_SKIP) {
2999 2999 (void) fprintf(vfile,
3000 3000 gettext("tar: cannot open "
3001 3001 "%sattribute %s of file %s: %s\n"),
3002 3002 attrinfo->attr_rw_sysattr ? gettext(
3003 3003 "system ") : "",
3004 3004 comp, dirp, strerror(errno));
3005 3005 }
3006 3006 free(xattrhead);
3007 3007 xattrp = NULL;
3008 3008 xattr_linkp = NULL;
3009 3009 xattrhead = NULL;
3010 3010
3011 3011 passtape();
3012 3012 continue;
3013 3013 } else {
3014 3014 dirfd = attrinfo->attr_parentfd;
3015 3015 rw_sysattr = attrinfo->attr_rw_sysattr;
3016 3016 }
3017 3017 } else {
3018 3018 dirfd = open(dirp, O_RDONLY);
3019 3019 }
3020 3020 #else
3021 3021 dirfd = open(dirp, O_RDONLY);
3022 3022 #endif
3023 3023 if (dirfd == -1) {
3024 3024 (void) fprintf(vfile, gettext(
3025 3025 "tar: cannot open %s: %s\n"),
3026 3026 dirp, strerror(errno));
3027 3027 passtape();
3028 3028 continue;
3029 3029 }
3030 3030
3031 3031 if (xhdr_flgs & _X_LINKPATH)
3032 3032 (void) strcpy(templink, Xtarhdr.x_linkpath);
3033 3033 else {
3034 3034 #if defined(O_XATTR)
3035 3035 if (xattrp && dblock.dbuf.typeflag == '1') {
3036 3036 (void) sprintf(templink, "%.*s", NAMSIZ,
3037 3037 xattrp->h_names);
3038 3038 } else {
3039 3039 (void) sprintf(templink, "%.*s", NAMSIZ,
3040 3040 dblock.dbuf.linkname);
3041 3041 }
3042 3042 #else
3043 3043 (void) sprintf(templink, "%.*s", NAMSIZ,
3044 3044 dblock.dbuf.linkname);
3045 3045 #endif
3046 3046 }
3047 3047
3048 3048 if (Fflag) {
3049 3049 if (checkf(namep, is_directory(namep), Fflag) == 0) {
3050 3050 passtape();
3051 3051 continue;
3052 3052 }
3053 3053 }
3054 3054
3055 3055 if (checkw('x', namep) == 0) {
3056 3056 passtape();
3057 3057 continue;
3058 3058 }
3059 3059 if (once) {
3060 3060 if (strcmp(dblock.dbuf.magic, magic_type) == 0) {
3061 3061 if (geteuid() == (uid_t)0) {
3062 3062 checkflag = 1;
3063 3063 pflag = 1;
3064 3064 } else {
3065 3065 /* get file creation mask */
3066 3066 Oumask = umask(0);
3067 3067 (void) umask(Oumask);
3068 3068 }
3069 3069 once = 0;
3070 3070 } else {
3071 3071 if (geteuid() == (uid_t)0) {
3072 3072 pflag = 1;
3073 3073 checkflag = 2;
3074 3074 }
3075 3075 if (!pflag) {
3076 3076 /* get file creation mask */
3077 3077 Oumask = umask(0);
3078 3078 (void) umask(Oumask);
3079 3079 }
3080 3080 once = 0;
3081 3081 }
3082 3082 }
3083 3083
3084 3084 #if defined(O_XATTR)
3085 3085 /*
3086 3086 * Handle extraction of hidden attr dir.
3087 3087 * Dir is automatically created, we only
3088 3088 * need to update mode and perm's.
3089 3089 */
3090 3090 if ((xattrp != NULL) && Hiddendir == 1) {
3091 3091 bytes = stbuf.st_size;
3092 3092 blocks = TBLOCKS(bytes);
3093 3093 if (vflag) {
3094 3094 (void) fprintf(vfile,
3095 3095 "x %s%s%s, %" FMT_off_t " %s, ", namep,
3096 3096 gettext(" attribute "),
3097 3097 xattrapath, bytes,
3098 3098 gettext("bytes"));
3099 3099 if (NotTape)
3100 3100 (void) fprintf(vfile,
3101 3101 "%" FMT_blkcnt_t "K\n", K(blocks));
3102 3102 else
3103 3103 (void) fprintf(vfile, gettext("%"
3104 3104 FMT_blkcnt_t " tape blocks\n"),
3105 3105 blocks);
3106 3106 }
3107 3107
3108 3108 /*
3109 3109 * Set the permissions and mode of the attribute
3110 3110 * unless the attribute is a system attribute (can't
3111 3111 * successfully do this) or the hidden attribute
3112 3112 * directory (".") of an attribute (when the attribute
3113 3113 * is restored, the hidden attribute directory of an
3114 3114 * attribute is transient). Note: when the permissions
3115 3115 * and mode are set for the hidden attribute directory
3116 3116 * of a file on a system supporting extended system
3117 3117 * attributes, even though it returns successfully, it
3118 3118 * will not have any affect since the attribute
3119 3119 * directory is transient.
3120 3120 */
3121 3121 if (attrinfo->attr_parent == NULL) {
3122 3122 if (fchownat(dirfd, ".", stbuf.st_uid,
3123 3123 stbuf.st_gid, 0) != 0) {
3124 3124 vperror(0, gettext(
3125 3125 "%s%s%s: failed to set ownership "
3126 3126 "of attribute directory"), namep,
3127 3127 gettext(" attribute "), xattrapath);
3128 3128 }
3129 3129
3130 3130 if (fchmod(dirfd, stbuf.st_mode) != 0) {
3131 3131 vperror(0, gettext(
3132 3132 "%s%s%s: failed to set permissions "
3133 3133 "of attribute directory"), namep,
3134 3134 gettext(" attribute "), xattrapath);
3135 3135 }
3136 3136 }
3137 3137 goto filedone;
3138 3138 }
3139 3139 #endif
3140 3140
3141 3141 if (dircreate && (!is_posix || dblock.dbuf.typeflag == '5')) {
3142 3142 dir = 1;
3143 3143 if (vflag) {
3144 3144 (void) fprintf(vfile, "x %s, 0 %s, ",
3145 3145 &dirname[0], gettext("bytes"));
3146 3146 if (NotTape)
3147 3147 (void) fprintf(vfile, "0K\n");
3148 3148 else
3149 3149 (void) fprintf(vfile, gettext("%"
3150 3150 FMT_blkcnt_t " tape blocks\n"),
3151 3151 (blkcnt_t)0);
↓ open down ↓ |
492 lines elided |
↑ open up ↑ |
3152 3152 }
3153 3153 goto filedone;
3154 3154 }
3155 3155
3156 3156 if (dblock.dbuf.typeflag == '6') { /* FIFO */
3157 3157 if (rmdir(namep) < 0) {
3158 3158 if (errno == ENOTDIR)
3159 3159 (void) unlink(namep);
3160 3160 }
3161 3161 linkp = templink;
3162 - if (*linkp != NULL) {
3162 + if (*linkp != '\0') {
3163 3163 if (Aflag && *linkp == '/')
3164 3164 linkp++;
3165 3165 if (link(linkp, namep) < 0) {
3166 3166 (void) fprintf(stderr, gettext(
3167 3167 "tar: %s: cannot link\n"), namep);
3168 3168 continue;
3169 3169 }
3170 3170 if (vflag)
3171 3171 (void) fprintf(vfile, gettext(
3172 3172 "x %s linked to %s\n"), namep,
3173 3173 linkp);
3174 3174 xcnt++; /* increment # files extracted */
3175 3175 continue;
3176 3176 }
3177 3177 if (mknod(namep, (int)(Gen.g_mode|S_IFIFO),
3178 3178 (int)Gen.g_devmajor) < 0) {
3179 3179 vperror(0, gettext("%s: mknod failed"), namep);
3180 3180 continue;
3181 3181 }
3182 3182 bytes = stbuf.st_size;
3183 3183 blocks = TBLOCKS(bytes);
3184 3184 if (vflag) {
3185 3185 (void) fprintf(vfile, "x %s, %" FMT_off_t
3186 3186 " %s, ", namep, bytes, gettext("bytes"));
3187 3187 if (NotTape)
3188 3188 (void) fprintf(vfile, "%" FMT_blkcnt_t
3189 3189 "K\n", K(blocks));
3190 3190 else
3191 3191 (void) fprintf(vfile, gettext("%"
3192 3192 FMT_blkcnt_t " tape blocks\n"),
↓ open down ↓ |
20 lines elided |
↑ open up ↑ |
3193 3193 blocks);
3194 3194 }
3195 3195 goto filedone;
3196 3196 }
3197 3197 if (dblock.dbuf.typeflag == '3' && !Uid) { /* CHAR SPECIAL */
3198 3198 if (rmdir(namep) < 0) {
3199 3199 if (errno == ENOTDIR)
3200 3200 (void) unlink(namep);
3201 3201 }
3202 3202 linkp = templink;
3203 - if (*linkp != NULL) {
3203 + if (*linkp != '\0') {
3204 3204 if (Aflag && *linkp == '/')
3205 3205 linkp++;
3206 3206 if (link(linkp, namep) < 0) {
3207 3207 (void) fprintf(stderr, gettext(
3208 3208 "tar: %s: cannot link\n"), namep);
3209 3209 continue;
3210 3210 }
3211 3211 if (vflag)
3212 3212 (void) fprintf(vfile, gettext(
3213 3213 "x %s linked to %s\n"), namep,
3214 3214 linkp);
3215 3215 xcnt++; /* increment # files extracted */
3216 3216 continue;
3217 3217 }
3218 3218 if (mknod(namep, (int)(Gen.g_mode|S_IFCHR),
3219 3219 (int)makedev(Gen.g_devmajor, Gen.g_devminor)) < 0) {
3220 3220 vperror(0, gettext(
3221 3221 "%s: mknod failed"), namep);
3222 3222 continue;
3223 3223 }
3224 3224 bytes = stbuf.st_size;
3225 3225 blocks = TBLOCKS(bytes);
3226 3226 if (vflag) {
3227 3227 (void) fprintf(vfile, "x %s, %" FMT_off_t
3228 3228 " %s, ", namep, bytes, gettext("bytes"));
3229 3229 if (NotTape)
3230 3230 (void) fprintf(vfile, "%" FMT_blkcnt_t
3231 3231 "K\n", K(blocks));
3232 3232 else
3233 3233 (void) fprintf(vfile, gettext("%"
3234 3234 FMT_blkcnt_t " tape blocks\n"),
3235 3235 blocks);
3236 3236 }
3237 3237 goto filedone;
3238 3238 } else if (dblock.dbuf.typeflag == '3' && Uid) {
3239 3239 (void) fprintf(stderr, gettext(
3240 3240 "Can't create special %s\n"), namep);
3241 3241 continue;
↓ open down ↓ |
28 lines elided |
↑ open up ↑ |
3242 3242 }
3243 3243
3244 3244 /* BLOCK SPECIAL */
3245 3245
3246 3246 if (dblock.dbuf.typeflag == '4' && !Uid) {
3247 3247 if (rmdir(namep) < 0) {
3248 3248 if (errno == ENOTDIR)
3249 3249 (void) unlink(namep);
3250 3250 }
3251 3251 linkp = templink;
3252 - if (*linkp != NULL) {
3252 + if (*linkp != '\0') {
3253 3253 if (Aflag && *linkp == '/')
3254 3254 linkp++;
3255 3255 if (link(linkp, namep) < 0) {
3256 3256 (void) fprintf(stderr, gettext(
3257 3257 "tar: %s: cannot link\n"), namep);
3258 3258 continue;
3259 3259 }
3260 3260 if (vflag)
3261 3261 (void) fprintf(vfile, gettext(
3262 3262 "x %s linked to %s\n"), namep,
3263 3263 linkp);
3264 3264 xcnt++; /* increment # files extracted */
3265 3265 continue;
3266 3266 }
3267 3267 if (mknod(namep, (int)(Gen.g_mode|S_IFBLK),
3268 3268 (int)makedev(Gen.g_devmajor, Gen.g_devminor)) < 0) {
3269 3269 vperror(0, gettext("%s: mknod failed"), namep);
3270 3270 continue;
3271 3271 }
3272 3272 bytes = stbuf.st_size;
3273 3273 blocks = TBLOCKS(bytes);
3274 3274 if (vflag) {
3275 3275 (void) fprintf(vfile, gettext("x %s, %"
3276 3276 FMT_off_t " bytes, "), namep, bytes);
3277 3277 if (NotTape)
3278 3278 (void) fprintf(vfile, "%" FMT_blkcnt_t
3279 3279 "K\n", K(blocks));
3280 3280 else
3281 3281 (void) fprintf(vfile, gettext("%"
3282 3282 FMT_blkcnt_t " tape blocks\n"),
3283 3283 blocks);
3284 3284 }
3285 3285 goto filedone;
3286 3286 } else if (dblock.dbuf.typeflag == '4' && Uid) {
3287 3287 (void) fprintf(stderr,
3288 3288 gettext("Can't create special %s\n"), namep);
3289 3289 continue;
3290 3290 }
3291 3291 if (dblock.dbuf.typeflag == '2') { /* symlink */
3292 3292 if ((Tflag) && (lk_rpath_flag == 1))
3293 3293 linkp = lk_real_path;
3294 3294 else
3295 3295 linkp = templink;
3296 3296 if (Aflag && *linkp == '/')
3297 3297 linkp++;
3298 3298 if (rmdir(namep) < 0) {
3299 3299 if (errno == ENOTDIR)
3300 3300 (void) unlink(namep);
3301 3301 }
3302 3302 if (symlink(linkp, namep) < 0) {
3303 3303 vperror(0, gettext("%s: symbolic link failed"),
3304 3304 namep);
3305 3305 continue;
3306 3306 }
3307 3307 if (vflag)
3308 3308 (void) fprintf(vfile, gettext(
3309 3309 "x %s symbolic link to %s\n"),
3310 3310 namep, linkp);
3311 3311
3312 3312 symflag = AT_SYMLINK_NOFOLLOW;
3313 3313 goto filedone;
3314 3314 }
3315 3315 if (dblock.dbuf.typeflag == '1') {
3316 3316 linkp = templink;
3317 3317 if (Aflag && *linkp == '/')
3318 3318 linkp++;
3319 3319 if (unlinkat(dirfd, comp, AT_REMOVEDIR) < 0) {
3320 3320 if (errno == ENOTDIR)
3321 3321 (void) unlinkat(dirfd, comp, 0);
3322 3322 }
3323 3323 #if defined(O_XATTR)
3324 3324 if (xattrp && xattr_linkp) {
3325 3325 if (fchdir(dirfd) < 0) {
3326 3326 vperror(0, gettext(
3327 3327 "Cannot fchdir to attribute "
3328 3328 "directory %s"),
3329 3329 (attrinfo->attr_parent == NULL) ?
3330 3330 dirp : attrinfo->attr_parent);
3331 3331 exit(1);
3332 3332 }
3333 3333
3334 3334 error = link(xattr_linkaname, xattrapath);
3335 3335 } else {
3336 3336 error = link(linkp, namep);
3337 3337 }
3338 3338 #else
3339 3339 error = link(linkp, namep);
3340 3340 #endif
3341 3341
3342 3342 if (error < 0) {
3343 3343 (void) fprintf(stderr, gettext(
3344 3344 "tar: %s%s%s: cannot link\n"),
3345 3345 namep, (xattr_linkp != NULL) ?
3346 3346 gettext(" attribute ") : "",
3347 3347 (xattr_linkp != NULL) ?
3348 3348 xattrapath : "");
3349 3349 continue;
3350 3350 }
3351 3351 if (vflag)
3352 3352 (void) fprintf(vfile, gettext(
3353 3353 "x %s%s%s linked to %s%s%s\n"), namep,
3354 3354 (xattr_linkp != NULL) ?
3355 3355 gettext(" attribute ") : "",
3356 3356 (xattr_linkp != NULL) ?
3357 3357 xattr_linkaname : "",
3358 3358 linkp,
3359 3359 (xattr_linkp != NULL) ?
3360 3360 gettext(" attribute ") : "",
3361 3361 (xattr_linkp != NULL) ? xattrapath : "");
3362 3362 xcnt++; /* increment # files extracted */
3363 3363 #if defined(O_XATTR)
3364 3364 if (xattrp != NULL) {
3365 3365 free(xattrhead);
3366 3366 xattrp = NULL;
3367 3367 xattr_linkp = NULL;
3368 3368 xattrhead = NULL;
3369 3369 }
3370 3370 #endif
3371 3371 continue;
3372 3372 }
3373 3373
3374 3374 /* REGULAR FILES */
3375 3375
3376 3376 if (convtoreg(stbuf.st_size)) {
3377 3377 convflag = 1;
3378 3378 if (errflag) {
3379 3379 (void) fprintf(stderr, gettext(
3380 3380 "tar: %s: typeflag '%c' not recognized\n"),
3381 3381 namep, dblock.dbuf.typeflag);
↓ open down ↓ |
119 lines elided |
↑ open up ↑ |
3382 3382 done(1);
3383 3383 } else {
3384 3384 (void) fprintf(stderr, gettext(
3385 3385 "tar: %s: typeflag '%c' not recognized, "
3386 3386 "converting to regular file\n"), namep,
3387 3387 dblock.dbuf.typeflag);
3388 3388 Errflg = 1;
3389 3389 }
3390 3390 }
3391 3391 if (dblock.dbuf.typeflag == '0' ||
3392 - dblock.dbuf.typeflag == NULL || convflag) {
3392 + dblock.dbuf.typeflag == '\0' || convflag) {
3393 3393 delete_target(dirfd, comp, namep);
3394 3394 linkp = templink;
3395 - if (*linkp != NULL) {
3395 + if (*linkp != '\0') {
3396 3396 if (Aflag && *linkp == '/')
3397 3397 linkp++;
3398 3398 if (link(linkp, comp) < 0) {
3399 3399 (void) fprintf(stderr, gettext(
3400 3400 "tar: %s: cannot link\n"), namep);
3401 3401 continue;
3402 3402 }
3403 3403 if (vflag)
3404 3404 (void) fprintf(vfile, gettext(
3405 3405 "x %s linked to %s\n"), comp,
3406 3406 linkp);
3407 3407 xcnt++; /* increment # files extracted */
3408 3408 #if defined(O_XATTR)
3409 3409 if (xattrp != NULL) {
3410 3410 free(xattrhead);
3411 3411 xattrp = NULL;
3412 3412 xattr_linkp = NULL;
3413 3413 xattrhead = NULL;
3414 3414 }
3415 3415 #endif
3416 3416 continue;
3417 3417 }
3418 3418 newfile = ((fstatat(dirfd, comp,
3419 3419 &xtractbuf, 0) == -1) ? TRUE : FALSE);
3420 3420 ofile = openat(dirfd, comp, O_RDWR|O_CREAT|O_TRUNC,
3421 3421 stbuf.st_mode & MODEMASK);
3422 3422 saveerrno = errno;
3423 3423
3424 3424 #if defined(O_XATTR)
3425 3425 if (xattrp != NULL) {
3426 3426 if (ofile < 0) {
3427 3427 ofile = retry_open_attr(dirfd, cwd,
3428 3428 dirp, attrinfo->attr_parent, comp,
3429 3429 O_RDWR|O_CREAT|O_TRUNC,
3430 3430 stbuf.st_mode & MODEMASK);
3431 3431 }
3432 3432 }
3433 3433 #endif
3434 3434 if (ofile < 0) {
3435 3435 errno = saveerrno;
3436 3436 (void) fprintf(stderr, gettext(
3437 3437 "tar: %s%s%s%s - cannot create\n"),
3438 3438 (xattrp == NULL) ? "" : (rw_sysattr ?
3439 3439 gettext("system attribute ") :
3440 3440 gettext("attribute ")),
3441 3441 (xattrp == NULL) ? "" : xattrapath,
3442 3442 (xattrp == NULL) ? "" : gettext(" of "),
3443 3443 (xattrp == NULL) ? comp : namep);
3444 3444 if (errflag)
3445 3445 done(1);
3446 3446 else
3447 3447 Errflg = 1;
3448 3448 #if defined(O_XATTR)
3449 3449 if (xattrp != NULL) {
3450 3450 dblock.dbuf.typeflag = _XATTR_HDRTYPE;
3451 3451 free(xattrhead);
3452 3452 xattrp = NULL;
3453 3453 xattr_linkp = NULL;
3454 3454 xattrhead = NULL;
3455 3455 }
3456 3456 #endif
3457 3457 passtape();
3458 3458 continue;
3459 3459 }
3460 3460
3461 3461 if (Tflag && (check_ext_attr(namep) == 0)) {
3462 3462 if (errflag)
3463 3463 done(1);
3464 3464 else
3465 3465 Errflg = 1;
3466 3466 passtape();
3467 3467 continue;
3468 3468 }
3469 3469
3470 3470 if (extno != 0) { /* file is in pieces */
3471 3471 if (extotal < 1 || extotal > MAXEXT)
3472 3472 (void) fprintf(stderr, gettext(
3473 3473 "tar: ignoring bad extent info for "
3474 3474 "%s%s%s%s\n"),
3475 3475 (xattrp == NULL) ? "" : (rw_sysattr ?
3476 3476 gettext("system attribute ") :
3477 3477 gettext("attribute ")),
3478 3478 (xattrp == NULL) ? "" : xattrapath,
3479 3479 (xattrp == NULL) ? "" : gettext(" of "),
3480 3480 (xattrp == NULL) ? comp : namep);
3481 3481 else {
3482 3482 /* extract it */
3483 3483 (void) xsfile(rw_sysattr, ofile);
3484 3484 }
3485 3485 }
3486 3486 extno = 0; /* let everyone know file is not split */
3487 3487 bytes = stbuf.st_size;
3488 3488 blocks = TBLOCKS(bytes);
3489 3489 if (vflag) {
3490 3490 (void) fprintf(vfile,
3491 3491 "x %s%s%s, %" FMT_off_t " %s, ",
3492 3492 (xattrp == NULL) ? "" : dirp,
3493 3493 (xattrp == NULL) ? "" : (rw_sysattr ?
3494 3494 gettext(" system attribute ") :
3495 3495 gettext(" attribute ")),
3496 3496 (xattrp == NULL) ? namep : xattrapath, bytes,
3497 3497 gettext("bytes"));
3498 3498 if (NotTape)
3499 3499 (void) fprintf(vfile, "%" FMT_blkcnt_t "K\n",
3500 3500 K(blocks));
3501 3501 else
3502 3502 (void) fprintf(vfile, gettext("%"
3503 3503 FMT_blkcnt_t " tape blocks\n"), blocks);
3504 3504 }
3505 3505
3506 3506 if (xblocks(rw_sysattr, bytes, ofile) != 0) {
3507 3507 #if defined(O_XATTR)
3508 3508 if (xattrp != NULL) {
3509 3509 free(xattrhead);
3510 3510 xattrp = NULL;
3511 3511 xattr_linkp = NULL;
3512 3512 xattrhead = NULL;
3513 3513 }
3514 3514 #endif
3515 3515 continue;
3516 3516 }
3517 3517 filedone:
3518 3518 if (mflag == 0 && !symflag) {
3519 3519 if (dir)
3520 3520 doDirTimes(namep, stbuf.st_mtim);
3521 3521
3522 3522 else
3523 3523 #if defined(O_XATTR)
3524 3524 if (xattrp != NULL) {
3525 3525 /*
3526 3526 * Set the time on the attribute unless
3527 3527 * the attribute is a system attribute
3528 3528 * (can't successfully do this) or the
3529 3529 * hidden attribute directory, "." (the
3530 3530 * time on the hidden attribute
3531 3531 * directory will be updated when
3532 3532 * attributes are restored, otherwise
3533 3533 * it's transient).
3534 3534 */
3535 3535 if (!rw_sysattr && (Hiddendir == 0)) {
3536 3536 setPathTimes(dirfd, comp,
3537 3537 stbuf.st_mtim);
3538 3538 }
3539 3539 } else
3540 3540 setPathTimes(dirfd, comp,
3541 3541 stbuf.st_mtim);
3542 3542 #else
3543 3543 setPathTimes(dirfd, comp, stbuf.st_mtim);
3544 3544 #endif
3545 3545 }
3546 3546
3547 3547 /* moved this code from above */
3548 3548 if (pflag && !symflag && Hiddendir == 0) {
3549 3549 if (xattrp != NULL)
3550 3550 (void) fchmod(ofile, stbuf.st_mode & MODEMASK);
3551 3551 else
3552 3552 (void) chmod(namep, stbuf.st_mode & MODEMASK);
3553 3553 }
3554 3554
3555 3555
3556 3556 /*
3557 3557 * Because ancillary file preceeds the normal file,
3558 3558 * acl info may have been retrieved (in aclp).
3559 3559 * All file types are directed here (go filedone).
3560 3560 * Always restore ACLs if there are ACLs.
3561 3561 */
3562 3562 if (aclp != NULL) {
3563 3563 int ret;
3564 3564
3565 3565 #if defined(O_XATTR)
3566 3566 if (xattrp != NULL) {
3567 3567 if (Hiddendir)
3568 3568 ret = facl_set(dirfd, aclp);
3569 3569 else
3570 3570 ret = facl_set(ofile, aclp);
3571 3571 } else {
3572 3572 ret = acl_set(namep, aclp);
3573 3573 }
3574 3574 #else
3575 3575 ret = acl_set(namep, aclp);
3576 3576 #endif
3577 3577 if (ret < 0) {
3578 3578 if (pflag) {
3579 3579 (void) fprintf(stderr, gettext(
3580 3580 "%s%s%s%s: failed to set acl "
3581 3581 "entries\n"), namep,
3582 3582 (xattrp == NULL) ? "" :
3583 3583 (rw_sysattr ? gettext(
3584 3584 " system attribute ") :
3585 3585 gettext(" attribute ")),
3586 3586 (xattrp == NULL) ? "" :
3587 3587 xattrapath);
3588 3588 }
3589 3589 /* else: silent and continue */
3590 3590 }
↓ open down ↓ |
185 lines elided |
↑ open up ↑ |
3591 3591 acl_free(aclp);
3592 3592 aclp = NULL;
3593 3593 }
3594 3594
3595 3595 if (!oflag)
3596 3596 /* set file ownership */
3597 3597 resugname(dirfd, comp, symflag);
3598 3598
3599 3599 if (pflag && newfile == TRUE && !dir &&
3600 3600 (dblock.dbuf.typeflag == '0' ||
3601 - dblock.dbuf.typeflag == NULL ||
3601 + dblock.dbuf.typeflag == '\0' ||
3602 3602 convflag || dblock.dbuf.typeflag == '1')) {
3603 3603 if (fstat(ofile, &xtractbuf) == -1)
3604 3604 (void) fprintf(stderr, gettext(
3605 3605 "tar: cannot stat extracted file "
3606 3606 "%s%s%s%s\n"),
3607 3607 (xattrp == NULL) ? "" : (rw_sysattr ?
3608 3608 gettext("system attribute ") :
3609 3609 gettext("attribute ")),
3610 3610 (xattrp == NULL) ? "" : xattrapath,
3611 3611 (xattrp == NULL) ? "" :
3612 3612 gettext(" of "), namep);
3613 3613
3614 3614 else if ((xtractbuf.st_mode & (MODEMASK & ~S_IFMT))
3615 3615 != (stbuf.st_mode & (MODEMASK & ~S_IFMT))) {
3616 3616 (void) fprintf(stderr, gettext(
3617 3617 "tar: warning - file permissions have "
3618 3618 "changed for %s%s%s%s (are 0%o, should be "
3619 3619 "0%o)\n"),
3620 3620 (xattrp == NULL) ? "" : (rw_sysattr ?
3621 3621 gettext("system attribute ") :
3622 3622 gettext("attribute ")),
3623 3623 (xattrp == NULL) ? "" : xattrapath,
3624 3624 (xattrp == NULL) ? "" :
3625 3625 gettext(" of "), namep,
3626 3626 xtractbuf.st_mode, stbuf.st_mode);
3627 3627
3628 3628 }
3629 3629 }
3630 3630 #if defined(O_XATTR)
3631 3631 if (xattrp != NULL) {
3632 3632 free(xattrhead);
3633 3633 xattrp = NULL;
3634 3634 xattr_linkp = NULL;
3635 3635 xattrhead = NULL;
3636 3636 }
3637 3637 #endif
3638 3638
3639 3639 if (ofile != -1) {
3640 3640 (void) close(dirfd);
3641 3641 dirfd = -1;
3642 3642 if (close(ofile) != 0)
3643 3643 vperror(2, gettext("close error"));
3644 3644 ofile = -1;
3645 3645 }
3646 3646 xcnt++; /* increment # files extracted */
3647 3647 }
3648 3648
3649 3649 /*
3650 3650 * Process ancillary file.
3651 3651 *
3652 3652 */
3653 3653
3654 3654 if (dblock.dbuf.typeflag == 'A') { /* acl info */
3655 3655 char buf[TBLOCK];
3656 3656 char *secp;
3657 3657 char *tp;
3658 3658 int attrsize;
3659 3659 int cnt;
3660 3660
3661 3661 /* reset Trusted Extensions flags */
3662 3662 dir_flag = 0;
3663 3663 mld_flag = 0;
3664 3664 lk_rpath_flag = 0;
3665 3665 rpath_flag = 0;
3666 3666
3667 3667 if (pflag) {
3668 3668 bytes = stbuf.st_size;
3669 3669 if ((secp = malloc((int)bytes)) == NULL) {
3670 3670 (void) fprintf(stderr, gettext(
3671 3671 "Insufficient memory for acl\n"));
3672 3672 passtape();
3673 3673 continue;
3674 3674 }
3675 3675 tp = secp;
3676 3676 blocks = TBLOCKS(bytes);
3677 3677
3678 3678 /*
3679 3679 * Display a line for each ancillary file.
3680 3680 */
3681 3681 if (vflag && Tflag)
3682 3682 (void) fprintf(vfile, "x %s(A), %"
3683 3683 FMT_blkcnt_t " %s, %"
3684 3684 FMT_blkcnt_t " %s\n",
3685 3685 namep, bytes, gettext("bytes"),
3686 3686 blocks, gettext("tape blocks"));
3687 3687
3688 3688 while (blocks-- > 0) {
3689 3689 readtape(buf);
3690 3690 if (bytes <= TBLOCK) {
3691 3691 (void) memcpy(tp, buf,
3692 3692 (size_t)bytes);
3693 3693 break;
3694 3694 } else {
3695 3695 (void) memcpy(tp, buf,
3696 3696 TBLOCK);
3697 3697 tp += TBLOCK;
3698 3698 }
3699 3699 bytes -= TBLOCK;
3700 3700 }
3701 3701 bytes = stbuf.st_size;
3702 3702 /* got all attributes in secp */
3703 3703 tp = secp;
3704 3704 do {
3705 3705 attr = (struct sec_attr *)tp;
3706 3706 switch (attr->attr_type) {
3707 3707 case UFSD_ACL:
3708 3708 case ACE_ACL:
3709 3709 (void) sscanf(attr->attr_len,
3710 3710 "%7o",
3711 3711 (uint_t *)
3712 3712 &cnt);
3713 3713 /* header is 8 */
3714 3714 attrsize = 8 + (int)strlen(
3715 3715 &attr->attr_info[0]) + 1;
3716 3716 error =
3717 3717 acl_fromtext(
3718 3718 &attr->attr_info[0], &aclp);
3719 3719
3720 3720 if (error != 0) {
3721 3721 (void) fprintf(stderr,
3722 3722 gettext(
3723 3723 "aclfromtext "
3724 3724 "failed: %s\n"),
3725 3725 acl_strerror(
3726 3726 error));
3727 3727 bytes -= attrsize;
3728 3728 break;
3729 3729 }
3730 3730 if (acl_cnt(aclp) != cnt) {
3731 3731 (void) fprintf(stderr,
3732 3732 gettext(
3733 3733 "aclcnt error\n"));
3734 3734 bytes -= attrsize;
3735 3735 break;
3736 3736 }
3737 3737 bytes -= attrsize;
3738 3738 break;
3739 3739
3740 3740 /* Trusted Extensions */
3741 3741
3742 3742 case DIR_TYPE:
3743 3743 case LBL_TYPE:
3744 3744 case APRIV_TYPE:
3745 3745 case FPRIV_TYPE:
3746 3746 case COMP_TYPE:
3747 3747 case LK_COMP_TYPE:
3748 3748 case ATTR_FLAG_TYPE:
3749 3749 attrsize =
3750 3750 sizeof (struct sec_attr) +
3751 3751 strlen(&attr->attr_info[0]);
3752 3752 bytes -= attrsize;
3753 3753 if (Tflag)
3754 3754 extract_attr(&namep,
3755 3755 attr);
3756 3756 break;
3757 3757
3758 3758 default:
3759 3759 (void) fprintf(stderr, gettext(
3760 3760 "unrecognized attr"
3761 3761 " type\n"));
3762 3762 bytes = (off_t)0;
3763 3763 break;
3764 3764 }
3765 3765
3766 3766 /* next attributes */
3767 3767 tp += attrsize;
3768 3768 } while (bytes != 0);
3769 3769 free(secp);
3770 3770 } else {
3771 3771 passtape();
3772 3772 }
3773 3773 } /* acl */
3774 3774
3775 3775 } /* for */
3776 3776
3777 3777 /*
3778 3778 * Ensure that all the directories still on the directory stack
3779 3779 * get their modification times set correctly by flushing the
3780 3780 * stack.
3781 3781 */
3782 3782
3783 3783 doDirTimes(NULL, time_zero);
3784 3784
3785 3785 #if defined(O_XATTR)
3786 3786 if (xattrp != NULL) {
3787 3787 free(xattrhead);
3788 3788 xattrp = NULL;
3789 3789 xattr_linkp = NULL;
3790 3790 xattrhead = NULL;
3791 3791 }
3792 3792 #endif
3793 3793
3794 3794 /*
3795 3795 * Check if the number of files extracted is different from the
3796 3796 * number of files listed on the command line
3797 3797 */
3798 3798 if (fcnt > xcnt) {
3799 3799 (void) fprintf(stderr,
3800 3800 gettext("tar: %d file(s) not extracted\n"),
3801 3801 fcnt-xcnt);
3802 3802 Errflg = 1;
3803 3803 }
3804 3804 }
3805 3805
3806 3806 /*
3807 3807 * xblocks extract file/extent from tape to output file
3808 3808 *
3809 3809 * xblocks(issysattr, bytes, ofile);
3810 3810 *
3811 3811 * issysattr flag set if the files being extracted
3812 3812 * is an extended system attribute file.
3813 3813 * unsigned long long bytes size of extent or file to be extracted
3814 3814 * ofile output file
3815 3815 *
3816 3816 * called by doxtract() and xsfile()
3817 3817 */
3818 3818
3819 3819 static int
3820 3820 xblocks(int issysattr, off_t bytes, int ofile)
3821 3821 {
3822 3822 char *buf;
3823 3823 char tempname[NAMSIZ+1];
3824 3824 size_t maxwrite;
3825 3825 size_t bytesread;
3826 3826 size_t piosize; /* preferred I/O size */
3827 3827 struct stat tsbuf;
3828 3828
3829 3829 /* Don't need to do anything if this is a zero size file */
3830 3830 if (bytes <= 0) {
3831 3831 return (0);
3832 3832 }
3833 3833
3834 3834 /*
3835 3835 * To figure out the size of the buffer used to accumulate data
3836 3836 * from readtape() and to write to the file, we need to determine
3837 3837 * the largest chunk of data to be written to the file at one time.
3838 3838 * This is determined based on the smallest of the following two
3839 3839 * things:
3840 3840 * 1) The size of the archived file.
3841 3841 * 2) The preferred I/O size of the file.
3842 3842 */
3843 3843 if (issysattr || (bytes <= TBLOCK)) {
3844 3844 /*
3845 3845 * Writes to system attribute files must be
3846 3846 * performed in one operation.
3847 3847 */
3848 3848 maxwrite = bytes;
3849 3849 } else {
3850 3850 /*
3851 3851 * fstat() the file to get the preferred I/O size.
3852 3852 * If it fails, then resort back to just writing
3853 3853 * one block at a time.
3854 3854 */
3855 3855 if (fstat(ofile, &tsbuf) == 0) {
3856 3856 piosize = tsbuf.st_blksize;
3857 3857 } else {
3858 3858 piosize = TBLOCK;
3859 3859 }
3860 3860 maxwrite = min(bytes, piosize);
3861 3861 }
3862 3862
3863 3863 /*
3864 3864 * The buffer used to accumulate the data for the write operation
3865 3865 * needs to be the maximum number of bytes to be written rounded up
3866 3866 * to the nearest TBLOCK since readtape reads one block at a time.
3867 3867 */
3868 3868 if ((buf = malloc(TBLOCKS(maxwrite) * TBLOCK)) == NULL) {
3869 3869 fatal(gettext("cannot allocate buffer"));
3870 3870 }
3871 3871
3872 3872 while (bytes > 0) {
3873 3873
3874 3874 /*
3875 3875 * readtape() obtains one block (TBLOCK) of data at a time.
3876 3876 * Accumulate as many blocks of data in buf as we can write
3877 3877 * in one operation.
3878 3878 */
3879 3879 for (bytesread = 0; bytesread < maxwrite; bytesread += TBLOCK) {
3880 3880 readtape(buf + bytesread);
3881 3881 }
3882 3882
3883 3883 if (write(ofile, buf, maxwrite) < 0) {
3884 3884 int saveerrno = errno;
3885 3885
3886 3886 if (xhdr_flgs & _X_PATH)
3887 3887 (void) strlcpy(tempname, Xtarhdr.x_path,
3888 3888 sizeof (tempname));
3889 3889 else
3890 3890 (void) sprintf(tempname, "%.*s", NAMSIZ,
3891 3891 dblock.dbuf.name);
3892 3892 /*
3893 3893 * If the extended system attribute being extracted
3894 3894 * contains attributes that the user needs privileges
3895 3895 * for, then just display a warning message, skip
3896 3896 * the extraction of this file, and return.
3897 3897 */
3898 3898 if ((saveerrno == EPERM) && issysattr) {
3899 3899 (void) fprintf(stderr, gettext(
3900 3900 "tar: unable to extract system attribute "
3901 3901 "%s: insufficient privileges\n"), tempname);
3902 3902 Errflg = 1;
3903 3903 (void) free(buf);
3904 3904 return (1);
3905 3905 } else {
3906 3906 (void) fprintf(stderr, gettext(
3907 3907 "tar: %s: HELP - extract write error\n"),
3908 3908 tempname);
3909 3909 done(2);
3910 3910 }
3911 3911 }
3912 3912 bytes -= maxwrite;
3913 3913
3914 3914 /*
3915 3915 * If we've reached this point and there is still data
3916 3916 * to be written, maxwrite had to have been determined
3917 3917 * by the preferred I/O size. If the number of bytes
3918 3918 * left to write is smaller than the preferred I/O size,
3919 3919 * then we're about to do our final write to the file, so
3920 3920 * just set maxwrite to the number of bytes left to write.
3921 3921 */
3922 3922 if ((bytes > 0) && (bytes < maxwrite)) {
3923 3923 maxwrite = bytes;
3924 3924 }
3925 3925 }
3926 3926 free(buf);
3927 3927
3928 3928 return (0);
3929 3929 }
3930 3930
3931 3931 /*
3932 3932 * xsfile extract split file
3933 3933 *
3934 3934 * xsfile(ofd); ofd = output file descriptor
3935 3935 *
3936 3936 * file extracted and put in ofd via xblocks()
3937 3937 *
3938 3938 * NOTE: only called by doxtract() to extract one large file
3939 3939 */
3940 3940
3941 3941 static union hblock savedblock; /* to ensure same file across volumes */
3942 3942
3943 3943 static int
3944 3944 xsfile(int issysattr, int ofd)
3945 3945 {
3946 3946 int i, c;
3947 3947 int sysattrerr = 0;
3948 3948 char name[PATH_MAX+1]; /* holds name for diagnostics */
3949 3949 int extents, totalext;
3950 3950 off_t bytes, totalbytes;
3951 3951
3952 3952 if (xhdr_flgs & _X_PATH)
3953 3953 (void) strcpy(name, Xtarhdr.x_path);
3954 3954 else
3955 3955 (void) sprintf(name, "%.*s", NAMSIZ, dblock.dbuf.name);
3956 3956
3957 3957 totalbytes = (off_t)0; /* in case we read in half the file */
3958 3958 totalext = 0; /* these keep count */
3959 3959
3960 3960 (void) fprintf(stderr, gettext(
3961 3961 "tar: %s split across %d volumes\n"), name, extotal);
3962 3962
3963 3963 /* make sure we do extractions in order */
3964 3964 if (extno != 1) { /* starting in middle of file? */
3965 3965 (void) printf(gettext(
3966 3966 "tar: first extent read is not #1\n"
3967 3967 "OK to read file beginning with extent #%d (%s/%s) ? "),
3968 3968 extno, yesstr, nostr);
3969 3969 if (yes() == 0) {
3970 3970 canit:
3971 3971 passtape();
3972 3972 if (close(ofd) != 0)
3973 3973 vperror(2, gettext("close error"));
3974 3974 if (sysattrerr) {
3975 3975 return (1);
3976 3976 } else {
3977 3977 return (0);
3978 3978 }
3979 3979 }
3980 3980 }
3981 3981 extents = extotal;
3982 3982 i = extno;
3983 3983 /*CONSTCOND*/
3984 3984 while (1) {
3985 3985 if (xhdr_flgs & _X_SIZE) {
3986 3986 bytes = extsize;
3987 3987 } else {
3988 3988 bytes = stbuf.st_size;
3989 3989 }
3990 3990
3991 3991 if (vflag)
3992 3992 (void) fprintf(vfile, "+++ x %s [%s #%d], %"
3993 3993 FMT_off_t " %s, %ldK\n",
3994 3994 name, gettext("extent"), extno,
3995 3995 bytes, gettext("bytes"),
3996 3996 (long)K(TBLOCKS(bytes)));
3997 3997 if (xblocks(issysattr, bytes, ofd) != 0) {
3998 3998 sysattrerr = 1;
3999 3999 goto canit;
4000 4000 }
4001 4001
4002 4002 totalbytes += bytes;
4003 4003 totalext++;
4004 4004 if (++i > extents)
4005 4005 break;
4006 4006
4007 4007 /* get next volume and verify it's the right one */
4008 4008 copy(&savedblock, &dblock);
4009 4009 tryagain:
4010 4010 newvol();
4011 4011 xhdr_flgs = 0;
4012 4012 getdir();
4013 4013 if (Xhdrflag > 0)
4014 4014 (void) get_xdata(); /* Get x-header & regular hdr */
4015 4015 if ((dblock.dbuf.typeflag != 'A') && (xhdr_flgs != 0)) {
4016 4016 load_info_from_xtarhdr(xhdr_flgs, &Xtarhdr);
4017 4017 xhdr_flgs |= _X_XHDR;
4018 4018 }
4019 4019 if (endtape()) { /* seemingly empty volume */
4020 4020 (void) fprintf(stderr, gettext(
4021 4021 "tar: first record is null\n"));
4022 4022 asknicely:
4023 4023 (void) fprintf(stderr, gettext(
4024 4024 "tar: need volume with extent #%d of %s\n"),
4025 4025 i, name);
4026 4026 goto tryagain;
4027 4027 }
4028 4028 if (notsame()) {
4029 4029 (void) fprintf(stderr, gettext(
4030 4030 "tar: first file on that volume is not "
4031 4031 "the same file\n"));
4032 4032 goto asknicely;
4033 4033 }
4034 4034 if (i != extno) {
4035 4035 (void) fprintf(stderr, gettext(
4036 4036 "tar: extent #%d received out of order\ntar: "
4037 4037 "should be #%d\n"), extno, i);
4038 4038 (void) fprintf(stderr, gettext(
4039 4039 "Ignore error, Abort this file, or "
4040 4040 "load New volume (i/a/n) ? "));
4041 4041 c = response();
4042 4042 if (c == 'a')
4043 4043 goto canit;
4044 4044 if (c != 'i') /* default to new volume */
4045 4045 goto asknicely;
4046 4046 i = extno; /* okay, start from there */
4047 4047 }
4048 4048 }
4049 4049 if (vflag)
4050 4050 (void) fprintf(vfile, gettext(
4051 4051 "x %s (in %d extents), %" FMT_off_t " bytes, %ldK\n"),
4052 4052 name, totalext, totalbytes, (long)K(TBLOCKS(totalbytes)));
4053 4053
4054 4054 return (0);
4055 4055 }
4056 4056
4057 4057
4058 4058 /*
4059 4059 * notsame() check if extract file extent is invalid
4060 4060 *
4061 4061 * returns true if anything differs between savedblock and dblock
4062 4062 * except extno (extent number), checksum, or size (extent size).
4063 4063 * Determines if this header belongs to the same file as the one we're
4064 4064 * extracting.
4065 4065 *
4066 4066 * NOTE: though rather bulky, it is only called once per file
4067 4067 * extension, and it can withstand changes in the definition
4068 4068 * of the header structure.
4069 4069 *
4070 4070 * WARNING: this routine is local to xsfile() above
4071 4071 */
4072 4072
4073 4073 static int
4074 4074 notsame(void)
4075 4075 {
4076 4076 return (
4077 4077 (strncmp(savedblock.dbuf.name, dblock.dbuf.name, NAMSIZ)) ||
4078 4078 (strcmp(savedblock.dbuf.mode, dblock.dbuf.mode)) ||
4079 4079 (strcmp(savedblock.dbuf.uid, dblock.dbuf.uid)) ||
4080 4080 (strcmp(savedblock.dbuf.gid, dblock.dbuf.gid)) ||
4081 4081 (strcmp(savedblock.dbuf.mtime, dblock.dbuf.mtime)) ||
4082 4082 (savedblock.dbuf.typeflag != dblock.dbuf.typeflag) ||
4083 4083 (strncmp(savedblock.dbuf.linkname, dblock.dbuf.linkname, NAMSIZ)) ||
4084 4084 (savedblock.dbuf.extotal != dblock.dbuf.extotal) ||
4085 4085 (strcmp(savedblock.dbuf.efsize, dblock.dbuf.efsize)));
4086 4086 }
4087 4087
4088 4088 static void
4089 4089 dotable(char *argv[])
4090 4090 {
4091 4091 int tcnt = 0; /* count # files tabled */
4092 4092 int fcnt = 0; /* count # files in argv list */
4093 4093 char *namep, *dirp, *comp;
4094 4094 int want;
4095 4095 char aclchar = ' '; /* either blank or '+' */
4096 4096 char templink[PATH_MAX+1];
4097 4097 attr_data_t *attrinfo = NULL;
4098 4098
4099 4099 dumping = 0;
4100 4100
4101 4101 /* if not on magtape, maximize seek speed */
4102 4102 if (NotTape && !bflag) {
4103 4103 #if SYS_BLOCK > TBLOCK
4104 4104 nblock = SYS_BLOCK / TBLOCK;
4105 4105 #else
4106 4106 nblock = 1;
4107 4107 #endif
4108 4108 }
4109 4109
4110 4110 for (;;) {
4111 4111
4112 4112 /* namep is set by wantit to point to the full name */
4113 4113 if ((want = wantit(argv, &namep, &dirp, &comp, &attrinfo)) == 0)
4114 4114 continue;
4115 4115 if (want == -1)
4116 4116 break;
4117 4117 if (dblock.dbuf.typeflag != 'A')
4118 4118 ++tcnt;
4119 4119
4120 4120 if (Fflag) {
4121 4121 if (checkf(namep, is_directory(namep), Fflag) == 0) {
4122 4122 passtape();
4123 4123 continue;
4124 4124 }
4125 4125 }
4126 4126 /*
4127 4127 * ACL support:
4128 4128 * aclchar is introduced to indicate if there are
4129 4129 * acl entries. longt() now takes one extra argument.
4130 4130 */
4131 4131 if (vflag) {
4132 4132 if (dblock.dbuf.typeflag == 'A') {
4133 4133 aclchar = '+';
4134 4134 passtape();
4135 4135 continue;
4136 4136 }
4137 4137 longt(&stbuf, aclchar);
4138 4138 aclchar = ' ';
4139 4139 }
4140 4140
4141 4141
4142 4142 #if defined(O_XATTR)
4143 4143 if (xattrp != NULL) {
4144 4144 int issysattr;
4145 4145 char *bn = basename(attrinfo->attr_path);
4146 4146
4147 4147 /*
4148 4148 * We could use sysattr_type() to test whether or not
4149 4149 * the attribute we are processing is really an
4150 4150 * extended system attribute, which as of this writing
4151 4151 * just does a strcmp(), however, sysattr_type() may
4152 4152 * be changed to issue a pathconf() call instead, which
4153 4153 * would require being changed into the parent attribute
4154 4154 * directory. So instead, just do simple string
4155 4155 * comparisons to see if we are processing an extended
4156 4156 * system attribute.
4157 4157 */
4158 4158 issysattr = is_sysattr(bn);
4159 4159
4160 4160 (void) printf(gettext("%s %sattribute %s"),
4161 4161 xattrp->h_names,
4162 4162 issysattr ? gettext("system ") : "",
4163 4163 attrinfo->attr_path);
4164 4164 } else {
4165 4165 (void) printf("%s", namep);
4166 4166 }
4167 4167 #else
4168 4168 (void) printf("%s", namep);
4169 4169 #endif
4170 4170
4171 4171 if (extno != 0) {
4172 4172 if (vflag) {
4173 4173 /* keep the '\n' for backwards compatibility */
4174 4174 (void) fprintf(vfile, gettext(
4175 4175 "\n [extent #%d of %d]"), extno, extotal);
4176 4176 } else {
4177 4177 (void) fprintf(vfile, gettext(
4178 4178 " [extent #%d of %d]"), extno, extotal);
4179 4179 }
4180 4180 }
4181 4181 if (xhdr_flgs & _X_LINKPATH) {
4182 4182 (void) strcpy(templink, Xtarhdr.x_linkpath);
4183 4183 } else {
4184 4184 #if defined(O_XATTR)
4185 4185 if (xattrp != NULL) {
4186 4186 (void) sprintf(templink,
4187 4187 "file %.*s", NAMSIZ, xattrp->h_names);
4188 4188 } else {
4189 4189 (void) sprintf(templink, "%.*s", NAMSIZ,
4190 4190 dblock.dbuf.linkname);
4191 4191 }
4192 4192 #else
4193 4193 (void) sprintf(templink, "%.*s", NAMSIZ,
4194 4194 dblock.dbuf.linkname);
4195 4195 #endif
4196 4196 templink[NAMSIZ] = '\0';
4197 4197 }
4198 4198 if (dblock.dbuf.typeflag == '1') {
4199 4199 /*
4200 4200 * TRANSLATION_NOTE
4201 4201 * Subject is omitted here.
4202 4202 * Translate this as if
4203 4203 * <subject> linked to %s
4204 4204 */
4205 4205 #if defined(O_XATTR)
4206 4206 if (xattrp != NULL) {
4207 4207 (void) printf(
4208 4208 gettext(" linked to attribute %s"),
4209 4209 xattr_linkp->h_names +
4210 4210 strlen(xattr_linkp->h_names) + 1);
4211 4211 } else {
4212 4212 (void) printf(
4213 4213 gettext(" linked to %s"), templink);
4214 4214 }
4215 4215 #else
4216 4216 (void) printf(
4217 4217 gettext(" linked to %s"), templink);
4218 4218
4219 4219 #endif
4220 4220 }
4221 4221 if (dblock.dbuf.typeflag == '2')
4222 4222 (void) printf(gettext(
4223 4223 /*
4224 4224 * TRANSLATION_NOTE
4225 4225 * Subject is omitted here.
4226 4226 * Translate this as if
4227 4227 * <subject> symbolic link to %s
4228 4228 */
4229 4229 " symbolic link to %s"), templink);
4230 4230 (void) printf("\n");
4231 4231 #if defined(O_XATTR)
4232 4232 if (xattrp != NULL) {
4233 4233 free(xattrhead);
4234 4234 xattrp = NULL;
4235 4235 xattrhead = NULL;
4236 4236 }
4237 4237 #endif
4238 4238 passtape();
4239 4239 }
4240 4240 /*
4241 4241 * Check if the number of files tabled is different from the
4242 4242 * number of files listed on the command line
4243 4243 */
4244 4244 if (fcnt > tcnt) {
4245 4245 (void) fprintf(stderr, gettext(
4246 4246 "tar: %d file(s) not found\n"), fcnt-tcnt);
4247 4247 Errflg = 1;
4248 4248 }
4249 4249 }
4250 4250
4251 4251 static void
4252 4252 putempty(blkcnt_t n)
4253 4253 {
4254 4254 char buf[TBLOCK];
4255 4255 char *cp;
4256 4256
4257 4257 for (cp = buf; cp < &buf[TBLOCK]; )
4258 4258 *cp++ = '\0';
4259 4259 while (n-- > 0)
4260 4260 (void) writetbuf(buf, 1);
4261 4261 }
4262 4262
4263 4263 static ushort_t Ftype = S_IFMT;
4264 4264
4265 4265 static void
4266 4266 verbose(struct stat *st, char aclchar)
4267 4267 {
4268 4268 int i, j, temp;
4269 4269 mode_t mode;
4270 4270 char modestr[12];
4271 4271
4272 4272 for (i = 0; i < 11; i++)
4273 4273 modestr[i] = '-';
4274 4274 modestr[i] = '\0';
4275 4275
4276 4276 /* a '+' sign is printed if there is ACL */
4277 4277 modestr[i-1] = aclchar;
4278 4278
4279 4279 mode = st->st_mode;
4280 4280 for (i = 0; i < 3; i++) {
4281 4281 temp = (mode >> (6 - (i * 3)));
4282 4282 j = (i * 3) + 1;
4283 4283 if (S_IROTH & temp)
4284 4284 modestr[j] = 'r';
4285 4285 if (S_IWOTH & temp)
4286 4286 modestr[j + 1] = 'w';
4287 4287 if (S_IXOTH & temp)
4288 4288 modestr[j + 2] = 'x';
4289 4289 }
4290 4290 temp = st->st_mode & Ftype;
4291 4291 switch (temp) {
4292 4292 case (S_IFIFO):
4293 4293 modestr[0] = 'p';
4294 4294 break;
4295 4295 case (S_IFCHR):
4296 4296 modestr[0] = 'c';
4297 4297 break;
4298 4298 case (S_IFDIR):
4299 4299 modestr[0] = 'd';
4300 4300 break;
4301 4301 case (S_IFBLK):
4302 4302 modestr[0] = 'b';
4303 4303 break;
4304 4304 case (S_IFREG): /* was initialized to '-' */
4305 4305 break;
4306 4306 case (S_IFLNK):
4307 4307 modestr[0] = 'l';
4308 4308 break;
4309 4309 default:
4310 4310 /* This field may be zero in old archives. */
4311 4311 if (is_posix && dblock.dbuf.typeflag != '1') {
4312 4312 /*
4313 4313 * For POSIX compliant archives, the mode field
4314 4314 * consists of 12 bits, ie: the file type bits
4315 4315 * are not stored in dblock.dbuf.mode.
4316 4316 * For files other than hard links, getdir() sets
4317 4317 * the file type bits in the st_mode field of the
4318 4318 * stat structure based upon dblock.dbuf.typeflag.
4319 4319 */
4320 4320 (void) fprintf(stderr, gettext(
4321 4321 "tar: impossible file type"));
4322 4322 }
4323 4323 }
4324 4324
4325 4325 if ((S_ISUID & Gen.g_mode) == S_ISUID)
4326 4326 modestr[3] = 's';
4327 4327 if ((S_ISVTX & Gen.g_mode) == S_ISVTX)
4328 4328 modestr[9] = 't';
4329 4329 if ((S_ISGID & Gen.g_mode) == S_ISGID && modestr[6] == 'x')
4330 4330 modestr[6] = 's';
4331 4331 else if ((S_ENFMT & Gen.g_mode) == S_ENFMT && modestr[6] != 'x')
4332 4332 modestr[6] = 'l';
4333 4333 (void) fprintf(vfile, "%s", modestr);
4334 4334 }
4335 4335
4336 4336 static void
4337 4337 longt(struct stat *st, char aclchar)
4338 4338 {
4339 4339 char fileDate[30];
4340 4340 struct tm *tm;
4341 4341
4342 4342 verbose(st, aclchar);
4343 4343 (void) fprintf(vfile, "%3ld/%-3ld", st->st_uid, st->st_gid);
4344 4344
4345 4345 if (dblock.dbuf.typeflag == '2') {
4346 4346 if (xhdr_flgs & _X_LINKPATH)
4347 4347 st->st_size = (off_t)strlen(Xtarhdr.x_linkpath);
4348 4348 else
4349 4349 st->st_size = (off_t)(memchr(dblock.dbuf.linkname,
4350 4350 '\0', NAMSIZ) ?
4351 4351 (strlen(dblock.dbuf.linkname)) : (NAMSIZ));
4352 4352 }
4353 4353 (void) fprintf(vfile, " %6" FMT_off_t, st->st_size);
4354 4354
4355 4355 tm = localtime(&(st->st_mtime));
4356 4356 (void) strftime(fileDate, sizeof (fileDate),
4357 4357 dcgettext((const char *)0, "%b %e %R %Y", LC_TIME), tm);
4358 4358 (void) fprintf(vfile, " %s ", fileDate);
4359 4359 }
4360 4360
4361 4361
4362 4362 /*
4363 4363 * checkdir - Attempt to ensure that the path represented in name
4364 4364 * exists, and return 1 if this is true and name itself is a
4365 4365 * directory.
4366 4366 * Return 0 if this path cannot be created or if name is not
4367 4367 * a directory.
4368 4368 */
4369 4369
4370 4370 static int
4371 4371 checkdir(char *name)
4372 4372 {
4373 4373 char lastChar; /* the last character in name */
4374 4374 char *cp; /* scratch pointer into name */
4375 4375 char *firstSlash = NULL; /* first slash in name */
4376 4376 char *lastSlash = NULL; /* last slash in name */
4377 4377 int nameLen; /* length of name */
4378 4378 int trailingSlash; /* true if name ends in slash */
4379 4379 int leadingSlash; /* true if name begins with slash */
4380 4380 int markedDir; /* true if name denotes a directory */
4381 4381 int success; /* status of makeDir call */
4382 4382
4383 4383
4384 4384 /*
4385 4385 * Scan through the name, and locate first and last slashes.
4386 4386 */
4387 4387
4388 4388 for (cp = name; *cp; cp++) {
4389 4389 if (*cp == '/') {
4390 4390 if (! firstSlash) {
4391 4391 firstSlash = cp;
4392 4392 }
4393 4393 lastSlash = cp;
4394 4394 }
4395 4395 }
4396 4396
4397 4397 /*
4398 4398 * Determine what you can from the proceeds of the scan.
4399 4399 */
4400 4400
4401 4401 lastChar = *(cp - 1);
4402 4402 nameLen = (int)(cp - name);
4403 4403 trailingSlash = (lastChar == '/');
4404 4404 leadingSlash = (*name == '/');
4405 4405 markedDir = (dblock.dbuf.typeflag == '5' || trailingSlash);
4406 4406
4407 4407 if (! lastSlash && ! markedDir) {
4408 4408 /*
4409 4409 * The named file does not have any subdrectory
4410 4410 * structure; just bail out.
4411 4411 */
4412 4412
4413 4413 return (0);
4414 4414 }
4415 4415
4416 4416 /*
4417 4417 * Make sure that name doesn`t end with slash for the loop.
4418 4418 * This ensures that the makeDir attempt after the loop is
4419 4419 * meaningful.
4420 4420 */
4421 4421
4422 4422 if (trailingSlash) {
4423 4423 name[nameLen-1] = '\0';
4424 4424 }
4425 4425
4426 4426 /*
4427 4427 * Make the path one component at a time.
4428 4428 */
4429 4429
4430 4430 for (cp = strchr(leadingSlash ? name+1 : name, '/');
4431 4431 cp;
4432 4432 cp = strchr(cp+1, '/')) {
4433 4433 *cp = '\0';
4434 4434 success = makeDir(name);
4435 4435 *cp = '/';
4436 4436
4437 4437 if (!success) {
4438 4438 name[nameLen-1] = lastChar;
4439 4439 return (0);
4440 4440 }
4441 4441 }
4442 4442
4443 4443 /*
4444 4444 * This makes the last component of the name, if it is a
4445 4445 * directory.
4446 4446 */
4447 4447
4448 4448 if (markedDir) {
4449 4449 if (! makeDir(name)) {
4450 4450 name[nameLen-1] = lastChar;
4451 4451 return (0);
4452 4452 }
4453 4453 }
4454 4454
4455 4455 name[nameLen-1] = (lastChar == '/') ? '\0' : lastChar;
4456 4456 return (markedDir);
4457 4457 }
4458 4458
4459 4459 /*
4460 4460 * resugname - Restore the user name and group name. Search the NIS
4461 4461 * before using the uid and gid.
4462 4462 * (It is presumed that an archive entry cannot be
4463 4463 * simultaneously a symlink and some other type.)
4464 4464 */
4465 4465
4466 4466 static void
4467 4467 resugname(int dirfd, /* dir fd file resides in */
4468 4468 char *name, /* name of the file to be modified */
4469 4469 int symflag) /* true if file is a symbolic link */
4470 4470 {
4471 4471 uid_t duid;
4472 4472 gid_t dgid;
4473 4473 struct stat *sp = &stbuf;
4474 4474 char *u_g_name;
4475 4475
4476 4476 if (checkflag == 1) { /* Extended tar format and euid == 0 */
4477 4477
4478 4478 /*
4479 4479 * Try and extract the intended uid and gid from the name
4480 4480 * service before believing the uid and gid in the header.
4481 4481 *
4482 4482 * In the case where we archived a setuid or setgid file
4483 4483 * owned by someone with a large uid, then it will
4484 4484 * have made it into the archive with a uid of nobody. If
4485 4485 * the corresponding username doesn't appear to exist, then we
4486 4486 * want to make sure it *doesn't* end up as setuid nobody!
4487 4487 *
4488 4488 * Our caller will print an error message about the fact
4489 4489 * that the restore didn't work out quite right ..
4490 4490 */
4491 4491 if (xhdr_flgs & _X_UNAME)
4492 4492 u_g_name = Xtarhdr.x_uname;
4493 4493 else
4494 4494 u_g_name = dblock.dbuf.uname;
4495 4495 if ((duid = getuidbyname(u_g_name)) == -1) {
4496 4496 if (S_ISREG(sp->st_mode) && sp->st_uid == UID_NOBODY &&
4497 4497 (sp->st_mode & S_ISUID) == S_ISUID)
4498 4498 (void) chmod(name,
4499 4499 MODEMASK & sp->st_mode & ~S_ISUID);
4500 4500 duid = sp->st_uid;
4501 4501 }
4502 4502
4503 4503 /* (Ditto for gids) */
4504 4504
4505 4505 if (xhdr_flgs & _X_GNAME)
4506 4506 u_g_name = Xtarhdr.x_gname;
4507 4507 else
4508 4508 u_g_name = dblock.dbuf.gname;
4509 4509 if ((dgid = getgidbyname(u_g_name)) == -1) {
4510 4510 if (S_ISREG(sp->st_mode) && sp->st_gid == GID_NOBODY &&
4511 4511 (sp->st_mode & S_ISGID) == S_ISGID)
4512 4512 (void) chmod(name,
4513 4513 MODEMASK & sp->st_mode & ~S_ISGID);
4514 4514 dgid = sp->st_gid;
4515 4515 }
4516 4516 } else if (checkflag == 2) { /* tar format and euid == 0 */
4517 4517 duid = sp->st_uid;
4518 4518 dgid = sp->st_gid;
4519 4519 }
4520 4520 if ((checkflag == 1) || (checkflag == 2))
4521 4521 (void) fchownat(dirfd, name, duid, dgid, symflag);
4522 4522 }
4523 4523
4524 4524 /*ARGSUSED*/
4525 4525 static void
4526 4526 onintr(int sig)
4527 4527 {
4528 4528 (void) signal(SIGINT, SIG_IGN);
4529 4529 term++;
4530 4530 }
4531 4531
4532 4532 /*ARGSUSED*/
4533 4533 static void
4534 4534 onquit(int sig)
4535 4535 {
4536 4536 (void) signal(SIGQUIT, SIG_IGN);
4537 4537 term++;
4538 4538 }
4539 4539
4540 4540 /*ARGSUSED*/
4541 4541 static void
4542 4542 onhup(int sig)
4543 4543 {
4544 4544 (void) signal(SIGHUP, SIG_IGN);
4545 4545 term++;
4546 4546 }
4547 4547
4548 4548 static void
4549 4549 tomodes(struct stat *sp)
4550 4550 {
4551 4551 uid_t uid;
4552 4552 gid_t gid;
4553 4553
4554 4554 bzero(dblock.dummy, TBLOCK);
4555 4555
4556 4556 /*
4557 4557 * If the uid or gid is too large, we can't put it into
4558 4558 * the archive. We could fail to put anything in the
4559 4559 * archive at all .. but most of the time the name service
4560 4560 * will save the day when we do a lookup at restore time.
4561 4561 *
4562 4562 * Instead we choose a "safe" uid and gid, and fix up whether
4563 4563 * or not the setuid and setgid bits are left set to extraction
4564 4564 * time.
4565 4565 */
4566 4566 if (Eflag) {
4567 4567 if ((ulong_t)(uid = sp->st_uid) > (ulong_t)OCTAL7CHAR) {
4568 4568 xhdr_flgs |= _X_UID;
4569 4569 Xtarhdr.x_uid = uid;
4570 4570 }
4571 4571 if ((ulong_t)(gid = sp->st_gid) > (ulong_t)OCTAL7CHAR) {
4572 4572 xhdr_flgs |= _X_GID;
4573 4573 Xtarhdr.x_gid = gid;
4574 4574 }
4575 4575 if (sp->st_size > TAR_OFFSET_MAX) {
4576 4576 xhdr_flgs |= _X_SIZE;
4577 4577 Xtarhdr.x_filesz = sp->st_size;
4578 4578 (void) sprintf(dblock.dbuf.size, "%011" FMT_off_t_o,
4579 4579 (off_t)0);
4580 4580 } else
4581 4581 (void) sprintf(dblock.dbuf.size, "%011" FMT_off_t_o,
4582 4582 sp->st_size);
4583 4583 } else {
4584 4584 (void) sprintf(dblock.dbuf.size, "%011" FMT_off_t_o,
4585 4585 sp->st_size);
4586 4586 }
4587 4587 if ((ulong_t)(uid = sp->st_uid) > (ulong_t)OCTAL7CHAR)
4588 4588 uid = UID_NOBODY;
4589 4589 if ((ulong_t)(gid = sp->st_gid) > (ulong_t)OCTAL7CHAR)
4590 4590 gid = GID_NOBODY;
4591 4591 (void) sprintf(dblock.dbuf.gid, "%07lo", gid);
4592 4592 (void) sprintf(dblock.dbuf.uid, "%07lo", uid);
4593 4593 (void) sprintf(dblock.dbuf.mode, "%07lo", sp->st_mode & POSIXMODES);
4594 4594 (void) sprintf(dblock.dbuf.mtime, "%011lo", sp->st_mtime);
4595 4595 }
4596 4596
4597 4597 static int
4598 4598 #ifdef EUC
4599 4599 /*
4600 4600 * Warning: the result of this function depends whether 'char' is a
4601 4601 * signed or unsigned data type. This a source of potential
4602 4602 * non-portability among heterogeneous systems. It is retained here
4603 4603 * for backward compatibility.
4604 4604 */
4605 4605 checksum_signed(union hblock *dblockp)
4606 4606 #else
4607 4607 checksum(union hblock *dblockp)
4608 4608 #endif /* EUC */
4609 4609 {
4610 4610 int i;
4611 4611 char *cp;
4612 4612
4613 4613 for (cp = dblockp->dbuf.chksum;
4614 4614 cp < &dblockp->dbuf.chksum[sizeof (dblockp->dbuf.chksum)]; cp++)
4615 4615 *cp = ' ';
4616 4616 i = 0;
4617 4617 for (cp = dblockp->dummy; cp < &(dblockp->dummy[TBLOCK]); cp++)
4618 4618 i += *cp;
4619 4619 return (i);
4620 4620 }
4621 4621
4622 4622 #ifdef EUC
4623 4623 /*
4624 4624 * Generate unsigned checksum, regardless of what C compiler is
4625 4625 * used. Survives in the face of arbitrary 8-bit clean filenames,
4626 4626 * e.g., internationalized filenames.
4627 4627 */
4628 4628 static int
4629 4629 checksum(union hblock *dblockp)
4630 4630 {
4631 4631 unsigned i;
4632 4632 unsigned char *cp;
4633 4633
4634 4634 for (cp = (unsigned char *) dblockp->dbuf.chksum;
4635 4635 cp < (unsigned char *)
4636 4636 &(dblockp->dbuf.chksum[sizeof (dblockp->dbuf.chksum)]); cp++)
4637 4637 *cp = ' ';
4638 4638 i = 0;
4639 4639 for (cp = (unsigned char *) dblockp->dummy;
4640 4640 cp < (unsigned char *) &(dblockp->dummy[TBLOCK]); cp++)
4641 4641 i += *cp;
4642 4642
4643 4643 return (i);
4644 4644 }
4645 4645 #endif /* EUC */
4646 4646
4647 4647 /*
4648 4648 * If the w flag is set, output the action to be taken and the name of the
4649 4649 * file. Perform the action if the user response is affirmative.
4650 4650 */
4651 4651
4652 4652 static int
4653 4653 checkw(char c, char *name)
4654 4654 {
4655 4655 if (wflag) {
4656 4656 (void) fprintf(vfile, "%c ", c);
4657 4657 if (vflag)
4658 4658 longt(&stbuf, ' '); /* do we have acl info here */
4659 4659 (void) fprintf(vfile, "%s: ", name);
4660 4660 if (yes() == 1) {
4661 4661 return (1);
4662 4662 }
4663 4663 return (0);
4664 4664 }
4665 4665 return (1);
4666 4666 }
4667 4667
4668 4668 /*
4669 4669 * When the F flag is set, exclude RCS and SCCS directories (and any files
4670 4670 * or directories under them). If F is set twice, also exclude .o files,
4671 4671 * and files names errs, core, and a.out.
4672 4672 *
4673 4673 * Return 0 if file should be excluded, 1 otherwise.
4674 4674 */
4675 4675
4676 4676 static int
4677 4677 checkf(char *longname, int is_dir, int howmuch)
4678 4678 {
4679 4679 static char fullname[PATH_MAX + 1];
4680 4680 char *dir, *name;
4681 4681
4682 4682 #if defined(O_XATTR)
4683 4683 /*
4684 4684 * If there is an xattr_buf structure associated with this file,
4685 4685 * always return 1.
4686 4686 */
4687 4687 if (xattrp) {
4688 4688 return (1);
4689 4689 }
4690 4690 #endif
4691 4691
4692 4692 /*
4693 4693 * First check to see if the base name is an RCS or SCCS directory.
4694 4694 */
4695 4695 if (strlcpy(fullname, longname, sizeof (fullname)) >= sizeof (fullname))
4696 4696 return (1);
4697 4697
4698 4698 name = basename(fullname);
4699 4699 if (is_dir) {
4700 4700 if ((strcmp(name, "SCCS") == 0) || (strcmp(name, "RCS") == 0))
4701 4701 return (0);
4702 4702 }
4703 4703
4704 4704 /*
4705 4705 * If two -F command line options were given then exclude .o files,
4706 4706 * and files named errs, core, and a.out.
4707 4707 */
4708 4708 if (howmuch > 1 && !is_dir) {
4709 4709 size_t l = strlen(name);
4710 4710
4711 4711 if (l >= 3 && name[l - 2] == '.' && name[l - 1] == 'o')
4712 4712 return (0);
4713 4713 if (strcmp(name, "core") == 0 || strcmp(name, "errs") == 0 ||
4714 4714 strcmp(name, "a.out") == 0)
4715 4715 return (0);
4716 4716 }
4717 4717
4718 4718 /*
4719 4719 * At this point, check to see if this file has a parent directory
4720 4720 * named RCS or SCCS. If so, then this file should be excluded too.
4721 4721 * The strcpy() operation is done again, because basename(3C) may
4722 4722 * modify the path string passed to it.
4723 4723 */
4724 4724 if (strlcpy(fullname, longname, sizeof (fullname)) >= sizeof (fullname))
4725 4725 return (1);
4726 4726
4727 4727 dir = dirname(fullname);
4728 4728 while (strcmp(dir, ".") != 0) {
4729 4729 name = basename(dir);
4730 4730 if ((strcmp(name, "SCCS") == 0) || (strcmp(name, "RCS") == 0))
4731 4731 return (0);
4732 4732 dir = dirname(dir);
4733 4733 }
4734 4734
4735 4735 return (1);
4736 4736 }
4737 4737
4738 4738 static int
4739 4739 response(void)
4740 4740 {
4741 4741 int c;
4742 4742
4743 4743 c = getchar();
4744 4744 if (c != '\n')
4745 4745 while (getchar() != '\n')
4746 4746 ;
4747 4747 else c = 'n';
4748 4748 return ((c >= 'A' && c <= 'Z') ? c + ('a'-'A') : c);
4749 4749 }
4750 4750
4751 4751 /* Has file been modified since being put into archive? If so, return > 0. */
4752 4752
4753 4753 static off_t lookup(char *);
4754 4754
4755 4755 static int
4756 4756 checkupdate(char *arg)
4757 4757 {
4758 4758 char name[PATH_MAX+1];
4759 4759 time_t mtime;
4760 4760 long nsecs;
4761 4761 off_t seekp;
4762 4762
4763 4763 rewind(tfile);
4764 4764 if ((seekp = lookup(arg)) < 0)
4765 4765 return (1);
4766 4766 (void) fseek(tfile, seekp, 0);
4767 4767 (void) fscanf(tfile, "%s %ld.%ld", name, &mtime, &nsecs);
4768 4768
4769 4769 /*
4770 4770 * Unless nanoseconds were stored in the file, only use seconds for
4771 4771 * comparison of time. Nanoseconds are stored when -E is specified.
4772 4772 */
4773 4773 if (Eflag == 0)
4774 4774 return (stbuf.st_mtime > mtime);
4775 4775
4776 4776 if ((stbuf.st_mtime < mtime) ||
4777 4777 ((stbuf.st_mtime == mtime) && (stbuf.st_mtim.tv_nsec <= nsecs)))
4778 4778 return (0);
4779 4779 return (1);
4780 4780 }
4781 4781
4782 4782
4783 4783 /*
4784 4784 * newvol get new floppy (or tape) volume
4785 4785 *
4786 4786 * newvol(); resets tapepos and first to TRUE, prompts for
4787 4787 * for new volume, and waits.
4788 4788 * if dumping, end-of-file is written onto the tape.
4789 4789 */
4790 4790
4791 4791 static void
4792 4792 newvol(void)
4793 4793 {
4794 4794 int c;
4795 4795
4796 4796 if (dumping) {
4797 4797 #ifdef DEBUG
4798 4798 DEBUG("newvol called with 'dumping' set\n", 0, 0);
4799 4799 #endif
4800 4800 putempty((blkcnt_t)2); /* 2 EOT marks */
4801 4801 closevol();
4802 4802 flushtape();
4803 4803 sync();
4804 4804 tapepos = 0;
4805 4805 } else
4806 4806 first = TRUE;
4807 4807 if (close(mt) != 0)
4808 4808 vperror(2, gettext("close error"));
4809 4809 mt = 0;
4810 4810 (void) fprintf(stderr, gettext(
4811 4811 "tar: \007please insert new volume, then press RETURN."));
4812 4812 (void) fseek(stdin, (off_t)0, 2); /* scan over read-ahead */
4813 4813 while ((c = getchar()) != '\n' && ! term)
4814 4814 if (c == EOF)
4815 4815 done(Errflg);
4816 4816 if (term)
4817 4817 done(Errflg);
4818 4818
4819 4819 errno = 0;
4820 4820
4821 4821 if (strcmp(usefile, "-") == 0) {
4822 4822 mt = dup(1);
4823 4823 } else {
4824 4824 mt = open(usefile, dumping ? update : 0);
4825 4825 }
4826 4826
4827 4827 if (mt < 0) {
4828 4828 (void) fprintf(stderr, gettext(
4829 4829 "tar: cannot reopen %s (%s)\n"),
4830 4830 dumping ? gettext("output") : gettext("input"), usefile);
4831 4831
4832 4832 #ifdef DEBUG
4833 4833 DEBUG("update=%d, usefile=%s ", update, usefile);
4834 4834 DEBUG("mt=%d, [%s]\n", mt, strerror(errno));
4835 4835 #endif
4836 4836
4837 4837 done(2);
4838 4838 }
4839 4839 }
4840 4840
4841 4841 /*
4842 4842 * Write a trailer portion to close out the current output volume.
4843 4843 */
4844 4844
4845 4845 static void
4846 4846 closevol(void)
4847 4847 {
4848 4848 if (mulvol) {
4849 4849 /*
4850 4850 * blocklim does not count the 2 EOT marks;
4851 4851 * tapepos does count the 2 EOT marks;
4852 4852 * therefore we need the +2 below.
4853 4853 */
4854 4854 putempty(blocklim + (blkcnt_t)2 - tapepos);
4855 4855 }
4856 4856 }
4857 4857
4858 4858 static void
4859 4859 done(int n)
4860 4860 {
4861 4861 /*
4862 4862 * If we were terminated in some way, and we would otherwise have
4863 4863 * exited with a value of 0, adjust to 1, so that external callers
4864 4864 * can determine this by looking at the exit status.
4865 4865 */
4866 4866 if (term && n == 0)
4867 4867 n = 1;
4868 4868
4869 4869 if (tfile != NULL)
4870 4870 (void) unlink(tname);
4871 4871 if (compress_opt != NULL)
4872 4872 (void) free(compress_opt);
4873 4873 if (mt > 0) {
4874 4874 if ((close(mt) != 0) || (fclose(stdout) != 0)) {
4875 4875 perror(gettext("tar: close error"));
4876 4876 exit(2);
4877 4877 }
4878 4878 }
4879 4879 exit(n);
4880 4880 }
4881 4881
4882 4882 /*
4883 4883 * Determine if s1 is a prefix portion of s2 (or the same as s2).
4884 4884 */
4885 4885
4886 4886 static int
4887 4887 is_prefix(char *s1, char *s2)
4888 4888 {
4889 4889 while (*s1)
4890 4890 if (*s1++ != *s2++)
4891 4891 return (0);
4892 4892 if (*s2)
4893 4893 return (*s2 == '/');
4894 4894 return (1);
4895 4895 }
4896 4896
4897 4897 /*
4898 4898 * lookup and bsrch look through tfile entries to find a match for a name.
4899 4899 * The name can be up to PATH_MAX bytes. bsrch compares what it sees between
4900 4900 * a pair of newline chars, so the buffer it uses must be long enough for
4901 4901 * two lines: name and modification time as well as period, newline and space.
4902 4902 *
4903 4903 * A kludge was added to bsrch to take care of matching on the first entry
4904 4904 * in the file--there is no leading newline. So, if we are reading from the
4905 4905 * start of the file, read into byte two and set the first byte to a newline.
4906 4906 * Otherwise, the first entry cannot be matched.
4907 4907 *
4908 4908 */
4909 4909
4910 4910 #define N (2 * (PATH_MAX + TIME_MAX_DIGITS + LONG_MAX_DIGITS + 3))
4911 4911 static off_t
4912 4912 lookup(char *s)
4913 4913 {
4914 4914 int i;
4915 4915 off_t a;
4916 4916
4917 4917 for (i = 0; s[i]; i++)
4918 4918 if (s[i] == ' ')
4919 4919 break;
4920 4920 a = bsrch(s, i, low, high);
4921 4921 return (a);
4922 4922 }
4923 4923
4924 4924 static off_t
4925 4925 bsrch(char *s, int n, off_t l, off_t h)
4926 4926 {
4927 4927 int i, j;
4928 4928 char b[N];
4929 4929 off_t m, m1;
4930 4930
4931 4931
4932 4932 loop:
4933 4933 if (l >= h)
4934 4934 return ((off_t)-1);
4935 4935 m = l + (h-l)/2 - N/2;
4936 4936 if (m < l)
4937 4937 m = l;
4938 4938 (void) fseek(tfile, m, 0);
4939 4939 if (m == 0) {
4940 4940 (void) fread(b+1, 1, N-1, tfile);
4941 4941 b[0] = '\n';
4942 4942 m--;
4943 4943 } else
4944 4944 (void) fread(b, 1, N, tfile);
4945 4945 for (i = 0; i < N; i++) {
4946 4946 if (b[i] == '\n')
4947 4947 break;
4948 4948 m++;
4949 4949 }
4950 4950 if (m >= h)
4951 4951 return ((off_t)-1);
4952 4952 m1 = m;
4953 4953 j = i;
4954 4954 for (i++; i < N; i++) {
4955 4955 m1++;
4956 4956 if (b[i] == '\n')
4957 4957 break;
4958 4958 }
4959 4959 i = cmp(b+j, s, n);
4960 4960 if (i < 0) {
4961 4961 h = m;
4962 4962 goto loop;
4963 4963 }
4964 4964 if (i > 0) {
4965 4965 l = m1;
4966 4966 goto loop;
4967 4967 }
4968 4968 if (m < 0)
4969 4969 m = 0;
4970 4970 return (m);
4971 4971 }
4972 4972
4973 4973 static int
4974 4974 cmp(char *b, char *s, int n)
4975 4975 {
4976 4976 int i;
4977 4977
4978 4978 assert(b[0] == '\n');
4979 4979
4980 4980 for (i = 0; i < n; i++) {
4981 4981 if (b[i+1] > s[i])
4982 4982 return (-1);
4983 4983 if (b[i+1] < s[i])
4984 4984 return (1);
4985 4985 }
4986 4986 return (b[i+1] == ' '? 0 : -1);
4987 4987 }
4988 4988
4989 4989
4990 4990 /*
4991 4991 * seekdisk seek to next file on archive
4992 4992 *
4993 4993 * called by passtape() only
4994 4994 *
4995 4995 * WARNING: expects "nblock" to be set, that is, readtape() to have
4996 4996 * already been called. Since passtape() is only called
4997 4997 * after a file header block has been read (why else would
4998 4998 * we skip to next file?), this is currently safe.
4999 4999 *
5000 5000 * changed to guarantee SYS_BLOCK boundary
5001 5001 */
5002 5002
5003 5003 static void
5004 5004 seekdisk(blkcnt_t blocks)
5005 5005 {
5006 5006 off_t seekval;
5007 5007 #if SYS_BLOCK > TBLOCK
5008 5008 /* handle non-multiple of SYS_BLOCK */
5009 5009 blkcnt_t nxb; /* # extra blocks */
5010 5010 #endif
5011 5011
5012 5012 tapepos += blocks;
5013 5013 #ifdef DEBUG
5014 5014 DEBUG("seekdisk(%" FMT_blkcnt_t ") called\n", blocks, 0);
5015 5015 #endif
5016 5016 if (recno + blocks <= nblock) {
5017 5017 recno += blocks;
5018 5018 return;
5019 5019 }
5020 5020 if (recno > nblock)
5021 5021 recno = nblock;
5022 5022 seekval = (off_t)blocks - (nblock - recno);
5023 5023 recno = nblock; /* so readtape() reads next time through */
5024 5024 #if SYS_BLOCK > TBLOCK
5025 5025 nxb = (blkcnt_t)(seekval % (off_t)(SYS_BLOCK / TBLOCK));
5026 5026 #ifdef DEBUG
5027 5027 DEBUG("xtrablks=%" FMT_blkcnt_t " seekval=%" FMT_blkcnt_t " blks\n",
5028 5028 nxb, seekval);
5029 5029 #endif
5030 5030 if (nxb && nxb > seekval) /* don't seek--we'll read */
5031 5031 goto noseek;
5032 5032 seekval -= nxb; /* don't seek quite so far */
5033 5033 #endif
5034 5034 if (lseek(mt, (off_t)(TBLOCK * seekval), 1) == (off_t)-1) {
5035 5035 (void) fprintf(stderr, gettext(
5036 5036 "tar: device seek error\n"));
5037 5037 done(3);
5038 5038 }
5039 5039 #if SYS_BLOCK > TBLOCK
5040 5040 /* read those extra blocks */
5041 5041 noseek:
5042 5042 if (nxb) {
5043 5043 #ifdef DEBUG
5044 5044 DEBUG("reading extra blocks\n", 0, 0);
5045 5045 #endif
5046 5046 if (read(mt, tbuf, TBLOCK*nblock) < 0) {
5047 5047 (void) fprintf(stderr, gettext(
5048 5048 "tar: read error while skipping file\n"));
5049 5049 done(8);
5050 5050 }
5051 5051 recno = nxb; /* so we don't read in next readtape() */
5052 5052 }
5053 5053 #endif
5054 5054 }
5055 5055
5056 5056 static void
5057 5057 readtape(char *buffer)
5058 5058 {
5059 5059 int i, j;
5060 5060
5061 5061 ++tapepos;
5062 5062 if (recno >= nblock || first) {
5063 5063 if (first) {
5064 5064 /*
5065 5065 * set the number of blocks to read initially, based on
5066 5066 * the defined defaults for the device, or on the
5067 5067 * explicit block factor given.
5068 5068 */
5069 5069 if (bflag || defaults_used || NotTape)
5070 5070 j = nblock;
5071 5071 else
5072 5072 j = NBLOCK;
5073 5073 } else
5074 5074 j = nblock;
5075 5075
5076 5076 if ((i = read(mt, tbuf, TBLOCK*j)) < 0) {
5077 5077 (void) fprintf(stderr, gettext(
5078 5078 "tar: tape read error\n"));
5079 5079 done(3);
5080 5080 /*
5081 5081 * i == 0 and !rflag means that EOF is reached and we are
5082 5082 * trying to update or replace an empty tar file, so exit
5083 5083 * with an error.
5084 5084 *
5085 5085 * If i == 0 and !first and NotTape, it means the pointer
5086 5086 * has gone past the EOF. It could happen if two processes
5087 5087 * try to update the same tar file simultaneously. So exit
5088 5088 * with an error.
5089 5089 */
5090 5090
5091 5091 } else if (i == 0) {
5092 5092 if (first && !rflag) {
5093 5093 (void) fprintf(stderr, gettext(
5094 5094 "tar: blocksize = %d\n"), i);
5095 5095 done(Errflg);
5096 5096 } else if (!first && (!rflag || NotTape)) {
5097 5097 mterr("read", 0, 2);
5098 5098 }
5099 5099 } else if ((!first || Bflag) && i != TBLOCK*j) {
5100 5100 /*
5101 5101 * Short read - try to get the remaining bytes.
5102 5102 */
5103 5103
5104 5104 int remaining = (TBLOCK * j) - i;
5105 5105 char *b = (char *)tbuf + i;
5106 5106 int r;
5107 5107
5108 5108 do {
5109 5109 if ((r = read(mt, b, remaining)) < 0) {
5110 5110 (void) fprintf(stderr,
5111 5111 gettext("tar: tape read error\n"));
5112 5112 done(3);
5113 5113 }
5114 5114 b += r;
5115 5115 remaining -= r;
5116 5116 i += r;
5117 5117 } while (remaining > 0 && r != 0);
5118 5118 }
5119 5119 if (first) {
5120 5120 if ((i % TBLOCK) != 0) {
5121 5121 (void) fprintf(stderr, gettext(
5122 5122 "tar: tape blocksize error\n"));
5123 5123 done(3);
5124 5124 }
5125 5125 i /= TBLOCK;
5126 5126 if (vflag && i != nblock && i != 1) {
5127 5127 if (!NotTape)
5128 5128 (void) fprintf(stderr, gettext(
5129 5129 "tar: blocksize = %d\n"), i);
5130 5130 }
5131 5131
5132 5132 /*
5133 5133 * If we are reading a tape, then a short read is
5134 5134 * understood to signify that the amount read is
5135 5135 * the tape's actual blocking factor. We adapt
5136 5136 * nblock accordingly. There is no reason to do
5137 5137 * this when the device is not blocked.
5138 5138 */
5139 5139
5140 5140 if (!NotTape)
5141 5141 nblock = i;
5142 5142 }
5143 5143 recno = 0;
5144 5144 }
5145 5145
5146 5146 first = FALSE;
5147 5147 copy(buffer, &tbuf[recno++]);
5148 5148 }
5149 5149
5150 5150
5151 5151 /*
5152 5152 * replacement for writetape.
5153 5153 */
5154 5154
5155 5155 static int
5156 5156 writetbuf(char *buffer, int n)
5157 5157 {
5158 5158 int i;
5159 5159
5160 5160 tapepos += n; /* output block count */
5161 5161
5162 5162 if (recno >= nblock) {
5163 5163 i = write(mt, (char *)tbuf, TBLOCK*nblock);
5164 5164 if (i != TBLOCK*nblock)
5165 5165 mterr("write", i, 2);
5166 5166 recno = 0;
5167 5167 }
5168 5168
5169 5169 /*
5170 5170 * Special case: We have an empty tape buffer, and the
5171 5171 * users data size is >= the tape block size: Avoid
5172 5172 * the bcopy and dma direct to tape. BIG WIN. Add the
5173 5173 * residual to the tape buffer.
5174 5174 */
5175 5175 while (recno == 0 && n >= nblock) {
5176 5176 i = (int)write(mt, buffer, TBLOCK*nblock);
5177 5177 if (i != TBLOCK*nblock)
5178 5178 mterr("write", i, 2);
5179 5179 n -= nblock;
5180 5180 buffer += (nblock * TBLOCK);
5181 5181 }
5182 5182
5183 5183 while (n-- > 0) {
5184 5184 (void) memcpy((char *)&tbuf[recno++], buffer, TBLOCK);
5185 5185 buffer += TBLOCK;
5186 5186 if (recno >= nblock) {
5187 5187 i = (int)write(mt, (char *)tbuf, TBLOCK*nblock);
5188 5188 if (i != TBLOCK*nblock)
5189 5189 mterr("write", i, 2);
5190 5190 recno = 0;
5191 5191 }
5192 5192 }
5193 5193
5194 5194 /* Tell the user how much to write to get in sync */
5195 5195 return (nblock - recno);
5196 5196 }
5197 5197
5198 5198 /*
5199 5199 * backtape - reposition tape after reading soft "EOF" record
5200 5200 *
5201 5201 * Backtape tries to reposition the tape back over the EOF
5202 5202 * record. This is for the 'u' and 'r' function letters so that the
5203 5203 * tape can be extended. This code is not well designed, but
5204 5204 * I'm confident that the only callers who care about the
5205 5205 * backspace-over-EOF feature are those involved in 'u' and 'r'.
5206 5206 *
5207 5207 * The proper way to backup the tape is through the use of mtio.
5208 5208 * Earlier spins used lseek combined with reads in a confusing
5209 5209 * maneuver that only worked on 4.x, but shouldn't have, even
5210 5210 * there. Lseeks are explicitly not supported for tape devices.
5211 5211 */
5212 5212
5213 5213 static void
5214 5214 backtape(void)
5215 5215 {
5216 5216 struct mtop mtcmd;
5217 5217 #ifdef DEBUG
5218 5218 DEBUG("backtape() called, recno=%" FMT_blkcnt_t " nblock=%d\n", recno,
5219 5219 nblock);
5220 5220 #endif
5221 5221 /*
5222 5222 * Backup to the position in the archive where the record
5223 5223 * currently sitting in the tbuf buffer is situated.
5224 5224 */
5225 5225
5226 5226 if (NotTape) {
5227 5227 /*
5228 5228 * For non-tape devices, this means lseeking to the
5229 5229 * correct position. The absolute location tapepos-recno
5230 5230 * should be the beginning of the current record.
5231 5231 */
5232 5232
5233 5233 if (lseek(mt, (off_t)(TBLOCK*(tapepos-recno)), SEEK_SET) ==
5234 5234 (off_t)-1) {
5235 5235 (void) fprintf(stderr,
5236 5236 gettext("tar: lseek to end of archive failed\n"));
5237 5237 done(4);
5238 5238 }
5239 5239 } else {
5240 5240 /*
5241 5241 * For tape devices, we backup over the most recently
5242 5242 * read record.
5243 5243 */
5244 5244
5245 5245 mtcmd.mt_op = MTBSR;
5246 5246 mtcmd.mt_count = 1;
5247 5247
5248 5248 if (ioctl(mt, MTIOCTOP, &mtcmd) < 0) {
5249 5249 (void) fprintf(stderr,
5250 5250 gettext("tar: backspace over record failed\n"));
5251 5251 done(4);
5252 5252 }
5253 5253 }
5254 5254
5255 5255 /*
5256 5256 * Decrement the tape and tbuf buffer indices to prepare for the
5257 5257 * coming write to overwrite the soft EOF record.
5258 5258 */
5259 5259
5260 5260 recno--;
5261 5261 tapepos--;
5262 5262 }
5263 5263
5264 5264
5265 5265 /*
5266 5266 * flushtape write buffered block(s) onto tape
5267 5267 *
5268 5268 * recno points to next free block in tbuf. If nonzero, a write is done.
5269 5269 * Care is taken to write in multiples of SYS_BLOCK when device is
5270 5270 * non-magtape in case raw i/o is used.
5271 5271 *
5272 5272 * NOTE: this is called by writetape() to do the actual writing
5273 5273 */
5274 5274
5275 5275 static void
5276 5276 flushtape(void)
5277 5277 {
5278 5278 #ifdef DEBUG
5279 5279 DEBUG("flushtape() called, recno=%" FMT_blkcnt_t "\n", recno, 0);
5280 5280 #endif
5281 5281 if (recno > 0) { /* anything buffered? */
5282 5282 if (NotTape) {
5283 5283 #if SYS_BLOCK > TBLOCK
5284 5284 int i;
5285 5285
5286 5286 /*
5287 5287 * an odd-block write can only happen when
5288 5288 * we are at the end of a volume that is not a tape.
5289 5289 * Here we round recno up to an even SYS_BLOCK
5290 5290 * boundary.
5291 5291 */
5292 5292 if ((i = recno % (SYS_BLOCK / TBLOCK)) != 0) {
5293 5293 #ifdef DEBUG
5294 5294 DEBUG("flushtape() %d rounding blocks\n", i, 0);
5295 5295 #endif
5296 5296 recno += i; /* round up to even SYS_BLOCK */
5297 5297 }
5298 5298 #endif
5299 5299 if (recno > nblock)
5300 5300 recno = nblock;
5301 5301 }
5302 5302 #ifdef DEBUG
5303 5303 DEBUG("writing out %" FMT_blkcnt_t " blocks of %" FMT_blkcnt_t
5304 5304 " bytes\n", (blkcnt_t)(NotTape ? recno : nblock),
5305 5305 (blkcnt_t)(NotTape ? recno : nblock) * TBLOCK);
5306 5306 #endif
5307 5307 if (write(mt, tbuf,
5308 5308 (size_t)(NotTape ? recno : nblock) * TBLOCK) < 0) {
5309 5309 (void) fprintf(stderr, gettext(
5310 5310 "tar: tape write error\n"));
5311 5311 done(2);
5312 5312 }
5313 5313 recno = 0;
5314 5314 }
5315 5315 }
5316 5316
5317 5317 static void
5318 5318 copy(void *dst, void *src)
5319 5319 {
5320 5320 (void) memcpy(dst, src, TBLOCK);
5321 5321 }
5322 5322
5323 5323 /*
5324 5324 * kcheck()
5325 5325 * - checks the validity of size values for non-tape devices
5326 5326 * - if size is zero, mulvol tar is disabled and size is
5327 5327 * assumed to be infinite.
5328 5328 * - returns volume size in TBLOCKS
5329 5329 */
5330 5330
5331 5331 static blkcnt_t
5332 5332 kcheck(char *kstr)
5333 5333 {
5334 5334 blkcnt_t kval;
5335 5335
5336 5336 kval = strtoll(kstr, NULL, 0);
5337 5337 if (kval == (blkcnt_t)0) { /* no multi-volume; size is infinity. */
5338 5338 mulvol = 0; /* definitely not mulvol, but we must */
5339 5339 return (0); /* took out setting of NotTape */
5340 5340 }
5341 5341 if (kval < (blkcnt_t)MINSIZE) {
5342 5342 (void) fprintf(stderr, gettext(
5343 5343 "tar: sizes below %luK not supported (%" FMT_blkcnt_t
5344 5344 ").\n"), (ulong_t)MINSIZE, kval);
5345 5345 (void) fprintf(stderr, gettext(
5346 5346 "bad size entry for %s in %s.\n"),
5347 5347 archive, DEF_FILE);
5348 5348 done(1);
5349 5349 }
5350 5350 mulvol++;
5351 5351 NotTape++; /* implies non-tape */
5352 5352 return (kval * 1024 / TBLOCK); /* convert to TBLOCKS */
5353 5353 }
5354 5354
5355 5355
5356 5356 /*
5357 5357 * bcheck()
5358 5358 * - checks the validity of blocking factors
5359 5359 * - returns blocking factor
5360 5360 */
5361 5361
5362 5362 static int
5363 5363 bcheck(char *bstr)
5364 5364 {
5365 5365 blkcnt_t bval;
5366 5366
5367 5367 bval = strtoll(bstr, NULL, 0);
5368 5368 if ((bval <= 0) || (bval > INT_MAX / TBLOCK)) {
5369 5369 (void) fprintf(stderr, gettext(
5370 5370 "tar: invalid blocksize \"%s\".\n"), bstr);
5371 5371 if (!bflag)
5372 5372 (void) fprintf(stderr, gettext(
5373 5373 "bad blocksize entry for '%s' in %s.\n"),
5374 5374 archive, DEF_FILE);
5375 5375 done(1);
5376 5376 }
5377 5377
5378 5378 return ((int)bval);
5379 5379 }
5380 5380
5381 5381
5382 5382 /*
5383 5383 * defset()
5384 5384 * - reads DEF_FILE for the set of default values specified.
5385 5385 * - initializes 'usefile', 'nblock', and 'blocklim', and 'NotTape'.
5386 5386 * - 'usefile' points to static data, so will be overwritten
5387 5387 * if this routine is called a second time.
5388 5388 * - the pattern specified by 'arch' must be followed by four
5389 5389 * blank-separated fields (1) device (2) blocking,
5390 5390 * (3) size(K), and (4) tape
5391 5391 * for example: archive0=/dev/fd 1 400 n
5392 5392 */
5393 5393
5394 5394 static int
5395 5395 defset(char *arch)
5396 5396 {
5397 5397 char *bp;
5398 5398
5399 5399 if (defopen(DEF_FILE) != 0)
5400 5400 return (FALSE);
5401 5401 if (defcntl(DC_SETFLAGS, (DC_STD & ~(DC_CASE))) == -1) {
5402 5402 (void) fprintf(stderr, gettext(
5403 5403 "tar: error setting parameters for %s.\n"), DEF_FILE);
5404 5404 return (FALSE); /* & following ones too */
5405 5405 }
5406 5406 if ((bp = defread(arch)) == NULL) {
5407 5407 (void) fprintf(stderr, gettext(
5408 5408 "tar: missing or invalid '%s' entry in %s.\n"),
5409 5409 arch, DEF_FILE);
5410 5410 return (FALSE);
5411 5411 }
5412 5412 if ((usefile = strtok(bp, " \t")) == NULL) {
5413 5413 (void) fprintf(stderr, gettext(
5414 5414 "tar: '%s' entry in %s is empty!\n"), arch, DEF_FILE);
5415 5415 return (FALSE);
5416 5416 }
5417 5417 if ((bp = strtok(NULL, " \t")) == NULL) {
5418 5418 (void) fprintf(stderr, gettext(
5419 5419 "tar: block component missing in '%s' entry in %s.\n"),
5420 5420 arch, DEF_FILE);
5421 5421 return (FALSE);
5422 5422 }
5423 5423 nblock = bcheck(bp);
5424 5424 if ((bp = strtok(NULL, " \t")) == NULL) {
5425 5425 (void) fprintf(stderr, gettext(
5426 5426 "tar: size component missing in '%s' entry in %s.\n"),
5427 5427 arch, DEF_FILE);
5428 5428 return (FALSE);
5429 5429 }
5430 5430 blocklim = kcheck(bp);
5431 5431 if ((bp = strtok(NULL, " \t")) != NULL)
5432 5432 NotTape = (*bp == 'n' || *bp == 'N');
5433 5433 else
5434 5434 NotTape = (blocklim != 0);
5435 5435 (void) defopen(NULL);
5436 5436 #ifdef DEBUG
5437 5437 DEBUG("defset: archive='%s'; usefile='%s'\n", arch, usefile);
5438 5438 DEBUG("defset: nblock='%d'; blocklim='%" FMT_blkcnt_t "'\n",
5439 5439 nblock, blocklim);
5440 5440 DEBUG("defset: not tape = %d\n", NotTape, 0);
5441 5441 #endif
5442 5442 return (TRUE);
5443 5443 }
5444 5444
5445 5445
5446 5446 /*
5447 5447 * Following code handles excluded and included files.
5448 5448 * A hash table of file names to be {in,ex}cluded is built.
5449 5449 * For excluded files, before writing or extracting a file
5450 5450 * check to see if it is in the exclude_tbl.
5451 5451 * For included files, the wantit() procedure will check to
5452 5452 * see if the named file is in the include_tbl.
5453 5453 */
5454 5454
5455 5455 static void
5456 5456 build_table(file_list_t *table[], char *file)
5457 5457 {
5458 5458 FILE *fp;
5459 5459 char buf[PATH_MAX + 1];
5460 5460
5461 5461 if ((fp = fopen(file, "r")) == (FILE *)NULL)
5462 5462 vperror(1, gettext("could not open %s"), file);
5463 5463 while (fgets(buf, sizeof (buf), fp) != NULL) {
5464 5464 if (buf[strlen(buf) - 1] == '\n')
5465 5465 buf[strlen(buf) - 1] = '\0';
5466 5466 /* Only add to table if line has something in it */
5467 5467 if (strspn(buf, " \t") != strlen(buf))
5468 5468 add_file_to_table(table, buf);
5469 5469 }
5470 5470 (void) fclose(fp);
5471 5471 }
5472 5472
5473 5473
5474 5474 /*
5475 5475 * Add a file name to the the specified table, if the file name has any
5476 5476 * trailing '/'s then delete them before inserting into the table
5477 5477 */
↓ open down ↓ |
1866 lines elided |
↑ open up ↑ |
5478 5478
5479 5479 static void
5480 5480 add_file_to_table(file_list_t *table[], char *str)
5481 5481 {
5482 5482 char name[PATH_MAX + 1];
5483 5483 unsigned int h;
5484 5484 file_list_t *exp;
5485 5485
5486 5486 (void) strcpy(name, str);
5487 5487 while (name[strlen(name) - 1] == '/') {
5488 - name[strlen(name) - 1] = NULL;
5488 + name[strlen(name) - 1] = '\0';
5489 5489 }
5490 5490
5491 5491 h = hash(name);
5492 5492 if ((exp = (file_list_t *)calloc(sizeof (file_list_t),
5493 5493 sizeof (char))) == NULL) {
5494 5494 (void) fprintf(stderr, gettext(
5495 5495 "tar: out of memory, exclude/include table(entry)\n"));
5496 5496 exit(1);
5497 5497 }
5498 5498
5499 5499 if ((exp->name = strdup(name)) == NULL) {
5500 5500 (void) fprintf(stderr, gettext(
5501 5501 "tar: out of memory, exclude/include table(file name)\n"));
5502 5502 exit(1);
5503 5503 }
5504 5504
5505 5505 exp->next = table[h];
5506 5506 table[h] = exp;
5507 5507 }
5508 5508
5509 5509
5510 5510 /*
5511 5511 * See if a file name or any of the file's parent directories is in the
5512 5512 * specified table, if the file name has any trailing '/'s then delete
5513 5513 * them before searching the table
5514 5514 */
5515 5515
↓ open down ↓ |
17 lines elided |
↑ open up ↑ |
5516 5516 static int
5517 5517 is_in_table(file_list_t *table[], char *str)
5518 5518 {
5519 5519 char name[PATH_MAX + 1];
5520 5520 unsigned int h;
5521 5521 file_list_t *exp;
5522 5522 char *ptr;
5523 5523
5524 5524 (void) strcpy(name, str);
5525 5525 while (name[strlen(name) - 1] == '/') {
5526 - name[strlen(name) - 1] = NULL;
5526 + name[strlen(name) - 1] = '\0';
5527 5527 }
5528 5528
5529 5529 /*
5530 5530 * check for the file name in the passed list
5531 5531 */
5532 5532 h = hash(name);
5533 5533 exp = table[h];
5534 5534 while (exp != NULL) {
5535 5535 if (strcmp(name, exp->name) == 0) {
5536 5536 return (1);
5537 5537 }
5538 5538 exp = exp->next;
5539 5539 }
5540 5540
5541 5541 /*
5542 5542 * check for any parent directories in the file list
5543 5543 */
5544 5544 while ((ptr = strrchr(name, '/'))) {
5545 - *ptr = NULL;
5545 + *ptr = '\0';
5546 5546 h = hash(name);
5547 5547 exp = table[h];
5548 5548 while (exp != NULL) {
5549 5549 if (strcmp(name, exp->name) == 0) {
5550 5550 return (1);
5551 5551 }
5552 5552 exp = exp->next;
5553 5553 }
5554 5554 }
5555 5555
5556 5556 return (0);
5557 5557 }
5558 5558
5559 5559
5560 5560 /*
5561 5561 * Compute a hash from a string.
5562 5562 */
5563 5563
5564 5564 static unsigned int
5565 5565 hash(char *str)
5566 5566 {
5567 5567 char *cp;
5568 5568 unsigned int h;
5569 5569
5570 5570 h = 0;
5571 5571 for (cp = str; *cp; cp++) {
5572 5572 h += *cp;
5573 5573 }
5574 5574 return (h % TABLE_SIZE);
5575 5575 }
5576 5576
5577 5577 static void *
5578 5578 getmem(size_t size)
5579 5579 {
5580 5580 void *p = calloc((unsigned)size, sizeof (char));
5581 5581
5582 5582 if (p == NULL && freemem) {
5583 5583 (void) fprintf(stderr, gettext(
5584 5584 "tar: out of memory, link and directory modtime "
5585 5585 "info lost\n"));
5586 5586 freemem = 0;
5587 5587 if (errflag)
5588 5588 done(1);
5589 5589 else
5590 5590 Errflg = 1;
5591 5591 }
5592 5592 return (p);
5593 5593 }
5594 5594
5595 5595 /*
5596 5596 * vperror() --variable argument perror.
5597 5597 * Takes 3 args: exit_status, formats, args. If exit_status is 0, then
5598 5598 * the errflag (exit on error) is checked -- if it is non-zero, tar exits
5599 5599 * with the value of whatever "errno" is set to. If exit_status is not
5600 5600 * zero, then tar exits with that error status. If errflag and exit_status
5601 5601 * are both zero, the routine returns to where it was called and sets Errflg
5602 5602 * to errno.
5603 5603 */
5604 5604
5605 5605 static void
5606 5606 vperror(int exit_status, char *fmt, ...)
5607 5607 {
5608 5608 va_list ap;
5609 5609
5610 5610 va_start(ap, fmt);
5611 5611 (void) fputs("tar: ", stderr);
5612 5612 (void) vfprintf(stderr, fmt, ap);
5613 5613 (void) fprintf(stderr, ": %s\n", strerror(errno));
5614 5614 va_end(ap);
5615 5615 if (exit_status)
5616 5616 done(exit_status);
5617 5617 else
5618 5618 if (errflag)
5619 5619 done(errno);
5620 5620 else
5621 5621 Errflg = errno;
5622 5622 }
5623 5623
5624 5624
5625 5625 static void
5626 5626 fatal(char *format, ...)
5627 5627 {
5628 5628 va_list ap;
5629 5629
5630 5630 va_start(ap, format);
5631 5631 (void) fprintf(stderr, "tar: ");
5632 5632 (void) vfprintf(stderr, format, ap);
5633 5633 (void) fprintf(stderr, "\n");
5634 5634 va_end(ap);
5635 5635 done(1);
5636 5636 }
5637 5637
5638 5638
5639 5639 /*
5640 5640 * Check to make sure that argument is a char * ptr.
5641 5641 * Actually, we just check to see that it is non-null.
5642 5642 * If it is null, print out the message and call usage(), bailing out.
5643 5643 */
5644 5644
5645 5645 static void
5646 5646 assert_string(char *s, char *msg)
5647 5647 {
5648 5648 if (s == NULL) {
5649 5649 (void) fprintf(stderr, msg);
5650 5650 usage();
5651 5651 }
5652 5652 }
5653 5653
5654 5654
5655 5655 static void
5656 5656 mterr(char *operation, int i, int exitcode)
5657 5657 {
5658 5658 (void) fprintf(stderr, gettext(
5659 5659 "tar: %s error: "), operation);
5660 5660 if (i < 0)
5661 5661 perror("");
5662 5662 else
5663 5663 (void) fprintf(stderr, gettext("unexpected EOF\n"));
5664 5664 done(exitcode);
5665 5665 }
5666 5666
5667 5667 static int
5668 5668 wantit(char *argv[], char **namep, char **dirp, char **component,
5669 5669 attr_data_t **attrinfo)
5670 5670 {
5671 5671 char **cp;
5672 5672 int gotit; /* true if we've found a match */
5673 5673 int ret;
5674 5674
5675 5675 top:
5676 5676 if (xhdr_flgs & _X_XHDR) {
5677 5677 xhdr_flgs = 0;
5678 5678 }
5679 5679 getdir();
5680 5680 if (Xhdrflag > 0) {
5681 5681 ret = get_xdata();
5682 5682 if (ret != 0) { /* Xhdr items and regular header */
5683 5683 setbytes_to_skip(&stbuf, ret);
5684 5684 passtape();
5685 5685 return (0); /* Error--don't want to extract */
5686 5686 }
5687 5687 }
5688 5688
5689 5689 /*
5690 5690 * If typeflag is not 'A' and xhdr_flgs is set, then processing
5691 5691 * of ancillary file is either over or ancillary file
5692 5692 * processing is not required, load info from Xtarhdr and set
5693 5693 * _X_XHDR bit in xhdr_flgs.
5694 5694 */
5695 5695 if ((dblock.dbuf.typeflag != 'A') && (xhdr_flgs != 0)) {
5696 5696 load_info_from_xtarhdr(xhdr_flgs, &Xtarhdr);
5697 5697 xhdr_flgs |= _X_XHDR;
5698 5698 }
5699 5699
5700 5700 #if defined(O_XATTR)
5701 5701 if (dblock.dbuf.typeflag == _XATTR_HDRTYPE && xattrbadhead == 0) {
5702 5702 /*
5703 5703 * Always needs to read the extended header. If atflag, saflag,
5704 5704 * or tflag isn't set, then we'll have the correct info for
5705 5705 * passtape() later.
5706 5706 */
5707 5707 (void) read_xattr_hdr(attrinfo);
5708 5708 goto top;
5709 5709 }
5710 5710 /*
5711 5711 * Now that we've read the extended header, call passtape()
5712 5712 * if we don't want to restore attributes or system attributes.
5713 5713 * Don't restore the attribute if we are extracting
5714 5714 * a file from an archive (as opposed to doing a table of
5715 5715 * contents) and any of the following are true:
5716 5716 * 1. neither -@ or -/ was specified.
5717 5717 * 2. -@ was specified, -/ wasn't specified, and we're
5718 5718 * processing a hidden attribute directory of an attribute
5719 5719 * or we're processing a read-write system attribute file.
5720 5720 * 3. -@ wasn't specified, -/ was specified, and the file
5721 5721 * we're processing is not a read-write system attribute file,
5722 5722 * or we're processing the hidden attribute directory of an
5723 5723 * attribute.
5724 5724 *
5725 5725 * We always process the attributes if we're just generating
5726 5726 * generating a table of contents, or if both -@ and -/ were
5727 5727 * specified.
5728 5728 */
5729 5729 if (xattrp != NULL) {
5730 5730 attr_data_t *ainfo = *attrinfo;
5731 5731
5732 5732 if (!tflag &&
5733 5733 ((!atflag && !saflag) ||
5734 5734 (atflag && !saflag && ((ainfo->attr_parent != NULL) ||
5735 5735 ainfo->attr_rw_sysattr)) ||
5736 5736 (!atflag && saflag && ((ainfo->attr_parent != NULL) ||
5737 5737 !ainfo->attr_rw_sysattr)))) {
5738 5738 passtape();
5739 5739 return (0);
5740 5740 }
5741 5741 }
5742 5742 #endif
5743 5743
5744 5744 /* sets *namep to point at the proper name */
5745 5745 if (check_prefix(namep, dirp, component) != 0) {
5746 5746 passtape();
5747 5747 return (0);
5748 5748 }
5749 5749
5750 5750 if (endtape()) {
5751 5751 if (Bflag) {
5752 5752 /*
5753 5753 * Logically at EOT - consume any extra blocks
5754 5754 * so that write to our stdin won't fail and
5755 5755 * emit an error message; otherwise something
5756 5756 * like "dd if=foo.tar | (cd bar; tar xvf -)"
5757 5757 * will produce a bogus error message from "dd".
5758 5758 */
5759 5759
5760 5760 while (read(mt, tbuf, TBLOCK*nblock) > 0) {
5761 5761 /* empty body */
5762 5762 }
5763 5763 }
5764 5764 return (-1);
5765 5765 }
5766 5766
5767 5767 gotit = 0;
5768 5768
5769 5769 if ((Iflag && is_in_table(include_tbl, *namep)) ||
5770 5770 (! Iflag && *argv == NULL)) {
5771 5771 gotit = 1;
5772 5772 } else {
5773 5773 for (cp = argv; *cp; cp++) {
5774 5774 if (is_prefix(*cp, *namep)) {
5775 5775 gotit = 1;
5776 5776 break;
5777 5777 }
5778 5778 }
5779 5779 }
5780 5780
5781 5781 if (! gotit) {
5782 5782 passtape();
5783 5783 return (0);
5784 5784 }
5785 5785
5786 5786 if (Xflag && is_in_table(exclude_tbl, *namep)) {
5787 5787 if (vflag) {
5788 5788 (void) fprintf(stderr, gettext("%s excluded\n"),
5789 5789 *namep);
5790 5790 }
5791 5791 passtape();
5792 5792 return (0);
5793 5793 }
5794 5794
5795 5795 return (1);
5796 5796 }
5797 5797
5798 5798
5799 5799 static void
5800 5800 setbytes_to_skip(struct stat *st, int err)
5801 5801 {
5802 5802 /*
5803 5803 * In a scenario where a typeflag 'X' was followed by
5804 5804 * a typeflag 'A' and typeflag 'O', then the number of
5805 5805 * bytes to skip should be the size of ancillary file,
5806 5806 * plus the dblock for regular file, and the size
5807 5807 * from Xtarhdr. However, if the typeflag was just 'X'
5808 5808 * followed by typeflag 'O', then the number of bytes
5809 5809 * to skip should be the size from Xtarhdr.
5810 5810 */
5811 5811 if ((err != 0) && (dblock.dbuf.typeflag == 'A') &&
5812 5812 (xhdr_flgs & _X_SIZE)) {
5813 5813 st->st_size += TBLOCK + Xtarhdr.x_filesz;
5814 5814 xhdr_flgs |= _X_XHDR;
5815 5815 } else if ((dblock.dbuf.typeflag != 'A') &&
5816 5816 (xhdr_flgs & _X_SIZE)) {
5817 5817 st->st_size += Xtarhdr.x_filesz;
5818 5818 xhdr_flgs |= _X_XHDR;
5819 5819 }
5820 5820 }
5821 5821
5822 5822 static int
5823 5823 fill_in_attr_info(char *attr, char *longname, char *attrparent, int atparentfd,
5824 5824 int rw_sysattr, attr_data_t **attrinfo)
5825 5825 {
5826 5826 size_t pathlen;
5827 5827 char *tpath;
5828 5828 char *tparent;
5829 5829
5830 5830 /* parent info */
5831 5831 if (attrparent != NULL) {
5832 5832 if ((tparent = strdup(attrparent)) == NULL) {
5833 5833 vperror(0, gettext(
5834 5834 "unable to allocate memory for attribute parent "
5835 5835 "name for %sattribute %s/%s of %s"),
5836 5836 rw_sysattr ? gettext("system ") : "",
5837 5837 attrparent, attr, longname);
5838 5838 return (1);
5839 5839 }
5840 5840 } else {
5841 5841 tparent = NULL;
5842 5842 }
5843 5843
5844 5844 /* path info */
5845 5845 pathlen = strlen(attr) + 1;
5846 5846 if (attrparent != NULL) {
5847 5847 pathlen += strlen(attrparent) + 1; /* add 1 for '/' */
5848 5848 }
5849 5849 if ((tpath = calloc(1, pathlen)) == NULL) {
5850 5850 vperror(0, gettext(
5851 5851 "unable to allocate memory for full "
5852 5852 "attribute path name for %sattribute %s%s%s of %s"),
5853 5853 rw_sysattr ? gettext("system ") : "",
5854 5854 (attrparent == NULL) ? "" : attrparent,
5855 5855 (attrparent == NULL) ? "" : "/",
5856 5856 attr, longname);
5857 5857 if (tparent != NULL) {
5858 5858 free(tparent);
5859 5859 }
5860 5860 return (1);
5861 5861 }
5862 5862 (void) snprintf(tpath, pathlen, "%s%s%s",
5863 5863 (attrparent == NULL) ? "" : attrparent,
5864 5864 (attrparent == NULL) ? "" : "/",
5865 5865 attr);
5866 5866
5867 5867 /* fill in the attribute info */
5868 5868 if (*attrinfo == NULL) {
5869 5869 if ((*attrinfo = malloc(sizeof (attr_data_t))) == NULL) {
5870 5870 vperror(0, gettext(
5871 5871 "unable to allocate memory for attribute "
5872 5872 "information for %sattribute %s%s%s of %s"),
5873 5873 rw_sysattr ? gettext("system ") : "",
5874 5874 (attrparent == NULL) ? "" : attrparent,
5875 5875 (attrparent == NULL) ? "" : gettext("/"),
5876 5876 attr, longname);
5877 5877 if (tparent != NULL) {
5878 5878 free(tparent);
5879 5879 }
5880 5880 free(tpath);
5881 5881 return (1);
5882 5882 }
5883 5883 } else {
5884 5884 if ((*attrinfo)->attr_parent != NULL) {
5885 5885 free((*attrinfo)->attr_parent);
5886 5886 }
5887 5887 if ((*attrinfo)->attr_path != NULL) {
5888 5888 free((*attrinfo)->attr_path);
5889 5889 }
5890 5890 /*
5891 5891 * The parent file descriptor is passed in, so don't
5892 5892 * close it here as it should be closed by the function
5893 5893 * that opened it.
5894 5894 */
5895 5895 }
5896 5896 (*attrinfo)->attr_parent = tparent;
5897 5897 (*attrinfo)->attr_path = tpath;
5898 5898 (*attrinfo)->attr_rw_sysattr = rw_sysattr;
5899 5899 (*attrinfo)->attr_parentfd = atparentfd;
5900 5900
5901 5901 return (0);
5902 5902 }
5903 5903
5904 5904 /*
5905 5905 * Test to see if name is a directory.
5906 5906 *
5907 5907 * Return 1 if true, 0 otherwise.
5908 5908 */
5909 5909
5910 5910 static int
5911 5911 is_directory(char *name)
5912 5912 {
5913 5913 #if defined(O_XATTR)
5914 5914 /*
5915 5915 * If there is an xattr_buf structure associated with this file,
5916 5916 * then the directory test is based on whether the name has a
5917 5917 * trailing slash.
5918 5918 */
5919 5919 if (xattrp)
5920 5920 return (name[strlen(name) - 1] == '/');
5921 5921 #endif
5922 5922 if (is_posix)
5923 5923 return (dblock.dbuf.typeflag == '5');
5924 5924 else
5925 5925 return (name[strlen(name) - 1] == '/');
5926 5926 }
5927 5927
5928 5928 /*
5929 5929 * Version of chdir that handles directory pathnames of greater than PATH_MAX
5930 5930 * length, by changing the working directory to manageable portions of the
5931 5931 * complete directory pathname. If any of these attempts fail, then it exits
5932 5932 * non-zero.
5933 5933 *
5934 5934 * If a segment (i.e. a portion of "path" between two "/"'s) of the overall
5935 5935 * pathname is greater than PATH_MAX, then this still won't work, and this
5936 5936 * routine will return -1 with errno set to ENAMETOOLONG.
5937 5937 *
5938 5938 * NOTE: this routine is semantically different to the system chdir in
5939 5939 * that it is remotely possible for the currently working directory to be
5940 5940 * changed to a different directory, if a chdir call fails when processing
5941 5941 * one of the segments of a path that is greater than PATH_MAX. This isn't
5942 5942 * a problem as this is tar's own specific version of chdir.
5943 5943 */
5944 5944
5945 5945 static int
5946 5946 tar_chdir(const char *path) {
5947 5947 const char *sep = "/";
5948 5948 char *path_copy = NULL;
5949 5949 char *ptr = NULL;
5950 5950
5951 5951 /* The trivial case. */
5952 5952 if (chdir(path) == 0) {
5953 5953 return (0);
5954 5954 }
5955 5955 if (errno == ENAMETOOLONG) {
5956 5956 if (path[0] == '/' && chdir(sep) != 0)
5957 5957 return (-1);
5958 5958
5959 5959 /* strtok(3C) modifies the string, so make a copy. */
5960 5960 if ((path_copy = strdup(path)) == NULL) {
5961 5961 return (-1);
5962 5962 }
5963 5963
5964 5964 /* chdir(2) for every path element. */
5965 5965 for (ptr = strtok(path_copy, sep);
5966 5966 ptr != NULL;
5967 5967 ptr = strtok(NULL, sep)) {
5968 5968 if (chdir(ptr) != 0) {
5969 5969 free(path_copy);
5970 5970 return (-1);
5971 5971 }
5972 5972 }
5973 5973 free(path_copy);
5974 5974 return (0);
5975 5975 }
5976 5976
5977 5977 /* If chdir fails for any reason except ENAMETOOLONG. */
5978 5978 return (-1);
5979 5979 }
5980 5980
5981 5981 /*
5982 5982 * Test if name has a '..' sequence in it.
5983 5983 *
5984 5984 * Return 1 if found, 0 otherwise.
5985 5985 */
5986 5986
5987 5987 static int
5988 5988 has_dot_dot(char *name)
5989 5989 {
5990 5990 char *s;
5991 5991 size_t name_len = strlen(name);
5992 5992
5993 5993 for (s = name; s < (name + name_len - 2); s++) {
5994 5994 if (s[0] == '.' && s[1] == '.' && ((s[2] == '/') || !s[2]))
5995 5995 return (1);
5996 5996
5997 5997 while (! (*s == '/')) {
5998 5998 if (! *s++)
5999 5999 return (0);
6000 6000 }
6001 6001 }
6002 6002
6003 6003 return (0);
6004 6004 }
6005 6005
6006 6006 /*
6007 6007 * Test if name is an absolute path name.
6008 6008 *
6009 6009 * Return 1 if true, 0 otherwise.
6010 6010 */
6011 6011
6012 6012 static int
6013 6013 is_absolute(char *name)
6014 6014 {
6015 6015 #if defined(O_XATTR)
6016 6016 /*
6017 6017 * If this is an extended attribute (whose name will begin with
6018 6018 * "/dev/null/", always return 0 as they should be extracted with
6019 6019 * the name intact, to allow other tar archiving programs that
6020 6020 * don't understand extended attributes, to correctly throw them away.
6021 6021 */
6022 6022 if (xattrp)
6023 6023 return (0);
6024 6024 #endif
6025 6025
6026 6026 return (name[0] == '/');
6027 6027 }
6028 6028
6029 6029 /*
6030 6030 * Adjust the pathname to make it a relative one. Strip off any leading
6031 6031 * '/' characters and if the pathname contains any '..' sequences, strip
6032 6032 * upto and including the last occurance of '../' (or '..' if found at
6033 6033 * the very end of the pathname).
6034 6034 *
6035 6035 * Return the relative pathname. stripped_prefix will also return the
6036 6036 * portion of name that was stripped off and should be freed by the
6037 6037 * calling routine when no longer needed.
6038 6038 */
6039 6039
6040 6040 static char *
6041 6041 make_relative_name(char *name, char **stripped_prefix)
6042 6042 {
6043 6043 char *s;
6044 6044 size_t prefix_len = 0;
6045 6045 size_t name_len = strlen(name);
6046 6046
6047 6047 for (s = name + prefix_len; s < (name + name_len - 2); ) {
6048 6048 if (s[0] == '.' && s[1] == '.' && ((s[2] == '/') || !s[2]))
6049 6049 prefix_len = s + 2 - name;
6050 6050
6051 6051 do {
6052 6052 char c = *s++;
6053 6053
6054 6054 if (c == '/')
6055 6055 break;
6056 6056 } while (*s);
6057 6057 }
6058 6058
6059 6059 for (s = name + prefix_len; *s == '/'; s++)
6060 6060 continue;
6061 6061 prefix_len = s - name;
6062 6062
6063 6063 /* Create the portion of the name that was stripped off. */
6064 6064 s = malloc(prefix_len + 1);
6065 6065 memcpy(s, name, prefix_len);
6066 6066 s[prefix_len] = 0;
6067 6067 *stripped_prefix = s;
6068 6068 s = &name[prefix_len];
6069 6069
6070 6070 return (s);
6071 6071 }
6072 6072
6073 6073 /*
6074 6074 * Return through *namep a pointer to the proper fullname (i.e "<name> |
6075 6075 * <prefix>/<name>"), as represented in the header entry dblock.dbuf.
6076 6076 *
6077 6077 * Returns 0 if successful, otherwise returns 1.
6078 6078 */
6079 6079
6080 6080 static int
6081 6081 check_prefix(char **namep, char **dirp, char **compp)
6082 6082 {
6083 6083 static char fullname[PATH_MAX + 1];
6084 6084 static char dir[PATH_MAX + 1];
6085 6085 static char component[PATH_MAX + 1];
6086 6086 static char savename[PATH_MAX + 1];
6087 6087 char *s;
6088 6088
6089 6089 (void) memset(dir, 0, sizeof (dir));
6090 6090 (void) memset(component, 0, sizeof (component));
6091 6091
6092 6092 if (xhdr_flgs & _X_PATH) {
6093 6093 (void) strcpy(fullname, Xtarhdr.x_path);
6094 6094 } else {
6095 6095 if (dblock.dbuf.prefix[0] != '\0')
6096 6096 (void) sprintf(fullname, "%.*s/%.*s", PRESIZ,
6097 6097 dblock.dbuf.prefix, NAMSIZ, dblock.dbuf.name);
6098 6098 else
6099 6099 (void) sprintf(fullname, "%.*s", NAMSIZ,
6100 6100 dblock.dbuf.name);
6101 6101 }
6102 6102
6103 6103 /*
6104 6104 * If we are printing a table of contents or extracting an archive,
6105 6105 * make absolute pathnames relative and prohibit the unpacking of
6106 6106 * files contain ".." in their name (unless the user has supplied
6107 6107 * the -P option).
6108 6108 */
6109 6109 if ((tflag || xflag) && !Pflag) {
6110 6110 if (is_absolute(fullname) || has_dot_dot(fullname)) {
6111 6111 char *stripped_prefix;
6112 6112 size_t prefix_len = 0;
6113 6113
6114 6114 (void) strcpy(savename, fullname);
6115 6115 strcpy(fullname,
6116 6116 make_relative_name(savename, &stripped_prefix));
6117 6117 (void) fprintf(stderr,
6118 6118 gettext("tar: Removing leading '%s' from '%s'\n"),
6119 6119 stripped_prefix, savename);
6120 6120 free(stripped_prefix);
6121 6121 }
6122 6122 }
6123 6123
6124 6124 /*
6125 6125 * Set dir and component names
6126 6126 */
6127 6127
6128 6128 get_parent(fullname, dir);
6129 6129
6130 6130 #if defined(O_XATTR)
6131 6131 if (xattrp == NULL) {
6132 6132 #endif
6133 6133 /*
6134 6134 * Save of real name since were going to chop off the
6135 6135 * trailing slashes.
6136 6136 */
6137 6137 (void) strcpy(savename, fullname);
6138 6138 /*
6139 6139 * first strip of trailing slashes.
6140 6140 */
6141 6141 chop_endslashes(savename);
6142 6142 s = get_component(savename);
6143 6143 (void) strcpy(component, s);
6144 6144
6145 6145 #if defined(O_XATTR)
6146 6146 } else {
6147 6147 (void) strcpy(fullname, xattrp->h_names);
6148 6148 (void) strcpy(dir, fullname);
6149 6149 (void) strcpy(component, basename(xattrp->h_names +
6150 6150 strlen(xattrp->h_names) + 1));
6151 6151 }
6152 6152 #endif
6153 6153 *namep = fullname;
6154 6154 *dirp = dir;
6155 6155 *compp = component;
6156 6156
6157 6157 return (0);
6158 6158 }
6159 6159
6160 6160 /*
6161 6161 * Return true if the object indicated by the file descriptor and type
6162 6162 * is a tape device, false otherwise
6163 6163 */
6164 6164
6165 6165 static int
6166 6166 istape(int fd, int type)
6167 6167 {
6168 6168 int result = 0;
6169 6169
6170 6170 if (S_ISCHR(type)) {
6171 6171 struct mtget mtg;
6172 6172
6173 6173 if (ioctl(fd, MTIOCGET, &mtg) != -1) {
6174 6174 result = 1;
6175 6175 }
6176 6176 }
6177 6177
6178 6178 return (result);
6179 6179 }
6180 6180
6181 6181 #include <utmpx.h>
6182 6182
6183 6183 struct utmpx utmpx;
6184 6184
6185 6185 #define NMAX (sizeof (utmpx.ut_name))
6186 6186
6187 6187 typedef struct cachenode { /* this struct must be zeroed before using */
6188 6188 struct cachenode *next; /* next in hash chain */
6189 6189 int val; /* the uid or gid of this entry */
6190 6190 int namehash; /* name's hash signature */
6191 6191 char name[NMAX+1]; /* the string that val maps to */
6192 6192 } cachenode_t;
6193 6193
6194 6194 #define HASHSIZE 256
6195 6195
6196 6196 static cachenode_t *names[HASHSIZE];
6197 6197 static cachenode_t *groups[HASHSIZE];
6198 6198 static cachenode_t *uids[HASHSIZE];
6199 6199 static cachenode_t *gids[HASHSIZE];
6200 6200
6201 6201 static int
6202 6202 hash_byname(char *name)
6203 6203 {
6204 6204 int i, c, h = 0;
6205 6205
6206 6206 for (i = 0; i < NMAX; i++) {
6207 6207 c = name[i];
6208 6208 if (c == '\0')
6209 6209 break;
6210 6210 h = (h << 4) + h + c;
6211 6211 }
6212 6212 return (h);
6213 6213 }
6214 6214
6215 6215 static cachenode_t *
6216 6216 hash_lookup_byval(cachenode_t *table[], int val)
6217 6217 {
6218 6218 int h = val;
6219 6219 cachenode_t *c;
6220 6220
6221 6221 for (c = table[h & (HASHSIZE - 1)]; c != NULL; c = c->next) {
6222 6222 if (c->val == val)
6223 6223 return (c);
6224 6224 }
6225 6225 return (NULL);
6226 6226 }
6227 6227
6228 6228 static cachenode_t *
6229 6229 hash_lookup_byname(cachenode_t *table[], char *name)
6230 6230 {
6231 6231 int h = hash_byname(name);
6232 6232 cachenode_t *c;
6233 6233
6234 6234 for (c = table[h & (HASHSIZE - 1)]; c != NULL; c = c->next) {
6235 6235 if (c->namehash == h && strcmp(c->name, name) == 0)
6236 6236 return (c);
6237 6237 }
6238 6238 return (NULL);
6239 6239 }
6240 6240
6241 6241 static cachenode_t *
6242 6242 hash_insert(cachenode_t *table[], char *name, int value)
6243 6243 {
6244 6244 cachenode_t *c;
6245 6245 int signature;
6246 6246
6247 6247 c = calloc(1, sizeof (cachenode_t));
6248 6248 if (c == NULL) {
6249 6249 perror("malloc");
6250 6250 exit(1);
6251 6251 }
6252 6252 if (name != NULL) {
6253 6253 (void) strncpy(c->name, name, NMAX);
6254 6254 c->namehash = hash_byname(name);
6255 6255 }
6256 6256 c->val = value;
6257 6257 if (table == uids || table == gids)
6258 6258 signature = c->val;
6259 6259 else
6260 6260 signature = c->namehash;
6261 6261 c->next = table[signature & (HASHSIZE - 1)];
6262 6262 table[signature & (HASHSIZE - 1)] = c;
6263 6263 return (c);
6264 6264 }
6265 6265
6266 6266 static char *
6267 6267 getname(uid_t uid)
6268 6268 {
6269 6269 cachenode_t *c;
6270 6270
6271 6271 if ((c = hash_lookup_byval(uids, uid)) == NULL) {
6272 6272 struct passwd *pwent = getpwuid(uid);
6273 6273 c = hash_insert(uids, pwent ? pwent->pw_name : NULL, uid);
6274 6274 }
6275 6275 return (c->name);
6276 6276 }
6277 6277
6278 6278 static char *
6279 6279 getgroup(gid_t gid)
6280 6280 {
6281 6281 cachenode_t *c;
6282 6282
6283 6283 if ((c = hash_lookup_byval(gids, gid)) == NULL) {
6284 6284 struct group *grent = getgrgid(gid);
6285 6285 c = hash_insert(gids, grent ? grent->gr_name : NULL, gid);
6286 6286 }
6287 6287 return (c->name);
6288 6288 }
6289 6289
6290 6290 static uid_t
6291 6291 getuidbyname(char *name)
6292 6292 {
6293 6293 cachenode_t *c;
6294 6294
6295 6295 if ((c = hash_lookup_byname(names, name)) == NULL) {
6296 6296 struct passwd *pwent = getpwnam(name);
6297 6297 c = hash_insert(names, name, pwent ? (int)pwent->pw_uid : -1);
6298 6298 }
6299 6299 return ((uid_t)c->val);
6300 6300 }
6301 6301
6302 6302 static gid_t
6303 6303 getgidbyname(char *group)
6304 6304 {
6305 6305 cachenode_t *c;
6306 6306
6307 6307 if ((c = hash_lookup_byname(groups, group)) == NULL) {
6308 6308 struct group *grent = getgrnam(group);
6309 6309 c = hash_insert(groups, group, grent ? (int)grent->gr_gid : -1);
6310 6310 }
6311 6311 return ((gid_t)c->val);
6312 6312 }
6313 6313
6314 6314 /*
6315 6315 * Build the header.
6316 6316 * Determine whether or not an extended header is also needed. If needed,
6317 6317 * create and write the extended header and its data.
6318 6318 * Writing of the extended header assumes that "tomodes" has been called and
6319 6319 * the relevant information has been placed in the header block.
6320 6320 */
6321 6321
6322 6322 static int
6323 6323 build_dblock(
6324 6324 const char *name,
6325 6325 const char *linkname,
6326 6326 const char typeflag,
6327 6327 const int filetype,
6328 6328 const struct stat *sp,
6329 6329 const dev_t device,
6330 6330 const char *prefix)
6331 6331 {
6332 6332 int nblks;
6333 6333 major_t dev;
6334 6334 const char *filename;
6335 6335 const char *lastslash;
6336 6336
6337 6337 if (filetype == XATTR_FILE)
6338 6338 dblock.dbuf.typeflag = _XATTR_HDRTYPE;
6339 6339 else
6340 6340 dblock.dbuf.typeflag = typeflag;
6341 6341 (void) memset(dblock.dbuf.name, '\0', NAMSIZ);
6342 6342 (void) memset(dblock.dbuf.linkname, '\0', NAMSIZ);
6343 6343 (void) memset(dblock.dbuf.prefix, '\0', PRESIZ);
6344 6344
6345 6345 if (xhdr_flgs & _X_PATH)
6346 6346 filename = Xtarhdr.x_path;
6347 6347 else
6348 6348 filename = name;
6349 6349
6350 6350 if ((dev = major(device)) > OCTAL7CHAR) {
6351 6351 if (Eflag) {
6352 6352 xhdr_flgs |= _X_DEVMAJOR;
6353 6353 Xtarhdr.x_devmajor = dev;
6354 6354 } else {
6355 6355 (void) fprintf(stderr, gettext(
6356 6356 "Device major too large for %s. Use -E flag."),
6357 6357 filename);
6358 6358 if (errflag)
6359 6359 done(1);
6360 6360 else
6361 6361 Errflg = 1;
6362 6362 }
6363 6363 dev = 0;
6364 6364 }
6365 6365 (void) sprintf(dblock.dbuf.devmajor, "%07lo", dev);
6366 6366 if ((dev = minor(device)) > OCTAL7CHAR) {
6367 6367 if (Eflag) {
6368 6368 xhdr_flgs |= _X_DEVMINOR;
6369 6369 Xtarhdr.x_devminor = dev;
6370 6370 } else {
6371 6371 (void) fprintf(stderr, gettext(
6372 6372 "Device minor too large for %s. Use -E flag."),
6373 6373 filename);
6374 6374 if (errflag)
6375 6375 done(1);
6376 6376 else
6377 6377 Errflg = 1;
6378 6378 }
6379 6379 dev = 0;
6380 6380 }
6381 6381 (void) sprintf(dblock.dbuf.devminor, "%07lo", dev);
6382 6382
6383 6383 (void) strncpy(dblock.dbuf.name, name, NAMSIZ);
6384 6384 (void) strncpy(dblock.dbuf.linkname, linkname, NAMSIZ);
6385 6385 (void) sprintf(dblock.dbuf.magic, "%.5s", magic_type);
6386 6386 (void) sprintf(dblock.dbuf.version, "00");
6387 6387 (void) sprintf(dblock.dbuf.uname, "%.31s", getname(sp->st_uid));
6388 6388 (void) sprintf(dblock.dbuf.gname, "%.31s", getgroup(sp->st_gid));
6389 6389 (void) strncpy(dblock.dbuf.prefix, prefix, PRESIZ);
6390 6390 (void) sprintf(dblock.dbuf.chksum, "%07o", checksum(&dblock));
6391 6391
6392 6392 if (Eflag) {
6393 6393 (void) bcopy(dblock.dummy, xhdr_buf.dummy, TBLOCK);
6394 6394 (void) memset(xhdr_buf.dbuf.name, '\0', NAMSIZ);
6395 6395 lastslash = strrchr(name, '/');
6396 6396 if (lastslash == NULL)
6397 6397 lastslash = name;
6398 6398 else
6399 6399 lastslash++;
6400 6400 (void) strcpy(xhdr_buf.dbuf.name, lastslash);
6401 6401 (void) memset(xhdr_buf.dbuf.linkname, '\0', NAMSIZ);
6402 6402 (void) memset(xhdr_buf.dbuf.prefix, '\0', PRESIZ);
6403 6403 (void) strcpy(xhdr_buf.dbuf.prefix, xhdr_dirname);
6404 6404 xhdr_count++;
6405 6405 xrec_offset = 0;
6406 6406 gen_date("mtime", sp->st_mtim);
6407 6407 xhdr_buf.dbuf.typeflag = 'X';
6408 6408 if (gen_utf8_names(filename) != 0)
6409 6409 return (1);
6410 6410
6411 6411 #ifdef XHDR_DEBUG
6412 6412 Xtarhdr.x_uname = dblock.dbuf.uname;
6413 6413 Xtarhdr.x_gname = dblock.dbuf.gname;
6414 6414 xhdr_flgs |= (_X_UNAME | _X_GNAME);
6415 6415 #endif
6416 6416 if (xhdr_flgs) {
6417 6417 if (xhdr_flgs & _X_DEVMAJOR)
6418 6418 gen_num("SUN.devmajor", Xtarhdr.x_devmajor);
6419 6419 if (xhdr_flgs & _X_DEVMINOR)
6420 6420 gen_num("SUN.devminor", Xtarhdr.x_devminor);
6421 6421 if (xhdr_flgs & _X_GID)
6422 6422 gen_num("gid", Xtarhdr.x_gid);
6423 6423 if (xhdr_flgs & _X_UID)
6424 6424 gen_num("uid", Xtarhdr.x_uid);
6425 6425 if (xhdr_flgs & _X_SIZE)
6426 6426 gen_num("size", Xtarhdr.x_filesz);
6427 6427 if (xhdr_flgs & _X_PATH)
6428 6428 gen_string("path", Xtarhdr.x_path);
6429 6429 if (xhdr_flgs & _X_LINKPATH)
6430 6430 gen_string("linkpath", Xtarhdr.x_linkpath);
6431 6431 if (xhdr_flgs & _X_GNAME)
6432 6432 gen_string("gname", Xtarhdr.x_gname);
6433 6433 if (xhdr_flgs & _X_UNAME)
6434 6434 gen_string("uname", Xtarhdr.x_uname);
6435 6435 }
6436 6436 (void) sprintf(xhdr_buf.dbuf.size,
6437 6437 "%011" FMT_off_t_o, xrec_offset);
6438 6438 (void) sprintf(xhdr_buf.dbuf.chksum, "%07o",
6439 6439 checksum(&xhdr_buf));
6440 6440 (void) writetbuf((char *)&xhdr_buf, 1);
6441 6441 nblks = TBLOCKS(xrec_offset);
6442 6442 (void) writetbuf(xrec_ptr, nblks);
6443 6443 }
6444 6444 return (0);
6445 6445 }
6446 6446
6447 6447
6448 6448 /*
6449 6449 * makeDir - ensure that a directory with the pathname denoted by name
6450 6450 * exists, and return 1 on success, and 0 on failure (e.g.,
6451 6451 * read-only file system, exists but not-a-directory).
6452 6452 */
6453 6453
6454 6454 static int
6455 6455 makeDir(char *name)
6456 6456 {
6457 6457 struct stat buf;
6458 6458
6459 6459 if (access(name, 0) < 0) { /* name doesn't exist */
6460 6460 if (mkdir(name, 0777) < 0) {
6461 6461 vperror(0, "%s", name);
6462 6462 return (0);
6463 6463 }
6464 6464 } else { /* name exists */
6465 6465 if (stat(name, &buf) < 0) {
6466 6466 vperror(0, "%s", name);
6467 6467 return (0);
6468 6468 }
6469 6469
6470 6470 return ((buf.st_mode & S_IFMT) == S_IFDIR);
6471 6471 }
6472 6472
6473 6473 return (1);
6474 6474 }
6475 6475
6476 6476
6477 6477 /*
6478 6478 * Save this directory and its mtime on the stack, popping and setting
6479 6479 * the mtimes of any stacked dirs which aren't parents of this one.
6480 6480 * A null name causes the entire stack to be unwound and set.
6481 6481 *
6482 6482 * Since all the elements of the directory "stack" share a common
6483 6483 * prefix, we can make do with one string. We keep only the current
6484 6484 * directory path, with an associated array of mtime's. A negative
6485 6485 * mtime means no mtime.
6486 6486 *
6487 6487 * This stack algorithm is not guaranteed to work for tapes created
6488 6488 * with the 'r' function letter, but the vast majority of tapes with
6489 6489 * directories are not. This avoids saving every directory record on
6490 6490 * the tape and setting all the times at the end.
6491 6491 *
6492 6492 * (This was borrowed from the 4.1.3 source, and adapted to the 5.x
6493 6493 * environment)
6494 6494 */
6495 6495
6496 6496 static void
6497 6497 doDirTimes(char *name, timestruc_t modTime)
6498 6498 {
6499 6499 static char dirstack[PATH_MAX+2];
6500 6500 /* Add spaces for the last slash and last NULL */
6501 6501 static timestruc_t modtimes[PATH_MAX+1]; /* hash table */
6502 6502 char *p = dirstack;
6503 6503 char *q = name;
6504 6504 char *savp;
6505 6505
6506 6506 if (q) {
6507 6507 /*
6508 6508 * Find common prefix
6509 6509 */
6510 6510
6511 6511 while (*p == *q && *p) {
6512 6512 p++; q++;
6513 6513 }
6514 6514 }
6515 6515
6516 6516 savp = p;
6517 6517 while (*p) {
6518 6518 /*
6519 6519 * Not a child: unwind the stack, setting the times.
6520 6520 * The order we do this doesn't matter, so we go "forward."
6521 6521 */
6522 6522
6523 6523 if (*p == '/')
6524 6524 if (modtimes[p - dirstack].tv_sec >= 0) {
6525 6525 *p = '\0'; /* zap the slash */
6526 6526 setPathTimes(AT_FDCWD, dirstack,
6527 6527 modtimes[p - dirstack]);
6528 6528 *p = '/';
6529 6529 }
6530 6530 ++p;
6531 6531 }
6532 6532
6533 6533 p = savp;
6534 6534
6535 6535 /*
6536 6536 * Push this one on the "stack"
6537 6537 */
6538 6538
6539 6539 if (q) {
6540 6540
6541 6541 /*
6542 6542 * Since the name parameter points the dir pathname
6543 6543 * which is limited only to contain PATH_MAX chars
6544 6544 * at maximum, we can ignore the overflow case of p.
6545 6545 */
6546 6546
6547 6547 while ((*p = *q++)) { /* append the rest of the new dir */
6548 6548 modtimes[p - dirstack].tv_sec = -1;
6549 6549 p++;
6550 6550 }
6551 6551
6552 6552 /*
6553 6553 * If the tar file had used 'P' or 'E' function modifier,
6554 6554 * append the last slash.
6555 6555 */
6556 6556 if (*(p - 1) != '/') {
6557 6557 *p++ = '/';
6558 6558 *p = '\0';
6559 6559 }
6560 6560 /* overwrite the last one */
6561 6561 modtimes[p - dirstack - 1] = modTime;
6562 6562 }
6563 6563 }
6564 6564
6565 6565
6566 6566 /*
6567 6567 * setPathTimes - set the modification time for given path. Return 1 if
6568 6568 * successful and 0 if not successful.
6569 6569 */
6570 6570
6571 6571 static void
6572 6572 setPathTimes(int dirfd, char *path, timestruc_t modTime)
6573 6573
6574 6574 {
6575 6575 struct timeval timebuf[2];
6576 6576
6577 6577 /*
6578 6578 * futimesat takes an array of two timeval structs.
6579 6579 * The first entry contains access time.
6580 6580 * The second entry contains modification time.
6581 6581 * Unlike a timestruc_t, which uses nanoseconds, timeval uses
6582 6582 * microseconds.
6583 6583 */
6584 6584 timebuf[0].tv_sec = time((time_t *)0);
6585 6585 timebuf[0].tv_usec = 0;
6586 6586 timebuf[1].tv_sec = modTime.tv_sec;
6587 6587
6588 6588 /* Extended header: use microseconds */
6589 6589 timebuf[1].tv_usec = (xhdr_flgs & _X_MTIME) ? modTime.tv_nsec/1000 : 0;
6590 6590
6591 6591 if (futimesat(dirfd, path, timebuf) < 0)
6592 6592 vperror(0, gettext("can't set time on %s"), path);
6593 6593 }
6594 6594
6595 6595
6596 6596 /*
6597 6597 * If hflag is set then delete the symbolic link's target.
6598 6598 * If !hflag then delete the target.
6599 6599 */
6600 6600
6601 6601 static void
6602 6602 delete_target(int fd, char *comp, char *namep)
6603 6603 {
6604 6604 struct stat xtractbuf;
6605 6605 char buf[PATH_MAX + 1];
6606 6606 int n;
6607 6607
↓ open down ↓ |
1052 lines elided |
↑ open up ↑ |
6608 6608
6609 6609 if (unlinkat(fd, comp, AT_REMOVEDIR) < 0) {
6610 6610 if (errno == ENOTDIR && !hflag) {
6611 6611 (void) unlinkat(fd, comp, 0);
6612 6612 } else if (errno == ENOTDIR && hflag) {
6613 6613 if (!lstat(namep, &xtractbuf)) {
6614 6614 if ((xtractbuf.st_mode & S_IFMT) != S_IFLNK) {
6615 6615 (void) unlinkat(fd, comp, 0);
6616 6616 } else if ((n = readlink(namep, buf,
6617 6617 PATH_MAX)) != -1) {
6618 - buf[n] = (char)NULL;
6618 + buf[n] = '\0';
6619 6619 (void) unlinkat(fd, buf,
6620 6620 AT_REMOVEDIR);
6621 6621 if (errno == ENOTDIR)
6622 6622 (void) unlinkat(fd, buf, 0);
6623 6623 } else {
6624 6624 (void) unlinkat(fd, comp, 0);
6625 6625 }
6626 6626 } else {
6627 6627 (void) unlinkat(fd, comp, 0);
6628 6628 }
6629 6629 }
6630 6630 }
6631 6631 }
6632 6632
6633 6633
6634 6634 /*
6635 6635 * ACL changes:
6636 6636 * putfile():
6637 6637 * Get acl info after stat. Write out ancillary file
6638 6638 * before the normal file, i.e. directory, regular, FIFO,
6639 6639 * link, special. If acl count is less than 4, no need to
6640 6640 * create ancillary file. (i.e. standard permission is in
6641 6641 * use.
6642 6642 * doxtract():
6643 6643 * Process ancillary file. Read it in and set acl info.
6644 6644 * watch out for 'o' function modifier.
6645 6645 * 't' function letter to display table
6646 6646 */
6647 6647
6648 6648 /*
6649 6649 * New functions for ACLs and other security attributes
6650 6650 */
6651 6651
6652 6652 /*
6653 6653 * The function appends the new security attribute info to the end of
6654 6654 * existing secinfo.
6655 6655 */
6656 6656 int
6657 6657 append_secattr(
6658 6658 char **secinfo, /* existing security info */
6659 6659 int *secinfo_len, /* length of existing security info */
6660 6660 int size, /* new attribute size: unit depends on type */
6661 6661 char *attrtext, /* new attribute text */
6662 6662 char attr_type) /* new attribute type */
6663 6663 {
6664 6664 char *new_secinfo;
6665 6665 int newattrsize;
6666 6666 int oldsize;
6667 6667 struct sec_attr *attr;
6668 6668
6669 6669 /* no need to add */
6670 6670 if (attr_type != DIR_TYPE) {
6671 6671 if (attrtext == NULL)
6672 6672 return (0);
6673 6673 }
6674 6674
6675 6675 switch (attr_type) {
6676 6676 case UFSD_ACL:
6677 6677 case ACE_ACL:
6678 6678 if (attrtext == NULL) {
6679 6679 (void) fprintf(stderr, gettext("acltotext failed\n"));
6680 6680 return (-1);
6681 6681 }
6682 6682 /* header: type + size = 8 */
6683 6683 newattrsize = 8 + (int)strlen(attrtext) + 1;
6684 6684 attr = (struct sec_attr *)malloc(newattrsize);
6685 6685 if (attr == NULL) {
6686 6686 (void) fprintf(stderr,
6687 6687 gettext("can't allocate memory\n"));
6688 6688 return (-1);
6689 6689 }
6690 6690 attr->attr_type = attr_type;
6691 6691 (void) sprintf(attr->attr_len,
6692 6692 "%06o", size); /* acl entry count */
6693 6693 (void) strcpy((char *)&attr->attr_info[0], attrtext);
6694 6694 free(attrtext);
6695 6695 break;
6696 6696
6697 6697 /* Trusted Extensions */
6698 6698 case DIR_TYPE:
6699 6699 case LBL_TYPE:
6700 6700 newattrsize = sizeof (struct sec_attr) + strlen(attrtext);
6701 6701 attr = (struct sec_attr *)malloc(newattrsize);
6702 6702 if (attr == NULL) {
6703 6703 (void) fprintf(stderr,
6704 6704 gettext("can't allocate memory\n"));
6705 6705 return (-1);
6706 6706 }
6707 6707 attr->attr_type = attr_type;
6708 6708 (void) sprintf(attr->attr_len,
6709 6709 "%06d", size); /* len of attr data */
6710 6710 (void) strcpy((char *)&attr->attr_info[0], attrtext);
6711 6711 break;
6712 6712
6713 6713 default:
6714 6714 (void) fprintf(stderr,
6715 6715 gettext("unrecognized attribute type\n"));
6716 6716 return (-1);
6717 6717 }
6718 6718
6719 6719 /* old security info + new attr header(8) + new attr */
6720 6720 oldsize = *secinfo_len;
6721 6721 *secinfo_len += newattrsize;
6722 6722 new_secinfo = (char *)malloc(*secinfo_len);
6723 6723 if (new_secinfo == NULL) {
6724 6724 (void) fprintf(stderr, gettext("can't allocate memory\n"));
6725 6725 *secinfo_len -= newattrsize;
6726 6726 free(attr);
6727 6727 return (-1);
6728 6728 }
6729 6729
6730 6730 (void) memcpy(new_secinfo, *secinfo, oldsize);
6731 6731 (void) memcpy(new_secinfo + oldsize, attr, newattrsize);
6732 6732
6733 6733 free(*secinfo);
6734 6734 free(attr);
6735 6735 *secinfo = new_secinfo;
6736 6736 return (0);
6737 6737 }
6738 6738
6739 6739 /*
6740 6740 * write_ancillary(): write out an ancillary file.
6741 6741 * The file has the same header as normal file except the type and size
6742 6742 * fields. The type is 'A' and size is the sum of all attributes
6743 6743 * in bytes.
6744 6744 * The body contains a list of attribute type, size and info. Currently,
6745 6745 * there is only ACL info. This file is put before the normal file.
6746 6746 */
6747 6747 void
6748 6748 write_ancillary(union hblock *dblockp, char *secinfo, int len, char hdrtype)
6749 6749 {
6750 6750 long blocks;
6751 6751 int savflag;
6752 6752 int savsize;
6753 6753
6754 6754 /* Just tranditional permissions or no security attribute info */
6755 6755 if (len == 0 || secinfo == NULL)
6756 6756 return;
6757 6757
6758 6758 /* save flag and size */
6759 6759 savflag = (dblockp->dbuf).typeflag;
6760 6760 (void) sscanf(dblockp->dbuf.size, "%12o", (uint_t *)&savsize);
6761 6761
6762 6762 /* special flag for ancillary file */
6763 6763 if (hdrtype == _XATTR_HDRTYPE)
6764 6764 dblockp->dbuf.typeflag = _XATTR_HDRTYPE;
6765 6765 else
6766 6766 dblockp->dbuf.typeflag = 'A';
6767 6767
6768 6768 /* for pre-2.5 versions of tar, need to make sure */
6769 6769 /* the ACL file is readable */
6770 6770 (void) sprintf(dblock.dbuf.mode, "%07lo",
6771 6771 (stbuf.st_mode & POSIXMODES) | 0000200);
6772 6772 (void) sprintf(dblockp->dbuf.size, "%011o", len);
6773 6773 (void) sprintf(dblockp->dbuf.chksum, "%07o", checksum(dblockp));
6774 6774
6775 6775 /* write out the header */
6776 6776 (void) writetbuf((char *)dblockp, 1);
6777 6777
6778 6778 /* write out security info */
6779 6779 blocks = TBLOCKS(len);
6780 6780 (void) writetbuf((char *)secinfo, (int)blocks);
6781 6781
6782 6782 /* restore mode, flag and size */
6783 6783 (void) sprintf(dblock.dbuf.mode, "%07lo", stbuf.st_mode & POSIXMODES);
6784 6784 dblockp->dbuf.typeflag = savflag;
6785 6785 (void) sprintf(dblockp->dbuf.size, "%011o", savsize);
6786 6786 }
6787 6787
6788 6788 /*
6789 6789 * Read the data record for extended headers and then the regular header.
6790 6790 * The data are read into the buffer and then null-terminated. Entries
6791 6791 * for typeflag 'X' extended headers are of the format:
6792 6792 * "%d %s=%s\n"
6793 6793 *
6794 6794 * When an extended header record is found, the extended header must
6795 6795 * be processed and its values used to override the values in the
6796 6796 * normal header. The way this is done is to process the extended
6797 6797 * header data record and set the data values, then call getdir
6798 6798 * to process the regular header, then then to reconcile the two
6799 6799 * sets of data.
6800 6800 */
6801 6801
6802 6802 static int
6803 6803 get_xdata(void)
6804 6804 {
6805 6805 struct keylist_pair {
6806 6806 int keynum;
6807 6807 char *keylist;
6808 6808 } keylist_pair[] = { _X_DEVMAJOR, "SUN.devmajor",
6809 6809 _X_DEVMINOR, "SUN.devminor",
6810 6810 _X_GID, "gid",
6811 6811 _X_GNAME, "gname",
6812 6812 _X_LINKPATH, "linkpath",
6813 6813 _X_PATH, "path",
6814 6814 _X_SIZE, "size",
6815 6815 _X_UID, "uid",
6816 6816 _X_UNAME, "uname",
6817 6817 _X_MTIME, "mtime",
6818 6818 _X_LAST, "NULL" };
6819 6819 char *lineloc;
6820 6820 int length, i;
6821 6821 char *keyword, *value;
6822 6822 blkcnt_t nblocks;
6823 6823 int bufneeded;
6824 6824 int errors;
6825 6825
6826 6826 (void) memset(&Xtarhdr, 0, sizeof (Xtarhdr));
6827 6827 xhdr_count++;
6828 6828 errors = 0;
6829 6829
6830 6830 nblocks = TBLOCKS(stbuf.st_size);
6831 6831 bufneeded = nblocks * TBLOCK;
6832 6832 if (bufneeded >= xrec_size) {
6833 6833 free(xrec_ptr);
6834 6834 xrec_size = bufneeded + 1;
6835 6835 if ((xrec_ptr = malloc(xrec_size)) == NULL)
6836 6836 fatal(gettext("cannot allocate buffer"));
6837 6837 }
6838 6838
6839 6839 lineloc = xrec_ptr;
6840 6840
6841 6841 while (nblocks-- > 0) {
6842 6842 readtape(lineloc);
6843 6843 lineloc += TBLOCK;
6844 6844 }
6845 6845 lineloc = xrec_ptr;
6846 6846 xrec_ptr[stbuf.st_size] = '\0';
6847 6847 while (lineloc < xrec_ptr + stbuf.st_size) {
6848 6848 if (dblock.dbuf.typeflag == 'L') {
6849 6849 length = xrec_size;
6850 6850 keyword = "path";
6851 6851 value = lineloc;
6852 6852 } else {
6853 6853 length = atoi(lineloc);
6854 6854 *(lineloc + length - 1) = '\0';
6855 6855 keyword = strchr(lineloc, ' ') + 1;
6856 6856 value = strchr(keyword, '=') + 1;
6857 6857 *(value - 1) = '\0';
6858 6858 }
6859 6859 i = 0;
6860 6860 lineloc += length;
6861 6861 while (keylist_pair[i].keynum != (int)_X_LAST) {
6862 6862 if (strcmp(keyword, keylist_pair[i].keylist) == 0)
6863 6863 break;
6864 6864 i++;
6865 6865 }
6866 6866 errno = 0;
6867 6867 switch (keylist_pair[i].keynum) {
6868 6868 case _X_DEVMAJOR:
6869 6869 Xtarhdr.x_devmajor = (major_t)strtoul(value, NULL, 0);
6870 6870 if (errno) {
6871 6871 (void) fprintf(stderr, gettext(
6872 6872 "tar: Extended header major value error "
6873 6873 "for file # %llu.\n"), xhdr_count);
6874 6874 errors++;
6875 6875 } else
6876 6876 xhdr_flgs |= _X_DEVMAJOR;
6877 6877 break;
6878 6878 case _X_DEVMINOR:
6879 6879 Xtarhdr.x_devminor = (minor_t)strtoul(value, NULL, 0);
6880 6880 if (errno) {
6881 6881 (void) fprintf(stderr, gettext(
6882 6882 "tar: Extended header minor value error "
6883 6883 "for file # %llu.\n"), xhdr_count);
6884 6884 errors++;
6885 6885 } else
6886 6886 xhdr_flgs |= _X_DEVMINOR;
6887 6887 break;
6888 6888 case _X_GID:
6889 6889 xhdr_flgs |= _X_GID;
6890 6890 Xtarhdr.x_gid = strtol(value, NULL, 0);
6891 6891 if ((errno) || (Xtarhdr.x_gid > UID_MAX)) {
6892 6892 (void) fprintf(stderr, gettext(
6893 6893 "tar: Extended header gid value error "
6894 6894 "for file # %llu.\n"), xhdr_count);
6895 6895 Xtarhdr.x_gid = GID_NOBODY;
6896 6896 }
6897 6897 break;
6898 6898 case _X_GNAME:
6899 6899 if (utf8_local("gname", &Xtarhdr.x_gname,
6900 6900 local_gname, value, _POSIX_NAME_MAX) == 0)
6901 6901 xhdr_flgs |= _X_GNAME;
6902 6902 break;
6903 6903 case _X_LINKPATH:
6904 6904 if (utf8_local("linkpath", &Xtarhdr.x_linkpath,
6905 6905 local_linkpath, value, PATH_MAX) == 0)
6906 6906 xhdr_flgs |= _X_LINKPATH;
6907 6907 else
6908 6908 errors++;
6909 6909 break;
6910 6910 case _X_PATH:
6911 6911 if (utf8_local("path", &Xtarhdr.x_path,
6912 6912 local_path, value, PATH_MAX) == 0)
6913 6913 xhdr_flgs |= _X_PATH;
6914 6914 else
6915 6915 errors++;
6916 6916 break;
6917 6917 case _X_SIZE:
6918 6918 Xtarhdr.x_filesz = strtoull(value, NULL, 0);
6919 6919 if (errno) {
6920 6920 (void) fprintf(stderr, gettext(
6921 6921 "tar: Extended header invalid filesize "
6922 6922 "for file # %llu.\n"), xhdr_count);
6923 6923 errors++;
6924 6924 } else
6925 6925 xhdr_flgs |= _X_SIZE;
6926 6926 break;
6927 6927 case _X_UID:
6928 6928 xhdr_flgs |= _X_UID;
6929 6929 Xtarhdr.x_uid = strtol(value, NULL, 0);
6930 6930 if ((errno) || (Xtarhdr.x_uid > UID_MAX)) {
6931 6931 (void) fprintf(stderr, gettext(
6932 6932 "tar: Extended header uid value error "
6933 6933 "for file # %llu.\n"), xhdr_count);
6934 6934 Xtarhdr.x_uid = UID_NOBODY;
6935 6935 }
6936 6936 break;
6937 6937 case _X_UNAME:
6938 6938 if (utf8_local("uname", &Xtarhdr.x_uname,
6939 6939 local_uname, value, _POSIX_NAME_MAX) == 0)
6940 6940 xhdr_flgs |= _X_UNAME;
6941 6941 break;
6942 6942 case _X_MTIME:
6943 6943 get_xtime(value, &(Xtarhdr.x_mtime));
6944 6944 if (errno)
6945 6945 (void) fprintf(stderr, gettext(
6946 6946 "tar: Extended header modification time "
6947 6947 "value error for file # %llu.\n"),
6948 6948 xhdr_count);
6949 6949 else
6950 6950 xhdr_flgs |= _X_MTIME;
6951 6951 break;
6952 6952 default:
6953 6953 (void) fprintf(stderr,
6954 6954 gettext("tar: unrecognized extended"
6955 6955 " header keyword '%s'. Ignored.\n"), keyword);
6956 6956 break;
6957 6957 }
6958 6958 }
6959 6959
6960 6960 getdir(); /* get regular header */
6961 6961 if (errors && errflag)
6962 6962 done(1);
6963 6963 else
6964 6964 if (errors)
6965 6965 Errflg = 1;
6966 6966 return (errors);
6967 6967 }
6968 6968
6969 6969 /*
6970 6970 * load_info_from_xtarhdr - sets Gen and stbuf variables from
6971 6971 * extended header
6972 6972 * load_info_from_xtarhdr(flag, xhdrp);
6973 6973 * u_longlong_t flag; xhdr_flgs
6974 6974 * struct xtar_hdr *xhdrp; pointer to extended header
6975 6975 * NOTE: called when typeflag is not 'A' and xhdr_flgs
6976 6976 * is set.
6977 6977 */
6978 6978 static void
6979 6979 load_info_from_xtarhdr(u_longlong_t flag, struct xtar_hdr *xhdrp)
6980 6980 {
6981 6981 if (flag & _X_DEVMAJOR) {
6982 6982 Gen.g_devmajor = xhdrp->x_devmajor;
6983 6983 }
6984 6984 if (flag & _X_DEVMINOR) {
6985 6985 Gen.g_devminor = xhdrp->x_devminor;
6986 6986 }
6987 6987 if (flag & _X_GID) {
6988 6988 Gen.g_gid = xhdrp->x_gid;
6989 6989 stbuf.st_gid = xhdrp->x_gid;
6990 6990 }
6991 6991 if (flag & _X_UID) {
6992 6992 Gen.g_uid = xhdrp->x_uid;
6993 6993 stbuf.st_uid = xhdrp->x_uid;
6994 6994 }
6995 6995 if (flag & _X_SIZE) {
6996 6996 Gen.g_filesz = xhdrp->x_filesz;
6997 6997 stbuf.st_size = xhdrp->x_filesz;
6998 6998 }
6999 6999 if (flag & _X_MTIME) {
7000 7000 Gen.g_mtime = xhdrp->x_mtime.tv_sec;
7001 7001 stbuf.st_mtim.tv_sec = xhdrp->x_mtime.tv_sec;
7002 7002 stbuf.st_mtim.tv_nsec = xhdrp->x_mtime.tv_nsec;
7003 7003 }
7004 7004 }
7005 7005
7006 7006 /*
7007 7007 * gen_num creates a string from a keyword and an usigned long long in the
7008 7008 * format: %d %s=%s\n
7009 7009 * This is part of the extended header data record.
7010 7010 */
7011 7011
7012 7012 void
7013 7013 gen_num(const char *keyword, const u_longlong_t number)
7014 7014 {
7015 7015 char save_val[ULONGLONG_MAX_DIGITS + 1];
7016 7016 int len;
7017 7017 char *curr_ptr;
7018 7018
7019 7019 (void) sprintf(save_val, "%llu", number);
7020 7020 /*
7021 7021 * len = length of entire line, including itself. len will be
7022 7022 * two digits. So, add the string lengths plus the length of len,
7023 7023 * plus a blank, an equal sign, and a newline.
7024 7024 */
7025 7025 len = strlen(save_val) + strlen(keyword) + 5;
7026 7026 if (xrec_offset + len > xrec_size) {
7027 7027 if (((curr_ptr = realloc(xrec_ptr, 2 * xrec_size)) == NULL))
7028 7028 fatal(gettext(
7029 7029 "cannot allocate extended header buffer"));
7030 7030 xrec_ptr = curr_ptr;
7031 7031 xrec_size *= 2;
7032 7032 }
7033 7033 (void) sprintf(&xrec_ptr[xrec_offset],
7034 7034 "%d %s=%s\n", len, keyword, save_val);
7035 7035 xrec_offset += len;
7036 7036 }
7037 7037
7038 7038 /*
7039 7039 * gen_date creates a string from a keyword and a timestruc_t in the
7040 7040 * format: %d %s=%s\n
7041 7041 * This is part of the extended header data record.
7042 7042 * Currently, granularity is only microseconds, so the low-order three digits
7043 7043 * will be truncated.
7044 7044 */
7045 7045
7046 7046 void
7047 7047 gen_date(const char *keyword, const timestruc_t time_value)
7048 7048 {
7049 7049 /* Allow for <seconds>.<nanoseconds>\n */
7050 7050 char save_val[TIME_MAX_DIGITS + LONG_MAX_DIGITS + 2];
7051 7051 int len;
7052 7052 char *curr_ptr;
7053 7053
7054 7054 (void) sprintf(save_val, "%ld", time_value.tv_sec);
7055 7055 len = strlen(save_val);
7056 7056 save_val[len] = '.';
7057 7057 (void) sprintf(&save_val[len + 1], "%9.9ld", time_value.tv_nsec);
7058 7058
7059 7059 /*
7060 7060 * len = length of entire line, including itself. len will be
7061 7061 * two digits. So, add the string lengths plus the length of len,
7062 7062 * plus a blank, an equal sign, and a newline.
7063 7063 */
7064 7064 len = strlen(save_val) + strlen(keyword) + 5;
7065 7065 if (xrec_offset + len > xrec_size) {
7066 7066 if (((curr_ptr = realloc(xrec_ptr, 2 * xrec_size)) == NULL))
7067 7067 fatal(gettext(
7068 7068 "cannot allocate extended header buffer"));
7069 7069 xrec_ptr = curr_ptr;
7070 7070 xrec_size *= 2;
7071 7071 }
7072 7072 (void) sprintf(&xrec_ptr[xrec_offset],
7073 7073 "%d %s=%s\n", len, keyword, save_val);
7074 7074 xrec_offset += len;
7075 7075 }
7076 7076
7077 7077 /*
7078 7078 * gen_string creates a string from a keyword and a char * in the
7079 7079 * format: %d %s=%s\n
7080 7080 * This is part of the extended header data record.
7081 7081 */
7082 7082
7083 7083 void
7084 7084 gen_string(const char *keyword, const char *value)
7085 7085 {
7086 7086 int len;
7087 7087 char *curr_ptr;
7088 7088
7089 7089 /*
7090 7090 * len = length of entire line, including itself. The character length
7091 7091 * of len must be 1-4 characters, because the maximum size of the path
7092 7092 * or the name is PATH_MAX, which is 1024. So, assume 1 character
7093 7093 * for len, one for the space, one for the "=", and one for the newline.
7094 7094 * Then adjust as needed.
7095 7095 */
7096 7096 /* LINTED constant expression */
7097 7097 assert(PATH_MAX <= 9996);
7098 7098 len = strlen(value) + strlen(keyword) + 4;
7099 7099 if (len > 997)
7100 7100 len += 3;
7101 7101 else if (len > 98)
7102 7102 len += 2;
7103 7103 else if (len > 9)
7104 7104 len += 1;
7105 7105 if (xrec_offset + len > xrec_size) {
7106 7106 if (((curr_ptr = realloc(xrec_ptr, 2 * xrec_size)) == NULL))
7107 7107 fatal(gettext(
7108 7108 "cannot allocate extended header buffer"));
7109 7109 xrec_ptr = curr_ptr;
7110 7110 xrec_size *= 2;
7111 7111 }
7112 7112 #ifdef XHDR_DEBUG
7113 7113 if (strcmp(keyword+1, "name") != 0)
7114 7114 #endif
7115 7115 (void) sprintf(&xrec_ptr[xrec_offset],
7116 7116 "%d %s=%s\n", len, keyword, value);
7117 7117 #ifdef XHDR_DEBUG
7118 7118 else {
7119 7119 len += 11;
7120 7120 (void) sprintf(&xrec_ptr[xrec_offset],
7121 7121 "%d %s=%snametoolong\n", len, keyword, value);
7122 7122 }
7123 7123 #endif
7124 7124 xrec_offset += len;
7125 7125 }
7126 7126
7127 7127 /*
7128 7128 * Convert time found in the extended header data to seconds and nanoseconds.
7129 7129 */
7130 7130
7131 7131 void
7132 7132 get_xtime(char *value, timestruc_t *xtime)
7133 7133 {
7134 7134 char nanosec[10];
7135 7135 char *period;
7136 7136 int i;
7137 7137
7138 7138 (void) memset(nanosec, '0', 9);
7139 7139 nanosec[9] = '\0';
7140 7140
7141 7141 period = strchr(value, '.');
7142 7142 if (period != NULL)
7143 7143 period[0] = '\0';
7144 7144 xtime->tv_sec = strtol(value, NULL, 10);
7145 7145 if (period == NULL)
7146 7146 xtime->tv_nsec = 0;
7147 7147 else {
7148 7148 i = strlen(period +1);
7149 7149 (void) strncpy(nanosec, period + 1, min(i, 9));
7150 7150 xtime->tv_nsec = strtol(nanosec, NULL, 10);
7151 7151 }
7152 7152 }
7153 7153
7154 7154 /*
7155 7155 * Check linkpath for length.
7156 7156 * Emit an error message and return 1 if too long.
7157 7157 */
7158 7158
7159 7159 int
7160 7160 chk_path_build(
7161 7161 char *name,
7162 7162 char *longname,
7163 7163 char *linkname,
7164 7164 char *prefix,
7165 7165 char type,
7166 7166 int filetype)
7167 7167 {
7168 7168
7169 7169 if (strlen(linkname) > (size_t)NAMSIZ) {
7170 7170 if (Eflag > 0) {
7171 7171 xhdr_flgs |= _X_LINKPATH;
7172 7172 Xtarhdr.x_linkpath = linkname;
7173 7173 } else {
7174 7174 (void) fprintf(stderr, gettext(
7175 7175 "tar: %s: linked to %s\n"), longname, linkname);
7176 7176 (void) fprintf(stderr, gettext(
7177 7177 "tar: %s: linked name too long\n"), linkname);
7178 7178 if (errflag)
7179 7179 done(1);
7180 7180 else
7181 7181 Errflg = 1;
7182 7182 return (1);
7183 7183 }
7184 7184 }
7185 7185 if (xhdr_flgs & _X_LINKPATH)
7186 7186 return (build_dblock(name, tchar, type,
7187 7187 filetype, &stbuf, stbuf.st_dev,
7188 7188 prefix));
7189 7189 else
7190 7190 return (build_dblock(name, linkname, type,
7191 7191 filetype, &stbuf, stbuf.st_dev, prefix));
7192 7192 }
7193 7193
7194 7194 /*
7195 7195 * Convert from UTF-8 to local character set.
7196 7196 */
7197 7197
7198 7198 static int
7199 7199 utf8_local(
7200 7200 char *option,
7201 7201 char **Xhdr_ptrptr,
7202 7202 char *target,
7203 7203 const char *source,
7204 7204 int max_val)
7205 7205 {
7206 7206 static iconv_t iconv_cd;
7207 7207 char *nl_target;
7208 7208 const char *iconv_src;
7209 7209 char *iconv_trg;
7210 7210 size_t inlen;
7211 7211 size_t outlen;
7212 7212
7213 7213 if (charset_type == -1) { /* iconv_open failed in earlier try */
7214 7214 (void) fprintf(stderr, gettext(
7215 7215 "tar: file # %llu: (%s) UTF-8 conversion failed.\n"),
7216 7216 xhdr_count, source);
7217 7217 return (1);
7218 7218 } else if (charset_type == 0) { /* iconv_open has not yet been done */
7219 7219 nl_target = nl_langinfo(CODESET);
7220 7220 if (strlen(nl_target) == 0) /* locale using 7-bit codeset */
7221 7221 nl_target = "646";
7222 7222 if (strcmp(nl_target, "646") == 0)
7223 7223 charset_type = 1;
7224 7224 else if (strcmp(nl_target, "UTF-8") == 0)
7225 7225 charset_type = 3;
7226 7226 else {
7227 7227 if (strncmp(nl_target, "ISO", 3) == 0)
7228 7228 nl_target += 3;
7229 7229 charset_type = 2;
7230 7230 errno = 0;
7231 7231 if ((iconv_cd = iconv_open(nl_target, "UTF-8")) ==
7232 7232 (iconv_t)-1) {
7233 7233 if (errno == EINVAL)
7234 7234 (void) fprintf(stderr, gettext(
7235 7235 "tar: conversion routines not "
7236 7236 "available for current locale. "));
7237 7237 (void) fprintf(stderr, gettext(
7238 7238 "file # %llu: (%s) UTF-8 conversion"
7239 7239 " failed.\n"), xhdr_count, source);
7240 7240 charset_type = -1;
7241 7241 return (1);
7242 7242 }
7243 7243 }
7244 7244 }
7245 7245
7246 7246 /* locale using 7-bit codeset or UTF-8 locale */
7247 7247 if (charset_type == 1 || charset_type == 3) {
7248 7248 if (strlen(source) > max_val) {
7249 7249 (void) fprintf(stderr, gettext(
7250 7250 "tar: file # %llu: Extended header %s too long.\n"),
7251 7251 xhdr_count, option);
7252 7252 return (1);
7253 7253 }
7254 7254 if (charset_type == 3)
7255 7255 (void) strcpy(target, source);
7256 7256 else if (c_utf8(target, source) != 0) {
7257 7257 (void) fprintf(stderr, gettext(
7258 7258 "tar: file # %llu: (%s) UTF-8 conversion"
7259 7259 " failed.\n"), xhdr_count, source);
7260 7260 return (1);
7261 7261 }
7262 7262 *Xhdr_ptrptr = target;
7263 7263 return (0);
7264 7264 }
7265 7265
7266 7266 iconv_src = source;
7267 7267 iconv_trg = target;
7268 7268 inlen = strlen(source);
7269 7269 outlen = max_val * UTF_8_FACTOR;
7270 7270 if (iconv(iconv_cd, &iconv_src, &inlen, &iconv_trg, &outlen) ==
7271 7271 (size_t)-1) { /* Error occurred: didn't convert */
7272 7272 (void) fprintf(stderr, gettext(
7273 7273 "tar: file # %llu: (%s) UTF-8 conversion failed.\n"),
7274 7274 xhdr_count, source);
7275 7275 /* Get remaining output; reinitialize conversion descriptor */
7276 7276 iconv_src = (const char *)NULL;
7277 7277 inlen = 0;
7278 7278 (void) iconv(iconv_cd, &iconv_src, &inlen, &iconv_trg, &outlen);
7279 7279 return (1);
7280 7280 }
7281 7281 /* Get remaining output; reinitialize conversion descriptor */
7282 7282 iconv_src = (const char *)NULL;
7283 7283 inlen = 0;
7284 7284 if (iconv(iconv_cd, &iconv_src, &inlen, &iconv_trg, &outlen) ==
7285 7285 (size_t)-1) { /* Error occurred: didn't convert */
7286 7286 (void) fprintf(stderr, gettext(
7287 7287 "tar: file # %llu: (%s) UTF-8 conversion failed.\n"),
7288 7288 xhdr_count, source);
7289 7289 return (1);
7290 7290 }
7291 7291
7292 7292 *iconv_trg = '\0'; /* Null-terminate iconv output string */
7293 7293 if (strlen(target) > max_val) {
7294 7294 (void) fprintf(stderr, gettext(
7295 7295 "tar: file # %llu: Extended header %s too long.\n"),
7296 7296 xhdr_count, option);
7297 7297 return (1);
7298 7298 }
7299 7299 *Xhdr_ptrptr = target;
7300 7300 return (0);
7301 7301 }
7302 7302
7303 7303 /*
7304 7304 * Check gname, uname, path, and linkpath to see if they need to go in an
7305 7305 * extended header. If they are already slated to be in an extended header,
7306 7306 * or if they are not ascii, then they need to be in the extended header.
7307 7307 * Then, convert all extended names to UTF-8.
7308 7308 */
7309 7309
7310 7310 int
7311 7311 gen_utf8_names(const char *filename)
7312 7312 {
7313 7313 static iconv_t iconv_cd;
7314 7314 char *nl_target;
7315 7315 char tempbuf[MAXNAM + 1];
7316 7316 int nbytes;
7317 7317 int errors;
7318 7318
7319 7319 if (charset_type == -1) { /* Previous failure to open. */
7320 7320 (void) fprintf(stderr, gettext(
7321 7321 "tar: file # %llu: UTF-8 conversion failed.\n"),
7322 7322 xhdr_count);
7323 7323 return (1);
7324 7324 }
7325 7325
7326 7326 if (charset_type == 0) { /* Need to get conversion descriptor */
7327 7327 nl_target = nl_langinfo(CODESET);
7328 7328 if (strlen(nl_target) == 0) /* locale using 7-bit codeset */
7329 7329 nl_target = "646";
7330 7330 if (strcmp(nl_target, "646") == 0)
7331 7331 charset_type = 1;
7332 7332 else if (strcmp(nl_target, "UTF-8") == 0)
7333 7333 charset_type = 3;
7334 7334 else {
7335 7335 if (strncmp(nl_target, "ISO", 3) == 0)
7336 7336 nl_target += 3;
7337 7337 charset_type = 2;
7338 7338 errno = 0;
7339 7339 #ifdef ICONV_DEBUG
7340 7340 (void) fprintf(stderr,
7341 7341 gettext("Opening iconv_cd with target %s\n"),
7342 7342 nl_target);
7343 7343 #endif
7344 7344 if ((iconv_cd = iconv_open("UTF-8", nl_target)) ==
7345 7345 (iconv_t)-1) {
7346 7346 if (errno == EINVAL)
7347 7347 (void) fprintf(stderr, gettext(
7348 7348 "tar: conversion routines not "
7349 7349 "available for current locale. "));
7350 7350 (void) fprintf(stderr, gettext(
7351 7351 "file (%s): UTF-8 conversion failed.\n"),
7352 7352 filename);
7353 7353 charset_type = -1;
7354 7354 return (1);
7355 7355 }
7356 7356 }
7357 7357 }
7358 7358
7359 7359 errors = 0;
7360 7360
7361 7361 errors += local_utf8(&Xtarhdr.x_gname, local_gname,
7362 7362 dblock.dbuf.gname, iconv_cd, _X_GNAME, _POSIX_NAME_MAX);
7363 7363 errors += local_utf8(&Xtarhdr.x_uname, local_uname,
7364 7364 dblock.dbuf.uname, iconv_cd, _X_UNAME, _POSIX_NAME_MAX);
7365 7365 if ((xhdr_flgs & _X_LINKPATH) == 0) { /* Need null-terminated str. */
7366 7366 (void) strncpy(tempbuf, dblock.dbuf.linkname, NAMSIZ);
7367 7367 tempbuf[NAMSIZ] = '\0';
7368 7368 }
7369 7369 errors += local_utf8(&Xtarhdr.x_linkpath, local_linkpath,
7370 7370 tempbuf, iconv_cd, _X_LINKPATH, PATH_MAX);
7371 7371 if ((xhdr_flgs & _X_PATH) == 0) { /* Concatenate prefix & name */
7372 7372 (void) strncpy(tempbuf, dblock.dbuf.prefix, PRESIZ);
7373 7373 tempbuf[PRESIZ] = '\0';
7374 7374 nbytes = strlen(tempbuf);
7375 7375 if (nbytes > 0) {
7376 7376 tempbuf[nbytes++] = '/';
7377 7377 tempbuf[nbytes] = '\0';
7378 7378 }
7379 7379 (void) strncat(tempbuf + nbytes, dblock.dbuf.name,
7380 7380 (MAXNAM - nbytes));
7381 7381 tempbuf[MAXNAM] = '\0';
7382 7382 }
7383 7383 errors += local_utf8(&Xtarhdr.x_path, local_path,
7384 7384 tempbuf, iconv_cd, _X_PATH, PATH_MAX);
7385 7385
7386 7386 if (errors > 0)
7387 7387 (void) fprintf(stderr, gettext(
7388 7388 "tar: file (%s): UTF-8 conversion failed.\n"), filename);
7389 7389
7390 7390 if (errors && errflag)
7391 7391 done(1);
7392 7392 else
7393 7393 if (errors)
7394 7394 Errflg = 1;
7395 7395 return (errors);
7396 7396 }
7397 7397
7398 7398 static int
7399 7399 local_utf8(
7400 7400 char **Xhdr_ptrptr,
7401 7401 char *target,
7402 7402 const char *source,
7403 7403 iconv_t iconv_cd,
7404 7404 int xhdrflg,
7405 7405 int max_val)
7406 7406 {
7407 7407 const char *iconv_src;
7408 7408 const char *starting_src;
7409 7409 char *iconv_trg;
7410 7410 size_t inlen;
7411 7411 size_t outlen;
7412 7412 #ifdef ICONV_DEBUG
7413 7413 unsigned char c_to_hex;
7414 7414 #endif
7415 7415
7416 7416 /*
7417 7417 * If the item is already slated for extended format, get the string
7418 7418 * to convert from the extended header record. Otherwise, get it from
7419 7419 * the regular (dblock) area.
7420 7420 */
7421 7421 if (xhdr_flgs & xhdrflg) {
7422 7422 if (charset_type == 3) { /* Already UTF-8, just copy */
7423 7423 (void) strcpy(target, *Xhdr_ptrptr);
7424 7424 *Xhdr_ptrptr = target;
7425 7425 return (0);
7426 7426 } else
7427 7427 iconv_src = (const char *) *Xhdr_ptrptr;
7428 7428 } else {
7429 7429 if (charset_type == 3) /* Already in UTF-8 format */
7430 7430 return (0); /* Don't create xhdr record */
7431 7431 iconv_src = source;
7432 7432 }
7433 7433 starting_src = iconv_src;
7434 7434 iconv_trg = target;
7435 7435 if ((inlen = strlen(iconv_src)) == 0)
7436 7436 return (0);
7437 7437
7438 7438 if (charset_type == 1) { /* locale using 7-bit codeset */
7439 7439 if (c_utf8(target, starting_src) != 0) {
7440 7440 (void) fprintf(stderr,
7441 7441 gettext("tar: invalid character in"
7442 7442 " UTF-8 conversion of '%s'\n"), starting_src);
7443 7443 return (1);
7444 7444 }
7445 7445 return (0);
7446 7446 }
7447 7447
7448 7448 outlen = max_val * UTF_8_FACTOR;
7449 7449 errno = 0;
7450 7450 if (iconv(iconv_cd, &iconv_src, &inlen, &iconv_trg, &outlen) ==
7451 7451 (size_t)-1) {
7452 7452 /* An error occurred, or not all characters were converted */
7453 7453 if (errno == EILSEQ)
7454 7454 (void) fprintf(stderr,
7455 7455 gettext("tar: invalid character in"
7456 7456 " UTF-8 conversion of '%s'\n"), starting_src);
7457 7457 else
7458 7458 (void) fprintf(stderr, gettext(
7459 7459 "tar: conversion to UTF-8 aborted for '%s'.\n"),
7460 7460 starting_src);
7461 7461 /* Get remaining output; reinitialize conversion descriptor */
7462 7462 iconv_src = (const char *)NULL;
7463 7463 inlen = 0;
7464 7464 (void) iconv(iconv_cd, &iconv_src, &inlen, &iconv_trg, &outlen);
7465 7465 return (1);
7466 7466 }
7467 7467 /* Get remaining output; reinitialize conversion descriptor */
7468 7468 iconv_src = (const char *)NULL;
7469 7469 inlen = 0;
7470 7470 if (iconv(iconv_cd, &iconv_src, &inlen, &iconv_trg, &outlen) ==
7471 7471 (size_t)-1) { /* Error occurred: didn't convert */
7472 7472 if (errno == EILSEQ)
7473 7473 (void) fprintf(stderr,
7474 7474 gettext("tar: invalid character in"
7475 7475 " UTF-8 conversion of '%s'\n"), starting_src);
7476 7476 else
7477 7477 (void) fprintf(stderr, gettext(
7478 7478 "tar: conversion to UTF-8 aborted for '%s'.\n"),
7479 7479 starting_src);
7480 7480 return (1);
7481 7481 }
7482 7482
7483 7483 *iconv_trg = '\0'; /* Null-terminate iconv output string */
7484 7484 if (strcmp(starting_src, target) != 0) {
7485 7485 *Xhdr_ptrptr = target;
7486 7486 xhdr_flgs |= xhdrflg;
7487 7487 #ifdef ICONV_DEBUG
7488 7488 (void) fprintf(stderr, "*** inlen: %d %d; outlen: %d %d\n",
7489 7489 strlen(starting_src), inlen, max_val, outlen);
7490 7490 (void) fprintf(stderr, "Input string:\n ");
7491 7491 for (inlen = 0; inlen < strlen(starting_src); inlen++) {
7492 7492 c_to_hex = (unsigned char)starting_src[inlen];
7493 7493 (void) fprintf(stderr, " %2.2x", c_to_hex);
7494 7494 if (inlen % 20 == 19)
7495 7495 (void) fprintf(stderr, "\n ");
7496 7496 }
7497 7497 (void) fprintf(stderr, "\nOutput string:\n ");
7498 7498 for (inlen = 0; inlen < strlen(target); inlen++) {
7499 7499 c_to_hex = (unsigned char)target[inlen];
7500 7500 (void) fprintf(stderr, " %2.2x", c_to_hex);
7501 7501 if (inlen % 20 == 19)
7502 7502 (void) fprintf(stderr, "\n ");
7503 7503 }
7504 7504 (void) fprintf(stderr, "\n");
7505 7505 #endif
7506 7506 }
7507 7507
7508 7508 return (0);
7509 7509 }
7510 7510
7511 7511 /*
7512 7512 * Function to test each byte of the source string to make sure it is
7513 7513 * in within bounds (value between 0 and 127).
7514 7514 * If valid, copy source to target.
7515 7515 */
7516 7516
7517 7517 int
7518 7518 c_utf8(char *target, const char *source)
7519 7519 {
7520 7520 size_t len;
7521 7521 const char *thischar;
7522 7522
7523 7523 len = strlen(source);
7524 7524 thischar = source;
7525 7525 while (len-- > 0) {
7526 7526 if (!isascii((int)(*thischar++)))
7527 7527 return (1);
7528 7528 }
7529 7529
7530 7530 (void) strcpy(target, source);
7531 7531 return (0);
7532 7532 }
7533 7533
7534 7534
7535 7535 #if defined(O_XATTR)
7536 7536 #define ROUNDTOTBLOCK(a) ((a + (TBLOCK -1)) & ~(TBLOCK -1))
7537 7537
7538 7538 static void
7539 7539 prepare_xattr(
7540 7540 char **attrbuf,
7541 7541 char *filename,
7542 7542 char *attrpath,
7543 7543 char typeflag,
7544 7544 struct linkbuf *linkinfo,
7545 7545 int *rlen)
7546 7546 {
7547 7547 char *bufhead; /* ptr to full buffer */
7548 7548 char *aptr;
7549 7549 struct xattr_hdr *hptr; /* ptr to header in bufhead */
7550 7550 struct xattr_buf *tptr; /* ptr to pathing pieces */
7551 7551 int totalen; /* total buffer length */
7552 7552 int len; /* length returned to user */
7553 7553 int stringlen; /* length of filename + attr */
7554 7554 /*
7555 7555 * length of filename + attr
7556 7556 * in link section
7557 7557 */
7558 7558 int linkstringlen;
7559 7559 int complen; /* length of pathing section */
7560 7560 int linklen; /* length of link section */
7561 7561 int attrnames_index; /* attrnames starting index */
7562 7562
7563 7563 /*
7564 7564 * Release previous buffer
7565 7565 */
7566 7566
7567 7567 if (*attrbuf != (char *)NULL) {
7568 7568 free(*attrbuf);
7569 7569 *attrbuf = NULL;
7570 7570 }
7571 7571
7572 7572 /*
7573 7573 * First add in fixed size stuff
7574 7574 */
7575 7575 len = sizeof (struct xattr_hdr) + sizeof (struct xattr_buf);
7576 7576
7577 7577 /*
7578 7578 * Add space for two nulls
7579 7579 */
7580 7580 stringlen = strlen(attrpath) + strlen(filename) + 2;
7581 7581 complen = stringlen + sizeof (struct xattr_buf);
7582 7582
7583 7583 len += stringlen;
7584 7584
7585 7585 /*
7586 7586 * Now add on space for link info if any
7587 7587 */
7588 7588
7589 7589 if (linkinfo != NULL) {
7590 7590 /*
7591 7591 * Again add space for two nulls
7592 7592 */
7593 7593 linkstringlen = strlen(linkinfo->pathname) +
7594 7594 strlen(linkinfo->attrname) + 2;
7595 7595 linklen = linkstringlen + sizeof (struct xattr_buf);
7596 7596 len += linklen;
7597 7597 } else {
7598 7598 linklen = 0;
7599 7599 }
7600 7600
7601 7601 /*
7602 7602 * Now add padding to end to fill out TBLOCK
7603 7603 *
7604 7604 * Function returns size of real data and not size + padding.
7605 7605 */
7606 7606
7607 7607 totalen = ROUNDTOTBLOCK(len);
7608 7608
7609 7609 if ((bufhead = calloc(1, totalen)) == NULL) {
7610 7610 fatal(gettext("Out of memory."));
7611 7611 }
7612 7612
7613 7613
7614 7614 /*
7615 7615 * Now we can fill in the necessary pieces
7616 7616 */
7617 7617
7618 7618 /*
7619 7619 * first fill in the fixed header
7620 7620 */
7621 7621 hptr = (struct xattr_hdr *)bufhead;
7622 7622 (void) sprintf(hptr->h_version, "%s", XATTR_ARCH_VERS);
7623 7623 (void) sprintf(hptr->h_component_len, "%0*d",
7624 7624 sizeof (hptr->h_component_len) - 1, complen);
7625 7625 (void) sprintf(hptr->h_link_component_len, "%0*d",
7626 7626 sizeof (hptr->h_link_component_len) - 1, linklen);
7627 7627 (void) sprintf(hptr->h_size, "%0*d", sizeof (hptr->h_size) - 1, len);
7628 7628
7629 7629 /*
7630 7630 * Now fill in the filename + attrnames section
7631 7631 * The filename and attrnames section can be composed of two or more
7632 7632 * path segments separated by a null character. The first segment
7633 7633 * is the path to the parent file that roots the entire sequence in
7634 7634 * the normal name space. The remaining segments describes a path
7635 7635 * rooted at the hidden extended attribute directory of the leaf file of
7636 7636 * the previous segment, making it possible to name attributes on
7637 7637 * attributes. Thus, if we are just archiving an extended attribute,
7638 7638 * the second segment will contain the attribute name. If we are
7639 7639 * archiving a system attribute of an extended attribute, then the
7640 7640 * second segment will contain the attribute name, and a third segment
7641 7641 * will contain the system attribute name. The attribute pathing
7642 7642 * information is obtained from 'attrpath'.
7643 7643 */
7644 7644
7645 7645 tptr = (struct xattr_buf *)(bufhead + sizeof (struct xattr_hdr));
7646 7646 (void) sprintf(tptr->h_namesz, "%0*d", sizeof (tptr->h_namesz) - 1,
7647 7647 stringlen);
7648 7648 (void) strcpy(tptr->h_names, filename);
7649 7649 attrnames_index = strlen(filename) + 1;
7650 7650 (void) strcpy(&tptr->h_names[attrnames_index], attrpath);
7651 7651 tptr->h_typeflag = typeflag;
7652 7652
7653 7653 /*
7654 7654 * Split the attrnames section into two segments if 'attrpath'
7655 7655 * contains pathing information for a system attribute of an
7656 7656 * extended attribute. We split them by replacing the '/' with
7657 7657 * a '\0'.
7658 7658 */
7659 7659 if ((aptr = strpbrk(&tptr->h_names[attrnames_index], "/")) != NULL) {
7660 7660 *aptr = '\0';
7661 7661 }
7662 7662
7663 7663 /*
7664 7664 * Now fill in the optional link section if we have one
7665 7665 */
7666 7666
7667 7667 if (linkinfo != (struct linkbuf *)NULL) {
7668 7668 tptr = (struct xattr_buf *)(bufhead +
7669 7669 sizeof (struct xattr_hdr) + complen);
7670 7670
7671 7671 (void) sprintf(tptr->h_namesz, "%0*d",
7672 7672 sizeof (tptr->h_namesz) - 1, linkstringlen);
7673 7673 (void) strcpy(tptr->h_names, linkinfo->pathname);
7674 7674 (void) strcpy(
7675 7675 &tptr->h_names[strlen(linkinfo->pathname) + 1],
7676 7676 linkinfo->attrname);
7677 7677 tptr->h_typeflag = typeflag;
7678 7678 }
7679 7679 *attrbuf = (char *)bufhead;
7680 7680 *rlen = len;
7681 7681 }
7682 7682
7683 7683 #else
7684 7684 static void
7685 7685 prepare_xattr(
7686 7686 char **attrbuf,
7687 7687 char *filename,
7688 7688 char *attrname,
7689 7689 char typeflag,
7690 7690 struct linkbuf *linkinfo,
7691 7691 int *rlen)
7692 7692 {
7693 7693 *attrbuf = NULL;
7694 7694 *rlen = 0;
7695 7695 }
7696 7696 #endif
7697 7697
7698 7698 int
7699 7699 getstat(int dirfd, char *longname, char *shortname, char *attrparent)
7700 7700 {
7701 7701
7702 7702 int i, j;
7703 7703 int printerr;
7704 7704 int slnkerr;
7705 7705 struct stat symlnbuf;
7706 7706
7707 7707 if (!hflag)
7708 7708 i = fstatat(dirfd, shortname, &stbuf, AT_SYMLINK_NOFOLLOW);
7709 7709 else
7710 7710 i = fstatat(dirfd, shortname, &stbuf, 0);
7711 7711
7712 7712 if (i < 0) {
7713 7713 /* Initialize flag to print error mesg. */
7714 7714 printerr = 1;
7715 7715 /*
7716 7716 * If stat is done, then need to do lstat
7717 7717 * to determine whether it's a sym link
7718 7718 */
7719 7719 if (hflag) {
7720 7720 /* Save returned error */
7721 7721 slnkerr = errno;
7722 7722
7723 7723 j = fstatat(dirfd, shortname,
7724 7724 &symlnbuf, AT_SYMLINK_NOFOLLOW);
7725 7725 /*
7726 7726 * Suppress error message when file is a symbolic link
7727 7727 * and function modifier 'l' is off. Exception: when
7728 7728 * a symlink points to a symlink points to a
7729 7729 * symlink ... and we get past MAXSYMLINKS. That
7730 7730 * error will cause a file not to be archived, and
7731 7731 * needs to be printed.
7732 7732 */
7733 7733 if ((j == 0) && (!linkerrok) && (slnkerr != ELOOP) &&
7734 7734 (S_ISLNK(symlnbuf.st_mode)))
7735 7735 printerr = 0;
7736 7736
7737 7737 /*
7738 7738 * Restore errno in case the lstat
7739 7739 * on symbolic link change
7740 7740 */
7741 7741 errno = slnkerr;
7742 7742 }
7743 7743
7744 7744 if (printerr) {
7745 7745 (void) fprintf(stderr, gettext(
7746 7746 "tar: %s%s%s%s: %s\n"),
7747 7747 (attrparent == NULL) ? "" : gettext("attribute "),
7748 7748 (attrparent == NULL) ? "" : attrparent,
7749 7749 (attrparent == NULL) ? "" : gettext(" of "),
7750 7750 longname, strerror(errno));
7751 7751 Errflg = 1;
7752 7752 }
7753 7753 return (1);
7754 7754 }
7755 7755 return (0);
7756 7756 }
7757 7757
7758 7758 /*
7759 7759 * Recursively archive the extended attributes and/or extended system attributes
7760 7760 * of the base file, longname. Note: extended system attribute files will be
7761 7761 * archived only if the extended system attributes are not transient (i.e. the
7762 7762 * extended system attributes are other than the default values).
7763 7763 *
7764 7764 * If -@ was specified and the underlying file system supports it, archive the
7765 7765 * extended attributes, and if there is a system attribute associated with the
7766 7766 * extended attribute, then recursively call xattrs_put() to archive the
7767 7767 * hidden attribute directory and the extended system attribute. If -/ was
7768 7768 * specified and the underlying file system supports it, archive the extended
7769 7769 * system attributes. Read-only extended system attributes are never archived.
7770 7770 *
7771 7771 * Currently, there cannot be attributes on attributes; only system
7772 7772 * attributes on attributes. In addition, there cannot be attributes on
7773 7773 * system attributes. A file and it's attribute directory hierarchy looks as
7774 7774 * follows:
7775 7775 * longname ----> . ("." is the hidden attribute directory)
7776 7776 * |
7777 7777 * ----------------------------
7778 7778 * | |
7779 7779 * <sys_attr_name> <attr_name> ----> .
7780 7780 * |
7781 7781 * <sys_attr_name>
7782 7782 *
7783 7783 */
7784 7784 #if defined(O_XATTR)
7785 7785 static void
7786 7786 xattrs_put(char *longname, char *shortname, char *parent, char *attrparent)
7787 7787 {
7788 7788 char *filename = (attrparent == NULL) ? shortname : attrparent;
7789 7789 int arc_rwsysattr = 0;
7790 7790 int dirfd;
7791 7791 int fd = -1;
7792 7792 int rw_sysattr = 0;
7793 7793 int ext_attr = 0;
7794 7794 int rc;
7795 7795 DIR *dirp;
7796 7796 struct dirent *dp;
7797 7797 attr_data_t *attrinfo = NULL;
7798 7798
7799 7799 /*
7800 7800 * If the underlying file system supports it, then archive the extended
7801 7801 * attributes if -@ was specified, and the extended system attributes
7802 7802 * if -/ was specified.
7803 7803 */
7804 7804 if (verify_attr_support(filename, (attrparent == NULL), ARC_CREATE,
7805 7805 &ext_attr) != ATTR_OK) {
7806 7806 return;
7807 7807 }
7808 7808
7809 7809 /*
7810 7810 * Only want to archive a read-write extended system attribute file
7811 7811 * if it contains extended system attribute settings that are not the
7812 7812 * default values.
7813 7813 */
7814 7814 #if defined(_PC_SATTR_ENABLED)
7815 7815 if (saflag) {
7816 7816 int filefd;
7817 7817 nvlist_t *slist = NULL;
7818 7818
7819 7819 /* Determine if there are non-transient system attributes */
7820 7820 errno = 0;
7821 7821 if ((filefd = open(filename, O_RDONLY)) == -1) {
7822 7822 if (attrparent == NULL) {
7823 7823 vperror(0, gettext(
7824 7824 "unable to open file %s"), longname);
7825 7825 }
7826 7826 return;
7827 7827 }
7828 7828 if (((slist = sysattr_list(basename(myname), filefd,
7829 7829 filename)) != NULL) || (errno != 0)) {
7830 7830 arc_rwsysattr = 1;
7831 7831 }
7832 7832 if (slist != NULL) {
7833 7833 (void) nvlist_free(slist);
7834 7834 slist = NULL;
7835 7835 }
7836 7836 (void) close(filefd);
7837 7837 }
7838 7838
7839 7839 /*
7840 7840 * If we aren't archiving extended system attributes, and we are
7841 7841 * processing an attribute, or if we are archiving extended system
7842 7842 * attributes, and there are are no extended attributes, then there's
7843 7843 * no need to open up the attribute directory of the file unless the
7844 7844 * extended system attributes are not transient (i.e, the system
7845 7845 * attributes are not the default values).
7846 7846 */
7847 7847 if ((arc_rwsysattr == 0) && ((attrparent != NULL) ||
7848 7848 (saflag && !ext_attr))) {
7849 7849 return;
7850 7850 }
7851 7851 #endif /* _PC_SATTR_ENABLED */
7852 7852
7853 7853 /* open the parent attribute directory */
7854 7854 fd = attropen(filename, ".", O_RDONLY);
7855 7855 if (fd < 0) {
7856 7856 vperror(0, gettext(
7857 7857 "unable to open attribute directory for %s%s%sfile %s"),
7858 7858 (attrparent == NULL) ? "" : gettext("attribute "),
7859 7859 (attrparent == NULL) ? "" : attrparent,
7860 7860 (attrparent == NULL) ? "" : gettext(" of "),
7861 7861 longname);
7862 7862 return;
7863 7863 }
7864 7864
7865 7865 /*
7866 7866 * We need to change into the parent's attribute directory to determine
7867 7867 * if each of the attributes should be archived.
7868 7868 */
7869 7869 if (fchdir(fd) < 0) {
7870 7870 vperror(0, gettext(
7871 7871 "cannot change to attribute directory of %s%s%sfile %s"),
7872 7872 (attrparent == NULL) ? "" : gettext("attribute "),
7873 7873 (attrparent == NULL) ? "" : attrparent,
7874 7874 (attrparent == NULL) ? "" : gettext(" of "),
7875 7875 longname);
7876 7876 (void) close(fd);
7877 7877 return;
7878 7878 }
7879 7879
7880 7880 if (((dirfd = dup(fd)) == -1) ||
7881 7881 ((dirp = fdopendir(dirfd)) == NULL)) {
7882 7882 (void) fprintf(stderr, gettext(
7883 7883 "tar: unable to open dir pointer for %s%s%sfile %s\n"),
7884 7884 (attrparent == NULL) ? "" : gettext("attribute "),
7885 7885 (attrparent == NULL) ? "" : attrparent,
7886 7886 (attrparent == NULL) ? "" : gettext(" of "),
7887 7887 longname);
7888 7888 if (fd > 0) {
7889 7889 (void) close(fd);
7890 7890 }
7891 7891 return;
7892 7892 }
7893 7893
7894 7894 while (dp = readdir(dirp)) {
7895 7895 if (strcmp(dp->d_name, "..") == 0) {
7896 7896 continue;
7897 7897 } else if (strcmp(dp->d_name, ".") == 0) {
7898 7898 Hiddendir = 1;
7899 7899 } else {
7900 7900 Hiddendir = 0;
7901 7901 }
7902 7902
7903 7903 /* Determine if this attribute should be archived */
7904 7904 if (verify_attr(dp->d_name, attrparent, arc_rwsysattr,
7905 7905 &rw_sysattr) != ATTR_OK) {
7906 7906 continue;
7907 7907 }
7908 7908
7909 7909 /* gather the attribute's information to pass to putfile() */
7910 7910 if ((fill_in_attr_info(dp->d_name, longname, attrparent,
7911 7911 fd, rw_sysattr, &attrinfo)) == 1) {
7912 7912 continue;
7913 7913 }
7914 7914
7915 7915 /* add the attribute to the archive */
7916 7916 rc = putfile(longname, dp->d_name, parent, attrinfo,
7917 7917 XATTR_FILE, LEV0, SYMLINK_LEV0);
7918 7918
7919 7919 if (exitflag) {
7920 7920 break;
7921 7921 }
7922 7922
7923 7923 #if defined(_PC_SATTR_ENABLED)
7924 7924 /*
7925 7925 * If both -/ and -@ were specified, then archive the
7926 7926 * attribute's extended system attributes and hidden directory
7927 7927 * by making a recursive call to xattrs_put().
7928 7928 */
7929 7929 if (!rw_sysattr && saflag && atflag && (rc != PUT_AS_LINK) &&
7930 7930 (Hiddendir == 0)) {
7931 7931
7932 7932 xattrs_put(longname, shortname, parent, dp->d_name);
7933 7933
7934 7934 /*
7935 7935 * Change back to the parent's attribute directory
7936 7936 * to process any further attributes.
7937 7937 */
7938 7938 if (fchdir(fd) < 0) {
7939 7939 vperror(0, gettext(
7940 7940 "cannot change back to attribute directory "
7941 7941 "of file %s"), longname);
7942 7942 break;
7943 7943 }
7944 7944 }
7945 7945 #endif /* _PC_SATTR_ENABLED */
7946 7946 }
7947 7947
7948 7948 if (attrinfo != NULL) {
7949 7949 if (attrinfo->attr_parent != NULL) {
7950 7950 free(attrinfo->attr_parent);
7951 7951 }
7952 7952 free(attrinfo->attr_path);
7953 7953 free(attrinfo);
7954 7954 }
7955 7955 (void) closedir(dirp);
7956 7956 if (fd != -1) {
7957 7957 (void) close(fd);
7958 7958 }
7959 7959
7960 7960 /* Change back to the parent directory of the base file */
7961 7961 if (attrparent == NULL) {
7962 7962 (void) tar_chdir(parent);
7963 7963 }
7964 7964 Hiddendir = 0;
7965 7965 }
7966 7966 #else
7967 7967 static void
7968 7968 xattrs_put(char *longname, char *shortname, char *parent, char *attrppath)
7969 7969 {
7970 7970 }
7971 7971 #endif /* O_XATTR */
7972 7972
7973 7973 static int
7974 7974 put_link(char *name, char *longname, char *component, char *longattrname,
7975 7975 char *prefix, int filetype, char type)
7976 7976 {
7977 7977
7978 7978 if (stbuf.st_nlink > 1) {
7979 7979 struct linkbuf *lp;
7980 7980 int found = 0;
7981 7981
7982 7982 for (lp = ihead; lp != NULL; lp = lp->nextp)
7983 7983 if (lp->inum == stbuf.st_ino &&
7984 7984 lp->devnum == stbuf.st_dev) {
7985 7985 found++;
7986 7986 break;
7987 7987 }
7988 7988 if (found) {
7989 7989 #if defined(O_XATTR)
7990 7990 if (filetype == XATTR_FILE)
7991 7991 if (put_xattr_hdr(longname, component,
7992 7992 longattrname, prefix, type, filetype, lp)) {
7993 7993 goto out;
7994 7994 }
7995 7995 #endif
7996 7996 stbuf.st_size = (off_t)0;
7997 7997 if (filetype != XATTR_FILE) {
7998 7998 tomodes(&stbuf);
7999 7999 if (chk_path_build(name, longname, lp->pathname,
8000 8000 prefix, type, filetype) > 0) {
8001 8001 goto out;
8002 8002 }
8003 8003 }
8004 8004
8005 8005 if (mulvol && tapepos + 1 >= blocklim)
8006 8006 newvol();
8007 8007 (void) writetbuf((char *)&dblock, 1);
8008 8008 /*
8009 8009 * write_ancillary() is not needed here.
8010 8010 * The first link is handled in the following
8011 8011 * else statement. No need to process ACLs
8012 8012 * for other hard links since they are the
8013 8013 * same file.
8014 8014 */
8015 8015
8016 8016 if (vflag) {
8017 8017 #ifdef DEBUG
8018 8018 if (NotTape)
8019 8019 DEBUG("seek = %" FMT_blkcnt_t
8020 8020 "K\t", K(tapepos), 0);
8021 8021 #endif
8022 8022 if (filetype == XATTR_FILE) {
8023 8023 (void) fprintf(vfile, gettext(
8024 8024 "a %s attribute %s link to "
8025 8025 "%s attribute %s\n"),
8026 8026 name, component, name,
8027 8027 lp->attrname);
8028 8028 } else {
8029 8029 (void) fprintf(vfile, gettext(
8030 8030 "a %s link to %s\n"),
8031 8031 longname, lp->pathname);
8032 8032 }
8033 8033 }
8034 8034 lp->count--;
8035 8035 return (0);
8036 8036 } else {
8037 8037 lp = (struct linkbuf *)getmem(sizeof (*lp));
8038 8038 if (lp != (struct linkbuf *)NULL) {
8039 8039 lp->nextp = ihead;
8040 8040 ihead = lp;
8041 8041 lp->inum = stbuf.st_ino;
8042 8042 lp->devnum = stbuf.st_dev;
8043 8043 lp->count = stbuf.st_nlink - 1;
8044 8044 if (filetype == XATTR_FILE) {
8045 8045 (void) strcpy(lp->pathname, longname);
8046 8046 (void) strcpy(lp->attrname,
8047 8047 component);
8048 8048 } else {
8049 8049 (void) strcpy(lp->pathname, longname);
8050 8050 (void) strcpy(lp->attrname, "");
8051 8051 }
8052 8052 }
8053 8053 }
8054 8054 }
8055 8055
8056 8056 out:
8057 8057 return (1);
8058 8058 }
8059 8059
8060 8060 static int
8061 8061 put_extra_attributes(char *longname, char *shortname, char *longattrname,
8062 8062 char *prefix, int filetype, char typeflag)
8063 8063 {
8064 8064 static acl_t *aclp = NULL;
8065 8065 int error;
8066 8066
8067 8067 if (aclp != NULL) {
8068 8068 acl_free(aclp);
8069 8069 aclp = NULL;
8070 8070 }
8071 8071 #if defined(O_XATTR)
8072 8072 if ((atflag || saflag) && (filetype == XATTR_FILE)) {
8073 8073 if (put_xattr_hdr(longname, shortname, longattrname, prefix,
8074 8074 typeflag, filetype, NULL)) {
8075 8075 return (1);
8076 8076 }
8077 8077 }
8078 8078 #endif
8079 8079
8080 8080 /* ACL support */
8081 8081 if (pflag) {
8082 8082 char *secinfo = NULL;
8083 8083 int len = 0;
8084 8084
8085 8085 /* ACL support */
8086 8086 if (((stbuf.st_mode & S_IFMT) != S_IFLNK)) {
8087 8087 /*
8088 8088 * Get ACL info: dont bother allocating space if
8089 8089 * there is only a trivial ACL.
8090 8090 */
8091 8091 if ((error = acl_get(shortname, ACL_NO_TRIVIAL,
8092 8092 &aclp)) != 0) {
8093 8093 (void) fprintf(stderr, gettext(
8094 8094 "%s: failed to retrieve acl : %s\n"),
8095 8095 longname, acl_strerror(error));
8096 8096 return (1);
8097 8097 }
8098 8098 }
8099 8099
8100 8100 /* append security attributes if any */
8101 8101 if (aclp != NULL) {
8102 8102 (void) append_secattr(&secinfo, &len, acl_cnt(aclp),
8103 8103 acl_totext(aclp, ACL_APPEND_ID | ACL_COMPACT_FMT |
8104 8104 ACL_SID_FMT), (acl_type(aclp) == ACLENT_T) ?
8105 8105 UFSD_ACL : ACE_ACL);
8106 8106 }
8107 8107
8108 8108 if (Tflag) {
8109 8109 /* append Trusted Extensions extended attributes */
8110 8110 append_ext_attr(shortname, &secinfo, &len);
8111 8111 (void) write_ancillary(&dblock, secinfo, len, ACL_HDR);
8112 8112
8113 8113 } else if (aclp != NULL) {
8114 8114 (void) write_ancillary(&dblock, secinfo, len, ACL_HDR);
8115 8115 }
8116 8116 }
8117 8117 return (0);
8118 8118 }
8119 8119
8120 8120 #if defined(O_XATTR)
8121 8121 static int
8122 8122 put_xattr_hdr(char *longname, char *shortname, char *longattrname, char *prefix,
8123 8123 int typeflag, int filetype, struct linkbuf *lp)
8124 8124 {
8125 8125 char *lname = NULL;
8126 8126 char *sname = NULL;
8127 8127 int error = 0;
8128 8128 static char *attrbuf = NULL;
8129 8129 int attrlen;
8130 8130
8131 8131 lname = malloc(sizeof (char) * strlen("/dev/null") + 1 +
8132 8132 strlen(shortname) + strlen(".hdr") + 1);
8133 8133
8134 8134 if (lname == NULL) {
8135 8135 fatal(gettext("Out of Memory."));
8136 8136 }
8137 8137 sname = malloc(sizeof (char) * strlen(shortname) +
8138 8138 strlen(".hdr") + 1);
8139 8139 if (sname == NULL) {
8140 8140 fatal(gettext("Out of Memory."));
8141 8141 }
8142 8142
8143 8143 (void) sprintf(sname, "%s.hdr", shortname);
8144 8144 (void) sprintf(lname, "/dev/null/%s", sname);
8145 8145
8146 8146 if (strlcpy(dblock.dbuf.name, lname, sizeof (dblock.dbuf.name)) >=
8147 8147 sizeof (dblock.dbuf.name)) {
8148 8148 fatal(gettext(
8149 8149 "Buffer overflow writing extended attribute file name"));
8150 8150 }
8151 8151
8152 8152 /*
8153 8153 * dump extended attr lookup info
8154 8154 */
8155 8155 prepare_xattr(&attrbuf, longname, longattrname, typeflag, lp, &attrlen);
8156 8156 write_ancillary(&dblock, attrbuf, attrlen, _XATTR_HDRTYPE);
8157 8157
8158 8158 (void) sprintf(lname, "/dev/null/%s", shortname);
8159 8159 (void) strncpy(dblock.dbuf.name, sname, NAMSIZ);
8160 8160
8161 8161 /*
8162 8162 * Set up filename for attribute
8163 8163 */
8164 8164
8165 8165 error = build_dblock(lname, tchar, '0', filetype,
8166 8166 &stbuf, stbuf.st_dev, prefix);
8167 8167 free(lname);
8168 8168 free(sname);
8169 8169
8170 8170 return (error);
8171 8171 }
8172 8172 #endif
8173 8173
8174 8174 #if defined(O_XATTR)
8175 8175 static int
8176 8176 read_xattr_hdr(attr_data_t **attrinfo)
8177 8177 {
8178 8178 char buf[TBLOCK];
8179 8179 char *attrparent = NULL;
8180 8180 blkcnt_t blocks;
8181 8181 char *tp;
8182 8182 off_t bytes;
8183 8183 int comp_len, link_len;
8184 8184 int namelen;
8185 8185 int attrparentlen;
8186 8186 int parentfilelen;
8187 8187
8188 8188 if (dblock.dbuf.typeflag != _XATTR_HDRTYPE)
8189 8189 return (1);
8190 8190
8191 8191 bytes = stbuf.st_size;
8192 8192 if ((xattrhead = calloc(1, (int)bytes)) == NULL) {
8193 8193 (void) fprintf(stderr, gettext(
8194 8194 "Insufficient memory for extended attribute\n"));
8195 8195 return (1);
8196 8196 }
8197 8197
8198 8198 tp = (char *)xattrhead;
8199 8199 blocks = TBLOCKS(bytes);
8200 8200 while (blocks-- > 0) {
8201 8201 readtape(buf);
8202 8202 if (bytes <= TBLOCK) {
8203 8203 (void) memcpy(tp, buf, (size_t)bytes);
8204 8204 break;
8205 8205 } else {
8206 8206 (void) memcpy(tp, buf, TBLOCK);
8207 8207 tp += TBLOCK;
8208 8208 }
8209 8209 bytes -= TBLOCK;
8210 8210 }
8211 8211
8212 8212 /*
8213 8213 * Validate that we can handle header format
8214 8214 */
8215 8215 if (strcmp(xattrhead->h_version, XATTR_ARCH_VERS) != 0) {
8216 8216 (void) fprintf(stderr,
8217 8217 gettext("Unknown extended attribute format encountered\n"));
8218 8218 (void) fprintf(stderr,
8219 8219 gettext("Disabling extended attribute parsing\n"));
8220 8220 xattrbadhead = 1;
8221 8221 return (0);
8222 8222 }
8223 8223 (void) sscanf(xattrhead->h_component_len, "%10d", &comp_len);
8224 8224 (void) sscanf(xattrhead->h_link_component_len, "%10d", &link_len);
8225 8225 xattrp = (struct xattr_buf *)(((char *)xattrhead) +
8226 8226 sizeof (struct xattr_hdr));
8227 8227 (void) sscanf(xattrp->h_namesz, "%7d", &namelen);
8228 8228 if (link_len > 0)
8229 8229 xattr_linkp = (struct xattr_buf *)
8230 8230 ((int)xattrp + (int)comp_len);
8231 8231 else
8232 8232 xattr_linkp = NULL;
8233 8233
8234 8234 /*
8235 8235 * Gather the attribute path from the filename and attrnames section.
8236 8236 * The filename and attrnames section can be composed of two or more
8237 8237 * path segments separated by a null character. The first segment
8238 8238 * is the path to the parent file that roots the entire sequence in
8239 8239 * the normal name space. The remaining segments describes a path
8240 8240 * rooted at the hidden extended attribute directory of the leaf file of
8241 8241 * the previous segment, making it possible to name attributes on
8242 8242 * attributes.
8243 8243 */
8244 8244 parentfilelen = strlen(xattrp->h_names);
8245 8245 xattrapath = xattrp->h_names + parentfilelen + 1;
8246 8246 if ((strlen(xattrapath) + parentfilelen + 2) < namelen) {
8247 8247 /*
8248 8248 * The attrnames section contains a system attribute on an
8249 8249 * attribute. Save the name of the attribute for use later,
8250 8250 * and replace the null separating the attribute name from
8251 8251 * the system attribute name with a '/' so that xattrapath can
8252 8252 * be used to display messages with the full attribute path name
8253 8253 * rooted at the hidden attribute directory of the base file
8254 8254 * in normal name space.
8255 8255 */
8256 8256 attrparent = strdup(xattrapath);
8257 8257 attrparentlen = strlen(attrparent);
8258 8258 xattrapath[attrparentlen] = '/';
8259 8259 }
8260 8260 if ((fill_in_attr_info((attrparent == NULL) ? xattrapath :
8261 8261 xattrapath + attrparentlen + 1, xattrapath, attrparent,
8262 8262 -1, 0, attrinfo)) == 1) {
8263 8263 free(attrparent);
8264 8264 return (1);
8265 8265 }
8266 8266
8267 8267 /* Gather link info */
8268 8268 if (xattr_linkp) {
8269 8269 xattr_linkaname = xattr_linkp->h_names +
8270 8270 strlen(xattr_linkp->h_names) + 1;
8271 8271 } else {
8272 8272 xattr_linkaname = NULL;
8273 8273 }
8274 8274
8275 8275 return (0);
8276 8276 }
8277 8277 #else
8278 8278 static int
8279 8279 read_xattr_hdr(attr_data_t **attrinfo)
8280 8280 {
8281 8281 return (0);
8282 8282 }
8283 8283 #endif
8284 8284
8285 8285 /*
8286 8286 * skip over extra slashes in string.
8287 8287 *
8288 8288 * For example:
8289 8289 * /usr/tmp/////
8290 8290 *
8291 8291 * would return pointer at
8292 8292 * /usr/tmp/////
8293 8293 * ^
8294 8294 */
8295 8295 static char *
8296 8296 skipslashes(char *string, char *start)
8297 8297 {
8298 8298 while ((string > start) && *(string - 1) == '/') {
8299 8299 string--;
8300 8300 }
8301 8301
8302 8302 return (string);
8303 8303 }
8304 8304
8305 8305 /*
8306 8306 * Return the parent directory of a given path.
8307 8307 *
8308 8308 * Examples:
8309 8309 * /usr/tmp return /usr
8310 8310 * /usr/tmp/file return /usr/tmp
8311 8311 * / returns .
8312 8312 * /usr returns /
8313 8313 * file returns .
8314 8314 *
8315 8315 * dir is assumed to be at least as big as path.
8316 8316 */
8317 8317 static void
8318 8318 get_parent(char *path, char *dir)
8319 8319 {
8320 8320 char *s;
8321 8321 char tmpdir[PATH_MAX + 1];
8322 8322
8323 8323 if (strlen(path) > PATH_MAX) {
8324 8324 fatal(gettext("pathname is too long"));
8325 8325 }
8326 8326 (void) strcpy(tmpdir, path);
8327 8327 chop_endslashes(tmpdir);
8328 8328
8329 8329 if ((s = strrchr(tmpdir, '/')) == NULL) {
8330 8330 (void) strcpy(dir, ".");
8331 8331 } else {
8332 8332 s = skipslashes(s, tmpdir);
8333 8333 *s = '\0';
8334 8334 if (s == tmpdir)
8335 8335 (void) strcpy(dir, "/");
8336 8336 else
8337 8337 (void) strcpy(dir, tmpdir);
8338 8338 }
8339 8339 }
8340 8340
8341 8341 #if defined(O_XATTR)
8342 8342 static char *
8343 8343 get_component(char *path)
8344 8344 {
8345 8345 char *ptr;
8346 8346
8347 8347 ptr = strrchr(path, '/');
8348 8348 if (ptr == NULL) {
8349 8349 return (path);
8350 8350 } else {
8351 8351 /*
8352 8352 * Handle trailing slash
8353 8353 */
8354 8354 if (*(ptr + 1) == '\0')
8355 8355 return (ptr);
8356 8356 else
8357 8357 return (ptr + 1);
8358 8358 }
8359 8359 }
8360 8360 #else
8361 8361 static char *
8362 8362 get_component(char *path)
8363 8363 {
8364 8364 return (path);
8365 8365 }
8366 8366 #endif
8367 8367
8368 8368 #if defined(O_XATTR)
8369 8369 static int
8370 8370 retry_open_attr(int pdirfd, int cwd, char *dirp, char *pattr, char *name,
8371 8371 int oflag, mode_t mode)
8372 8372 {
8373 8373 int dirfd;
8374 8374 int ofilefd = -1;
8375 8375 struct timeval times[2];
8376 8376 mode_t newmode;
8377 8377 struct stat parentstat;
8378 8378 acl_t *aclp = NULL;
8379 8379 int error;
8380 8380
8381 8381 /*
8382 8382 * We couldn't get to attrdir. See if its
8383 8383 * just a mode problem on the parent file.
8384 8384 * for example: a mode such as r-xr--r--
8385 8385 * on a ufs file system without extended
8386 8386 * system attribute support won't let us
8387 8387 * create an attribute dir if it doesn't
8388 8388 * already exist, and on a ufs file system
8389 8389 * with extended system attribute support
8390 8390 * won't let us open the attribute for
8391 8391 * write.
8392 8392 *
8393 8393 * If file has a non-trivial ACL, then save it
8394 8394 * off so that we can place it back on after doing
8395 8395 * chmod's.
8396 8396 */
8397 8397 if ((dirfd = openat(cwd, (pattr == NULL) ? dirp : pattr,
8398 8398 O_RDONLY)) == -1) {
8399 8399 return (-1);
8400 8400 }
8401 8401 if (fstat(dirfd, &parentstat) == -1) {
8402 8402 (void) fprintf(stderr, gettext(
8403 8403 "tar: cannot stat %sfile %s: %s\n"),
8404 8404 (pdirfd == -1) ? "" : gettext("parent of "),
8405 8405 (pdirfd == -1) ? dirp : name, strerror(errno));
8406 8406 return (-1);
8407 8407 }
8408 8408 if ((error = facl_get(dirfd, ACL_NO_TRIVIAL, &aclp)) != 0) {
8409 8409 (void) fprintf(stderr, gettext(
8410 8410 "tar: failed to retrieve ACL on %sfile %s: %s\n"),
8411 8411 (pdirfd == -1) ? "" : gettext("parent of "),
8412 8412 (pdirfd == -1) ? dirp : name, strerror(errno));
8413 8413 return (-1);
8414 8414 }
8415 8415
8416 8416 newmode = S_IWUSR | parentstat.st_mode;
8417 8417 if (fchmod(dirfd, newmode) == -1) {
8418 8418 (void) fprintf(stderr,
8419 8419 gettext(
8420 8420 "tar: cannot fchmod %sfile %s to %o: %s\n"),
8421 8421 (pdirfd == -1) ? "" : gettext("parent of "),
8422 8422 (pdirfd == -1) ? dirp : name, newmode, strerror(errno));
8423 8423 if (aclp)
8424 8424 acl_free(aclp);
8425 8425 return (-1);
8426 8426 }
8427 8427
8428 8428
8429 8429 if (pdirfd == -1) {
8430 8430 /*
8431 8431 * We weren't able to create the attribute directory before.
8432 8432 * Now try again.
8433 8433 */
8434 8434 ofilefd = attropen(dirp, ".", oflag);
8435 8435 } else {
8436 8436 /*
8437 8437 * We weren't able to create open the attribute before.
8438 8438 * Now try again.
8439 8439 */
8440 8440 ofilefd = openat(pdirfd, name, oflag, mode);
8441 8441 }
8442 8442
8443 8443 /*
8444 8444 * Put mode back to original
8445 8445 */
8446 8446 if (fchmod(dirfd, parentstat.st_mode) == -1) {
8447 8447 (void) fprintf(stderr,
8448 8448 gettext("tar: cannot chmod %sfile %s to %o: %s\n"),
8449 8449 (pdirfd == -1) ? "" : gettext("parent of "),
8450 8450 (pdirfd == -1) ? dirp : name, newmode, strerror(errno));
8451 8451 }
8452 8452
8453 8453 if (aclp) {
8454 8454 error = facl_set(dirfd, aclp);
8455 8455 if (error) {
8456 8456 (void) fprintf(stderr,
8457 8457 gettext("tar: failed to set acl entries on "
8458 8458 "%sfile %s\n"),
8459 8459 (pdirfd == -1) ? "" : gettext("parent of "),
8460 8460 (pdirfd == -1) ? dirp : name);
8461 8461 }
8462 8462 acl_free(aclp);
8463 8463 }
8464 8464
8465 8465 /*
8466 8466 * Put back time stamps
8467 8467 */
8468 8468
8469 8469 times[0].tv_sec = parentstat.st_atime;
8470 8470 times[0].tv_usec = 0;
8471 8471 times[1].tv_sec = parentstat.st_mtime;
8472 8472 times[1].tv_usec = 0;
8473 8473
8474 8474 (void) futimesat(cwd, (pattr == NULL) ? dirp : pattr, times);
8475 8475
8476 8476 (void) close(dirfd);
8477 8477
8478 8478 return (ofilefd);
8479 8479 }
8480 8480 #endif
8481 8481
8482 8482 #if !defined(O_XATTR)
8483 8483 static int
8484 8484 openat64(int fd, const char *name, int oflag, mode_t cmode)
8485 8485 {
8486 8486 return (open64(name, oflag, cmode));
8487 8487 }
8488 8488
8489 8489 static int
8490 8490 openat(int fd, const char *name, int oflag, mode_t cmode)
8491 8491 {
8492 8492 return (open(name, oflag, cmode));
8493 8493 }
8494 8494
8495 8495 static int
8496 8496 fchownat(int fd, const char *name, uid_t owner, gid_t group, int flag)
8497 8497 {
8498 8498 if (flag == AT_SYMLINK_NOFOLLOW)
8499 8499 return (lchown(name, owner, group));
8500 8500 else
8501 8501 return (chown(name, owner, group));
8502 8502 }
8503 8503
8504 8504 static int
8505 8505 renameat(int fromfd, char *old, int tofd, char *new)
8506 8506 {
8507 8507 return (rename(old, new));
8508 8508 }
8509 8509
8510 8510 static int
8511 8511 futimesat(int fd, char *path, struct timeval times[2])
8512 8512 {
8513 8513 return (utimes(path, times));
8514 8514 }
8515 8515
8516 8516 static int
8517 8517 unlinkat(int dirfd, char *path, int flag)
8518 8518 {
8519 8519 if (flag == AT_REMOVEDIR)
8520 8520 return (rmdir(path));
8521 8521 else
8522 8522 return (unlink(path));
8523 8523 }
8524 8524
8525 8525 static int
8526 8526 fstatat(int fd, char *path, struct stat *buf, int flag)
8527 8527 {
8528 8528 if (flag == AT_SYMLINK_NOFOLLOW)
8529 8529 return (lstat(path, buf));
8530 8530 else
8531 8531 return (stat(path, buf));
8532 8532 }
8533 8533
8534 8534 static int
8535 8535 attropen(char *file, char *attr, int omode, mode_t cmode)
8536 8536 {
8537 8537 errno = ENOTSUP;
8538 8538 return (-1);
8539 8539 }
8540 8540 #endif
8541 8541
8542 8542 static void
8543 8543 chop_endslashes(char *path)
8544 8544 {
8545 8545 char *end, *ptr;
8546 8546
8547 8547 /*
8548 8548 * Chop of slashes, but not if all we have is slashes
8549 8549 * for example: ////
8550 8550 * should make no changes, otherwise it will screw up
8551 8551 * checkdir
8552 8552 */
8553 8553 end = &path[strlen(path) -1];
8554 8554 if (*end == '/' && end != path) {
8555 8555 ptr = skipslashes(end, path);
8556 8556 if (ptr != NULL && ptr != path) {
8557 8557 *ptr = '\0';
8558 8558 }
8559 8559 }
8560 8560 }
8561 8561 /* Trusted Extensions */
8562 8562
8563 8563 /*
8564 8564 * append_ext_attr():
8565 8565 *
8566 8566 * Append extended attributes and other information into the buffer
8567 8567 * that gets written to the ancillary file.
8568 8568 *
8569 8569 * With option 'T', we create a tarfile which
8570 8570 * has an ancillary file each corresponding archived file.
8571 8571 * Each ancillary file contains 1 or more of the
8572 8572 * following attributes:
8573 8573 *
8574 8574 * attribute type attribute process procedure
8575 8575 * ---------------- ---------------- --------------------------
8576 8576 * DIR_TYPE = 'D' directory flag append if a directory
8577 8577 * LBL_TYPE = 'L' SL[IL] or SL append ascii label
8578 8578 *
8579 8579 *
8580 8580 */
8581 8581 static void
8582 8582 append_ext_attr(char *shortname, char **secinfo, int *len)
8583 8583 {
8584 8584 bslabel_t b_slabel; /* binary sensitvity label */
8585 8585 char *ascii = NULL; /* ascii label */
8586 8586
8587 8587 /*
8588 8588 * For each attribute type, append it if it is
8589 8589 * relevant to the file type.
8590 8590 */
8591 8591
8592 8592 /*
8593 8593 * For attribute type DIR_TYPE,
8594 8594 * append it to the following file type:
8595 8595 *
8596 8596 * S_IFDIR: directories
8597 8597 */
8598 8598
8599 8599 /*
8600 8600 * For attribute type LBL_TYPE,
8601 8601 * append it to the following file type:
8602 8602 *
8603 8603 * S_IFDIR: directories (including mld, sld)
8604 8604 * S_IFLNK: symbolic link
8605 8605 * S_IFREG: regular file but not hard link
8606 8606 * S_IFIFO: FIFO file but not hard link
8607 8607 * S_IFCHR: char special file but not hard link
8608 8608 * S_IFBLK: block special file but not hard link
8609 8609 */
8610 8610 switch (stbuf.st_mode & S_IFMT) {
8611 8611
8612 8612 case S_IFDIR:
8613 8613
8614 8614 /*
8615 8615 * append DIR_TYPE
8616 8616 */
8617 8617 (void) append_secattr(secinfo, len, 1,
8618 8618 "\0", DIR_TYPE);
8619 8619
8620 8620 /*
8621 8621 * Get and append attribute types LBL_TYPE.
8622 8622 * For directories, LBL_TYPE contains SL.
8623 8623 */
8624 8624 /* get binary sensitivity label */
8625 8625 if (getlabel(shortname, &b_slabel) != 0) {
8626 8626 (void) fprintf(stderr,
8627 8627 gettext("tar: can't get sensitvity label for "
8628 8628 " %s, getlabel() error: %s\n"),
8629 8629 shortname, strerror(errno));
8630 8630 } else {
8631 8631 /* get ascii SL */
8632 8632 if (bsltos(&b_slabel, &ascii,
8633 8633 0, 0) <= 0) {
8634 8634 (void) fprintf(stderr,
8635 8635 gettext("tar: can't get ascii SL for"
8636 8636 " %s\n"), shortname);
8637 8637 } else {
8638 8638 /* append LBL_TYPE */
8639 8639 (void) append_secattr(secinfo, len,
8640 8640 strlen(ascii) + 1, ascii,
8641 8641 LBL_TYPE);
8642 8642
8643 8643 /* free storage */
8644 8644 if (ascii != NULL) {
8645 8645 free(ascii);
8646 8646 ascii = (char *)0;
8647 8647 }
8648 8648 }
8649 8649
8650 8650 }
8651 8651 break;
8652 8652
8653 8653 case S_IFLNK:
8654 8654 case S_IFREG:
8655 8655 case S_IFIFO:
8656 8656 case S_IFCHR:
8657 8657 case S_IFBLK:
8658 8658
8659 8659 /* get binary sensitivity label */
8660 8660 if (getlabel(shortname, &b_slabel) != 0) {
8661 8661 (void) fprintf(stderr,
8662 8662 gettext("tar: can't get sensitivty label for %s, "
8663 8663 "getlabel() error: %s\n"),
8664 8664 shortname, strerror(errno));
8665 8665 } else {
8666 8666 /* get ascii IL[SL] */
8667 8667 if (bsltos(&b_slabel, &ascii, 0, 0) <= 0) {
8668 8668 (void) fprintf(stderr,
8669 8669 gettext("tar: can't translate sensitivity "
8670 8670 " label for %s\n"), shortname);
8671 8671 } else {
8672 8672 char *cmw_label;
8673 8673 size_t cmw_length;
8674 8674
8675 8675 cmw_length = strlen("ADMIN_LOW [] ") +
8676 8676 strlen(ascii);
8677 8677 if ((cmw_label = malloc(cmw_length)) == NULL) {
8678 8678 (void) fprintf(stderr, gettext(
8679 8679 "Insufficient memory for label\n"));
8680 8680 exit(1);
8681 8681 }
8682 8682 /* append LBL_TYPE */
8683 8683 (void) snprintf(cmw_label, cmw_length,
8684 8684 "ADMIN_LOW [%s]", ascii);
8685 8685 (void) append_secattr(secinfo, len,
8686 8686 strlen(cmw_label) + 1, cmw_label,
8687 8687 LBL_TYPE);
8688 8688
8689 8689 /* free storage */
8690 8690 if (ascii != NULL) {
8691 8691 free(cmw_label);
8692 8692 free(ascii);
8693 8693 ascii = (char *)0;
8694 8694 }
8695 8695 }
8696 8696 }
8697 8697 break;
8698 8698
8699 8699 default:
8700 8700 break;
8701 8701 } /* end switch for LBL_TYPE */
8702 8702
8703 8703
8704 8704 /* DONE !! */
8705 8705 return;
8706 8706
8707 8707 } /* end of append_ext_attr */
8708 8708
8709 8709
8710 8710 /*
8711 8711 * Name: extract_attr()
8712 8712 *
8713 8713 * Description:
8714 8714 * Process attributes from the ancillary file due to
8715 8715 * the T option.
8716 8716 *
8717 8717 * Call by doxtract() as part of the switch case structure.
8718 8718 * Making this a separate routine because the nesting are too
8719 8719 * deep in doxtract, thus, leaving very little space
8720 8720 * on each line for instructions.
8721 8721 *
8722 8722 * With option 'T', we extract from a TS 8 or TS 2.5 ancillary file
8723 8723 *
8724 8724 * For option 'T', following are possible attributes in
8725 8725 * a TS 8 ancillary file: (NOTE: No IL support)
8726 8726 *
8727 8727 * attribute type attribute process procedure
8728 8728 * ---------------- ---------------- -------------------------
8729 8729 * # LBL_TYPE = 'L' SL construct binary label
8730 8730 * # APRIV_TYPE = 'P' allowed priv construct privileges
8731 8731 * # FPRIV_TYPE = 'p' forced priv construct privileges
8732 8732 * # COMP_TYPE = 'C' path component construct real path
8733 8733 * # DIR_TYPE = 'D' directory flag note it is a directory
8734 8734 * $ UFSD_ACL = '1' ACL data construct ACL entries
8735 8735 * ATTR_FLAG_TYPE = 'F' file attr flags construct binary flags
8736 8736 * LK_COMP_TYPE = 'K' linked path comp construct linked real path
8737 8737 *
8738 8738 * note: # = attribute names common between TS 8 & TS 2.5 ancillary
8739 8739 * files.
8740 8740 * $ = ACL attribute is processed for the option 'p', it doesn't
8741 8741 * need option 'T'.
8742 8742 *
8743 8743 * Trusted Extensions ignores APRIV_TYPE, FPRIV_TYPE, and ATTR_FLAG_TYPE
8744 8744 *
8745 8745 */
8746 8746 static void
8747 8747 extract_attr(char **file_ptr, struct sec_attr *attr)
8748 8748 {
8749 8749 int reterr, err;
8750 8750 char *dummy_buf; /* for attribute extract */
8751 8751
8752 8752 dummy_buf = attr->attr_info;
8753 8753
8754 8754 switch (attr->attr_type) {
8755 8755
8756 8756 case DIR_TYPE:
8757 8757
8758 8758 dir_flag++;
8759 8759 break;
8760 8760
8761 8761 case LBL_TYPE:
8762 8762
8763 8763 /*
8764 8764 * LBL_TYPE is used to indicate SL for directory, and
8765 8765 * CMW label for other file types.
8766 8766 */
8767 8767
8768 8768 if (!dir_flag) { /* not directory */
8769 8769 /* Skip over IL portion */
8770 8770 char *sl_ptr = strchr(dummy_buf, '[');
8771 8771
8772 8772 if (sl_ptr == NULL)
8773 8773 err = 0;
8774 8774 else
8775 8775 err = stobsl(sl_ptr, &bs_label,
8776 8776 NEW_LABEL, &reterr);
8777 8777 } else { /* directory */
8778 8778 err = stobsl(dummy_buf, &bs_label,
8779 8779 NEW_LABEL, &reterr);
8780 8780 }
8781 8781 if (err == 0) {
8782 8782 (void) fprintf(stderr, gettext("tar: "
8783 8783 "can't convert %s to binary label\n"),
8784 8784 dummy_buf);
8785 8785 bslundef(&bs_label);
8786 8786 } else if (!blequal(&bs_label, &admin_low) &&
8787 8787 !blequal(&bs_label, &admin_high)) {
8788 8788 bslabel_t *from_label;
8789 8789 char *buf;
8790 8790 char tempbuf[MAXPATHLEN];
8791 8791
8792 8792 if (*orig_namep != '/') {
8793 8793 /* got relative linked to path */
8794 8794 (void) getcwd(tempbuf, (sizeof (tempbuf)));
8795 8795 (void) strncat(tempbuf, "/", MAXPATHLEN);
8796 8796 } else
8797 8797 *tempbuf = '\0';
8798 8798
8799 8799 buf = real_path;
8800 8800 (void) strncat(tempbuf, orig_namep, MAXPATHLEN);
8801 8801 from_label = getlabelbypath(tempbuf);
8802 8802 if (from_label != NULL) {
8803 8803 if (blequal(from_label, &admin_low)) {
8804 8804 if ((getpathbylabel(tempbuf, buf,
8805 8805 MAXPATHLEN, &bs_label) == NULL)) {
8806 8806 (void) fprintf(stderr,
8807 8807 gettext("tar: "
8808 8808 "can't get zone root path for "
8809 8809 "%s\n"), tempbuf);
8810 8810 } else
8811 8811 rpath_flag = 1;
8812 8812 }
8813 8813 free(from_label);
8814 8814 }
8815 8815 }
8816 8816 break;
8817 8817
8818 8818 case COMP_TYPE:
8819 8819
8820 8820 rebuild_comp_path(dummy_buf, file_ptr);
8821 8821 break;
8822 8822
8823 8823 case LK_COMP_TYPE:
8824 8824
8825 8825 if (rebuild_lk_comp_path(dummy_buf, file_ptr)
8826 8826 == 0) {
8827 8827 lk_rpath_flag = 1;
8828 8828 } else {
8829 8829 (void) fprintf(stderr, gettext("tar: warning: link's "
8830 8830 "target pathname might be invalid.\n"));
8831 8831 lk_rpath_flag = 0;
8832 8832 }
8833 8833 break;
8834 8834 case APRIV_TYPE:
8835 8835 ignored_aprivs++;
8836 8836 break;
8837 8837 case FPRIV_TYPE:
8838 8838 ignored_fprivs++;
8839 8839 break;
8840 8840 case ATTR_FLAG_TYPE:
8841 8841 ignored_fattrs++;
8842 8842 break;
8843 8843
8844 8844 default:
8845 8845
8846 8846 break;
8847 8847 }
8848 8848
8849 8849 /* done */
8850 8850 return;
8851 8851
8852 8852 } /* end extract_attr */
8853 8853
8854 8854
8855 8855
8856 8856 /*
8857 8857 * Name: rebuild_comp_path()
8858 8858 *
8859 8859 * Description:
8860 8860 * Take the string of components passed down by the calling
8861 8861 * routine and parse the values and rebuild the path.
8862 8862 * This routine no longer needs to produce a new real_path
8863 8863 * string because it is produced when the 'L' LABEL_TYPE is
8864 8864 * interpreted. So the only thing done here is to distinguish
8865 8865 * between an SLD and an MLD entry. We only want one, so we
8866 8866 * ignore the MLD entry by setting the mld_flag.
8867 8867 *
8868 8868 * return value:
8869 8869 * none
8870 8870 */
8871 8871 static void
8872 8872 rebuild_comp_path(char *str, char **namep)
8873 8873 {
8874 8874 char *cp;
8875 8875
8876 8876 while (*str != '\0') {
8877 8877
8878 8878 switch (*str) {
8879 8879
8880 8880 case MLD_TYPE:
8881 8881
8882 8882 str++;
8883 8883 if ((cp = strstr(str, ";;")) != NULL) {
8884 8884 *cp = '\0';
8885 8885 str = cp + 2;
8886 8886 *cp = ';';
8887 8887 }
8888 8888 mld_flag = 1;
8889 8889 break;
8890 8890
8891 8891 case SLD_TYPE:
8892 8892
8893 8893 str++;
8894 8894 if ((cp = strstr(str, ";;")) != NULL) {
8895 8895 *cp = '\0';
8896 8896 str = cp + 2;
8897 8897 *cp = ';';
8898 8898 }
8899 8899 mld_flag = 0;
8900 8900 break;
8901 8901
8902 8902 case PATH_TYPE:
8903 8903
8904 8904 str++;
8905 8905 if ((cp = strstr(str, ";;")) != NULL) {
8906 8906 *cp = '\0';
8907 8907 str = cp + 2;
8908 8908 *cp = ';';
8909 8909 }
8910 8910 break;
8911 8911 }
8912 8912 }
8913 8913 if (rpath_flag)
8914 8914 *namep = real_path;
8915 8915 return;
8916 8916
8917 8917 } /* end rebuild_comp_path() */
8918 8918
8919 8919 /*
8920 8920 * Name: rebuild_lk_comp_path()
8921 8921 *
8922 8922 * Description:
8923 8923 * Take the string of components passed down by the calling
8924 8924 * routine and parse the values and rebuild the path.
8925 8925 *
8926 8926 * return value:
8927 8927 * 0 = succeeded
8928 8928 * -1 = failed
8929 8929 */
8930 8930 static int
8931 8931 rebuild_lk_comp_path(char *str, char **namep)
8932 8932 {
8933 8933 char *cp;
8934 8934 int reterr;
8935 8935 bslabel_t bslabel;
8936 8936 char *buf;
8937 8937 char pbuf[MAXPATHLEN];
8938 8938 char *ptr1, *ptr2;
8939 8939 int plen;
8940 8940 int use_pbuf;
8941 8941 char tempbuf[MAXPATHLEN];
8942 8942 int mismatch;
8943 8943 bslabel_t *from_label;
8944 8944 char zonename[ZONENAME_MAX];
8945 8945 zoneid_t zoneid;
8946 8946
8947 8947 /* init stuff */
8948 8948 use_pbuf = 0;
8949 8949 mismatch = 0;
8950 8950
8951 8951 /*
8952 8952 * For linked to pathname (LK_COMP_TYPE):
8953 8953 * - If the linked to pathname is absolute (start with /), we
8954 8954 * will use it as is.
8955 8955 * - If it is a relative pathname then it is relative to 1 of 2
8956 8956 * directories. For a hardlink, it is relative to the current
8957 8957 * directory. For a symbolic link, it is relative to the
8958 8958 * directory the symbolic link is in. For the symbolic link
8959 8959 * case, set a flag to indicate we need to use the prefix of
8960 8960 * the restored file's pathname with the linked to pathname.
8961 8961 *
8962 8962 * NOTE: At this point, we have no way to determine if we have
8963 8963 * a hardlink or a symbolic link. We will compare the 1st
8964 8964 * component in the prefix portion of the restore file's
8965 8965 * pathname to the 1st component in the attribute data
8966 8966 * (the linked pathname). If they are the same, we will assume
8967 8967 * the link pathname to reconstruct is relative to the current
8968 8968 * directory. Otherwise, we will set a flag indicate we need
8969 8969 * to use a prefix with the reconstructed name. Need to compare
8970 8970 * both the adorned and unadorned version before deciding a
8971 8971 * mismatch.
8972 8972 */
8973 8973
8974 8974 buf = lk_real_path;
8975 8975 if (*(str + 1) != '/') { /* got relative linked to path */
8976 8976 ptr1 = orig_namep;
8977 8977 ptr2 = strrchr(ptr1, '/');
8978 8978 plen = ptr2 - ptr1;
8979 8979 if (plen > 0) {
8980 8980 pbuf[0] = '\0';
8981 8981 plen++; /* include '/' */
8982 8982 (void) strncpy(pbuf, ptr1, plen);
8983 8983 *(pbuf + plen) = '\0';
8984 8984 ptr2 = strchr(pbuf, '/');
8985 8985 if (strncmp(pbuf, str + 1, ptr2 - pbuf) != 0)
8986 8986 mismatch = 1;
8987 8987 }
8988 8988
8989 8989 if (mismatch == 1)
8990 8990 use_pbuf = 1;
8991 8991 }
8992 8992
8993 8993 buf[0] = '\0';
8994 8994
8995 8995 while (*str != '\0') {
8996 8996
8997 8997 switch (*str) {
8998 8998
8999 8999 case MLD_TYPE:
9000 9000
9001 9001 str++;
9002 9002 if ((cp = strstr(str, ";;")) != NULL) {
9003 9003 *cp = '\0';
9004 9004
9005 9005 /*
9006 9006 * Ignore attempts to backup over .MLD.
9007 9007 */
9008 9008 if (strcmp(str, "../") != 0)
9009 9009 (void) strncat(buf, str, MAXPATHLEN);
9010 9010 str = cp + 2;
9011 9011 *cp = ';';
9012 9012 }
9013 9013 break;
9014 9014
9015 9015 case SLD_TYPE:
9016 9016
9017 9017 str++;
9018 9018 if ((cp = strstr(str, ";;")) != NULL) {
9019 9019 *cp = '\0';
9020 9020
9021 9021 /*
9022 9022 * Use the path name in the header if
9023 9023 * error occurs when processing the
9024 9024 * SLD type.
9025 9025 */
9026 9026
9027 9027 if (!stobsl(str, &bslabel,
9028 9028 NO_CORRECTION, &reterr)) {
9029 9029 (void) fprintf(stderr, gettext(
9030 9030 "tar: can't translate to binary"
9031 9031 "SL for SLD, stobsl() error:"
9032 9032 " %s\n"), strerror(errno));
9033 9033 return (-1);
9034 9034 }
9035 9035
9036 9036 str = cp + 2;
9037 9037 *cp = ';';
9038 9038
9039 9039 if (use_pbuf == 1) {
9040 9040 if (*pbuf != '/') {
9041 9041 /* relative linked to path */
9042 9042
9043 9043 (void) getcwd(tempbuf,
9044 9044 (sizeof (tempbuf)));
9045 9045 (void) strncat(tempbuf, "/",
9046 9046 MAXPATHLEN);
9047 9047 (void) strncat(tempbuf, pbuf,
9048 9048 MAXPATHLEN);
9049 9049 }
9050 9050 else
9051 9051 (void) strcpy(tempbuf, pbuf);
9052 9052
9053 9053 } else if (*buf != '/') {
9054 9054 /* got relative linked to path */
9055 9055
9056 9056 (void) getcwd(tempbuf,
9057 9057 (sizeof (tempbuf)));
9058 9058 (void) strncat(tempbuf, "/",
9059 9059 MAXPATHLEN);
9060 9060 } else
9061 9061 *tempbuf = '\0';
9062 9062
9063 9063 (void) strncat(tempbuf, buf, MAXPATHLEN);
9064 9064 *buf = '\0';
9065 9065
9066 9066 if (blequal(&bslabel, &admin_high)) {
9067 9067 bslabel = admin_low;
9068 9068 }
9069 9069
9070 9070
9071 9071 /*
9072 9072 * Check for cross-zone symbolic links
9073 9073 */
9074 9074 from_label = getlabelbypath(real_path);
9075 9075 if (rpath_flag && (from_label != NULL) &&
9076 9076 !blequal(&bslabel, from_label)) {
9077 9077 if ((zoneid =
9078 9078 getzoneidbylabel(&bslabel)) == -1) {
9079 9079 (void) fprintf(stderr,
9080 9080 gettext("tar: can't get "
9081 9081 "zone ID for %s\n"),
9082 9082 tempbuf);
9083 9083 return (-1);
9084 9084 }
9085 9085 if (zone_getattr(zoneid, ZONE_ATTR_NAME,
9086 9086 &zonename, ZONENAME_MAX) == -1) {
9087 9087 /* Badly configured zone info */
9088 9088 (void) fprintf(stderr,
9089 9089 gettext("tar: can't get "
9090 9090 "zonename for %s\n"),
9091 9091 tempbuf);
9092 9092 return (-1);
9093 9093 }
9094 9094 (void) strncpy(buf, AUTO_ZONE,
9095 9095 MAXPATHLEN);
9096 9096 (void) strncat(buf, "/",
9097 9097 MAXPATHLEN);
9098 9098 (void) strncat(buf, zonename,
9099 9099 MAXPATHLEN);
9100 9100 }
9101 9101 if (from_label != NULL)
9102 9102 free(from_label);
9103 9103 (void) strncat(buf, tempbuf, MAXPATHLEN);
9104 9104 break;
9105 9105 }
9106 9106 mld_flag = 0;
9107 9107 break;
9108 9108
9109 9109 case PATH_TYPE:
9110 9110
9111 9111 str++;
9112 9112 if ((cp = strstr(str, ";;")) != NULL) {
9113 9113 *cp = '\0';
9114 9114 (void) strncat(buf, str, MAXPATHLEN);
9115 9115 str = cp + 2;
9116 9116 *cp = ';';
9117 9117 }
9118 9118 break;
9119 9119
9120 9120 default:
9121 9121
9122 9122 (void) fprintf(stderr, gettext(
9123 9123 "tar: error rebuilding path %s\n"),
9124 9124 *namep);
9125 9125 *buf = '\0';
9126 9126 str++;
9127 9127 return (-1);
9128 9128 }
9129 9129 }
9130 9130
9131 9131 /*
9132 9132 * Done for LK_COMP_TYPE
9133 9133 */
9134 9134
9135 9135 return (0); /* component path is rebuilt successfully */
9136 9136
9137 9137 } /* end rebuild_lk_comp_path() */
9138 9138
9139 9139 /*
9140 9140 * Name: check_ext_attr()
9141 9141 *
9142 9142 * Description:
9143 9143 * Check the extended attributes for a file being extracted.
9144 9144 * The attributes being checked here are CMW labels.
9145 9145 * ACLs are not set here because they are set by the
9146 9146 * pflag in doxtract().
9147 9147 *
9148 9148 * If the label doesn't match, return 0
9149 9149 * else return 1
9150 9150 */
9151 9151 static int
9152 9152 check_ext_attr(char *filename)
9153 9153 {
9154 9154 bslabel_t currentlabel; /* label from zone */
9155 9155
9156 9156 if (bltype(&bs_label, SUN_SL_UN)) {
9157 9157 /* No label check possible */
9158 9158 return (0);
9159 9159 }
9160 9160 if (getlabel(filename, ¤tlabel) != 0) {
9161 9161 (void) fprintf(stderr,
9162 9162 gettext("tar: can't get label for "
9163 9163 " %s, getlabel() error: %s\n"),
9164 9164 filename, strerror(errno));
9165 9165 return (0);
9166 9166 } else if ((blequal(¤tlabel, &bs_label)) == 0) {
9167 9167 char *src_label = NULL; /* ascii label */
9168 9168
9169 9169 /* get current src SL */
9170 9170 if (bsltos(&bs_label, &src_label, 0, 0) <= 0) {
9171 9171 (void) fprintf(stderr,
9172 9172 gettext("tar: can't interpret requested label for"
9173 9173 " %s\n"), filename);
9174 9174 } else {
9175 9175 (void) fprintf(stderr,
9176 9176 gettext("tar: can't apply label %s to %s\n"),
9177 9177 src_label, filename);
9178 9178 free(src_label);
9179 9179 }
9180 9180 (void) fprintf(stderr,
9181 9181 gettext("tar: %s not restored\n"), filename);
9182 9182 return (0);
9183 9183 }
9184 9184 return (1);
9185 9185
9186 9186 } /* end check_ext_attr */
9187 9187
9188 9188 /* Compressing a tar file using compression method provided in 'opt' */
9189 9189
9190 9190 static void
9191 9191 compress_back()
9192 9192 {
9193 9193 pid_t pid;
9194 9194 int status;
9195 9195 int wret;
9196 9196 struct stat statb;
9197 9197
9198 9198 if (vflag) {
9199 9199 (void) fprintf(vfile,
9200 9200 gettext("Compressing '%s' with '%s'...\n"),
9201 9201 usefile, compress_opt);
9202 9202 }
9203 9203 if ((pid = fork()) == 0) {
9204 9204 verify_compress_opt(compress_opt);
9205 9205 (void) execlp(compress_opt, compress_opt,
9206 9206 usefile, NULL);
9207 9207 } else if (pid == -1) {
9208 9208 vperror(1, "%s", gettext("Could not fork"));
9209 9209 }
9210 9210 wait_pid(pid);
9211 9211 if (suffix == 0) {
9212 9212 (void) rename(tfname, usefile);
9213 9213 }
9214 9214 }
9215 9215
9216 9216 /* The magic numbers from /etc/magic */
9217 9217
9218 9218 #define GZIP_MAGIC "\037\213"
9219 9219 #define BZIP_MAGIC "BZh"
9220 9220 #define COMP_MAGIC "\037\235"
9221 9221 #define XZ_MAGIC "\375\067\172\130\132\000"
9222 9222
9223 9223 void
9224 9224 check_compression(void)
9225 9225 {
9226 9226 char magic[16];
9227 9227 FILE *fp;
9228 9228
9229 9229 if ((fp = fopen(usefile, "r")) != NULL) {
9230 9230 (void) fread(magic, sizeof (char), 6, fp);
9231 9231 (void) fclose(fp);
9232 9232 }
9233 9233
9234 9234 if (memcmp(magic, GZIP_MAGIC, 2) == 0) {
9235 9235 if (xflag || tflag) {
9236 9236 compress_opt = compress_malloc(strlen(GZCAT) + 1);
9237 9237 (void) strcpy(compress_opt, GZCAT);
9238 9238 } else if (uflag || rflag) {
9239 9239 compress_opt = compress_malloc(strlen(GZIP) + 1);
9240 9240 (void) strcpy(compress_opt, GZIP);
9241 9241 }
9242 9242 } else if (memcmp(magic, BZIP_MAGIC, 2) == 0) {
9243 9243 if (xflag || tflag) {
9244 9244 compress_opt = compress_malloc(strlen(BZCAT) + 1);
9245 9245 (void) strcpy(compress_opt, BZCAT);
9246 9246 } else if (uflag || rflag) {
9247 9247 compress_opt = compress_malloc(strlen(BZIP) + 1);
9248 9248 (void) strcpy(compress_opt, BZIP);
9249 9249 }
9250 9250 } else if (memcmp(magic, COMP_MAGIC, 2) == 0) {
9251 9251 if (xflag || tflag) {
9252 9252 compress_opt = compress_malloc(strlen(ZCAT) + 1);
9253 9253 (void) strcpy(compress_opt, ZCAT);
9254 9254 } else if (uflag || rflag) {
9255 9255 compress_opt = compress_malloc(strlen(COMPRESS) + 1);
9256 9256 (void) strcpy(compress_opt, COMPRESS);
9257 9257 }
9258 9258 } else if (memcmp(magic, XZ_MAGIC, 6) == 0) {
9259 9259 if (xflag || tflag) {
9260 9260 compress_opt = compress_malloc(strlen(XZCAT) + 1);
9261 9261 (void) strcpy(compress_opt, XZCAT);
9262 9262 } else if (uflag || rflag) {
9263 9263 compress_opt = compress_malloc(strlen(XZ) + 1);
9264 9264 (void) strcpy(compress_opt, XZ);
9265 9265 }
9266 9266 }
9267 9267 }
9268 9268
9269 9269 char *
9270 9270 add_suffix()
9271 9271 {
9272 9272 (void) strcpy(tfname, usefile);
9273 9273 if (strcmp(compress_opt, GZIP) == 0) {
9274 9274 if ((suffix = gz_suffix()) == NULL) {
9275 9275 strlcat(tfname, gsuffix[0], sizeof (tfname));
9276 9276 return (gsuffix[0]);
9277 9277 }
9278 9278 } else if (strcmp(compress_opt, COMPRESS) == 0) {
9279 9279 if ((suffix = gz_suffix()) == NULL) {
9280 9280 strlcat(tfname, gsuffix[6], sizeof (tfname));
9281 9281 return (gsuffix[6]);
9282 9282 }
9283 9283 } else if (strcmp(compress_opt, BZIP) == 0) {
9284 9284 if ((suffix = bz_suffix()) == NULL) {
9285 9285 strlcat(tfname, bsuffix[0], sizeof (tfname));
9286 9286 return (bsuffix[0]);
9287 9287 }
9288 9288 } else if (strcmp(compress_opt, XZ) == 0) {
9289 9289 if ((suffix = xz_suffix()) == NULL) {
9290 9290 strlcat(tfname, xsuffix[0], sizeof (tfname));
9291 9291 return (xsuffix[0]);
9292 9292 }
9293 9293 }
9294 9294 return (NULL);
9295 9295 }
9296 9296
9297 9297 /* Decompressing a tar file using compression method from the file type */
9298 9298 void
9299 9299 decompress_file(void)
9300 9300 {
9301 9301 pid_t pid;
9302 9302 int status;
9303 9303 char cmdstr[PATH_MAX];
9304 9304 char fname[PATH_MAX];
9305 9305 char *added_suffix;
9306 9306
9307 9307
9308 9308 added_suffix = add_suffix();
9309 9309 if (added_suffix != NULL) {
9310 9310 (void) rename(usefile, tfname);
9311 9311 }
9312 9312 if ((pid = fork()) == 0) {
9313 9313 if (vflag) {
9314 9314 (void) fprintf(vfile,
9315 9315 gettext("Decompressing '%s' with "
9316 9316 "'%s'...\n"), usefile, compress_opt);
9317 9317 }
9318 9318 verify_compress_opt(compress_opt);
9319 9319 (void) execlp(compress_opt, compress_opt, "-df",
9320 9320 tfname, NULL);
9321 9321 vperror(1, gettext("Could not exec %s"), compress_opt);
9322 9322 } else if (pid == -1) {
9323 9323 vperror(1, gettext("Could not fork"));
9324 9324 }
9325 9325 wait_pid(pid);
9326 9326 if (suffix != NULL) {
9327 9327 /* restore the file name - original file was without suffix */
9328 9328 *(usefile + strlen(usefile) - strlen(suffix)) = '\0';
9329 9329 }
9330 9330 }
9331 9331
9332 9332 /* Set the archive for writing and then compress the archive */
9333 9333 pid_t
9334 9334 compress_file(void)
9335 9335 {
9336 9336 int fd[2];
9337 9337 pid_t pid;
9338 9338
9339 9339 if (vflag) {
9340 9340 (void) fprintf(vfile, gettext("Compressing '%s' with "
9341 9341 "'%s'...\n"), usefile, compress_opt);
9342 9342 }
9343 9343
9344 9344 if (pipe(fd) < 0) {
9345 9345 vperror(1, gettext("Could not create pipe"));
9346 9346 }
9347 9347 if (pid = fork() > 0) {
9348 9348 mt = fd[1];
9349 9349 (void) close(fd[0]);
9350 9350 return (pid);
9351 9351 }
9352 9352 /* child */
9353 9353 (void) dup2(fd[0], STDIN_FILENO);
9354 9354 (void) close(fd[1]);
9355 9355 (void) dup2(mt, STDOUT_FILENO);
9356 9356 verify_compress_opt(compress_opt);
9357 9357 (void) execlp(compress_opt, compress_opt, NULL);
9358 9358 vperror(1, gettext("Could not exec %s"), compress_opt);
9359 9359 return (0); /*NOTREACHED*/
9360 9360 }
9361 9361
9362 9362 pid_t
9363 9363 uncompress_file(void)
9364 9364 {
9365 9365 int fd[2];
9366 9366 pid_t pid;
9367 9367
9368 9368 if (vflag) {
9369 9369 (void) fprintf(vfile, gettext("Decompressing '%s' with "
9370 9370 "'%s'...\n"), usefile, compress_opt);
9371 9371 }
9372 9372
9373 9373 if (pipe(fd) < 0) {
9374 9374 vperror(1, gettext("Could not create pipe"));
9375 9375 }
9376 9376 if (pid = fork() > 0) {
9377 9377 mt = fd[0];
9378 9378 (void) close(fd[1]);
9379 9379 return (pid);
9380 9380 }
9381 9381 /* child */
9382 9382 (void) dup2(fd[1], STDOUT_FILENO);
9383 9383 (void) close(fd[0]);
9384 9384 (void) dup2(mt, STDIN_FILENO);
9385 9385 verify_compress_opt(compress_opt);
9386 9386 (void) execlp(compress_opt, compress_opt, NULL);
9387 9387 vperror(1, gettext("Could not exec %s"), compress_opt);
9388 9388 return (0); /*NOTREACHED*/
9389 9389 }
9390 9390
9391 9391 /* Checking suffix validity */
9392 9392 char *
9393 9393 check_suffix(char **suf, int size)
9394 9394 {
9395 9395 int i;
9396 9396 int slen;
9397 9397 int nlen = strlen(usefile);
9398 9398
9399 9399 for (i = 0; i < size; i++) {
9400 9400 slen = strlen(suf[i]);
9401 9401 if (nlen < slen)
9402 9402 return (NULL);
9403 9403 if (strcmp(usefile + nlen - slen, suf[i]) == 0)
9404 9404 return (suf[i]);
9405 9405 }
9406 9406 return (NULL);
9407 9407 }
9408 9408
9409 9409 /* Checking valid 'bzip2' suffix */
9410 9410 char *
9411 9411 bz_suffix(void)
9412 9412 {
9413 9413 return (check_suffix(bsuffix, BSUF));
9414 9414 }
9415 9415
9416 9416 /* Checking valid 'gzip' suffix */
9417 9417 char *
9418 9418 gz_suffix(void)
9419 9419 {
9420 9420 return (check_suffix(gsuffix, GSUF));
9421 9421 }
9422 9422
9423 9423 /* Checking valid 'xz' suffix */
9424 9424 char *
9425 9425 xz_suffix(void)
9426 9426 {
9427 9427 return (check_suffix(xsuffix, XSUF));
9428 9428 }
9429 9429
9430 9430 void *
9431 9431 compress_malloc(size_t size)
9432 9432 {
9433 9433 void *opt;
9434 9434
9435 9435 if ((opt = malloc(size)) == NULL) {
9436 9436 vperror(1, "%s",
9437 9437 gettext("Could not allocate compress buffer\n"));
9438 9438 }
9439 9439 return (opt);
9440 9440 }
9441 9441
9442 9442 void
9443 9443 wait_pid(pid_t pid)
9444 9444 {
9445 9445 int status;
9446 9446
9447 9447 while (waitpid(pid, &status, 0) == -1 && errno == EINTR)
9448 9448 ;
9449 9449 }
9450 9450
9451 9451 static void
9452 9452 verify_compress_opt(const char *t)
9453 9453 {
9454 9454 struct stat statbuf;
9455 9455
9456 9456 if (stat(t, &statbuf) == -1)
9457 9457 vperror(1, "%s %s: %s\n", gettext("Could not stat"),
9458 9458 t, strerror(errno));
9459 9459 }
9460 9460
9461 9461 static void
9462 9462 detect_compress(void)
9463 9463 {
9464 9464 char *zsuf[] = {".Z"};
9465 9465 if (check_suffix(zsuf, 1) != NULL) {
9466 9466 Zflag = 1;
9467 9467 } else if (check_suffix(bsuffix, BSUF) != NULL) {
9468 9468 jflag = 1;
9469 9469 } else if (check_suffix(gsuffix, GSUF) != NULL) {
9470 9470 zflag = 1;
9471 9471 } else if (check_suffix(xsuffix, XSUF) != NULL) {
9472 9472 Jflag = 1;
9473 9473 } else {
9474 9474 vperror(1, "%s\n", gettext("No compression method detected"));
9475 9475 }
9476 9476 }
↓ open down ↓ |
2848 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX