1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.compress.archivers.tar;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.UncheckedIOException;
24 import java.math.BigDecimal;
25 import java.nio.file.DirectoryStream;
26 import java.nio.file.Files;
27 import java.nio.file.LinkOption;
28 import java.nio.file.Path;
29 import java.nio.file.attribute.BasicFileAttributes;
30 import java.nio.file.attribute.DosFileAttributes;
31 import java.nio.file.attribute.FileTime;
32 import java.nio.file.attribute.PosixFileAttributes;
33 import java.time.DateTimeException;
34 import java.time.Instant;
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.Comparator;
38 import java.util.Date;
39 import java.util.HashMap;
40 import java.util.List;
41 import java.util.Locale;
42 import java.util.Map;
43 import java.util.Objects;
44 import java.util.Set;
45 import java.util.regex.Pattern;
46 import java.util.stream.Collectors;
47
48 import org.apache.commons.compress.archivers.ArchiveEntry;
49 import org.apache.commons.compress.archivers.EntryStreamOffsets;
50 import org.apache.commons.compress.archivers.zip.ZipEncoding;
51 import org.apache.commons.compress.utils.ArchiveUtils;
52 import org.apache.commons.compress.utils.IOUtils;
53 import org.apache.commons.compress.utils.ParsingUtils;
54 import org.apache.commons.compress.utils.TimeUtils;
55 import org.apache.commons.io.file.attribute.FileTimes;
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 public class TarArchiveEntry implements ArchiveEntry, TarConstants, EntryStreamOffsets {
184
185 private static final TarArchiveEntry[] EMPTY_TAR_ARCHIVE_ENTRY_ARRAY = {};
186
187
188
189
190
191
192
193 public static final long UNKNOWN = -1L;
194
195
196 public static final int MAX_NAMELEN = 31;
197
198
199 public static final int DEFAULT_DIR_MODE = 040755;
200
201
202 public static final int DEFAULT_FILE_MODE = 0100644;
203
204
205
206
207
208
209 @Deprecated
210 public static final int MILLIS_PER_SECOND = 1000;
211
212
213
214
215
216
217
218
219 private static final Pattern PAX_EXTENDED_HEADER_FILE_TIMES_PATTERN = Pattern.compile("-?\\d{1,19}(?:\\.\\d{1,19})?");
220
221 private static FileTime fileTimeFromOptionalSeconds(final long seconds) {
222 if (seconds <= 0) {
223 return null;
224 }
225 return TimeUtils.unixTimeToFileTime(seconds);
226 }
227
228
229
230
231 private static String normalizeFileName(String fileName, final boolean preserveAbsolutePath) {
232 if (!preserveAbsolutePath) {
233 final String property = System.getProperty("os.name");
234 if (property != null) {
235 final String osName = property.toLowerCase(Locale.ROOT);
236
237
238
239
240 if (osName.startsWith("windows")) {
241 if (fileName.length() > 2) {
242 final char ch1 = fileName.charAt(0);
243 final char ch2 = fileName.charAt(1);
244
245 if (ch2 == ':' && (ch1 >= 'a' && ch1 <= 'z' || ch1 >= 'A' && ch1 <= 'Z')) {
246 fileName = fileName.substring(2);
247 }
248 }
249 } else if (osName.contains("netware")) {
250 final int colon = fileName.indexOf(':');
251 if (colon != -1) {
252 fileName = fileName.substring(colon + 1);
253 }
254 }
255 }
256 }
257
258 fileName = fileName.replace(File.separatorChar, '/');
259
260
261
262
263 while (!preserveAbsolutePath && fileName.startsWith("/")) {
264 fileName = fileName.substring(1);
265 }
266 return fileName;
267 }
268
269 private static Instant parseInstantFromDecimalSeconds(final String value) throws IOException {
270
271 if (!TarArchiveEntry.PAX_EXTENDED_HEADER_FILE_TIMES_PATTERN.matcher(value).matches()) {
272 throw new IOException("Corrupted PAX header. Time field value is invalid '" + value + "'");
273 }
274
275 final BigDecimal epochSeconds = new BigDecimal(value);
276 final long seconds = epochSeconds.longValue();
277 final long nanos = epochSeconds.remainder(BigDecimal.ONE).movePointRight(9).longValue();
278 try {
279 return Instant.ofEpochSecond(seconds, nanos);
280 } catch (DateTimeException | ArithmeticException e) {
281
282
283 throw new IOException("Corrupted PAX header. Time field value is invalid '" + value + "'", e);
284 }
285 }
286
287
288 private String name = "";
289
290
291 private final boolean preserveAbsolutePath;
292
293
294 private int mode;
295
296
297 private long userId;
298
299
300 private long groupId;
301
302
303 private long size;
304
305
306
307
308 private FileTime mTime;
309
310
311
312
313
314
315 private FileTime cTime;
316
317
318
319
320
321
322 private FileTime aTime;
323
324
325
326
327
328
329 private FileTime birthTime;
330
331
332 private boolean checkSumOK;
333
334
335 private byte linkFlag;
336
337
338 private String linkName = "";
339
340
341 private String magic = MAGIC_POSIX;
342
343
344 private String version = VERSION_POSIX;
345
346
347 private String userName;
348
349
350 private String groupName = "";
351
352
353 private int devMajor;
354
355
356 private int devMinor;
357
358
359 private List<TarArchiveStructSparse> sparseHeaders;
360
361
362 private boolean isExtended;
363
364
365 private long realSize;
366
367
368 private boolean paxGNUSparse;
369
370
371
372
373 private boolean paxGNU1XSparse;
374
375
376 private boolean starSparse;
377
378
379 private final Path file;
380
381
382 private final LinkOption[] linkOptions;
383
384
385 private final Map<String, String> extraPaxHeaders = new HashMap<>();
386
387 private long dataOffset = EntryStreamOffsets.OFFSET_UNKNOWN;
388
389
390
391
392 private TarArchiveEntry(final boolean preserveAbsolutePath) {
393 String user = System.getProperty("user.name", "");
394 if (user.length() > MAX_NAMELEN) {
395 user = user.substring(0, MAX_NAMELEN);
396 }
397 this.userName = user;
398 this.file = null;
399 this.linkOptions = IOUtils.EMPTY_LINK_OPTIONS;
400 this.preserveAbsolutePath = preserveAbsolutePath;
401 }
402
403
404
405
406
407
408
409 public TarArchiveEntry(final byte[] headerBuf) {
410 this(false);
411 parseTarHeader(headerBuf);
412 }
413
414
415
416
417
418
419
420
421
422
423 public TarArchiveEntry(final byte[] headerBuf, final ZipEncoding encoding) throws IOException {
424 this(headerBuf, encoding, false);
425 }
426
427
428
429
430
431
432
433
434
435
436
437
438 public TarArchiveEntry(final byte[] headerBuf, final ZipEncoding encoding, final boolean lenient) throws IOException {
439 this(Collections.emptyMap(), headerBuf, encoding, lenient);
440 }
441
442
443
444
445
446
447
448
449
450
451
452
453
454 public TarArchiveEntry(final byte[] headerBuf, final ZipEncoding encoding, final boolean lenient, final long dataOffset) throws IOException {
455 this(headerBuf, encoding, lenient);
456 setDataOffset(dataOffset);
457 }
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473 public TarArchiveEntry(final File file) {
474 this(file, file.getPath());
475 }
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491 public TarArchiveEntry(final File file, final String fileName) {
492 final String normalizedName = normalizeFileName(fileName, false);
493 this.file = file.toPath();
494 this.linkOptions = IOUtils.EMPTY_LINK_OPTIONS;
495 try {
496 readFileMode(this.file, normalizedName);
497 } catch (final IOException e) {
498
499
500 if (!file.isDirectory()) {
501 this.size = file.length();
502 }
503 }
504 this.userName = "";
505 try {
506 readOsSpecificProperties(this.file);
507 } catch (final IOException e) {
508
509
510 this.mTime = FileTime.fromMillis(file.lastModified());
511 }
512 preserveAbsolutePath = false;
513 }
514
515
516
517
518
519
520
521
522
523
524
525
526
527 public TarArchiveEntry(final Map<String, String> globalPaxHeaders, final byte[] headerBuf, final ZipEncoding encoding, final boolean lenient)
528 throws IOException {
529 this(false);
530 parseTarHeader(globalPaxHeaders, headerBuf, encoding, false, lenient);
531 }
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546 public TarArchiveEntry(final Map<String, String> globalPaxHeaders, final byte[] headerBuf, final ZipEncoding encoding, final boolean lenient,
547 final long dataOffset) throws IOException {
548 this(globalPaxHeaders, headerBuf, encoding, lenient);
549 setDataOffset(dataOffset);
550 }
551
552
553
554
555
556
557
558
559
560
561
562
563
564 public TarArchiveEntry(final Path file) throws IOException {
565 this(file, file.toString());
566 }
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581 public TarArchiveEntry(final Path file, final String fileName, final LinkOption... linkOptions) throws IOException {
582 final String normalizedName = normalizeFileName(fileName, false);
583 this.file = file;
584 this.linkOptions = linkOptions == null ? IOUtils.EMPTY_LINK_OPTIONS : linkOptions;
585 readFileMode(file, normalizedName, linkOptions);
586 this.userName = "";
587 readOsSpecificProperties(file);
588 preserveAbsolutePath = false;
589 }
590
591
592
593
594
595
596
597
598
599
600 public TarArchiveEntry(final String name) {
601 this(name, false);
602 }
603
604
605
606
607
608
609
610
611
612
613
614
615
616 public TarArchiveEntry(String name, final boolean preserveAbsolutePath) {
617 this(preserveAbsolutePath);
618 name = normalizeFileName(name, preserveAbsolutePath);
619 final boolean isDir = name.endsWith("/");
620 this.name = name;
621 this.mode = isDir ? DEFAULT_DIR_MODE : DEFAULT_FILE_MODE;
622 this.linkFlag = isDir ? LF_DIR : LF_NORMAL;
623 this.mTime = FileTime.from(Instant.now());
624 this.userName = "";
625 }
626
627
628
629
630
631
632
633
634
635
636
637 public TarArchiveEntry(final String name, final byte linkFlag) {
638 this(name, linkFlag, false);
639 }
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654 public TarArchiveEntry(final String name, final byte linkFlag, final boolean preserveAbsolutePath) {
655 this(name, preserveAbsolutePath);
656 this.linkFlag = linkFlag;
657 if (linkFlag == LF_GNUTYPE_LONGNAME) {
658 magic = MAGIC_GNU;
659 version = VERSION_GNU_SPACE;
660 }
661 }
662
663
664
665
666
667
668
669
670
671 public void addPaxHeader(final String name, final String value) {
672 try {
673 processPaxHeader(name, value);
674 } catch (final IOException ex) {
675 throw new IllegalArgumentException("Invalid input", ex);
676 }
677 }
678
679
680
681
682
683
684 public void clearExtraPaxHeaders() {
685 extraPaxHeaders.clear();
686 }
687
688
689
690
691
692
693
694 @Override
695 public boolean equals(final Object it) {
696 if (it == null || getClass() != it.getClass()) {
697 return false;
698 }
699 return equals((TarArchiveEntry) it);
700 }
701
702
703
704
705
706
707
708 public boolean equals(final TarArchiveEntry it) {
709 return it != null && getName().equals(it.getName());
710 }
711
712
713
714
715
716
717
718 private int evaluateType(final Map<String, String> globalPaxHeaders, final byte[] header) {
719 if (ArchiveUtils.matchAsciiBuffer(MAGIC_GNU, header, MAGIC_OFFSET, MAGICLEN)) {
720 return FORMAT_OLDGNU;
721 }
722 if (ArchiveUtils.matchAsciiBuffer(MAGIC_POSIX, header, MAGIC_OFFSET, MAGICLEN)) {
723 if (isXstar(globalPaxHeaders, header)) {
724 return FORMAT_XSTAR;
725 }
726 return FORMAT_POSIX;
727 }
728 return 0;
729 }
730
731 private int fill(final byte value, final int offset, final byte[] outbuf, final int length) {
732 for (int i = 0; i < length; i++) {
733 outbuf[offset + i] = value;
734 }
735 return offset + length;
736 }
737
738 private int fill(final int value, final int offset, final byte[] outbuf, final int length) {
739 return fill((byte) value, offset, outbuf, length);
740 }
741
742 void fillGNUSparse0xData(final Map<String, String> headers) throws IOException {
743 paxGNUSparse = true;
744 realSize = ParsingUtils.parseIntValue(headers.get(TarGnuSparseKeys.SIZE));
745 if (headers.containsKey(TarGnuSparseKeys.NAME)) {
746
747 name = headers.get(TarGnuSparseKeys.NAME);
748 }
749 }
750
751 void fillGNUSparse1xData(final Map<String, String> headers) throws IOException {
752 paxGNUSparse = true;
753 paxGNU1XSparse = true;
754 if (headers.containsKey(TarGnuSparseKeys.NAME)) {
755 name = headers.get(TarGnuSparseKeys.NAME);
756 }
757 if (headers.containsKey(TarGnuSparseKeys.REALSIZE)) {
758 realSize = ParsingUtils.parseIntValue(headers.get(TarGnuSparseKeys.REALSIZE));
759 }
760 }
761
762 void fillStarSparseData(final Map<String, String> headers) throws IOException {
763 starSparse = true;
764 if (headers.containsKey("SCHILY.realsize")) {
765 realSize = ParsingUtils.parseLongValue(headers.get("SCHILY.realsize"));
766 }
767 }
768
769
770
771
772
773
774
775 public FileTime getCreationTime() {
776 return birthTime;
777 }
778
779
780
781
782
783
784 @Override
785 public long getDataOffset() {
786 return dataOffset;
787 }
788
789
790
791
792
793
794
795 public int getDevMajor() {
796 return devMajor;
797 }
798
799
800
801
802
803
804
805 public int getDevMinor() {
806 return devMinor;
807 }
808
809
810
811
812
813
814
815
816
817
818 public TarArchiveEntry[] getDirectoryEntries() {
819 if (file == null || !isDirectory()) {
820 return EMPTY_TAR_ARCHIVE_ENTRY_ARRAY;
821 }
822 final List<TarArchiveEntry> entries = new ArrayList<>();
823 try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(file)) {
824 for (final Path p : dirStream) {
825 entries.add(new TarArchiveEntry(p));
826 }
827 } catch (final IOException e) {
828 return EMPTY_TAR_ARCHIVE_ENTRY_ARRAY;
829 }
830 return entries.toArray(EMPTY_TAR_ARCHIVE_ENTRY_ARRAY);
831 }
832
833
834
835
836
837
838
839
840 public String getExtraPaxHeader(final String name) {
841 return extraPaxHeaders.get(name);
842 }
843
844
845
846
847
848
849
850 public Map<String, String> getExtraPaxHeaders() {
851 return Collections.unmodifiableMap(extraPaxHeaders);
852 }
853
854
855
856
857
858
859
860
861
862
863 public File getFile() {
864 if (file == null) {
865 return null;
866 }
867 return file.toFile();
868 }
869
870
871
872
873
874
875
876 @Deprecated
877 public int getGroupId() {
878 return (int) (groupId & 0xffffffff);
879 }
880
881
882
883
884
885
886 public String getGroupName() {
887 return groupName;
888 }
889
890
891
892
893
894
895
896 public FileTime getLastAccessTime() {
897 return aTime;
898 }
899
900
901
902
903
904
905
906 @Override
907 public Date getLastModifiedDate() {
908 return getModTime();
909 }
910
911
912
913
914
915
916
917 public FileTime getLastModifiedTime() {
918 return mTime;
919 }
920
921
922
923
924
925
926
927 public byte getLinkFlag() {
928 return this.linkFlag;
929 }
930
931
932
933
934
935
936 public String getLinkName() {
937 return linkName;
938 }
939
940
941
942
943
944
945
946 public long getLongGroupId() {
947 return groupId;
948 }
949
950
951
952
953
954
955
956 public long getLongUserId() {
957 return userId;
958 }
959
960
961
962
963
964
965 public int getMode() {
966 return mode;
967 }
968
969
970
971
972
973
974
975 public Date getModTime() {
976 final FileTime fileTime = mTime;
977 return FileTimes.toDate(fileTime);
978 }
979
980
981
982
983
984
985
986
987
988 @Override
989 public String getName() {
990 return name;
991 }
992
993
994
995
996
997
998
999
1000 public List<TarArchiveStructSparse> getOrderedSparseHeaders() throws IOException {
1001 if (sparseHeaders == null || sparseHeaders.isEmpty()) {
1002 return Collections.emptyList();
1003 }
1004 final List<TarArchiveStructSparse> orderedAndFiltered = sparseHeaders.stream().filter(s -> s.getOffset() > 0 || s.getNumbytes() > 0)
1005 .sorted(Comparator.comparingLong(TarArchiveStructSparse::getOffset)).collect(Collectors.toList());
1006 final int numberOfHeaders = orderedAndFiltered.size();
1007 for (int i = 0; i < numberOfHeaders; i++) {
1008 final TarArchiveStructSparse str = orderedAndFiltered.get(i);
1009 if (i + 1 < numberOfHeaders && str.getOffset() + str.getNumbytes() > orderedAndFiltered.get(i + 1).getOffset()) {
1010 throw new IOException("Corrupted TAR archive. Sparse blocks for " + getName() + " overlap each other.");
1011 }
1012 if (str.getOffset() + str.getNumbytes() < 0) {
1013
1014 throw new IOException("Unreadable TAR archive. Offset and numbytes for sparse block in " + getName() + " too large.");
1015 }
1016 }
1017 if (!orderedAndFiltered.isEmpty()) {
1018 final TarArchiveStructSparse last = orderedAndFiltered.get(numberOfHeaders - 1);
1019 if (last.getOffset() + last.getNumbytes() > getRealSize()) {
1020 throw new IOException("Corrupted TAR archive. Sparse block extends beyond real size of the entry");
1021 }
1022 }
1023 return orderedAndFiltered;
1024 }
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036 public Path getPath() {
1037 return file;
1038 }
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051 public long getRealSize() {
1052 if (!isSparse()) {
1053 return getSize();
1054 }
1055 return realSize;
1056 }
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067 @Override
1068 public long getSize() {
1069 return size;
1070 }
1071
1072
1073
1074
1075
1076
1077
1078 public List<TarArchiveStructSparse> getSparseHeaders() {
1079 return sparseHeaders;
1080 }
1081
1082
1083
1084
1085
1086
1087
1088 public FileTime getStatusChangeTime() {
1089 return cTime;
1090 }
1091
1092
1093
1094
1095
1096
1097
1098 @Deprecated
1099 public int getUserId() {
1100 return (int) (userId & 0xffffffff);
1101 }
1102
1103
1104
1105
1106
1107
1108 public String getUserName() {
1109 return userName;
1110 }
1111
1112
1113
1114
1115
1116
1117 @Override
1118 public int hashCode() {
1119 return getName().hashCode();
1120 }
1121
1122
1123
1124
1125
1126
1127
1128 public boolean isBlockDevice() {
1129 return linkFlag == LF_BLK;
1130 }
1131
1132
1133
1134
1135
1136
1137
1138 public boolean isCharacterDevice() {
1139 return linkFlag == LF_CHR;
1140 }
1141
1142
1143
1144
1145
1146
1147
1148
1149 public boolean isCheckSumOK() {
1150 return checkSumOK;
1151 }
1152
1153
1154
1155
1156
1157
1158
1159 public boolean isDescendent(final TarArchiveEntry desc) {
1160 return desc.getName().startsWith(getName());
1161 }
1162
1163
1164
1165
1166
1167
1168 @Override
1169 public boolean isDirectory() {
1170 if (file != null) {
1171 return Files.isDirectory(file, linkOptions);
1172 }
1173 if (linkFlag == LF_DIR) {
1174 return true;
1175 }
1176 return !isPaxHeader() && !isGlobalPaxHeader() && getName().endsWith("/");
1177 }
1178
1179
1180
1181
1182
1183
1184 public boolean isExtended() {
1185 return isExtended;
1186 }
1187
1188
1189
1190
1191
1192
1193
1194 public boolean isFIFO() {
1195 return linkFlag == LF_FIFO;
1196 }
1197
1198
1199
1200
1201
1202
1203
1204 public boolean isFile() {
1205 if (file != null) {
1206 return Files.isRegularFile(file, linkOptions);
1207 }
1208 if (linkFlag == LF_OLDNORM || linkFlag == LF_NORMAL) {
1209 return true;
1210 }
1211 return linkFlag != LF_DIR && !getName().endsWith("/");
1212 }
1213
1214
1215
1216
1217
1218
1219
1220
1221 public boolean isGlobalPaxHeader() {
1222 return linkFlag == LF_PAX_GLOBAL_EXTENDED_HEADER;
1223 }
1224
1225
1226
1227
1228
1229
1230 public boolean isGNULongLinkEntry() {
1231 return linkFlag == LF_GNUTYPE_LONGLINK;
1232 }
1233
1234
1235
1236
1237
1238
1239 public boolean isGNULongNameEntry() {
1240 return linkFlag == LF_GNUTYPE_LONGNAME;
1241 }
1242
1243
1244
1245
1246
1247
1248 public boolean isGNUSparse() {
1249 return isOldGNUSparse() || isPaxGNUSparse();
1250 }
1251
1252 private boolean isInvalidPrefix(final byte[] header) {
1253
1254 if (header[XSTAR_PREFIX_OFFSET + 130] != 0) {
1255
1256 if (header[LF_OFFSET] != LF_MULTIVOLUME) {
1257 return true;
1258 }
1259
1260
1261
1262 if ((header[XSTAR_MULTIVOLUME_OFFSET] & 0x80) == 0 && header[XSTAR_MULTIVOLUME_OFFSET + 11] != ' ') {
1263 return true;
1264 }
1265 }
1266 return false;
1267 }
1268
1269 private boolean isInvalidXtarTime(final byte[] buffer, final int offset, final int length) {
1270
1271 if ((buffer[offset] & 0x80) == 0) {
1272 final int lastIndex = length - 1;
1273 for (int i = 0; i < lastIndex; i++) {
1274 final byte b = buffer[offset + i];
1275 if (b < '0' || b > '7') {
1276 return true;
1277 }
1278 }
1279
1280 final byte b = buffer[offset + lastIndex];
1281 if (b != ' ' && b != 0) {
1282 return true;
1283 }
1284 }
1285 return false;
1286 }
1287
1288
1289
1290
1291
1292
1293
1294 public boolean isLink() {
1295 return linkFlag == LF_LINK;
1296 }
1297
1298
1299
1300
1301
1302
1303
1304 public boolean isOldGNUSparse() {
1305 return linkFlag == LF_GNUTYPE_SPARSE;
1306 }
1307
1308
1309
1310
1311
1312
1313
1314 public boolean isPaxGNU1XSparse() {
1315 return paxGNU1XSparse;
1316 }
1317
1318
1319
1320
1321
1322
1323
1324 public boolean isPaxGNUSparse() {
1325 return paxGNUSparse;
1326 }
1327
1328
1329
1330
1331
1332
1333
1334
1335 public boolean isPaxHeader() {
1336 return linkFlag == LF_PAX_EXTENDED_HEADER_LC || linkFlag == LF_PAX_EXTENDED_HEADER_UC;
1337 }
1338
1339
1340
1341
1342
1343
1344
1345 public boolean isSparse() {
1346 return isGNUSparse() || isStarSparse();
1347 }
1348
1349
1350
1351
1352
1353
1354
1355 public boolean isStarSparse() {
1356 return starSparse;
1357 }
1358
1359
1360
1361
1362
1363
1364 @Override
1365 public boolean isStreamContiguous() {
1366 return true;
1367 }
1368
1369
1370
1371
1372
1373
1374
1375 public boolean isSymbolicLink() {
1376 return linkFlag == LF_SYMLINK;
1377 }
1378
1379
1380
1381
1382
1383
1384 private boolean isXstar(final Map<String, String> globalPaxHeaders, final byte[] header) {
1385
1386 if (ArchiveUtils.matchAsciiBuffer(MAGIC_XSTAR, header, XSTAR_MAGIC_OFFSET, XSTAR_MAGIC_LEN)) {
1387 return true;
1388 }
1389
1390
1391
1392
1393
1394
1395 final String archType = globalPaxHeaders.get("SCHILY.archtype");
1396 if (archType != null) {
1397 return "xustar".equals(archType) || "exustar".equals(archType);
1398 }
1399
1400 if (isInvalidPrefix(header)) {
1401 return false;
1402 }
1403 if (isInvalidXtarTime(header, XSTAR_ATIME_OFFSET, ATIMELEN_XSTAR)) {
1404 return false;
1405 }
1406 if (isInvalidXtarTime(header, XSTAR_CTIME_OFFSET, CTIMELEN_XSTAR)) {
1407 return false;
1408 }
1409 return true;
1410 }
1411
1412 private long parseOctalOrBinary(final byte[] header, final int offset, final int length, final boolean lenient) {
1413 if (lenient) {
1414 try {
1415 return TarUtils.parseOctalOrBinary(header, offset, length);
1416 } catch (final IllegalArgumentException ex) {
1417 return UNKNOWN;
1418 }
1419 }
1420 return TarUtils.parseOctalOrBinary(header, offset, length);
1421 }
1422
1423
1424
1425
1426
1427
1428
1429 public void parseTarHeader(final byte[] header) {
1430 try {
1431 parseTarHeader(header, TarUtils.DEFAULT_ENCODING);
1432 } catch (final IOException ex) {
1433 try {
1434 parseTarHeader(header, TarUtils.DEFAULT_ENCODING, true, false);
1435 } catch (final IOException ex2) {
1436
1437 throw new UncheckedIOException(ex2);
1438 }
1439 }
1440 }
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451 public void parseTarHeader(final byte[] header, final ZipEncoding encoding) throws IOException {
1452 parseTarHeader(header, encoding, false, false);
1453 }
1454
1455 private void parseTarHeader(final byte[] header, final ZipEncoding encoding, final boolean oldStyle, final boolean lenient) throws IOException {
1456 parseTarHeader(Collections.emptyMap(), header, encoding, oldStyle, lenient);
1457 }
1458
1459 private void parseTarHeader(final Map<String, String> globalPaxHeaders, final byte[] header, final ZipEncoding encoding, final boolean oldStyle,
1460 final boolean lenient) throws IOException {
1461 try {
1462 parseTarHeaderUnwrapped(globalPaxHeaders, header, encoding, oldStyle, lenient);
1463 } catch (final IllegalArgumentException ex) {
1464 throw new IOException("Corrupted TAR archive.", ex);
1465 }
1466 }
1467
1468 private void parseTarHeaderUnwrapped(final Map<String, String> globalPaxHeaders, final byte[] header, final ZipEncoding encoding, final boolean oldStyle,
1469 final boolean lenient) throws IOException {
1470 int offset = 0;
1471 name = oldStyle ? TarUtils.parseName(header, offset, NAMELEN) : TarUtils.parseName(header, offset, NAMELEN, encoding);
1472 offset += NAMELEN;
1473 mode = (int) parseOctalOrBinary(header, offset, MODELEN, lenient);
1474 offset += MODELEN;
1475 userId = (int) parseOctalOrBinary(header, offset, UIDLEN, lenient);
1476 offset += UIDLEN;
1477 groupId = (int) parseOctalOrBinary(header, offset, GIDLEN, lenient);
1478 offset += GIDLEN;
1479 size = TarUtils.parseOctalOrBinary(header, offset, SIZELEN);
1480 if (size < 0) {
1481 throw new IOException("broken archive, entry with negative size");
1482 }
1483 offset += SIZELEN;
1484 mTime = TimeUtils.unixTimeToFileTime(parseOctalOrBinary(header, offset, MODTIMELEN, lenient));
1485 offset += MODTIMELEN;
1486 checkSumOK = TarUtils.verifyCheckSum(header);
1487 offset += CHKSUMLEN;
1488 linkFlag = header[offset++];
1489 linkName = oldStyle ? TarUtils.parseName(header, offset, NAMELEN) : TarUtils.parseName(header, offset, NAMELEN, encoding);
1490 offset += NAMELEN;
1491 magic = TarUtils.parseName(header, offset, MAGICLEN);
1492 offset += MAGICLEN;
1493 version = TarUtils.parseName(header, offset, VERSIONLEN);
1494 offset += VERSIONLEN;
1495 userName = oldStyle ? TarUtils.parseName(header, offset, UNAMELEN) : TarUtils.parseName(header, offset, UNAMELEN, encoding);
1496 offset += UNAMELEN;
1497 groupName = oldStyle ? TarUtils.parseName(header, offset, GNAMELEN) : TarUtils.parseName(header, offset, GNAMELEN, encoding);
1498 offset += GNAMELEN;
1499 if (linkFlag == LF_CHR || linkFlag == LF_BLK) {
1500 devMajor = (int) parseOctalOrBinary(header, offset, DEVLEN, lenient);
1501 offset += DEVLEN;
1502 devMinor = (int) parseOctalOrBinary(header, offset, DEVLEN, lenient);
1503 offset += DEVLEN;
1504 } else {
1505 offset += 2 * DEVLEN;
1506 }
1507 final int type = evaluateType(globalPaxHeaders, header);
1508 switch (type) {
1509 case FORMAT_OLDGNU: {
1510 aTime = fileTimeFromOptionalSeconds(parseOctalOrBinary(header, offset, ATIMELEN_GNU, lenient));
1511 offset += ATIMELEN_GNU;
1512 cTime = fileTimeFromOptionalSeconds(parseOctalOrBinary(header, offset, CTIMELEN_GNU, lenient));
1513 offset += CTIMELEN_GNU;
1514 offset += OFFSETLEN_GNU;
1515 offset += LONGNAMESLEN_GNU;
1516 offset += PAD2LEN_GNU;
1517 sparseHeaders = new ArrayList<>(TarUtils.readSparseStructs(header, offset, SPARSE_HEADERS_IN_OLDGNU_HEADER));
1518 offset += SPARSELEN_GNU;
1519 isExtended = TarUtils.parseBoolean(header, offset);
1520 offset += ISEXTENDEDLEN_GNU;
1521 realSize = TarUtils.parseOctal(header, offset, REALSIZELEN_GNU);
1522 offset += REALSIZELEN_GNU;
1523 break;
1524 }
1525 case FORMAT_XSTAR: {
1526 final String xstarPrefix = oldStyle ? TarUtils.parseName(header, offset, PREFIXLEN_XSTAR)
1527 : TarUtils.parseName(header, offset, PREFIXLEN_XSTAR, encoding);
1528 offset += PREFIXLEN_XSTAR;
1529 if (!xstarPrefix.isEmpty()) {
1530 name = xstarPrefix + "/" + name;
1531 }
1532 aTime = fileTimeFromOptionalSeconds(parseOctalOrBinary(header, offset, ATIMELEN_XSTAR, lenient));
1533 offset += ATIMELEN_XSTAR;
1534 cTime = fileTimeFromOptionalSeconds(parseOctalOrBinary(header, offset, CTIMELEN_XSTAR, lenient));
1535 offset += CTIMELEN_XSTAR;
1536 break;
1537 }
1538 case FORMAT_POSIX:
1539 default: {
1540 final String prefix = oldStyle ? TarUtils.parseName(header, offset, PREFIXLEN) : TarUtils.parseName(header, offset, PREFIXLEN, encoding);
1541 offset += PREFIXLEN;
1542
1543
1544 if (isDirectory() && !name.endsWith("/")) {
1545 name += "/";
1546 }
1547 if (!prefix.isEmpty()) {
1548 name = prefix + "/" + name;
1549 }
1550 }
1551 }
1552 }
1553
1554
1555
1556
1557
1558
1559
1560
1561 private void processPaxHeader(final String key, final String val) throws IOException {
1562 processPaxHeader(key, val, extraPaxHeaders);
1563 }
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574 private void processPaxHeader(final String key, final String val, final Map<String, String> headers) throws IOException {
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585 switch (key) {
1586 case "path":
1587 setName(val);
1588 break;
1589 case "linkpath":
1590 setLinkName(val);
1591 break;
1592 case "gid":
1593 setGroupId(ParsingUtils.parseLongValue(val));
1594 break;
1595 case "gname":
1596 setGroupName(val);
1597 break;
1598 case "uid":
1599 setUserId(ParsingUtils.parseLongValue(val));
1600 break;
1601 case "uname":
1602 setUserName(val);
1603 break;
1604 case "size":
1605 final long size = ParsingUtils.parseLongValue(val);
1606 if (size < 0) {
1607 throw new IOException("Corrupted TAR archive. Entry size is negative");
1608 }
1609 setSize(size);
1610 break;
1611 case "mtime":
1612 setLastModifiedTime(FileTime.from(parseInstantFromDecimalSeconds(val)));
1613 break;
1614 case "atime":
1615 setLastAccessTime(FileTime.from(parseInstantFromDecimalSeconds(val)));
1616 break;
1617 case "ctime":
1618 setStatusChangeTime(FileTime.from(parseInstantFromDecimalSeconds(val)));
1619 break;
1620 case "LIBARCHIVE.creationtime":
1621 setCreationTime(FileTime.from(parseInstantFromDecimalSeconds(val)));
1622 break;
1623 case "SCHILY.devminor":
1624 final int devMinor = ParsingUtils.parseIntValue(val);
1625 if (devMinor < 0) {
1626 throw new IOException("Corrupted TAR archive. Dev-Minor is negative");
1627 }
1628 setDevMinor(devMinor);
1629 break;
1630 case "SCHILY.devmajor":
1631 final int devMajor = ParsingUtils.parseIntValue(val);
1632 if (devMajor < 0) {
1633 throw new IOException("Corrupted TAR archive. Dev-Major is negative");
1634 }
1635 setDevMajor(devMajor);
1636 break;
1637 case TarGnuSparseKeys.SIZE:
1638 fillGNUSparse0xData(headers);
1639 break;
1640 case TarGnuSparseKeys.REALSIZE:
1641 fillGNUSparse1xData(headers);
1642 break;
1643 case "SCHILY.filetype":
1644 if ("sparse".equals(val)) {
1645 fillStarSparseData(headers);
1646 }
1647 break;
1648 default:
1649 extraPaxHeaders.put(key, val);
1650 }
1651 }
1652
1653 private void readFileMode(final Path file, final String normalizedName, final LinkOption... options) throws IOException {
1654 if (Files.isDirectory(file, options)) {
1655 this.mode = DEFAULT_DIR_MODE;
1656 this.linkFlag = LF_DIR;
1657
1658 final int nameLength = normalizedName.length();
1659 if (nameLength == 0 || normalizedName.charAt(nameLength - 1) != '/') {
1660 this.name = normalizedName + "/";
1661 } else {
1662 this.name = normalizedName;
1663 }
1664 } else {
1665 this.mode = DEFAULT_FILE_MODE;
1666 this.linkFlag = LF_NORMAL;
1667 this.name = normalizedName;
1668 this.size = Files.size(file);
1669 }
1670 }
1671
1672 private void readOsSpecificProperties(final Path file, final LinkOption... options) throws IOException {
1673 final Set<String> availableAttributeViews = file.getFileSystem().supportedFileAttributeViews();
1674 if (availableAttributeViews.contains("posix")) {
1675 final PosixFileAttributes posixFileAttributes = Files.readAttributes(file, PosixFileAttributes.class, options);
1676 setLastModifiedTime(posixFileAttributes.lastModifiedTime());
1677 setCreationTime(posixFileAttributes.creationTime());
1678 setLastAccessTime(posixFileAttributes.lastAccessTime());
1679 this.userName = posixFileAttributes.owner().getName();
1680 this.groupName = posixFileAttributes.group().getName();
1681 if (availableAttributeViews.contains("unix")) {
1682 this.userId = ((Number) Files.getAttribute(file, "unix:uid", options)).longValue();
1683 this.groupId = ((Number) Files.getAttribute(file, "unix:gid", options)).longValue();
1684 try {
1685 setStatusChangeTime((FileTime) Files.getAttribute(file, "unix:ctime", options));
1686 } catch (final IllegalArgumentException ex) {
1687
1688 }
1689 }
1690 } else {
1691 if (availableAttributeViews.contains("dos")) {
1692 final DosFileAttributes dosFileAttributes = Files.readAttributes(file, DosFileAttributes.class, options);
1693 setLastModifiedTime(dosFileAttributes.lastModifiedTime());
1694 setCreationTime(dosFileAttributes.creationTime());
1695 setLastAccessTime(dosFileAttributes.lastAccessTime());
1696 } else {
1697 final BasicFileAttributes basicFileAttributes = Files.readAttributes(file, BasicFileAttributes.class, options);
1698 setLastModifiedTime(basicFileAttributes.lastModifiedTime());
1699 setCreationTime(basicFileAttributes.creationTime());
1700 setLastAccessTime(basicFileAttributes.lastAccessTime());
1701 }
1702 this.userName = Files.getOwner(file, options).getName();
1703 }
1704 }
1705
1706
1707
1708
1709
1710
1711
1712 public void setCreationTime(final FileTime time) {
1713 birthTime = time;
1714 }
1715
1716
1717
1718
1719
1720
1721
1722 public void setDataOffset(final long dataOffset) {
1723 if (dataOffset < 0) {
1724 throw new IllegalArgumentException("The offset can not be smaller than 0");
1725 }
1726 this.dataOffset = dataOffset;
1727 }
1728
1729
1730
1731
1732
1733
1734
1735
1736 public void setDevMajor(final int devNo) {
1737 if (devNo < 0) {
1738 throw new IllegalArgumentException("Major device number is out of " + "range: " + devNo);
1739 }
1740 this.devMajor = devNo;
1741 }
1742
1743
1744
1745
1746
1747
1748
1749
1750 public void setDevMinor(final int devNo) {
1751 if (devNo < 0) {
1752 throw new IllegalArgumentException("Minor device number is out of " + "range: " + devNo);
1753 }
1754 this.devMinor = devNo;
1755 }
1756
1757
1758
1759
1760
1761
1762 public void setGroupId(final int groupId) {
1763 setGroupId((long) groupId);
1764 }
1765
1766
1767
1768
1769
1770
1771
1772 public void setGroupId(final long groupId) {
1773 this.groupId = groupId;
1774 }
1775
1776
1777
1778
1779
1780
1781 public void setGroupName(final String groupName) {
1782 this.groupName = groupName;
1783 }
1784
1785
1786
1787
1788
1789
1790
1791 public void setIds(final int userId, final int groupId) {
1792 setUserId(userId);
1793 setGroupId(groupId);
1794 }
1795
1796
1797
1798
1799
1800
1801
1802 public void setLastAccessTime(final FileTime time) {
1803 aTime = time;
1804 }
1805
1806
1807
1808
1809
1810
1811
1812 public void setLastModifiedTime(final FileTime time) {
1813 mTime = Objects.requireNonNull(time, "Time must not be null");
1814 }
1815
1816
1817
1818
1819
1820
1821
1822
1823 public void setLinkName(final String link) {
1824 this.linkName = link;
1825 }
1826
1827
1828
1829
1830
1831
1832 public void setMode(final int mode) {
1833 this.mode = mode;
1834 }
1835
1836
1837
1838
1839
1840
1841
1842 public void setModTime(final Date time) {
1843 setLastModifiedTime(FileTimes.toFileTime(time));
1844 }
1845
1846
1847
1848
1849
1850
1851
1852
1853 public void setModTime(final FileTime time) {
1854 setLastModifiedTime(time);
1855 }
1856
1857
1858
1859
1860
1861
1862
1863 public void setModTime(final long time) {
1864 setLastModifiedTime(FileTime.fromMillis(time));
1865 }
1866
1867
1868
1869
1870
1871
1872 public void setName(final String name) {
1873 this.name = normalizeFileName(name, this.preserveAbsolutePath);
1874 }
1875
1876
1877
1878
1879
1880
1881
1882 public void setNames(final String userName, final String groupName) {
1883 setUserName(userName);
1884 setGroupName(groupName);
1885 }
1886
1887
1888
1889
1890
1891
1892
1893 public void setSize(final long size) {
1894 if (size < 0) {
1895 throw new IllegalArgumentException("Size is out of range: " + size);
1896 }
1897 this.size = size;
1898 }
1899
1900
1901
1902
1903
1904
1905
1906 public void setSparseHeaders(final List<TarArchiveStructSparse> sparseHeaders) {
1907 this.sparseHeaders = sparseHeaders;
1908 }
1909
1910
1911
1912
1913
1914
1915
1916 public void setStatusChangeTime(final FileTime time) {
1917 cTime = time;
1918 }
1919
1920
1921
1922
1923
1924
1925 public void setUserId(final int userId) {
1926 setUserId((long) userId);
1927 }
1928
1929
1930
1931
1932
1933
1934
1935 public void setUserId(final long userId) {
1936 this.userId = userId;
1937 }
1938
1939
1940
1941
1942
1943
1944 public void setUserName(final String userName) {
1945 this.userName = userName;
1946 }
1947
1948
1949
1950
1951
1952
1953
1954 void updateEntryFromPaxHeaders(final Map<String, String> headers) throws IOException {
1955 for (final Map.Entry<String, String> ent : headers.entrySet()) {
1956 processPaxHeader(ent.getKey(), ent.getValue(), headers);
1957 }
1958 }
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968 public void writeEntryHeader(final byte[] outbuf) {
1969 try {
1970 writeEntryHeader(outbuf, TarUtils.DEFAULT_ENCODING, false);
1971 } catch (final IOException ex) {
1972 try {
1973 writeEntryHeader(outbuf, TarUtils.FALLBACK_ENCODING, false);
1974 } catch (final IOException ex2) {
1975
1976 throw new UncheckedIOException(ex2);
1977 }
1978 }
1979 }
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991 public void writeEntryHeader(final byte[] outbuf, final ZipEncoding encoding, final boolean starMode) throws IOException {
1992 int offset = 0;
1993 offset = TarUtils.formatNameBytes(name, outbuf, offset, NAMELEN, encoding);
1994 offset = writeEntryHeaderField(mode, outbuf, offset, MODELEN, starMode);
1995 offset = writeEntryHeaderField(userId, outbuf, offset, UIDLEN, starMode);
1996 offset = writeEntryHeaderField(groupId, outbuf, offset, GIDLEN, starMode);
1997 offset = writeEntryHeaderField(size, outbuf, offset, SIZELEN, starMode);
1998 offset = writeEntryHeaderField(TimeUtils.toUnixTime(mTime), outbuf, offset, MODTIMELEN, starMode);
1999 final int csOffset = offset;
2000 offset = fill((byte) ' ', offset, outbuf, CHKSUMLEN);
2001 outbuf[offset++] = linkFlag;
2002 offset = TarUtils.formatNameBytes(linkName, outbuf, offset, NAMELEN, encoding);
2003 offset = TarUtils.formatNameBytes(magic, outbuf, offset, MAGICLEN);
2004 offset = TarUtils.formatNameBytes(version, outbuf, offset, VERSIONLEN);
2005 offset = TarUtils.formatNameBytes(userName, outbuf, offset, UNAMELEN, encoding);
2006 offset = TarUtils.formatNameBytes(groupName, outbuf, offset, GNAMELEN, encoding);
2007 offset = writeEntryHeaderField(devMajor, outbuf, offset, DEVLEN, starMode);
2008 offset = writeEntryHeaderField(devMinor, outbuf, offset, DEVLEN, starMode);
2009 if (starMode) {
2010
2011 offset = fill(0, offset, outbuf, PREFIXLEN_XSTAR);
2012 offset = writeEntryHeaderOptionalTimeField(aTime, offset, outbuf, ATIMELEN_XSTAR);
2013 offset = writeEntryHeaderOptionalTimeField(cTime, offset, outbuf, CTIMELEN_XSTAR);
2014
2015 offset = fill(0, offset, outbuf, 8);
2016
2017
2018 offset = fill(0, offset, outbuf, XSTAR_MAGIC_LEN);
2019 }
2020 offset = fill(0, offset, outbuf, outbuf.length - offset);
2021 final long chk = TarUtils.computeCheckSum(outbuf);
2022 TarUtils.formatCheckSumOctalBytes(chk, outbuf, csOffset, CHKSUMLEN);
2023 }
2024
2025 private int writeEntryHeaderField(final long value, final byte[] outbuf, final int offset, final int length, final boolean starMode) {
2026 if (!starMode && (value < 0 || value >= 1L << 3 * (length - 1))) {
2027
2028
2029
2030 return TarUtils.formatLongOctalBytes(0, outbuf, offset, length);
2031 }
2032 return TarUtils.formatLongOctalOrBinaryBytes(value, outbuf, offset, length);
2033 }
2034
2035 private int writeEntryHeaderOptionalTimeField(final FileTime time, int offset, final byte[] outbuf, final int fieldLength) {
2036 if (time != null) {
2037 offset = writeEntryHeaderField(TimeUtils.toUnixTime(time), outbuf, offset, fieldLength, true);
2038 } else {
2039 offset = fill(0, offset, outbuf, fieldLength);
2040 }
2041 return offset;
2042 }
2043
2044 }