You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by bo...@apache.org on 2011/08/15 12:47:05 UTC
svn commit: r1157769 - in /commons/proper/compress/trunk: ./
src/main/java/org/apache/commons/compress/archivers/dump/
Author: bodewig
Date: Mon Aug 15 10:47:04 2011
New Revision: 1157769
URL: http://svn.apache.org/viewvc?rev=1157769&view=rev
Log:
Initial code base for read-only Unix dump support contributed by Bear Giles. COMPRESS-132
Added:
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/Dirent.java (with props)
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveConstants.java (with props)
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveEntry.java (with props)
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveException.java (with props)
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStream.java (with props)
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveSummary.java (with props)
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveUtil.java (with props)
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/InvalidFormatException.java (with props)
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/ShortFileException.java (with props)
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/TapeInputStream.java (with props)
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/UnrecognizedFormatException.java (with props)
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/UnsupportedCompressionAlgorithmException.java (with props)
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/package.html (with props)
Modified:
commons/proper/compress/trunk/pom.xml
Modified: commons/proper/compress/trunk/pom.xml
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/pom.xml?rev=1157769&r1=1157768&r2=1157769&view=diff
==============================================================================
--- commons/proper/compress/trunk/pom.xml (original)
+++ commons/proper/compress/trunk/pom.xml Mon Aug 15 10:47:04 2011
@@ -89,6 +89,10 @@
<name>Christian Kohlschütte</name>
<email>ck@newsclub.de</email>
</contributor>
+ <contributor>
+ <name>Bear Giles</name>
+ <email>bgiles@coyotesong.com</email>
+ </contributor>
</contributors>
<scm>
Added: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/Dirent.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/Dirent.java?rev=1157769&view=auto
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/Dirent.java (added)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/Dirent.java Mon Aug 15 10:47:04 2011
@@ -0,0 +1,85 @@
+/*
+ * 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;
+
+
+/**
+ * Directory entry.
+ */
+public class Dirent {
+ private int ino;
+ private int parentIno;
+ private int type;
+ private String name;
+
+ /**
+ * Constructor
+ *
+ * @param ino
+ * @param parentIno
+ * @param type
+ * @param name
+ */
+ public Dirent(int ino, int parentIno, int type, String name) {
+ this.ino = ino;
+ this.parentIno = parentIno;
+ this.type = type;
+ this.name = name;
+ }
+
+ /**
+ * Get ino.
+ * @return
+ */
+ public int getIno() {
+ return ino;
+ }
+
+ /**
+ * Get ino of parent directory.
+ * @return
+ */
+ public int getParentIno() {
+ return parentIno;
+ }
+
+ /**
+ * Get entry type.
+ * @return
+ */
+ public int getType() {
+ return type;
+ }
+
+ /**
+ * Get name of directory entry.
+ * @return
+ */
+ public String getName() {
+ return name;
+ }
+
+ /**
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString() {
+ return String.format("[%d]: %s", ino, name);
+ }
+}
Propchange: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/Dirent.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveConstants.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveConstants.java?rev=1157769&view=auto
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveConstants.java (added)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveConstants.java Mon Aug 15 10:47:04 2011
@@ -0,0 +1,91 @@
+/*
+ * 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;
+
+/**
+ * Various constants associated with dump archives.
+ */
+public final class DumpArchiveConstants {
+ public static final int TP_SIZE = 1024;
+ public static final int NTREC = 10;
+ public static final int HIGH_DENSITY_NTREC = 32;
+ public static final int OFS_MAGIC = 60011;
+ public static final int NFS_MAGIC = 60012;
+ public static final int FS_UFS2_MAGIC = 0x19540119;
+ public static final int CHECKSUM = 84446;
+ public static final int LBLSIZE = 16;
+ public static final int NAMELEN = 64;
+
+ /* do not instantiate */
+ private DumpArchiveConstants() {
+ }
+
+ /**
+ * The type of tape segment.
+ */
+ public enum SEGMENT_TYPE {
+ TAPE(1),
+ INODE(2),
+ BITS(3),
+ ADDR(4),
+ END(5),
+ CLRI(6);
+
+ int code;
+
+ private SEGMENT_TYPE(int code) {
+ this.code = code;
+ }
+
+ public static SEGMENT_TYPE find(int code) {
+ for (SEGMENT_TYPE t : values()) {
+ if (t.code == code) {
+ return t;
+ }
+ }
+
+ return null;
+ }
+ }
+
+ /**
+ * The type of compression.
+ */
+ public enum COMPRESSION_TYPE {
+ ZLIB(0),
+ BZLIB(1),
+ LZO(2);
+
+ int code;
+
+ private COMPRESSION_TYPE(int code) {
+ this.code = code;
+ }
+
+ public static COMPRESSION_TYPE find(int code) {
+ for (COMPRESSION_TYPE t : values()) {
+ if (t.code == code) {
+ return t;
+ }
+ }
+
+ return null;
+ }
+ }
+}
Propchange: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveConstants.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveEntry.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveEntry.java?rev=1157769&view=auto
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveEntry.java (added)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveEntry.java Mon Aug 15 10:47:04 2011
@@ -0,0 +1,780 @@
+/*
+ * 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>
+ * 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>
+ * 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 filenames
+ * for a single physical file. You must read the contents of the directory
+ * entries to learn the mapping(s) from filename 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:
+ * <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></p>
+ *
+ * @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 Date atime;
+ private Date mtime;
+ private int uid;
+ private int gid;
+
+ private DumpArchiveSummary summary = null;
+
+ // this information is available from standard index.
+ private TapeSegmentHeader header = new TapeSegmentHeader();
+ private String simpleName;
+
+ // this information is available from QFA index
+ private int volume;
+ private long offset;
+ private int ino;
+ private int nlink;
+ private Date ctime;
+ private int generation;
+ private boolean isDeleted;
+
+ /**
+ * Default constructor.
+ */
+ public DumpArchiveEntry() {
+ }
+
+ /**
+ * Constructor taking only filename.
+ * @param name pathname
+ * @param simpleName actual filename.
+ */
+ public DumpArchiveEntry(String name, String simpleName) {
+ this.name = name;
+ this.simpleName = simpleName;
+ }
+
+ /**
+ * Constructor taking name, inode and type.
+ *
+ * @param name
+ * @param simpleName
+ * @param ino
+ * @param type
+ */
+ protected DumpArchiveEntry(String name, String simpleName, int ino,
+ TYPE type) {
+ this(name, simpleName);
+ setType(type);
+ this.ino = ino;
+ this.offset = 0;
+ }
+
+ /**
+ * Constructor taking tape buffer.
+ * @param buffer
+ * @param offset
+ */
+
+ /**
+ * Returns the path of the entry.
+ * @return
+ */
+ public String getSimpleName() {
+ return simpleName;
+ }
+
+ /**
+ * Sets the path of the entry.
+ * @return
+ */
+ protected void setSimpleName(String simpleName) {
+ this.simpleName = simpleName;
+ }
+
+ /**
+ * Returns the ino of the entry.
+ */
+ public int getIno() {
+ return header.getIno();
+ }
+
+ /**
+ * Return the number of hard links to the entry.
+ */
+ public int getNlink() {
+ return nlink;
+ }
+
+ /**
+ * Set the number of hard links.
+ */
+ public void setNlink(int nlink) {
+ this.nlink = nlink;
+ }
+
+ /**
+ * Get file creation time.
+ */
+ public Date getCreationTime() {
+ return ctime;
+ }
+
+ /**
+ * Set the file creation time.
+ */
+ public void setCreationTime(Date ctime) {
+ this.ctime = ctime;
+ }
+
+ /**
+ * Return the generation of the file.
+ */
+ public int getGeneration() {
+ return generation;
+ }
+
+ /**
+ * Set the generation of the file.
+ */
+ public void setGeneration(int generation) {
+ this.generation = generation;
+ }
+
+ /**
+ * Has this file been deleted? (On valid on incremental dumps.)
+ */
+ public boolean isDeleted() {
+ return isDeleted;
+ }
+
+ /**
+ * Set whether this file has been deleted.
+ */
+ public void setDeleted(boolean isDeleted) {
+ this.isDeleted = isDeleted;
+ }
+
+ /**
+ * Return the offset within the archive
+ */
+ public long getOffset() {
+ return offset;
+ }
+
+ /**
+ * Set the offset within the archive.
+ */
+ public void setOffset(long offset) {
+ this.offset = offset;
+ }
+
+ /**
+ * Return the tape volume where this file is located.
+ */
+ public int getVolume() {
+ return volume;
+ }
+
+ /**
+ * Set the tape volume.
+ */
+ public void setVolume(int volume) {
+ this.volume = volume;
+ }
+
+ /**
+ * Return the type of the tape segment header.
+ */
+ public DumpArchiveConstants.SEGMENT_TYPE getHeaderType() {
+ return header.getType();
+ }
+
+ /**
+ * Return the number of records in this segment.
+ */
+ public int getHeaderCount() {
+ return header.getCount();
+ }
+
+ /**
+ * Return the number of sparse records in this segment.
+ */
+ public int getHeaderHoles() {
+ return header.getHoles();
+ }
+
+ /**
+ * Is this a sparse record?
+ */
+ public boolean isSparseRecord(int idx) {
+ return (header.getCdata(idx) & 0x01) == 0;
+ }
+
+ /**
+ * @see java.lang.Object#hashCode()
+ */
+ @Override
+ public int hashCode() {
+ return ino;
+ }
+
+ /**
+ * @see java.lang.Object#equals(Object o)
+ */
+ @Override
+ public boolean equals(Object o) {
+ if (o == this) {
+ return true;
+ } else if (!o.getClass().equals(getClass())) {
+ return false;
+ }
+
+ DumpArchiveEntry rhs = (DumpArchiveEntry) o;
+
+ if ((header == null) || (rhs.header == null)) {
+ return false;
+ }
+
+ if (ino != rhs.ino) {
+ return false;
+ }
+
+ if ((summary != null) || summary.equals(rhs.summary)) {
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString() {
+ return getName();
+ }
+
+ /**
+ * Populate the dump archive entry and tape segment header with
+ * the contents of the buffer.
+ *
+ * @param buffer
+ * @throws Exception
+ */
+ public static DumpArchiveEntry parse(byte[] buffer) {
+ DumpArchiveEntry entry = new DumpArchiveEntry();
+ 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);
+ 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 = new Date(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.
+ */
+ public void update(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.
+ */
+ public static class TapeSegmentHeader {
+ private DumpArchiveConstants.SEGMENT_TYPE type;
+ private int volume;
+ private int ino;
+ private int count;
+ private int holes;
+ private 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(int ino) {
+ this.ino = ino;
+ }
+
+ public int getCount() {
+ return count;
+ }
+
+ public int getHoles() {
+ return holes;
+ }
+
+ public int getCdata(int idx) {
+ return cdata[idx];
+ }
+ }
+
+ /**
+ * Returns the name of the entry.
+ * @return
+ */
+ public String getName() {
+ return name;
+ }
+
+ /**
+ * Sets the name of the entry.
+ * @return
+ */
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ /** {@inheritDoc} */
+ public Date getLastModifiedDate() {
+ return mtime;
+ }
+
+ /**
+ * Is this a directory?
+ */
+ public boolean isDirectory() {
+ return type == TYPE.DIRECTORY;
+ }
+
+ /**
+ * Is this a regular file?
+ */
+ public boolean isFile() {
+ return type == TYPE.FILE;
+ }
+
+ /**
+ * Is this a network device?
+ */
+ public boolean isSocket() {
+ return type == TYPE.SOCKET;
+ }
+
+ /**
+ * Is this a character device?
+ */
+ public boolean isChrDev() {
+ return type == TYPE.CHRDEV;
+ }
+
+ /**
+ * Is this a block device?
+ */
+ public boolean isBlkDev() {
+ return type == TYPE.BLKDEV;
+ }
+
+ /**
+ * Is this a fifo/pipe?
+ */
+ public boolean isFifo() {
+ return type == TYPE.FIFO;
+ }
+
+ /**
+ * Get the type of the entry.
+ */
+ public TYPE getType() {
+ return type;
+ }
+
+ /**
+ * Set the type of the entry.
+ */
+ public void setType(TYPE type) {
+ this.type = type;
+ }
+
+ /**
+ * Return the access permissions on the entry.
+ */
+ public int getMode() {
+ return mode;
+ }
+
+ /**
+ * Set the access permissions on the entry.
+ */
+ public void setMode(int mode) {
+ this.mode = mode & 07777;
+ this.permissions = PERMISSION.find(mode);
+ }
+
+ /**
+ * Returns the permissions on the entry.
+ */
+ public Set<PERMISSION> getPermissions() {
+ return permissions;
+ }
+
+ /**
+ * Returns the size of the entry.
+ */
+ public long getSize() {
+ return size;
+ }
+
+ /**
+ * Set the size of the entry.
+ */
+ public void setSize(long size) {
+ this.size = size;
+ }
+
+ /**
+ * Set the time the file was last modified.
+ */
+ public void setLastModifiedDate(Date mtime) {
+ this.mtime = mtime;
+ }
+
+ /**
+ * Returns the time the file was last accessed.
+ */
+ public Date getAccessTime() {
+ return atime;
+ }
+
+ /**
+ * Set the time the file was last accessed.
+ */
+ public void setAccessTime(Date atime) {
+ this.atime = atime;
+ }
+
+ /**
+ * Return the user id.
+ */
+ public int getUserId() {
+ return uid;
+ }
+
+ /**
+ * Set the user id.
+ */
+ public void setUserId(int uid) {
+ this.uid = uid;
+ }
+
+ /**
+ * Return the group id
+ */
+ public int getGroupId() {
+ return gid;
+ }
+
+ /**
+ * Set the group id.
+ */
+ public void setGroupId(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 int code;
+
+ private TYPE(int code) {
+ this.code = code;
+ }
+
+ public static TYPE find(int code) {
+ TYPE type = UNKNOWN;
+
+ for (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 int code;
+
+ private PERMISSION(int code) {
+ this.code = code;
+ }
+
+ public static Set<PERMISSION> find(int code) {
+ Set<PERMISSION> set = new HashSet<PERMISSION>();
+
+ for (PERMISSION p : PERMISSION.values()) {
+ if ((code & p.code) == p.code) {
+ set.add(p);
+ }
+ }
+
+ if (set.isEmpty()) {
+ return Collections.emptySet();
+ }
+
+ return EnumSet.copyOf(set);
+ }
+ }
+}
Propchange: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveEntry.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveException.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveException.java?rev=1157769&view=auto
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveException.java (added)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveException.java Mon Aug 15 10:47:04 2011
@@ -0,0 +1,44 @@
+/*
+ * 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.io.IOException;
+
+
+/**
+ * Dump Archive Exception
+ */
+public class DumpArchiveException extends IOException {
+ private static final long serialVersionUID = 1L;
+
+ public DumpArchiveException() {
+ }
+
+ public DumpArchiveException(String msg) {
+ super(msg);
+ }
+
+ public DumpArchiveException(Throwable cause) {
+ super(cause);
+ }
+
+ public DumpArchiveException(String msg, Throwable cause) {
+ super(msg, cause);
+ }
+}
Propchange: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveException.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStream.java?rev=1157769&view=auto
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStream.java (added)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStream.java Mon Aug 15 10:47:04 2011
@@ -0,0 +1,480 @@
+/*
+ * 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 org.apache.commons.compress.archivers.ArchiveInputStream;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.PriorityQueue;
+import java.util.Queue;
+import java.util.Stack;
+
+
+/**
+ * The DumpArchiveInputStream reads a UNIX dump archive as an InputStream.
+ * Methods are provided to position at each successive entry in
+ * the archive, and the read each entry as a normal input stream
+ * using read().
+ *
+ * @NotThreadSafe
+ */
+public class DumpArchiveInputStream extends ArchiveInputStream {
+ private DumpArchiveSummary summary;
+ private DumpArchiveEntry active;
+ private boolean isClosed;
+ private boolean hasHitEOF;
+ private long entrySize;
+ private long entryOffset;
+ private int readIdx;
+ private byte[] readBuf = new byte[DumpArchiveConstants.TP_SIZE];
+ private byte[] blockBuffer;
+ private int recordOffset;
+ private long filepos;
+ protected TapeInputStream raw;
+
+ // map of ino -> dirent entry. We can use this to reconstruct full paths.
+ private Map<Integer, Dirent> names = new HashMap<Integer, Dirent>();
+
+ // map of ino -> (directory) entry when we're missing one or more elements in the path.
+ private Map<Integer, DumpArchiveEntry> pending = new HashMap<Integer, DumpArchiveEntry>();
+
+ // queue of (directory) entries where we now have the full path.
+ private Queue<DumpArchiveEntry> queue;
+
+ /**
+ * Constructor.
+ *
+ * @param is
+ * @throws Exception
+ */
+ public DumpArchiveInputStream(InputStream is) throws IOException {
+ this.raw = new TapeInputStream(is);
+ this.hasHitEOF = false;
+
+ // read header, verify it's a dump archive.
+ byte[] headerBytes = raw.readRecord();
+
+ if (!DumpArchiveUtil.verify(headerBytes)) {
+ throw new UnrecognizedFormatException();
+ }
+
+ // get summary information
+ summary = new DumpArchiveSummary(headerBytes);
+
+ // reset buffer with actual block size.
+ raw.resetBlockSize(summary.getNTRec(), summary.isCompressed());
+
+ // allocate our read buffer.
+ blockBuffer = new byte[4 * DumpArchiveConstants.TP_SIZE];
+
+ // skip past CLRI and BITS segments since we don't handle them yet.
+ readCLRI();
+ readBITS();
+
+ // put in a dummy record for the root node.
+ Dirent root = new Dirent(2, 2, 4, ".");
+ names.put(2, root);
+
+ // use priority based on queue to ensure parent directories are
+ // released first.
+ queue = new PriorityQueue<DumpArchiveEntry>(10,
+ new Comparator<DumpArchiveEntry>() {
+ public int compare(DumpArchiveEntry p, DumpArchiveEntry q) {
+ if ((p.getName() == null) || (q.getName() == null)) {
+ return Integer.MAX_VALUE;
+ }
+
+ return p.getName().compareTo(q.getName());
+ }
+ });
+ }
+
+ /**
+ * Return the archive summary information.
+ */
+ public DumpArchiveSummary getSummary() {
+ return summary;
+ }
+
+ /**
+ * Read CLRI (deleted inode) segment.
+ */
+ public void readCLRI() throws IOException {
+ byte[] readBuf = raw.readRecord();
+
+ if (!DumpArchiveUtil.verify(readBuf)) {
+ throw new InvalidFormatException();
+ }
+
+ active = DumpArchiveEntry.parse(readBuf);
+
+ if (DumpArchiveConstants.SEGMENT_TYPE.CLRI != active.getHeaderType()) {
+ throw new InvalidFormatException();
+ }
+
+ // we don't do anything with this yet.
+ raw.skip(DumpArchiveConstants.TP_SIZE * active.getHeaderCount());
+ readIdx = active.getHeaderCount();
+ }
+
+ /**
+ * Read BITS segment.
+ */
+ public void readBITS() throws IOException {
+ byte[] readBuf = raw.readRecord();
+
+ if (!DumpArchiveUtil.verify(readBuf)) {
+ throw new InvalidFormatException();
+ }
+
+ active = DumpArchiveEntry.parse(readBuf);
+
+ if (DumpArchiveConstants.SEGMENT_TYPE.BITS != active.getHeaderType()) {
+ throw new InvalidFormatException();
+ }
+
+ // we don't do anything with this yet.
+ raw.skip(DumpArchiveConstants.TP_SIZE * active.getHeaderCount());
+ readIdx = active.getHeaderCount();
+ }
+
+ /**
+ * Read the next entry.
+ */
+ public DumpArchiveEntry getNextEntry() throws IOException {
+ DumpArchiveEntry entry = null;
+ String path = null;
+
+ // is there anything in the queue?
+ if (!queue.isEmpty()) {
+ return queue.remove();
+ }
+
+ while (entry == null) {
+ if (hasHitEOF) {
+ return null;
+ }
+
+ // skip any remaining records in this segment for prior file.
+ // we might still have holes... easiest to do it
+ // block by block. We may want to revisit this if
+ // the unnecessary decompression time adds up.
+ while (readIdx < active.getHeaderCount()) {
+ if (!active.isSparseRecord(readIdx++)) {
+ raw.skip(DumpArchiveConstants.TP_SIZE);
+ }
+ }
+
+ readIdx = 0;
+ filepos = raw.getBytesRead();
+
+ byte[] headerBytes = raw.readRecord();
+
+ if (!DumpArchiveUtil.verify(headerBytes)) {
+ throw new InvalidFormatException();
+ }
+
+ active = DumpArchiveEntry.parse(headerBytes);
+
+ // skip any remaining segments for prior file.
+ while (DumpArchiveConstants.SEGMENT_TYPE.ADDR == active.getHeaderType()) {
+ raw.skip(DumpArchiveConstants.TP_SIZE * (active.getHeaderCount() -
+ active.getHeaderHoles()));
+
+ filepos = raw.getBytesRead();
+ headerBytes = raw.readRecord();
+
+ if (!DumpArchiveUtil.verify(headerBytes)) {
+ throw new InvalidFormatException();
+ }
+
+ active = DumpArchiveEntry.parse(headerBytes);
+ }
+
+ // check if this is an end-of-volume marker.
+ if (DumpArchiveConstants.SEGMENT_TYPE.END == active.getHeaderType()) {
+ hasHitEOF = true;
+ raw.close();
+
+ return null;
+ }
+
+ entry = active;
+
+ if (entry.isDirectory()) {
+ readDirectoryEntry(active);
+
+ // now we create an empty InputStream.
+ entryOffset = 0;
+ entrySize = 0;
+ readIdx = active.getHeaderCount();
+ } else {
+ entryOffset = 0;
+ entrySize = active.getSize();
+ readIdx = 0;
+ }
+
+ recordOffset = readBuf.length;
+
+ path = getPath(entry);
+
+ if (path == null) {
+ entry = null;
+ }
+ }
+
+ entry.setName(path);
+ entry.setSimpleName(names.get(entry.getIno()).getName());
+ entry.setOffset(filepos);
+
+ isClosed = false;
+
+ return entry;
+ }
+
+ /**
+ * Read directory entry.
+ */
+ public void readDirectoryEntry(DumpArchiveEntry entry)
+ throws IOException {
+ long size = entry.getSize();
+ boolean first = true;
+
+ while (first ||
+ (DumpArchiveConstants.SEGMENT_TYPE.ADDR == entry.getHeaderType())) {
+ // read the header that we just peeked at.
+ if (!first) {
+ raw.readRecord();
+ }
+
+ if (!names.containsKey(entry.getIno()) &&
+ (DumpArchiveConstants.SEGMENT_TYPE.INODE == entry.getHeaderType())) {
+ pending.put(entry.getIno(), entry);
+ }
+
+ int datalen = DumpArchiveConstants.TP_SIZE * entry.getHeaderCount();
+
+ if (blockBuffer.length < datalen) {
+ blockBuffer = new byte[datalen];
+ }
+
+ raw.read(blockBuffer, 0, datalen);
+
+ int reclen = 0;
+
+ for (int i = 0; (i < (datalen - 8)) && (i < (size - 8));
+ i += reclen) {
+ int ino = DumpArchiveUtil.convert32(blockBuffer, i);
+ reclen = DumpArchiveUtil.convert16(blockBuffer, i + 4);
+
+ byte type = blockBuffer[i + 6];
+
+ String name = new String(blockBuffer, i + 8, blockBuffer[i + 7]);
+
+ if (".".equals(name) || "..".equals(name)) {
+ // do nothing...
+ continue;
+ }
+
+ Dirent d = new Dirent(ino, entry.getIno(), type, name);
+
+ if ((type == 4) && names.containsKey(ino)) {
+ //System.out.println("we already have ino: " +
+ // names.get(ino));
+ }
+
+ names.put(ino, d);
+
+ // check whether this allows us to fill anything in the pending list.
+ for (Map.Entry<Integer, DumpArchiveEntry> e : pending.entrySet()) {
+ String path = getPath(e.getValue());
+
+ if (path != null) {
+ e.getValue().setName(path);
+ e.getValue()
+ .setSimpleName(names.get(e.getKey()).getName());
+ queue.add(e.getValue());
+ }
+ }
+
+ // remove anything that we found. (We can't do it earlier
+ // because of concurrent modification exceptions.)
+ for (DumpArchiveEntry e : queue) {
+ pending.remove(e.getIno());
+ }
+ }
+
+ byte[] peekBytes = raw.peek();
+
+ if (!DumpArchiveUtil.verify(peekBytes)) {
+ throw new InvalidFormatException();
+ }
+
+ entry = DumpArchiveEntry.parse(peekBytes);
+ first = false;
+ size -= DumpArchiveConstants.TP_SIZE;
+ }
+ }
+
+ /**
+ * Get full path for specified archive entry, or null if there's a gap.
+ *
+ * @param entry
+ * @return
+ */
+ public String getPath(DumpArchiveEntry entry) {
+ // build the stack of elements. It's possible that we're
+ // still missing an intermediate value and if so we
+ Stack<String> elements = new Stack<String>();
+ Dirent dirent = null;
+
+outer:
+ for (int i = entry.getIno();; i = dirent.getParentIno()) {
+ if (!names.containsKey(i)) {
+ elements.clear();
+
+ break outer;
+ }
+
+ dirent = names.get(i);
+ elements.push(dirent.getName());
+
+ if (dirent.getIno() == dirent.getParentIno()) {
+ break;
+ }
+ }
+
+ // if an element is missing defer the work and read next entry.
+ if (elements.isEmpty()) {
+ pending.put(entry.getIno(), entry);
+
+ return null;
+ }
+
+ // generate full path from stack of elements.
+ StringBuilder sb = new StringBuilder(elements.pop());
+
+ while (!elements.isEmpty()) {
+ sb.append('/');
+ sb.append(elements.pop());
+ }
+
+ return sb.toString();
+ }
+
+ /**
+ * Reads bytes from the current dump archive entry.
+ *
+ * This method is aware of the boundaries of the current
+ * entry in the archive and will deal with them as if they
+ * were this stream's start and EOF.
+ *
+ * @param buf The buffer into which to place bytes read.
+ * @param off The offset at which to place bytes read.
+ * @param len The number of bytes to read.
+ * @return The number of bytes read, or -1 at EOF.
+ * @throws IOException on error
+ */
+ public int read(byte[] buf, int off, int len) throws IOException {
+ int totalRead = 0;
+
+ if (isClosed || (entryOffset >= entrySize)) {
+ return -1;
+ }
+
+ if ((len + entryOffset) > entrySize) {
+ len = (int) (entrySize - entryOffset);
+ }
+
+ while (len > 0) {
+ int sz = (len > (readBuf.length - recordOffset))
+ ? (readBuf.length - recordOffset) : len;
+
+ // copy any data we have
+ if ((recordOffset + sz) <= readBuf.length) {
+ System.arraycopy(readBuf, recordOffset, buf, off, sz);
+ totalRead += sz;
+ recordOffset += sz;
+ len -= sz;
+ off += sz;
+ }
+
+ // load next block if necessary.
+ if (len > 0) {
+ if (readIdx >= 512) {
+ byte[] headerBytes = raw.readRecord();
+
+ if (!DumpArchiveUtil.verify(headerBytes)) {
+ throw new InvalidFormatException();
+ }
+
+ active = DumpArchiveEntry.parse(headerBytes);
+ readIdx = 0;
+ }
+
+ if (!active.isSparseRecord(readIdx++)) {
+ raw.read(readBuf, 0, readBuf.length);
+ } else {
+ Arrays.fill(readBuf, (byte) 0);
+ }
+
+ recordOffset = 0;
+ }
+ }
+
+ entryOffset += totalRead;
+
+ return totalRead;
+ }
+
+ /**
+ * Closes the stream for this entry.
+ */
+ @Override
+ public void close() throws IOException {
+ isClosed = true;
+ }
+
+ /**
+ * Look at the first few bytes of the file to decide if it's a dump
+ * archive. With 32 bytes we can look at the magic value, with a full
+ * 1k we can verify the checksum.
+ */
+ public static boolean matches(byte[] buffer, int length) {
+ // do we have enough of the header?
+ if (length < 32) {
+ return false;
+ }
+
+ // this is the best test
+ if (length >= DumpArchiveConstants.TP_SIZE) {
+ return DumpArchiveUtil.verify(buffer);
+ }
+
+ // this will work in a pinch.
+ return DumpArchiveConstants.NFS_MAGIC == DumpArchiveUtil.convert32(buffer,
+ 7);
+ }
+}
Propchange: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStream.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveSummary.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveSummary.java?rev=1157769&view=auto
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveSummary.java (added)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveSummary.java Mon Aug 15 10:47:04 2011
@@ -0,0 +1,335 @@
+/*
+ * 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.Date;
+
+
+/**
+ * This class represents identifying information about a Dump archive volume.
+ * It consists the archive's dump date, label, hostname, device name and possibly
+ * last mount point plus the volume's volume id andfirst record number.
+ *
+ * For the corresponding C structure see the header of {@link DumpArchiveEntry}.
+ */
+public class DumpArchiveSummary {
+ private Date dumpDate;
+ private Date previousDumpDate;
+ private int volume;
+ private String label;
+ private int level;
+ private String filesys;
+ private String devname;
+ private String hostname;
+ private int flags;
+ private int firstrec;
+ private int ntrec;
+
+ public DumpArchiveSummary(byte[] buffer) {
+ dumpDate = new Date(1000L * DumpArchiveUtil.convert32(buffer, 4));
+ previousDumpDate = new Date(1000L * DumpArchiveUtil.convert32(buffer, 8));
+ volume = DumpArchiveUtil.convert32(buffer, 12);
+ label = new String(buffer, 676, DumpArchiveConstants.LBLSIZE).trim();
+ level = DumpArchiveUtil.convert32(buffer, 692);
+ filesys = new String(buffer, 696, DumpArchiveConstants.NAMELEN).trim();
+ devname = new String(buffer, 760, DumpArchiveConstants.NAMELEN).trim();
+ hostname = new String(buffer, 824, DumpArchiveConstants.NAMELEN).trim();
+ flags = DumpArchiveUtil.convert32(buffer, 888);
+ firstrec = DumpArchiveUtil.convert32(buffer, 892);
+ ntrec = DumpArchiveUtil.convert32(buffer, 896);
+
+ //extAttributes = DumpArchiveUtil.convert32(buffer, 900);
+ }
+
+ /**
+ * Get the date of this dump.
+ * @return
+ */
+ public Date getDumpDate() {
+ return dumpDate;
+ }
+
+ /**
+ * Set dump date.
+ */
+ public void setDumpDate(Date dumpDate) {
+ this.dumpDate = dumpDate;
+ }
+
+ /**
+ * Get the date of the previous dump at this level higher.
+ * @return dumpdate may be null
+ */
+ public Date getPreviousDumpDate() {
+ return previousDumpDate;
+ }
+
+ /**
+ * Set previous dump date.
+ */
+ public void setPreviousDumpDate(Date previousDumpDate) {
+ this.previousDumpDate = previousDumpDate;
+ }
+
+ /**
+ * Get volume (tape) number.
+ * @return
+ */
+ public int getVolume() {
+ return volume;
+ }
+
+ /**
+ * Set volume (tape) number.
+ */
+ public void setVolume(int volume) {
+ this.volume = volume;
+ }
+
+ /**
+ * Get the level of this dump. This is a number between 0 and 9, inclusive,
+ * and a level 0 dump is a complete dump of the partition. For any other dump
+ * 'n' this dump contains all files that have changed since the last dump
+ * at this level or lower. This is used to support different levels of
+ * incremental backups.
+ * @return
+ */
+ public int getLevel() {
+ return level;
+ }
+
+ /**
+ * Set level.
+ */
+ public void setLevel(int level) {
+ this.level = level;
+ }
+
+ /**
+ * Get dump label. This may be autogenerated or it may be specified
+ * bu the user.
+ * @return
+ */
+ public String getLabel() {
+ return label;
+ }
+
+ /**
+ * Set dump label.
+ * @param label
+ */
+ public void setLabel(String label) {
+ this.label = label;
+ }
+
+ /**
+ * Get the last mountpoint, e.g., /home.
+ * @return
+ */
+ public String getFilesystem() {
+ return filesys;
+ }
+
+ /**
+ * Set the last mountpoint.
+ */
+ public void setFilesystem(String filesystem) {
+ this.filesys = filesystem;
+ }
+
+ /**
+ * Get the device name, e.g., /dev/sda3 or /dev/mapper/vg0-home.
+ * @return
+ */
+ public String getDevname() {
+ return devname;
+ }
+
+ /**
+ * Set the device name.
+ * @param devname
+ */
+ public void setDevname(String devname) {
+ this.devname = devname;
+ }
+
+ /**
+ * Get the hostname of the system where the dump was performed.
+ * @return
+ */
+ public String getHostname() {
+ return hostname;
+ }
+
+ /**
+ * Set the hostname.
+ */
+ public void setHostname(String hostname) {
+ this.hostname = hostname;
+ }
+
+ /**
+ * Get the miscellaneous flags. See below.
+ * @return
+ */
+ public int getFlags() {
+ return flags;
+ }
+
+ /**
+ * Set the miscellaneous flags.
+ * @param flags
+ */
+ public void setFlags(int flags) {
+ this.flags = flags;
+ }
+
+ /**
+ * Get the inode of the first record on this volume.
+ * @return
+ */
+ public int getFirstRecord() {
+ return firstrec;
+ }
+
+ /**
+ * Set the inode of the first record.
+ * @param firstrec
+ */
+ public void setFirstRecord(int firstrec) {
+ this.firstrec = firstrec;
+ }
+
+ /**
+ * Get the number of records per tape block. This is typically
+ * between 10 and 32.
+ * @return
+ */
+ public int getNTRec() {
+ return ntrec;
+ }
+
+ /**
+ * Set the number of records per tape block.
+ */
+ public void setNTRec(int ntrec) {
+ this.ntrec = ntrec;
+ }
+
+ /**
+ * Is this the new header format? (We do not currently support the
+ * old format.)
+ *
+ * @return
+ */
+ public boolean isNewHeader() {
+ return (flags & 0x0001) == 0x0001;
+ }
+
+ /**
+ * Is this the new inode format? (We do not currently support the
+ * old format.)
+ * @return
+ */
+ public boolean isNewInode() {
+ return (flags & 0x0002) == 0x0002;
+ }
+
+ /**
+ * Is this volume compressed? N.B., individual blocks may or may not be compressed.
+ * The first block is never compressed.
+ * @return
+ */
+ public boolean isCompressed() {
+ return (flags & 0x0080) == 0x0080;
+ }
+
+ /**
+ * Does this volume only contain metadata?
+ * @return
+ */
+ public boolean isMetaDataOnly() {
+ return (flags & 0x0100) == 0x0100;
+ }
+
+ /**
+ * Does this volume cotain extended attributes.
+ * @return
+ */
+ public boolean isExtendedAttributes() {
+ return (flags & 0x8000) == 0x8000;
+ }
+
+ /**
+ * @see java.lang.hashCode()
+ */
+ @Override
+ public int hashCode() {
+ int hash = 17;
+
+ if (label != null) {
+ hash = label.hashCode();
+ }
+
+ if (dumpDate != null) {
+ hash = (31 * dumpDate.hashCode()) + 17;
+ }
+
+ if (hostname != null) {
+ hash = (31 * hostname.hashCode()) + 17;
+ }
+
+ if (devname != null) {
+ hash = (31 * devname.hashCode()) + 17;
+ }
+
+ return hash;
+ }
+
+ /**
+ * @see java.lang.Object#equals(Object)
+ */
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+
+ if (!(o instanceof DumpArchiveSummary)) {
+ return false;
+ }
+
+ DumpArchiveSummary rhs = (DumpArchiveSummary) o;
+
+ if ((dumpDate == null) || !dumpDate.equals(rhs.dumpDate)) {
+ return false;
+ }
+
+ if ((getHostname() == null) ||
+ !getHostname().equals(rhs.getHostname())) {
+ return false;
+ }
+
+ if ((getDevname() == null) || !getDevname().equals(rhs.getDevname())) {
+ return false;
+ }
+
+ return true;
+ }
+}
Propchange: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveSummary.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveUtil.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveUtil.java?rev=1157769&view=auto
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveUtil.java (added)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveUtil.java Mon Aug 15 10:47:04 2011
@@ -0,0 +1,159 @@
+/*
+ * 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;
+
+
+/**
+ * Various utilities for dump archives.
+ */
+public class DumpArchiveUtil {
+ /**
+ * Private constructor to prevent instantiation.
+ */
+ private DumpArchiveUtil() {
+ }
+
+ /**
+ * Calculate checksum for buffer.
+ *
+ * @param buffer buffer containing tape segment header
+ * @returns checksum
+ */
+ public static int calculateChecksum(byte[] buffer) {
+ int calc = 0;
+
+ for (int i = 0; i < 256; i++) {
+ calc += DumpArchiveUtil.convert32(buffer, 4 * i);
+ }
+
+ return DumpArchiveConstants.CHECKSUM -
+ (calc - DumpArchiveUtil.convert32(buffer, 28));
+ }
+
+ /**
+ * Verify that the buffer contains a tape segment header.
+ *
+ * @param buffer
+ * @throws Exception
+ */
+ public static final boolean verify(byte[] buffer) {
+ // verify magic. for now only accept NFS_MAGIC.
+ int magic = convert32(buffer, 24);
+
+ if (magic != DumpArchiveConstants.NFS_MAGIC) {
+ return false;
+ }
+
+ //verify checksum...
+ int checksum = convert32(buffer, 28);
+
+ if (checksum != calculateChecksum(buffer)) {
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * Get the ino associated with this buffer.
+ *
+ * @param buffer
+ * @throws Exception
+ */
+ public static final int getIno(byte[] buffer) {
+ return convert32(buffer, 20);
+ }
+
+ /**
+ * Read 8-byte integer from buffer.
+ *
+ * @param buffer
+ * @param offset
+ * @return
+ */
+ public static final long convert64(byte[] buffer, int offset) {
+ long i = 0;
+ i += (((long) buffer[offset + 7]) << 56);
+ i += (((long) buffer[offset + 6] << 48) & 0x00FF000000000000L);
+ i += (((long) buffer[offset + 5] << 40) & 0x0000FF0000000000L);
+ i += (((long) buffer[offset + 4] << 32) & 0x000000FF00000000L);
+ i += (((long) buffer[offset + 3] << 24) & 0x00000000FF000000L);
+ i += (((long) buffer[offset + 2] << 16) & 0x0000000000FF0000L);
+ i += (((long) buffer[offset + 1] << 8) & 0x000000000000FF00L);
+ i += (((long) buffer[offset]) & 0x00000000000000FFL);
+
+ return i;
+ }
+
+ /**
+ * Read 4-byte integer from buffer.
+ *
+ * @param buffer
+ * @param offset
+ * @return
+ */
+ public static final int convert32(byte[] buffer, int offset) {
+ int i = 0;
+ i = ((int) buffer[offset + 3]) << 24;
+ i += (((int) buffer[offset + 2] << 16) & 0x00FF0000);
+ i += (((int) buffer[offset + 1] << 8) & 0x0000FF00);
+ i += (((int) buffer[offset]) & 0x000000FF);
+
+ return i;
+ }
+
+ /**
+ * Read 2-byte integer from buffer.
+ *
+ * @param buffer
+ * @param offset
+ * @return
+ */
+ public static final int convert16(byte[] buffer, int offset) {
+ int i = 0;
+ i += (((int) buffer[offset + 1] << 8) & 0x0000FF00);
+ i += (((int) buffer[offset]) & 0x000000FF);
+
+ return i;
+ }
+
+ /**
+ * Dump the start of a block.
+ *
+ * @param buffer
+ */
+ public static final void dumpBlock(byte[] buffer) {
+ for (int i = 0; i < 128; i += 32) {
+ System.out.printf("%08x ", DumpArchiveUtil.convert32(buffer, i));
+ System.out.printf("%08x ", DumpArchiveUtil.convert32(buffer, i + 4));
+ System.out.printf("%08x ", DumpArchiveUtil.convert32(buffer, i + 8));
+ System.out.printf("%08x - ",
+ DumpArchiveUtil.convert32(buffer, i + 12));
+ System.out.printf("%08x ", DumpArchiveUtil.convert32(buffer, i +
+ 16));
+ System.out.printf("%08x ", DumpArchiveUtil.convert32(buffer, i +
+ 20));
+ System.out.printf("%08x ", DumpArchiveUtil.convert32(buffer, i +
+ 24));
+ System.out.printf("%08x ", DumpArchiveUtil.convert32(buffer, i +
+ 28));
+ System.out.println();
+ }
+ }
+}
Propchange: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveUtil.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/InvalidFormatException.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/InvalidFormatException.java?rev=1157769&view=auto
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/InvalidFormatException.java (added)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/InvalidFormatException.java Mon Aug 15 10:47:04 2011
@@ -0,0 +1,43 @@
+/*
+ * 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;
+
+
+/**
+ * Invalid Format Exception. There was an error decoding a
+ * tape segment header.
+ */
+public class InvalidFormatException extends DumpArchiveException {
+ private static final long serialVersionUID = 1L;
+ protected long offset;
+
+ public InvalidFormatException() {
+ super("there was an error decoding a tape segment");
+ }
+
+ public InvalidFormatException(long offset) {
+ super("there was an error decoding a tape segment header at offset " +
+ offset + ".");
+ this.offset = offset;
+ }
+
+ public long getOffset() {
+ return offset;
+ }
+}
Propchange: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/InvalidFormatException.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/ShortFileException.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/ShortFileException.java?rev=1157769&view=auto
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/ShortFileException.java (added)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/ShortFileException.java Mon Aug 15 10:47:04 2011
@@ -0,0 +1,32 @@
+/*
+ * 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;
+
+
+/**
+ * Short File Exception. There was an unexpected EOF when reading
+ * the input stream.
+ */
+public class ShortFileException extends DumpArchiveException {
+ private static final long serialVersionUID = 1L;
+
+ public ShortFileException() {
+ super("unexpected EOF");
+ }
+}
Propchange: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/ShortFileException.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/TapeInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/TapeInputStream.java?rev=1157769&view=auto
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/TapeInputStream.java (added)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/TapeInputStream.java Mon Aug 15 10:47:04 2011
@@ -0,0 +1,348 @@
+/*
+ * 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.io.FilterInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import java.util.Arrays;
+import java.util.zip.DataFormatException;
+import java.util.zip.Inflater;
+
+
+/**
+ * Filter stream that mimics a physical tape drive capable of compressing
+ * the data stream.
+ *
+ * @NotThreadSafe
+ */
+public class TapeInputStream extends FilterInputStream {
+ private byte[] blockBuffer = new byte[DumpArchiveConstants.TP_SIZE];
+ private int currBlkIdx = -1;
+ private int blockSize = DumpArchiveConstants.TP_SIZE;
+ private int recordSize = DumpArchiveConstants.TP_SIZE;
+ private int readOffset = DumpArchiveConstants.TP_SIZE;
+ private boolean isCompressed = false;
+ private long bytesRead = 0;
+
+ /**
+ * Constructor
+ */
+ public TapeInputStream(InputStream in) {
+ super(in);
+ }
+
+ /**
+ * Set the DumpArchive Buffer's block size. We need to sync the block size with the
+ * dump archive's actual block size since compression is handled at the
+ * block level.
+ *
+ * @param blockSize
+ * actual blockSize according to dump archive
+ * @param isCompressed
+ * true if the archive is compressed
+ * @throws IOException
+ * more than one block has been read
+ * @throws IOException
+ * there was an error reading additional blocks.
+ */
+ public void resetBlockSize(int recsPerBlock, boolean isCompressed)
+ throws IOException {
+ this.isCompressed = isCompressed;
+
+ blockSize = recordSize * recsPerBlock;
+
+ // save first block in case we need it again
+ byte[] oldBuffer = blockBuffer;
+
+ // read rest of new block
+ blockBuffer = new byte[blockSize];
+ System.arraycopy(oldBuffer, 0, blockBuffer, 0, recordSize);
+ readFully(blockBuffer, recordSize, blockSize - recordSize);
+
+ this.currBlkIdx = 0;
+ this.readOffset = recordSize;
+ }
+
+ /**
+ * {@see java.io.InputStream#available}
+ */
+ @Override
+ public int available() throws IOException {
+ if (readOffset < blockSize) {
+ return blockSize - readOffset;
+ }
+
+ return in.available();
+ }
+
+ /**
+ * {@see java.io.InputStream#read()
+ */
+ @Override
+ public int read() throws IOException {
+ throw new IllegalArgumentException(
+ "all reads must be multiple of record size (" + recordSize +
+ " bytes.");
+ }
+
+ /**
+ * {@see java.io.InputStream#read(byte[], int, int) }
+ */
+ @Override
+ public int read(byte[] b, int off, int len) throws IOException {
+ if ((len % recordSize) != 0) {
+ throw new IllegalArgumentException(
+ "all reads must be multiple of record size (" + recordSize +
+ " bytes.");
+ }
+
+ int bytes = 0;
+
+ while (bytes < len) {
+ // we need to read from the underlying stream.
+ // this will reset readOffset value.
+ // return -1 if there's a problem.
+ if ((readOffset == blockSize) && !readBlock(true)) {
+ return -1;
+ }
+
+ int n = 0;
+
+ if ((readOffset + (len - bytes)) <= blockSize) {
+ // we can read entirely from the buffer.
+ n = len - bytes;
+ } else {
+ // copy what we can from the buffer.
+ n = blockSize - readOffset;
+ }
+
+ // copy data, increment counters.
+ System.arraycopy(blockBuffer, readOffset, b, off, n);
+ readOffset += n;
+ bytes += n;
+ off += n;
+ }
+
+ return bytes;
+ }
+
+ /**
+ * Skip bytes. Same as read but without the arraycopy.
+ * {@see java.io.InputStream#read(byte[], int, int) }
+ */
+ @Override
+ public long skip(long len) throws IOException {
+ if ((len % recordSize) != 0) {
+ throw new IllegalArgumentException(
+ "all reads must be multiple of record size (" + recordSize +
+ " bytes.");
+ }
+
+ long bytes = 0;
+
+ while (bytes < len) {
+ // we need to read from the underlying stream.
+ // this will reset readOffset value. We do not perform
+ // any decompression if we won't eventually read the data.
+ // return -1 if there's a problem.
+ if ((readOffset == blockSize) &&
+ !readBlock((len - bytes) < blockSize)) {
+ return -1;
+ }
+
+ long n = 0;
+
+ if ((readOffset + (len - bytes)) <= blockSize) {
+ // we can read entirely from the buffer.
+ n = len - bytes;
+ } else {
+ // copy what we can from the buffer.
+ n = blockSize - readOffset;
+ }
+
+ // do not copy data but still increment counters.
+ readOffset += n;
+ bytes += n;
+ }
+
+ return bytes;
+ }
+
+ /**
+ * Close the input stream.
+ *
+ * @throws IOException on error
+ */
+ @Override
+ public void close() throws IOException {
+ if (in != null) {
+ if (in != System.in) {
+ in.close();
+ }
+ }
+ }
+
+ /**
+ * Peek at the next record from the input stream and return the data.
+ *
+ * @return The record data.
+ * @throws IOException on error
+ */
+ public byte[] peek() throws IOException {
+ // we need to read from the underlying stream. This
+ // isn't a problem since it would be the first step in
+ // any subsequent read() anyway.
+ if ((readOffset == blockSize) && !readBlock(true)) {
+ return null;
+ }
+
+ // copy data, increment counters.
+ byte[] b = new byte[recordSize];
+ System.arraycopy(blockBuffer, readOffset, b, 0, b.length);
+
+ return b;
+ }
+
+ /**
+ * Read a record from the input stream and return the data.
+ *
+ * @return The record data.
+ * @throws IOException on error
+ */
+ public byte[] readRecord() throws IOException {
+ byte[] result = new byte[recordSize];
+
+ if (-1 == read(result, 0, result.length)) {
+ throw new ShortFileException();
+ }
+
+ return result;
+ }
+
+ /**
+ * Read next block. All decompression is handled here.
+ *
+ * @param decompress if false the buffer will not be decompressed.
+ * This is an optimization for longer seeks.
+ * @return false if End-Of-File, else true
+ */
+ private boolean readBlock(boolean decompress) throws IOException {
+ boolean success = true;
+
+ if (in == null) {
+ throw new IOException("input buffer is closed");
+ }
+
+ if (!isCompressed || (currBlkIdx == -1)) {
+ // file is not compressed
+ success = readFully(blockBuffer, 0, blockSize);
+ bytesRead += blockSize;
+ } else {
+ in.read(blockBuffer, 0, 4);
+ bytesRead += 4;
+
+ int h = DumpArchiveUtil.convert32(blockBuffer, 0);
+ boolean compressed = (h & 0x01) == 0x01;
+
+ if (!compressed) {
+ // file is compressed but this block is not.
+ success = readFully(blockBuffer, 0, blockSize);
+ bytesRead += blockSize;
+ } else {
+ // this block is compressed.
+ int flags = (h >> 1) & 0x07;
+ int length = (h >> 4) & 0x0FFFFFFF;
+ byte[] compBuffer = new byte[length];
+ success = readFully(compBuffer, 0, length);
+ bytesRead += length;
+
+ if (!decompress) {
+ // just in case someone reads the data.
+ Arrays.fill(blockBuffer, (byte) 0);
+ } else {
+ switch (DumpArchiveConstants.COMPRESSION_TYPE.find(flags &
+ 0x03)) {
+ case ZLIB:
+
+ try {
+ Inflater inflator = new Inflater();
+ inflator.setInput(compBuffer, 0, compBuffer.length);
+ length = inflator.inflate(blockBuffer);
+
+ if (length != blockSize) {
+ throw new ShortFileException();
+ }
+
+ inflator.end();
+ } catch (DataFormatException e) {
+ throw new DumpArchiveException("bad data", e);
+ }
+
+ break;
+
+ case BZLIB:
+ throw new UnsupportedCompressionAlgorithmException(
+ "BZLIB2");
+
+ case LZO:
+ throw new UnsupportedCompressionAlgorithmException(
+ "LZO");
+
+ default:
+ throw new UnsupportedCompressionAlgorithmException();
+ }
+ }
+ }
+ }
+
+ currBlkIdx++;
+ readOffset = 0;
+
+ return success;
+ }
+
+ /**
+ * Read buffer
+ */
+ private boolean readFully(byte[] b, int off, int len)
+ throws IOException {
+ int count = 0;
+
+ while (count < len) {
+ int n = in.read(b, off + count, len - count);
+
+ if (n == -1) {
+ throw new ShortFileException();
+ }
+
+ count += n;
+ }
+
+ return true;
+ }
+
+ /**
+ * Get number of bytes read.
+ */
+ public long getBytesRead() {
+ return bytesRead;
+ }
+}
Propchange: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/TapeInputStream.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/UnrecognizedFormatException.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/UnrecognizedFormatException.java?rev=1157769&view=auto
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/UnrecognizedFormatException.java (added)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/UnrecognizedFormatException.java Mon Aug 15 10:47:04 2011
@@ -0,0 +1,32 @@
+/*
+ * 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;
+
+
+/**
+ * Unrecognized Format Exception. This is either not a recognized dump archive or there's
+ * a bad tape segment header.
+ */
+public class UnrecognizedFormatException extends DumpArchiveException {
+ private static final long serialVersionUID = 1L;
+
+ public UnrecognizedFormatException() {
+ super("this is not a recognized format.");
+ }
+}
Propchange: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/UnrecognizedFormatException.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/UnsupportedCompressionAlgorithmException.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/UnsupportedCompressionAlgorithmException.java?rev=1157769&view=auto
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/UnsupportedCompressionAlgorithmException.java (added)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/UnsupportedCompressionAlgorithmException.java Mon Aug 15 10:47:04 2011
@@ -0,0 +1,38 @@
+/*
+ * 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;
+
+
+/**
+ * Unsupported compression algorithm. The dump archive uses an unsupported
+ * compression algorithm (BZLIB2 or LZO).
+ */
+public class UnsupportedCompressionAlgorithmException
+ extends DumpArchiveException {
+ private static final long serialVersionUID = 1L;
+
+ public UnsupportedCompressionAlgorithmException() {
+ super("this file uses an unsupported compression algorithm.");
+ }
+
+ public UnsupportedCompressionAlgorithmException(String alg) {
+ super("this file uses an unsupported compression algorithm: " + alg +
+ ".");
+ }
+}
Propchange: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/UnsupportedCompressionAlgorithmException.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/package.html
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/package.html?rev=1157769&view=auto
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/package.html (added)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/package.html Mon Aug 15 10:47:04 2011
@@ -0,0 +1,56 @@
+<html>
+<!--
+
+ 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.
+
+-->
+ <body>
+ <p>This package provides stream classes for reading archives
+ using the Unix DUMP format. This format is similar to (and
+ contemporary with) TAR but reads the raw filesystem directly.
+ This means that writers are filesystem-specific even though the
+ created archives are filesystem-agnostic.
+ </p>
+
+ <p>Unlike other formats DUMP offers clean support for sparse files,
+ extended attributes, and other file metadata. In addition DUMP
+ supports incremental dump files can capture (most) file deletion.
+ It also provides a native form of compression and will soon support
+ native encryption as well.
+ </p>
+
+ <p>In practice TAR archives are used for both distribution
+ and backups. DUMP archives are used exclusively for backups.
+ </p>
+
+ <p>Like any 30+-year-old application there are a number of variants.
+ For pragmatic reasons we will only support archives with the
+ 'new' tape header and inode formats. Other restrictions:
+
+ <ul>
+ <li>We only support ZLIB and BZLIB compression. The format
+ also permits LZO compression.</li>
+ <li>Sparse files will have the holes filled.</li>
+ <li>MacOS finder and resource streams are ignored.</li>
+ <li>Extended attributes are not currently provided.</li>
+ <li>SELinux labels are not currently provided.</li>
+ </ul>
+ </p>
+
+ <p>As of Apache Commons Compress 1.3 support for thedump format is
+ read-only.</p>
+ </body>
+</html>
Propchange: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/dump/package.html
------------------------------------------------------------------------------
svn:eol-style = native