-
Notifications
You must be signed in to change notification settings - Fork 767
/
tar.c
9514 lines (8594 loc) · 233 KB
/
tar.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright 2012 Milan Jurik. All rights reserved.
* Copyright 2015 Joyent, Inc.
* Copyright (c) 2016 by Delphix. All rights reserved.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/* Copyright (c) 1987, 1988 Microsoft Corporation */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
#include <unistd.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/mkdev.h>
#include <sys/wait.h>
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <signal.h>
#include <ctype.h>
#include <locale.h>
#include <nl_types.h>
#include <langinfo.h>
#include <pwd.h>
#include <grp.h>
#include <fcntl.h>
#include <string.h>
#include <malloc.h>
#include <time.h>
#include <utime.h>
#include <stdlib.h>
#include <stdarg.h>
#include <widec.h>
#include <sys/mtio.h>
#include <sys/acl.h>
#include <strings.h>
#include <deflt.h>
#include <limits.h>
#include <iconv.h>
#include <assert.h>
#include <libgen.h>
#include <libintl.h>
#include <aclutils.h>
#include <libnvpair.h>
#include <archives.h>
#if defined(__SunOS_5_6) || defined(__SunOS_5_7)
extern int defcntl();
#endif
#if defined(_PC_SATTR_ENABLED)
#include <attr.h>
#include <libcmdutils.h>
#endif
/* Trusted Extensions */
#include <zone.h>
#include <tsol/label.h>
#include <sys/tsol/label_macro.h>
#include "getresponse.h"
/*
* Source compatibility
*/
/*
* These constants come from archives.h and sys/fcntl.h
* and were introduced by the extended attributes project
* in Solaris 9.
*/
#if !defined(O_XATTR)
#define AT_SYMLINK_NOFOLLOW 0x1000
#define AT_REMOVEDIR 0x1
#define AT_FDCWD 0xffd19553
#define _XATTR_HDRTYPE 'E'
static int attropen();
static int fstatat();
static int renameat();
static int unlinkat();
static int openat();
static int fchownat();
static int futimesat();
#endif
/*
* Compiling with -D_XPG4_2 gets this but produces other problems, so
* instead of including sys/time.h and compiling with -D_XPG4_2, I'm
* explicitly doing the declaration here.
*/
int utimes(const char *path, const struct timeval timeval_ptr[]);
#ifndef MINSIZE
#define MINSIZE 250
#endif
#define DEF_FILE "/etc/default/tar"
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define TBLOCK 512 /* tape block size--should be universal */
#ifdef BSIZE
#define SYS_BLOCK BSIZE /* from sys/param.h: secondary block size */
#else /* BSIZE */
#define SYS_BLOCK 512 /* default if no BSIZE in param.h */
#endif /* BSIZE */
#define NBLOCK 20
#define NAMSIZ 100
#define PRESIZ 155
#define MAXNAM 256
#define MODEMASK 0777777 /* file creation mode mask */
#define POSIXMODES 07777 /* mask for POSIX mode bits */
#define MAXEXT 9 /* reasonable max # extents for a file */
#define EXTMIN 50 /* min blks left on floppy to split a file */
/* max value dblock.dbuf.efsize can store */
#define TAR_EFSIZE_MAX 0777777777
/*
* Symbols which specify the values at which the use of the 'E' function
* modifier is required to properly store a file.
*
* TAR_OFFSET_MAX - the largest file size we can archive
* OCTAL7CHAR - the limit for ustar gid, uid, dev
*/
#ifdef XHDR_DEBUG
/* tiny values which force the creation of extended header entries */
#define TAR_OFFSET_MAX 9
#define OCTAL7CHAR 2
#else
/* normal values */
#define TAR_OFFSET_MAX 077777777777ULL
#define OCTAL7CHAR 07777777
#endif
#define TBLOCKS(bytes) (((bytes) + TBLOCK - 1) / TBLOCK)
#define K(tblocks) ((tblocks+1)/2) /* tblocks to Kbytes for printing */
#define MAXLEV (PATH_MAX / 2)
#define LEV0 1
#define SYMLINK_LEV0 0
#define TRUE 1
#define FALSE 0
#define XATTR_FILE 1
#define NORMAL_FILE 0
#define PUT_AS_LINK 1
#define PUT_NOTAS_LINK 0
#ifndef VIEW_READONLY
#define VIEW_READONLY "SUNWattr_ro"
#endif
#ifndef VIEW_READWRITE
#define VIEW_READWRITE "SUNWattr_rw"
#endif
#if _FILE_OFFSET_BITS == 64
#define FMT_off_t "lld"
#define FMT_off_t_o "llo"
#define FMT_blkcnt_t "lld"
#else
#define FMT_off_t "ld"
#define FMT_off_t_o "lo"
#define FMT_blkcnt_t "ld"
#endif
/* ACL support */
static
struct sec_attr {
char attr_type;
char attr_len[7];
char attr_info[1];
} *attr;
#if defined(O_XATTR)
typedef enum {
ATTR_OK,
ATTR_SKIP,
ATTR_CHDIR_ERR,
ATTR_OPEN_ERR,
ATTR_XATTR_ERR,
ATTR_SATTR_ERR
} attr_status_t;
#endif
#if defined(O_XATTR)
typedef enum {
ARC_CREATE,
ARC_RESTORE
} arc_action_t;
#endif
typedef struct attr_data {
char *attr_parent;
char *attr_path;
int attr_parentfd;
int attr_rw_sysattr;
} attr_data_t;
/*
*
* Tar has been changed to support extended attributes.
*
* As part of this change tar now uses the new *at() syscalls
* such as openat, fchownat(), unlinkat()...
*
* This was done so that attributes can be handled with as few code changes
* as possible.
*
* What this means is that tar now opens the directory that a file or directory
* resides in and then performs *at() functions to manipulate the entry.
*
* For example a new file is now created like this:
*
* dfd = open(<some dir path>)
* fd = openat(dfd, <name>,....);
*
* or in the case of an extended attribute
*
* dfd = attropen(<pathname>, ".", ....)
*
* Once we have a directory file descriptor all of the *at() functions can
* be applied to it.
*
* unlinkat(dfd, <component name>,...)
* fchownat(dfd, <component name>,..)
*
* This works for both normal namespace files and extended attribute file
*
*/
/*
*
* Extended attribute Format
*
* Extended attributes are stored in two pieces.
* 1. An attribute header which has information about
* what file the attribute is for and what the attribute
* is named.
* 2. The attribute record itself. Stored as a normal file type
* of entry.
* Both the header and attribute record have special modes/typeflags
* associated with them.
*
* The names of the header in the archive look like:
* /dev/null/attr.hdr
*
* The name of the attribute looks like:
* /dev/null/attr
*
* This is done so that an archiver that doesn't understand these formats
* can just dispose of the attribute records.
*
* The format is composed of a fixed size header followed
* by a variable sized xattr_buf. If the attribute is a hard link
* to another attribute then another xattr_buf section is included
* for the link.
*
* The xattr_buf is used to define the necessary "pathing" steps
* to get to the extended attribute. This is necessary to support
* a fully recursive attribute model where an attribute may itself
* have an attribute.
*
* The basic layout looks like this.
*
* --------------------------------
* | |
* | xattr_hdr |
* | |
* --------------------------------
* --------------------------------
* | |
* | xattr_buf |
* | |
* --------------------------------
* --------------------------------
* | |
* | (optional link info) |
* | |
* --------------------------------
* --------------------------------
* | |
* | attribute itself |
* | stored as normal tar |
* | or cpio data with |
* | special mode or |
* | typeflag |
* | |
* --------------------------------
*
*/
/*
* xattrhead is a pointer to the xattr_hdr
*
* xattrp is a pointer to the xattr_buf structure
* which contains the "pathing" steps to get to attributes
*
* xattr_linkp is a pointer to another xattr_buf structure that is
* only used when an attribute is actually linked to another attribute
*
*/
static struct xattr_hdr *xattrhead;
static struct xattr_buf *xattrp;
static struct xattr_buf *xattr_linkp; /* pointer to link info, if any */
static char *xattrapath; /* attribute name */
static char *xattr_linkaname; /* attribute attribute is linked to */
static char Hiddendir; /* are we processing hidden xattr dir */
static char xattrbadhead;
/* Was statically allocated tbuf[NBLOCK] */
static
union hblock {
char dummy[TBLOCK];
struct header {
char name[NAMSIZ]; /* If non-null prefix, path is */
/* <prefix>/<name>; otherwise */
/* <name> */
char mode[8];
char uid[8];
char gid[8];
char size[12]; /* size of this extent if file split */
char mtime[12];
char chksum[8];
char typeflag;
char linkname[NAMSIZ];
char magic[6];
char version[2];
char uname[32];
char gname[32];
char devmajor[8];
char devminor[8];
char prefix[PRESIZ]; /* Together with "name", the path of */
/* the file: <prefix>/<name> */
char extno; /* extent #, null if not split */
char extotal; /* total extents */
char efsize[10]; /* size of entire file */
} dbuf;
} dblock, *tbuf, xhdr_buf;
static
struct xtar_hdr {
uid_t x_uid, /* Uid of file */
x_gid; /* Gid of file */
major_t x_devmajor; /* Device major node */
minor_t x_devminor; /* Device minor node */
off_t x_filesz; /* Length of file */
char *x_uname, /* Pointer to name of user */
*x_gname, /* Pointer to gid of user */
*x_linkpath, /* Path for a hard/symbolic link */
*x_path; /* Path of file */
timestruc_t x_mtime; /* Seconds and nanoseconds */
} Xtarhdr;
static
struct gen_hdr {
ulong_t g_mode; /* Mode of file */
uid_t g_uid, /* Uid of file */
g_gid; /* Gid of file */
off_t g_filesz; /* Length of file */
time_t g_mtime; /* Modification time */
uint_t g_cksum; /* Checksum of file */
ulong_t g_devmajor, /* File system of file */
g_devminor; /* Major/minor of special files */
} Gen;
static
struct linkbuf {
ino_t inum;
dev_t devnum;
int count;
char pathname[MAXNAM+1]; /* added 1 for last NULL */
char attrname[MAXNAM+1];
struct linkbuf *nextp;
} *ihead;
/* see comments before build_table() */
#define TABLE_SIZE 512
typedef struct file_list {
char *name; /* Name of file to {in,ex}clude */
struct file_list *next; /* Linked list */
} file_list_t;
static file_list_t *exclude_tbl[TABLE_SIZE],
*include_tbl[TABLE_SIZE];
static int append_secattr(char **, int *, int, char *, char);
static void write_ancillary(union hblock *, char *, int, char);
static void add_file_to_table(file_list_t *table[], char *str);
static void assert_string(char *s, char *msg);
static int istape(int fd, int type);
static void backtape(void);
static void build_table(file_list_t *table[], char *file);
static int check_prefix(char **namep, char **dirp, char **compp);
static void closevol(void);
static void copy(void *dst, void *src);
static int convtoreg(off_t);
static void delete_target(int fd, char *comp, char *namep);
static void doDirTimes(char *name, timestruc_t modTime);
static void done(int n);
static void dorep(char *argv[]);
static void dotable(char *argv[]);
static void doxtract(char *argv[]);
static int tar_chdir(const char *path);
static int is_directory(char *name);
static int has_dot_dot(char *name);
static int is_absolute(char *name);
static char *make_relative_name(char *name, char **stripped_prefix);
static void fatal(char *format, ...);
static void vperror(int exit_status, char *fmt, ...);
static void flushtape(void);
static void getdir(void);
static void *getmem(size_t);
static void longt(struct stat *st, char aclchar);
static void load_info_from_xtarhdr(u_longlong_t flag, struct xtar_hdr *xhdrp);
static int makeDir(char *name);
static void mterr(char *operation, int i, int exitcode);
static void newvol(void);
static void passtape(void);
static void putempty(blkcnt_t n);
static int putfile(char *longname, char *shortname, char *parent,
attr_data_t *attrinfo, int filetype, int lev, int symlink_lev);
static void readtape(char *buffer);
static void seekdisk(blkcnt_t blocks);
static void setPathTimes(int dirfd, char *path, timestruc_t modTime);
static void setbytes_to_skip(struct stat *st, int err);
static void splitfile(char *longname, int ifd, char *name,
char *prefix, int filetype);
static void tomodes(struct stat *sp);
static void usage(void);
static int xblocks(int issysattr, off_t bytes, int ofile);
static int xsfile(int issysattr, int ofd);
static void resugname(int dirfd, char *name, int symflag);
static int bcheck(char *bstr);
static int checkdir(char *name);
static int checksum(union hblock *dblockp);
#ifdef EUC
static int checksum_signed(union hblock *dblockp);
#endif /* EUC */
static int checkupdate(char *arg);
static int checkw(char c, char *name);
static int cmp(char *b, char *s, int n);
static int defset(char *arch);
static boolean_t endtape(void);
static int is_in_table(file_list_t *table[], char *str);
static int notsame(void);
static int is_prefix(char *s1, char *s2);
static int response(void);
static int build_dblock(const char *, const char *, const char,
const int filetype, const struct stat *, const dev_t, const char *);
static unsigned int hash(char *str);
static blkcnt_t kcheck(char *kstr);
static off_t bsrch(char *s, int n, off_t l, off_t h);
static void onintr(int sig);
static void onquit(int sig);
static void onhup(int sig);
static uid_t getuidbyname(char *);
static gid_t getgidbyname(char *);
static char *getname(gid_t);
static char *getgroup(gid_t);
static int checkf(char *name, int mode, int howmuch);
static int writetbuf(char *buffer, int n);
static int wantit(char *argv[], char **namep, char **dirp, char **comp,
attr_data_t **attrinfo);
static void append_ext_attr(char *shortname, char **secinfo, int *len);
static int get_xdata(void);
static void gen_num(const char *keyword, const u_longlong_t number);
static void gen_date(const char *keyword, const timestruc_t time_value);
static void gen_string(const char *keyword, const char *value);
static void get_xtime(char *value, timestruc_t *xtime);
static int chk_path_build(char *name, char *longname, char *linkname,
char *prefix, char type, int filetype);
static int gen_utf8_names(const char *filename);
static int utf8_local(char *option, char **Xhdr_ptrptr, char *target,
const char *src, int max_val);
static int local_utf8(char **Xhdr_ptrptr, char *target, const char *src,
iconv_t iconv_cd, int xhdrflg, int max_val);
static int c_utf8(char *target, const char *source);
static int getstat(int dirfd, char *longname, char *shortname,
char *attrparent);
static void xattrs_put(char *, char *, char *, char *);
static void prepare_xattr(char **, char *, char *,
char, struct linkbuf *, int *);
static int put_link(char *name, char *longname, char *component,
char *longattrname, char *prefix, int filetype, char typeflag);
static int put_extra_attributes(char *longname, char *shortname,
char *longattrname, char *prefix, int filetype, char typeflag);
static int put_xattr_hdr(char *longname, char *shortname, char *longattrname,
char *prefix, int typeflag, int filetype, struct linkbuf *lp);
static int read_xattr_hdr(attr_data_t **attrinfo);
/* Trusted Extensions */
#define AUTO_ZONE "/zone"
static void extract_attr(char **file_ptr, struct sec_attr *);
static int check_ext_attr(char *filename);
static void rebuild_comp_path(char *str, char **namep);
static int rebuild_lk_comp_path(char *str, char **namep);
static void get_parent(char *path, char *dir);
static char *get_component(char *path);
static int retry_open_attr(int pdirfd, int cwd, char *dirp, char *pattr,
char *name, int oflag, mode_t mode);
static char *skipslashes(char *string, char *start);
static void chop_endslashes(char *path);
static pid_t compress_file(void);
static void compress_back(void);
static void decompress_file(void);
static pid_t uncompress_file(void);
static void *compress_malloc(size_t);
static void check_compression(void);
static char *bz_suffix(void);
static char *gz_suffix(void);
static char *xz_suffix(void);
static char *add_suffix();
static void wait_pid(pid_t);
static void verify_compress_opt(const char *t);
static void detect_compress(void);
static void dlog(const char *, ...);
static boolean_t should_enable_debug(void);
static struct stat stbuf;
static char *myname;
static char *xtract_chdir = NULL;
static int checkflag = 0;
static int Xflag, Fflag, iflag, hflag, Bflag, Iflag;
static int rflag, xflag, vflag, tflag, mt, cflag, mflag, pflag;
static int uflag;
static int errflag;
static int oflag;
static int bflag, Aflag;
static int Pflag; /* POSIX conformant archive */
static int Eflag; /* Allow files greater than 8GB */
static int atflag; /* traverse extended attributes */
static int saflag; /* traverse extended sys attributes */
static int Dflag; /* Data change flag */
static int jflag; /* flag to use 'bzip2' */
static int zflag; /* flag to use 'gzip' */
static int Zflag; /* flag to use 'compress' */
static int Jflag; /* flag to use 'xz' */
static int aflag; /* flag to use autocompression */
/* Trusted Extensions */
static int Tflag; /* Trusted Extensions attr flags */
static int dir_flag; /* for attribute extract */
static int mld_flag; /* for attribute extract */
static char *orig_namep; /* original namep - unadorned */
static int rpath_flag; /* MLD real path is rebuilt */
static char real_path[MAXPATHLEN]; /* MLD real path */
static int lk_rpath_flag; /* linked to real path is rebuilt */
static char lk_real_path[MAXPATHLEN]; /* linked real path */
static bslabel_t bs_label; /* for attribute extract */
static bslabel_t admin_low;
static bslabel_t admin_high;
static int ignored_aprivs = 0;
static int ignored_fprivs = 0;
static int ignored_fattrs = 0;
static int term, chksum, wflag,
first = TRUE, defaults_used = FALSE, linkerrok;
static blkcnt_t recno;
static int freemem = 1;
static int nblock = NBLOCK;
static int Errflg = 0;
static int exitflag = 0;
static dev_t mt_dev; /* device containing output file */
static ino_t mt_ino; /* inode number of output file */
static int mt_devtype; /* dev type of archive, from stat structure */
static int update = 1; /* for `open' call */
static off_t low;
static off_t high;
static FILE *tfile;
static FILE *vfile = stdout;
static char *tmpdir;
static char *tmp_suffix = "/tarXXXXXX";
static char *tname;
static char archive[] = "archive0=";
static char *Xfile;
static char *usefile;
static char tfname[1024];
static int mulvol; /* multi-volume option selected */
static blkcnt_t blocklim; /* number of blocks to accept per volume */
static blkcnt_t tapepos; /* current block number to be written */
static int NotTape; /* true if tape is a disk */
static int dumping; /* true if writing a tape or other archive */
static int extno; /* number of extent: starts at 1 */
static int extotal; /* total extents in this file */
static off_t extsize; /* size of current extent during extraction */
static ushort_t Oumask = 0; /* old umask value */
static boolean_t is_posix; /* true if archive is POSIX-conformant */
static const char *magic_type = "ustar";
static size_t xrec_size = 8 * PATH_MAX; /* extended rec initial size */
static char *xrec_ptr;
static off_t xrec_offset = 0;
static int Xhdrflag;
static int charset_type = 0;
static u_longlong_t xhdr_flgs; /* Bits set determine which items */
/* need to be in extended header. */
static pid_t comp_pid = 0;
static boolean_t debug_output = B_FALSE;
#define _X_DEVMAJOR 0x1
#define _X_DEVMINOR 0x2
#define _X_GID 0x4
#define _X_GNAME 0x8
#define _X_LINKPATH 0x10
#define _X_PATH 0x20
#define _X_SIZE 0x40
#define _X_UID 0x80
#define _X_UNAME 0x100
#define _X_ATIME 0x200
#define _X_CTIME 0x400
#define _X_MTIME 0x800
#define _X_XHDR 0x1000 /* Bit flag that determines whether 'X' */
/* typeflag was followed by 'A' or non 'A' */
/* typeflag. */
#define _X_LAST 0x40000000
#define PID_MAX_DIGITS (10 * sizeof (pid_t) / 4)
#define TIME_MAX_DIGITS (10 * sizeof (time_t) / 4)
#define LONG_MAX_DIGITS (10 * sizeof (long) / 4)
#define ULONGLONG_MAX_DIGITS (10 * sizeof (u_longlong_t) / 4)
/*
* UTF_8 encoding requires more space than the current codeset equivalent.
* Currently a factor of 2-3 would suffice, but it is possible for a factor
* of 6 to be needed in the future, so for saftey, we use that here.
*/
#define UTF_8_FACTOR 6
static u_longlong_t xhdr_count = 0;
static char xhdr_dirname[PRESIZ + 1];
static char pidchars[PID_MAX_DIGITS + 1];
static char *tchar = ""; /* null linkpath */
static char local_path[UTF_8_FACTOR * PATH_MAX + 1];
static char local_linkpath[UTF_8_FACTOR * PATH_MAX + 1];
static char local_gname[UTF_8_FACTOR * _POSIX_NAME_MAX + 1];
static char local_uname[UTF_8_FACTOR * _POSIX_NAME_MAX + 1];
/*
* The following mechanism is provided to allow us to debug tar in complicated
* situations, like when it is part of a pipe. The idea is that you compile
* with -DWAITAROUND defined, and then add the 'D' function modifier to the
* target tar invocation, eg. "tar cDf tarfile file". If stderr is available,
* it will tell you to which pid to attach the debugger; otherwise, use ps to
* find it. Attach to the process from the debugger, and, *PRESTO*, you are
* there!
*
* Simply assign "waitaround = 0" once you attach to the process, and then
* proceed from there as usual.
*/
#ifdef WAITAROUND
int waitaround = 0; /* wait for rendezvous with the debugger */
#endif
#define BZIP "/usr/bin/bzip2"
#define GZIP "/usr/bin/gzip"
#define COMPRESS "/usr/bin/compress"
#define XZ "/usr/bin/xz"
#define BZCAT "/usr/bin/bzcat"
#define GZCAT "/usr/bin/gzcat"
#define ZCAT "/usr/bin/zcat"
#define XZCAT "/usr/bin/xzcat"
#define GSUF 8 /* number of valid 'gzip' sufixes */
#define BSUF 4 /* number of valid 'bzip2' sufixes */
#define XSUF 1 /* number of valid 'xz' suffixes */
static char *compress_opt; /* compression type */
static char *gsuffix[] = {".gz", "-gz", ".z", "-z", "_z", ".Z",
".tgz", ".taz"};
static char *bsuffix[] = {".bz2", ".bz", ".tbz2", ".tbz"};
static char *xsuffix[] = {".xz"};
static char *suffix;
int
main(int argc, char *argv[])
{
char *cp;
char *tmpdirp;
pid_t thispid;
(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
#define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
#endif
(void) textdomain(TEXT_DOMAIN);
if (argc < 2)
usage();
debug_output = should_enable_debug();
tfile = NULL;
if ((myname = strdup(argv[0])) == NULL) {
(void) fprintf(stderr, gettext(
"tar: cannot allocate program name\n"));
exit(1);
}
if (init_yes() < 0) {
(void) fprintf(stderr, gettext(ERR_MSG_INIT_YES),
strerror(errno));
exit(2);
}
/*
* For XPG4 compatibility, we must be able to accept the "--"
* argument normally recognized by getopt; it is used to delimit
* the end opt the options section, and so can only appear in
* the position of the first argument. We simply skip it.
*/
if (strcmp(argv[1], "--") == 0) {
argv++;
argc--;
if (argc < 3)
usage();
}
argv[argc] = NULL;
argv++;
/*
* Set up default values.
* Search the operand string looking for the first digit or an 'f'.
* If you find a digit, use the 'archive#' entry in DEF_FILE.
* If 'f' is given, bypass looking in DEF_FILE altogether.
* If no digit or 'f' is given, still look in DEF_FILE but use '0'.
*/
if ((usefile = getenv("TAPE")) == (char *)NULL) {
for (cp = *argv; *cp; ++cp)
if (isdigit(*cp) || *cp == 'f')
break;
if (*cp != 'f') {
archive[7] = (*cp)? *cp: '0';
if (!(defaults_used = defset(archive))) {
usefile = NULL;
nblock = 1;
blocklim = 0;
NotTape = 0;
}
}
}
for (cp = *argv++; *cp; cp++)
switch (*cp) {
#ifdef WAITAROUND
case 'D':
/* rendezvous with the debugger */
waitaround = 1;
break;
#endif
case 'f':
assert_string(*argv, gettext(
"tar: tarfile must be specified with 'f' "
"function modifier\n"));
usefile = *argv++;
break;
case 'F':
Fflag++;
break;
case 'c':
cflag++;
rflag++;
update = 1;
break;
#if defined(O_XATTR)
case '@':
atflag++;
break;
#endif /* O_XATTR */
#if defined(_PC_SATTR_ENABLED)
case '/':
saflag++;
break;
#endif /* _PC_SATTR_ENABLED */
case 'u':
uflag++; /* moved code after signals caught */
rflag++;
update = 2;
break;
case 'r':
rflag++;
update = 2;
break;
case 'v':
vflag++;
break;
case 'w':
wflag++;
break;
case 'x':
xflag++;
break;
case 'X':
assert_string(*argv, gettext(
"tar: exclude file must be specified with 'X' "
"function modifier\n"));
Xflag = 1;
Xfile = *argv++;
build_table(exclude_tbl, Xfile);
break;
case 't':
tflag++;
break;
case 'm':
mflag++;
break;
case 'p':
pflag++;
break;
case 'D':
Dflag++;
break;
case '-':
/* ignore this silently */
break;
case '0': /* numeric entries used only for defaults */
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
break;
case 'b':
assert_string(*argv, gettext(
"tar: blocking factor must be specified "
"with 'b' function modifier\n"));
bflag++;
nblock = bcheck(*argv++);
break;
case 'n': /* not a magtape (instead of 'k') */
NotTape++; /* assume non-magtape */
break;
case 'l':
linkerrok++;
break;
case 'e':
errflag++;
break;
case 'o':
oflag++;
break;
case 'h':
hflag++;
break;
case 'i':
iflag++;
break;
case 'B':
Bflag++;
break;
case 'P':
Pflag++;
break;
case 'E':
Eflag++;
Pflag++; /* Only POSIX archive made */
break;
case 'T':
Tflag++; /* Handle Trusted Extensions attrs */
pflag++; /* also set flag for ACL */
break;
case 'j': /* compession "bzip2" */
jflag = 1;
break;
case 'z': /* compression "gzip" */
zflag = 1;
break;
case 'Z': /* compression "compress" */
Zflag = 1;
break;
case 'J': /* compression "xz" */
Jflag = 1;
break;
case 'a':
aflag = 1; /* autocompression */
break;
default:
(void) fprintf(stderr, gettext(
"tar: %c: unknown function modifier\n"), *cp);
usage();
}
if (!rflag && !xflag && !tflag)
usage();
if ((rflag && xflag) || (xflag && tflag) || (rflag && tflag)) {
(void) fprintf(stderr, gettext(
"tar: specify only one of [ctxru].\n"));
usage();
}
if (cflag) {
if ((jflag + zflag + Zflag + Jflag + aflag) > 1) {
(void) fprintf(stderr, gettext(
"tar: specify only one of [ajJzZ] to "
"create a compressed file.\n"));
usage();
}
}
/* Trusted Extensions attribute handling */
if (Tflag && ((getzoneid() != GLOBAL_ZONEID) ||
!is_system_labeled())) {
(void) fprintf(stderr, gettext(
"tar: the 'T' option is only available with "
"Trusted Extensions\nand must be run from "
"the global zone.\n"));
usage();
}
if (cflag && *argv == NULL)
fatal(gettext("Missing filenames"));
if (usefile == NULL)
fatal(gettext("device argument required"));
/* alloc a buffer of the right size */
if ((tbuf = (union hblock *)
calloc(sizeof (union hblock) * nblock, sizeof (char))) ==
(union hblock *)NULL) {
(void) fprintf(stderr, gettext(
"tar: cannot allocate physio buffer\n"));
exit(1);
}
if ((xrec_ptr = malloc(xrec_size)) == NULL) {
(void) fprintf(stderr, gettext(
"tar: cannot allocate extended header buffer\n"));
exit(1);
}
#ifdef WAITAROUND
if (waitaround) {
(void) fprintf(stderr, gettext("Rendezvous with tar on pid"
" %d\n"), getpid());
while (waitaround) {
(void) sleep(10);
}
}
#endif
thispid = getpid();
(void) sprintf(pidchars, "%ld", thispid);
thispid = strlen(pidchars);
if ((tmpdirp = getenv("TMPDIR")) == (char *)NULL)
(void) strcpy(xhdr_dirname, "/tmp");
else {
/*
* Make sure that dir is no longer than what can
* fit in the prefix part of the header.
*/
if (strlen(tmpdirp) > (size_t)(PRESIZ - thispid - 12)) {
(void) strcpy(xhdr_dirname, "/tmp");