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.dump;
20
21 import java.util.Collections;
22 import java.util.Date;
23 import java.util.EnumSet;
24 import java.util.HashSet;
25 import java.util.Set;
26
27 import org.apache.commons.compress.archivers.ArchiveEntry;
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 public class DumpArchiveEntry implements ArchiveEntry {
175 public enum PERMISSION {
176 SETUID(04000), SETGUI(02000), STICKY(01000), USER_READ(00400), USER_WRITE(00200), USER_EXEC(00100), GROUP_READ(00040), GROUP_WRITE(00020),
177 GROUP_EXEC(00010), WORLD_READ(00004), WORLD_WRITE(00002), WORLD_EXEC(00001);
178
179 public static Set<PERMISSION> find(final int code) {
180 final Set<PERMISSION> set = new HashSet<>();
181
182 for (final PERMISSION p : PERMISSION.values()) {
183 if ((code & p.code) == p.code) {
184 set.add(p);
185 }
186 }
187
188 if (set.isEmpty()) {
189 return Collections.emptySet();
190 }
191
192 return EnumSet.copyOf(set);
193 }
194
195 private final int code;
196
197 PERMISSION(final int code) {
198 this.code = code;
199 }
200 }
201
202
203
204
205 static class TapeSegmentHeader {
206 private DumpArchiveConstants.SEGMENT_TYPE type;
207 private int volume;
208 private int ino;
209 private int count;
210 private int holes;
211 private final byte[] cdata = new byte[512];
212
213 public int getCdata(final int idx) {
214 return cdata[idx];
215 }
216
217 public int getCount() {
218 return count;
219 }
220
221 public int getHoles() {
222 return holes;
223 }
224
225 public int getIno() {
226 return ino;
227 }
228
229 public DumpArchiveConstants.SEGMENT_TYPE getType() {
230 return type;
231 }
232
233 public int getVolume() {
234 return volume;
235 }
236
237 void setIno(final int ino) {
238 this.ino = ino;
239 }
240 }
241
242 public enum TYPE {
243 WHITEOUT(14), SOCKET(12), LINK(10), FILE(8), BLKDEV(6), DIRECTORY(4), CHRDEV(2), FIFO(1), UNKNOWN(15);
244
245 public static TYPE find(final int code) {
246 TYPE type = UNKNOWN;
247
248 for (final TYPE t : TYPE.values()) {
249 if (code == t.code) {
250 type = t;
251 }
252 }
253
254 return type;
255 }
256
257 private final int code;
258
259 TYPE(final int code) {
260 this.code = code;
261 }
262 }
263
264
265
266
267
268
269 static DumpArchiveEntry parse(final byte[] buffer) {
270 final DumpArchiveEntry entry = new DumpArchiveEntry();
271 final TapeSegmentHeader header = entry.header;
272
273 header.type = DumpArchiveConstants.SEGMENT_TYPE.find(DumpArchiveUtil.convert32(buffer, 0));
274
275
276
277
278 header.volume = DumpArchiveUtil.convert32(buffer, 12);
279
280 entry.ino = header.ino = DumpArchiveUtil.convert32(buffer, 20);
281
282
283
284 final int m = DumpArchiveUtil.convert16(buffer, 32);
285
286
287 entry.setType(TYPE.find(m >> 12 & 0x0F));
288
289
290 entry.setMode(m);
291
292 entry.nlink = DumpArchiveUtil.convert16(buffer, 34);
293
294 entry.setSize(DumpArchiveUtil.convert64(buffer, 40));
295
296 long t = 1000L * DumpArchiveUtil.convert32(buffer, 48) + DumpArchiveUtil.convert32(buffer, 52) / 1000;
297 entry.setAccessTime(new Date(t));
298 t = 1000L * DumpArchiveUtil.convert32(buffer, 56) + DumpArchiveUtil.convert32(buffer, 60) / 1000;
299 entry.setLastModifiedDate(new Date(t));
300 t = 1000L * DumpArchiveUtil.convert32(buffer, 64) + DumpArchiveUtil.convert32(buffer, 68) / 1000;
301 entry.ctime = t;
302
303
304
305
306
307 entry.generation = DumpArchiveUtil.convert32(buffer, 140);
308 entry.setUserId(DumpArchiveUtil.convert32(buffer, 144));
309 entry.setGroupId(DumpArchiveUtil.convert32(buffer, 148));
310
311 header.count = DumpArchiveUtil.convert32(buffer, 160);
312
313 header.holes = 0;
314
315 for (int i = 0; i < 512 && i < header.count; i++) {
316 if (buffer[164 + i] == 0) {
317 header.holes++;
318 }
319 }
320
321 System.arraycopy(buffer, 164, header.cdata, 0, 512);
322
323 entry.volume = header.getVolume();
324
325
326 return entry;
327 }
328
329 private String name;
330 private TYPE type = TYPE.UNKNOWN;
331 private int mode;
332 private Set<PERMISSION> permissions = Collections.emptySet();
333 private long size;
334
335 private long atime;
336
337 private long mtime;
338 private int uid;
339 private int gid;
340
341
342
343
344 private final DumpArchiveSummary summary = null;
345
346
347
348
349 private final TapeSegmentHeader header = new TapeSegmentHeader();
350 private String simpleName;
351 private String originalName;
352
353
354
355
356 private int volume;
357 private long offset;
358 private int ino;
359
360 private int nlink;
361
362 private long ctime;
363
364 private int generation;
365
366 private boolean isDeleted;
367
368
369
370
371 public DumpArchiveEntry() {
372 }
373
374
375
376
377
378
379
380 public DumpArchiveEntry(final String name, final String simpleName) {
381 setName(name);
382 this.simpleName = simpleName;
383 }
384
385
386
387
388
389
390
391
392
393 protected DumpArchiveEntry(final String name, final String simpleName, final int ino, final TYPE type) {
394 setType(type);
395 setName(name);
396 this.simpleName = simpleName;
397 this.ino = ino;
398 this.offset = 0;
399 }
400
401 @Override
402 public boolean equals(final Object o) {
403 if (o == this) {
404 return true;
405 }
406 if (o == null || !o.getClass().equals(getClass())) {
407 return false;
408 }
409
410 final DumpArchiveEntry rhs = (DumpArchiveEntry) o;
411
412 if (ino != rhs.ino) {
413 return false;
414 }
415
416
417 if (summary == null && rhs.summary != null
418 || summary != null && !summary.equals(rhs.summary)) {
419 return false;
420 }
421
422 return true;
423 }
424
425
426
427
428
429
430 public Date getAccessTime() {
431 return new Date(atime);
432 }
433
434
435
436
437
438
439 public Date getCreationTime() {
440 return new Date(ctime);
441 }
442
443
444
445
446 long getEntrySize() {
447 return size;
448 }
449
450
451
452
453
454
455 public int getGeneration() {
456 return generation;
457 }
458
459
460
461
462
463
464 public int getGroupId() {
465 return gid;
466 }
467
468
469
470
471
472
473 public int getHeaderCount() {
474 return header.getCount();
475 }
476
477
478
479
480
481
482 public int getHeaderHoles() {
483 return header.getHoles();
484 }
485
486
487
488
489
490
491 public DumpArchiveConstants.SEGMENT_TYPE getHeaderType() {
492 return header.getType();
493 }
494
495
496
497
498
499
500 public int getIno() {
501 return header.getIno();
502 }
503
504
505
506
507
508
509 @Override
510 public Date getLastModifiedDate() {
511 return new Date(mtime);
512 }
513
514
515
516
517
518
519 public int getMode() {
520 return mode;
521 }
522
523
524
525
526
527
528
529
530
531
532 @Override
533 public String getName() {
534 return name;
535 }
536
537
538
539
540
541
542 public int getNlink() {
543 return nlink;
544 }
545
546
547
548
549
550
551 public long getOffset() {
552 return offset;
553 }
554
555
556
557
558
559
560 String getOriginalName() {
561 return originalName;
562 }
563
564
565
566
567
568
569 public Set<PERMISSION> getPermissions() {
570 return permissions;
571 }
572
573
574
575
576
577
578 public String getSimpleName() {
579 return simpleName;
580 }
581
582
583
584
585
586
587 @Override
588 public long getSize() {
589 return isDirectory() ? SIZE_UNKNOWN : size;
590 }
591
592
593
594
595
596
597 public TYPE getType() {
598 return type;
599 }
600
601
602
603
604
605
606 public int getUserId() {
607 return uid;
608 }
609
610
611
612
613
614
615 public int getVolume() {
616 return volume;
617 }
618
619 @Override
620 public int hashCode() {
621 return ino;
622 }
623
624
625
626
627
628
629 public boolean isBlkDev() {
630 return type == TYPE.BLKDEV;
631 }
632
633
634
635
636
637
638 public boolean isChrDev() {
639 return type == TYPE.CHRDEV;
640 }
641
642
643
644
645
646
647 public boolean isDeleted() {
648 return isDeleted;
649 }
650
651
652
653
654
655
656 @Override
657 public boolean isDirectory() {
658 return type == TYPE.DIRECTORY;
659 }
660
661
662
663
664
665
666 public boolean isFifo() {
667 return type == TYPE.FIFO;
668 }
669
670
671
672
673
674
675 public boolean isFile() {
676 return type == TYPE.FILE;
677 }
678
679
680
681
682
683
684 public boolean isSocket() {
685 return type == TYPE.SOCKET;
686 }
687
688
689
690
691
692
693
694 public boolean isSparseRecord(final int idx) {
695 return (header.getCdata(idx) & 0x01) == 0;
696 }
697
698
699
700
701
702
703 public void setAccessTime(final Date atime) {
704 this.atime = atime.getTime();
705 }
706
707
708
709
710
711
712 public void setCreationTime(final Date ctime) {
713 this.ctime = ctime.getTime();
714 }
715
716
717
718
719
720
721 public void setDeleted(final boolean isDeleted) {
722 this.isDeleted = isDeleted;
723 }
724
725
726
727
728
729
730 public void setGeneration(final int generation) {
731 this.generation = generation;
732 }
733
734
735
736
737
738
739 public void setGroupId(final int gid) {
740 this.gid = gid;
741 }
742
743
744
745
746
747
748 public void setLastModifiedDate(final Date mtime) {
749 this.mtime = mtime.getTime();
750 }
751
752
753
754
755
756
757 public void setMode(final int mode) {
758 this.mode = mode & 07777;
759 this.permissions = PERMISSION.find(mode);
760 }
761
762
763
764
765
766
767 public final void setName(String name) {
768 this.originalName = name;
769 if (name != null) {
770 if (isDirectory() && !name.endsWith("/")) {
771 name += "/";
772 }
773 if (name.startsWith("./")) {
774 name = name.substring(2);
775 }
776 }
777 this.name = name;
778 }
779
780
781
782
783
784
785 public void setNlink(final int nlink) {
786 this.nlink = nlink;
787 }
788
789
790
791
792
793
794 public void setOffset(final long offset) {
795 this.offset = offset;
796 }
797
798
799
800
801
802
803 protected void setSimpleName(final String simpleName) {
804 this.simpleName = simpleName;
805 }
806
807
808
809
810
811
812 public void setSize(final long size) {
813 this.size = size;
814 }
815
816
817
818
819
820
821 public void setType(final TYPE type) {
822 this.type = type;
823 }
824
825
826
827
828
829
830 public void setUserId(final int uid) {
831 this.uid = uid;
832 }
833
834
835
836
837
838
839 public void setVolume(final int volume) {
840 this.volume = volume;
841 }
842
843 @Override
844 public String toString() {
845 return getName();
846 }
847
848
849
850
851 void update(final byte[] buffer) {
852 header.volume = DumpArchiveUtil.convert32(buffer, 16);
853 header.count = DumpArchiveUtil.convert32(buffer, 160);
854
855 header.holes = 0;
856
857 for (int i = 0; i < 512 && i < header.count; i++) {
858 if (buffer[164 + i] == 0) {
859 header.holes++;
860 }
861 }
862
863 System.arraycopy(buffer, 164, header.cdata, 0, 512);
864 }
865 }