-
Notifications
You must be signed in to change notification settings - Fork 266
/
DumpArchiveEntry.java
842 lines (744 loc) · 22.6 KB
/
DumpArchiveEntry.java
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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.compress.archivers.dump;
import java.util.Collections;
import java.util.Date;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.compress.archivers.ArchiveEntry;
/**
* This class represents an entry in a Dump archive. It consists
* of the entry's header, the entry's File and any extended attributes.
* <p>
* DumpEntries that are created from the header bytes read from
* an archive are instantiated with the DumpArchiveEntry( byte[] )
* constructor. These entries will be used when extracting from
* or listing the contents of an archive. These entries have their
* header filled in using the header bytes. They also set the File
* to null, since they reference an archive entry not a file.
* <p>
* DumpEntries can also be constructed from nothing but a name.
* This allows the programmer to construct the entry by hand, for
* instance when only an InputStream is available for writing to
* the archive, and the header information is constructed from
* other information. In this case the header fields are set to
* defaults and the File is set to null.
*
* <p>
* The C structure for a Dump Entry's header is:
* <pre>
* #define TP_BSIZE 1024 // size of each file block
* #define NTREC 10 // number of blocks to write at once
* #define HIGHDENSITYTREC 32 // number of blocks to write on high-density tapes
* #define TP_NINDIR (TP_BSIZE/2) // number if indirect inodes in record
* #define TP_NINOS (TP_NINDIR / sizeof (int32_t))
* #define LBLSIZE 16
* #define NAMELEN 64
*
* #define OFS_MAGIC (int)60011 // old format magic value
* #define NFS_MAGIC (int)60012 // new format magic value
* #define FS_UFS2_MAGIC (int)0x19540119
* #define CHECKSUM (int)84446 // constant used in checksum algorithm
*
* struct s_spcl {
* int32_t c_type; // record type (see below)
* int32_t <b>c_date</b>; // date of this dump
* int32_t <b>c_ddate</b>; // date of previous dump
* int32_t c_volume; // dump volume number
* u_int32_t c_tapea; // logical block of this record
* dump_ino_t c_ino; // number of inode
* int32_t <b>c_magic</b>; // magic number (see above)
* int32_t c_checksum; // record checksum
* #ifdef __linux__
* struct new_bsd_inode c_dinode;
* #else
* #ifdef sunos
* struct new_bsd_inode c_dinode;
* #else
* struct dinode c_dinode; // ownership and mode of inode
* #endif
* #endif
* int32_t c_count; // number of valid c_addr entries
* union u_data c_data; // see above
* char <b>c_label[LBLSIZE]</b>; // dump label
* int32_t <b>c_level</b>; // level of this dump
* char <b>c_filesys[NAMELEN]</b>; // name of dumpped file system
* char <b>c_dev[NAMELEN]</b>; // name of dumpped device
* char <b>c_host[NAMELEN]</b>; // name of dumpped host
* int32_t c_flags; // additional information (see below)
* int32_t c_firstrec; // first record on volume
* int32_t c_ntrec; // blocksize on volume
* int32_t c_extattributes; // additional inode info (see below)
* int32_t c_spare[30]; // reserved for future uses
* } s_spcl;
*
* //
* // flag values
* //
* #define DR_NEWHEADER 0x0001 // new format tape header
* #define DR_NEWINODEFMT 0x0002 // new format inodes on tape
* #define DR_COMPRESSED 0x0080 // dump tape is compressed
* #define DR_METAONLY 0x0100 // only the metadata of the inode has been dumped
* #define DR_INODEINFO 0x0002 // [SIC] TS_END header contains c_inos information
* #define DR_EXTATTRIBUTES 0x8000
*
* //
* // extattributes inode info
* //
* #define EXT_REGULAR 0
* #define EXT_MACOSFNDRINFO 1
* #define EXT_MACOSRESFORK 2
* #define EXT_XATTR 3
*
* // used for EA on tape
* #define EXT2_GOOD_OLD_INODE_SIZE 128
* #define EXT2_XATTR_MAGIC 0xEA020000 // block EA
* #define EXT2_XATTR_MAGIC2 0xEA020001 // in inode EA
* </pre>
* <p>
* The fields in <b>bold</b> are the same for all blocks. (This permitted
* multiple dumps to be written to a single tape.)
* </p>
*
* <p>
* The C structure for the inode (file) information is:
* <pre>
* struct bsdtimeval { // **** alpha-*-linux is deviant
* __u32 tv_sec;
* __u32 tv_usec;
* };
*
* #define NDADDR 12
* #define NIADDR 3
*
* //
* // This is the new (4.4) BSD inode structure
* // copied from the FreeBSD 2.0 <ufs/ufs/dinode.h> include file
* //
* struct new_bsd_inode {
* __u16 di_mode; // file type, standard Unix permissions
* __s16 di_nlink; // number of hard links to file.
* union {
* __u16 oldids[2];
* __u32 inumber;
* } di_u;
* u_quad_t di_size; // file size
* struct bsdtimeval di_atime; // time file was last accessed
* struct bsdtimeval di_mtime; // time file was last modified
* struct bsdtimeval di_ctime; // time file was created
* __u32 di_db[NDADDR];
* __u32 di_ib[NIADDR];
* __u32 di_flags; //
* __s32 di_blocks; // number of disk blocks
* __s32 di_gen; // generation number
* __u32 di_uid; // user id (see /etc/passwd)
* __u32 di_gid; // group id (see /etc/group)
* __s32 di_spare[2]; // unused
* };
* </pre>
* <p>
* It is important to note that the header DOES NOT have the name of the
* file. It can't since hard links mean that you may have multiple file names
* for a single physical file. You must read the contents of the directory
* entries to learn the mapping(s) from file name to inode.
* </p>
*
* <p>
* The C structure that indicates if a specific block is a real block
* that contains data or is a sparse block that is not persisted to the
* disk is:</p>
* <pre>
* #define TP_BSIZE 1024
* #define TP_NINDIR (TP_BSIZE/2)
*
* union u_data {
* char s_addrs[TP_NINDIR]; // 1 => data; 0 => hole in inode
* int32_t s_inos[TP_NINOS]; // table of first inode on each volume
* } u_data;
* </pre>
*
* @NotThreadSafe
*/
public class DumpArchiveEntry implements ArchiveEntry {
private String name;
private TYPE type = TYPE.UNKNOWN;
private int mode;
private Set<PERMISSION> permissions = Collections.emptySet();
private long size;
private long atime;
private long mtime;
private int uid;
private int gid;
/**
* Currently unused
*/
private final DumpArchiveSummary summary = null;
// this information is available from standard index.
private final TapeSegmentHeader header = new TapeSegmentHeader();
private String simpleName;
private String originalName;
// this information is available from QFA index
private int volume;
private long offset;
private int ino;
private int nlink;
private long ctime;
private int generation;
private boolean isDeleted;
/**
* Default constructor.
*/
public DumpArchiveEntry() {
}
/**
* Constructor taking only file name.
* @param name pathname
* @param simpleName actual file name.
*/
public DumpArchiveEntry(final String name, final String simpleName) {
setName(name);
this.simpleName = simpleName;
}
/**
* Constructor taking name, inode and type.
*
* @param name the name
* @param simpleName the simple name
* @param ino the ino
* @param type the type
*/
protected DumpArchiveEntry(final String name, final String simpleName, final int ino,
final TYPE type) {
setType(type);
setName(name);
this.simpleName = simpleName;
this.ino = ino;
this.offset = 0;
}
/**
* Returns the path of the entry.
* @return the path of the entry.
*/
public String getSimpleName() {
return simpleName;
}
/**
* Sets the path of the entry.
* @param simpleName the simple name
*/
protected void setSimpleName(final String simpleName) {
this.simpleName = simpleName;
}
/**
* Returns the ino of the entry.
* @return the ino
*/
public int getIno() {
return header.getIno();
}
/**
* Return the number of hard links to the entry.
* @return the number of hard links
*/
public int getNlink() {
return nlink;
}
/**
* Set the number of hard links.
* @param nlink the number of hard links
*/
public void setNlink(final int nlink) {
this.nlink = nlink;
}
/**
* Get file creation time.
* @return the creation time
*/
public Date getCreationTime() {
return new Date(ctime);
}
/**
* Set the file creation time.
* @param ctime the creation time
*/
public void setCreationTime(final Date ctime) {
this.ctime = ctime.getTime();
}
/**
* Return the generation of the file.
* @return the generation
*/
public int getGeneration() {
return generation;
}
/**
* Set the generation of the file.
* @param generation the generation
*/
public void setGeneration(final int generation) {
this.generation = generation;
}
/**
* Has this file been deleted? (On valid on incremental dumps.)
* @return whether the file has been deleted
*/
public boolean isDeleted() {
return isDeleted;
}
/**
* Set whether this file has been deleted.
* @param isDeleted whether the file has been deleted
*/
public void setDeleted(final boolean isDeleted) {
this.isDeleted = isDeleted;
}
/**
* Return the offset within the archive
* @return the offset
*/
public long getOffset() {
return offset;
}
/**
* Set the offset within the archive.
* @param offset the offset
*/
public void setOffset(final long offset) {
this.offset = offset;
}
/**
* Return the tape volume where this file is located.
* @return the volume
*/
public int getVolume() {
return volume;
}
/**
* Set the tape volume.
* @param volume the volume
*/
public void setVolume(final int volume) {
this.volume = volume;
}
/**
* Return the type of the tape segment header.
* @return the segment header
*/
public DumpArchiveConstants.SEGMENT_TYPE getHeaderType() {
return header.getType();
}
/**
* Return the number of records in this segment.
* @return the number of records
*/
public int getHeaderCount() {
return header.getCount();
}
/**
* Return the number of sparse records in this segment.
* @return the number of sparse records
*/
public int getHeaderHoles() {
return header.getHoles();
}
/**
* Is this a sparse record?
* @param idx index of the record to check
* @return whether this is a sparse record
*/
public boolean isSparseRecord(final int idx) {
return (header.getCdata(idx) & 0x01) == 0;
}
@Override
public int hashCode() {
return ino;
}
@Override
public boolean equals(final Object o) {
if (o == this) {
return true;
}
if (o == null || !o.getClass().equals(getClass())) {
return false;
}
final DumpArchiveEntry rhs = (DumpArchiveEntry) o;
if (ino != rhs.ino) {
return false;
}
// summary is always null right now, but this may change some day
if ((summary == null && rhs.summary != null) // NOSONAR
|| (summary != null && !summary.equals(rhs.summary))) { // NOSONAR
return false;
}
return true;
}
@Override
public String toString() {
return getName();
}
/**
* Populate the dump archive entry and tape segment header with
* the contents of the buffer.
*
* @param buffer buffer to read content from
*/
static DumpArchiveEntry parse(final byte[] buffer) {
final DumpArchiveEntry entry = new DumpArchiveEntry();
final TapeSegmentHeader header = entry.header;
header.type = DumpArchiveConstants.SEGMENT_TYPE.find(DumpArchiveUtil.convert32(
buffer, 0));
//header.dumpDate = new Date(1000L * DumpArchiveUtil.convert32(buffer, 4));
//header.previousDumpDate = new Date(1000L * DumpArchiveUtil.convert32(
// buffer, 8));
header.volume = DumpArchiveUtil.convert32(buffer, 12);
//header.tapea = DumpArchiveUtil.convert32(buffer, 16);
entry.ino = header.ino = DumpArchiveUtil.convert32(buffer, 20);
//header.magic = DumpArchiveUtil.convert32(buffer, 24);
//header.checksum = DumpArchiveUtil.convert32(buffer, 28);
final int m = DumpArchiveUtil.convert16(buffer, 32);
// determine the type of the file.
entry.setType(TYPE.find((m >> 12) & 0x0F));
// determine the standard permissions
entry.setMode(m);
entry.nlink = DumpArchiveUtil.convert16(buffer, 34);
// inumber, oldids?
entry.setSize(DumpArchiveUtil.convert64(buffer, 40));
long t = (1000L * DumpArchiveUtil.convert32(buffer, 48)) +
(DumpArchiveUtil.convert32(buffer, 52) / 1000);
entry.setAccessTime(new Date(t));
t = (1000L * DumpArchiveUtil.convert32(buffer, 56)) +
(DumpArchiveUtil.convert32(buffer, 60) / 1000);
entry.setLastModifiedDate(new Date(t));
t = (1000L * DumpArchiveUtil.convert32(buffer, 64)) +
(DumpArchiveUtil.convert32(buffer, 68) / 1000);
entry.ctime = t;
// db: 72-119 - direct blocks
// id: 120-131 - indirect blocks
//entry.flags = DumpArchiveUtil.convert32(buffer, 132);
//entry.blocks = DumpArchiveUtil.convert32(buffer, 136);
entry.generation = DumpArchiveUtil.convert32(buffer, 140);
entry.setUserId(DumpArchiveUtil.convert32(buffer, 144));
entry.setGroupId(DumpArchiveUtil.convert32(buffer, 148));
// two 32-bit spare values.
header.count = DumpArchiveUtil.convert32(buffer, 160);
header.holes = 0;
for (int i = 0; (i < 512) && (i < header.count); i++) {
if (buffer[164 + i] == 0) {
header.holes++;
}
}
System.arraycopy(buffer, 164, header.cdata, 0, 512);
entry.volume = header.getVolume();
//entry.isSummaryOnly = false;
return entry;
}
/**
* Update entry with information from next tape segment header.
*/
void update(final byte[] buffer) {
header.volume = DumpArchiveUtil.convert32(buffer, 16);
header.count = DumpArchiveUtil.convert32(buffer, 160);
header.holes = 0;
for (int i = 0; (i < 512) && (i < header.count); i++) {
if (buffer[164 + i] == 0) {
header.holes++;
}
}
System.arraycopy(buffer, 164, header.cdata, 0, 512);
}
/**
* Archive entry as stored on tape. There is one TSH for (at most)
* every 512k in the file.
*/
static class TapeSegmentHeader {
private DumpArchiveConstants.SEGMENT_TYPE type;
private int volume;
private int ino;
private int count;
private int holes;
private final byte[] cdata = new byte[512]; // map of any 'holes'
public DumpArchiveConstants.SEGMENT_TYPE getType() {
return type;
}
public int getVolume() {
return volume;
}
public int getIno() {
return ino;
}
void setIno(final int ino) {
this.ino = ino;
}
public int getCount() {
return count;
}
public int getHoles() {
return holes;
}
public int getCdata(final int idx) {
return cdata[idx];
}
}
/**
* Returns the name of the entry.
*
* <p>This method returns the raw name as it is stored inside of the archive.</p>
*
* @return the name of the entry.
*/
@Override
public String getName() {
return name;
}
/**
* Returns the unmodified name of the entry.
* @return the name of the entry.
*/
String getOriginalName() {
return originalName;
}
/**
* Sets the name of the entry.
* @param name the name
*/
public final void setName(String name) {
this.originalName = name;
if (name != null) {
if (isDirectory() && !name.endsWith("/")) {
name += "/";
}
if (name.startsWith("./")) {
name = name.substring(2);
}
}
this.name = name;
}
/**
* The last modified date.
* @return the last modified date
*/
@Override
public Date getLastModifiedDate() {
return new Date(mtime);
}
/**
* Is this a directory?
* @return whether this is a directory
*/
@Override
public boolean isDirectory() {
return type == TYPE.DIRECTORY;
}
/**
* Is this a regular file?
* @return whether this is a regular file
*/
public boolean isFile() {
return type == TYPE.FILE;
}
/**
* Is this a network device?
* @return whether this is a socket
*/
public boolean isSocket() {
return type == TYPE.SOCKET;
}
/**
* Is this a character device?
* @return whether this is a character device
*/
public boolean isChrDev() {
return type == TYPE.CHRDEV;
}
/**
* Is this a block device?
* @return whether this is a block device
*/
public boolean isBlkDev() {
return type == TYPE.BLKDEV;
}
/**
* Is this a fifo/pipe?
* @return whether this is a fifo
*/
public boolean isFifo() {
return type == TYPE.FIFO;
}
/**
* Get the type of the entry.
* @return the type
*/
public TYPE getType() {
return type;
}
/**
* Set the type of the entry.
* @param type the type
*/
public void setType(final TYPE type) {
this.type = type;
}
/**
* Return the access permissions on the entry.
* @return the access permissions
*/
public int getMode() {
return mode;
}
/**
* Set the access permissions on the entry.
* @param mode the access permissions
*/
public void setMode(final int mode) {
this.mode = mode & 07777;
this.permissions = PERMISSION.find(mode);
}
/**
* Returns the permissions on the entry.
* @return the permissions
*/
public Set<PERMISSION> getPermissions() {
return permissions;
}
/**
* Returns the size of the entry.
* @return the size
*/
@Override
public long getSize() {
return isDirectory() ? SIZE_UNKNOWN : size;
}
/**
* Returns the size of the entry as read from the archive.
*/
long getEntrySize() {
return size;
}
/**
* Set the size of the entry.
* @param size the size
*/
public void setSize(final long size) {
this.size = size;
}
/**
* Set the time the file was last modified.
* @param mtime the last modified time
*/
public void setLastModifiedDate(final Date mtime) {
this.mtime = mtime.getTime();
}
/**
* Returns the time the file was last accessed.
* @return the access time
*/
public Date getAccessTime() {
return new Date(atime);
}
/**
* Set the time the file was last accessed.
* @param atime the access time
*/
public void setAccessTime(final Date atime) {
this.atime = atime.getTime();
}
/**
* Return the user id.
* @return the user id
*/
public int getUserId() {
return uid;
}
/**
* Set the user id.
* @param uid the user id
*/
public void setUserId(final int uid) {
this.uid = uid;
}
/**
* Return the group id
* @return the group id
*/
public int getGroupId() {
return gid;
}
/**
* Set the group id.
* @param gid the group id
*/
public void setGroupId(final int gid) {
this.gid = gid;
}
public enum TYPE {
WHITEOUT(14),
SOCKET(12),
LINK(10),
FILE(8),
BLKDEV(6),
DIRECTORY(4),
CHRDEV(2),
FIFO(1),
UNKNOWN(15);
private final int code;
TYPE(final int code) {
this.code = code;
}
public static TYPE find(final int code) {
TYPE type = UNKNOWN;
for (final TYPE t : TYPE.values()) {
if (code == t.code) {
type = t;
}
}
return type;
}
}
public enum PERMISSION {
SETUID(04000),
SETGUI(02000),
STICKY(01000),
USER_READ(00400),
USER_WRITE(00200),
USER_EXEC(00100),
GROUP_READ(00040),
GROUP_WRITE(00020),
GROUP_EXEC(00010),
WORLD_READ(00004),
WORLD_WRITE(00002),
WORLD_EXEC(00001);
private final int code;
PERMISSION(final int code) {
this.code = code;
}
public static Set<PERMISSION> find(final int code) {
final Set<PERMISSION> set = new HashSet<>();
for (final PERMISSION p : PERMISSION.values()) {
if ((code & p.code) == p.code) {
set.add(p);
}
}
if (set.isEmpty()) {
return Collections.emptySet();
}
return EnumSet.copyOf(set);
}
}
}