DumpArchiveSummary.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  * http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing,
  13.  * software distributed under the License is distributed on an
  14.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15.  * KIND, either express or implied.  See the License for the
  16.  * specific language governing permissions and limitations
  17.  * under the License.
  18.  */
  19. package org.apache.commons.compress.archivers.dump;

  20. import java.io.IOException;
  21. import java.util.Date;
  22. import java.util.Objects;

  23. import org.apache.commons.compress.archivers.zip.ZipEncoding;

  24. /**
  25.  * This class represents identifying information about a Dump archive volume. It consists the archive's dump date, label, hostname, device name and possibly
  26.  * last mount point plus the volume's volume id and first record number.
  27.  * <p>
  28.  * For the corresponding C structure see the header of {@link DumpArchiveEntry}.
  29.  * </p>
  30.  */
  31. public class DumpArchiveSummary {

  32.     private long dumpDate;
  33.     private long previousDumpDate;
  34.     private int volume;
  35.     private String label;
  36.     private int level;
  37.     private String filesys;
  38.     private String devname;
  39.     private String hostname;
  40.     private int flags;
  41.     private int firstrec;
  42.     private int ntrec;

  43.     DumpArchiveSummary(final byte[] buffer, final ZipEncoding encoding) throws IOException {
  44.         dumpDate = 1000L * DumpArchiveUtil.convert32(buffer, 4);
  45.         previousDumpDate = 1000L * DumpArchiveUtil.convert32(buffer, 8);
  46.         volume = DumpArchiveUtil.convert32(buffer, 12);
  47.         label = DumpArchiveUtil.decode(encoding, buffer, 676, DumpArchiveConstants.LBLSIZE).trim();
  48.         level = DumpArchiveUtil.convert32(buffer, 692);
  49.         filesys = DumpArchiveUtil.decode(encoding, buffer, 696, DumpArchiveConstants.NAMELEN).trim();
  50.         devname = DumpArchiveUtil.decode(encoding, buffer, 760, DumpArchiveConstants.NAMELEN).trim();
  51.         hostname = DumpArchiveUtil.decode(encoding, buffer, 824, DumpArchiveConstants.NAMELEN).trim();
  52.         flags = DumpArchiveUtil.convert32(buffer, 888);
  53.         firstrec = DumpArchiveUtil.convert32(buffer, 892);
  54.         ntrec = DumpArchiveUtil.convert32(buffer, 896);

  55.         // extAttributes = DumpArchiveUtil.convert32(buffer, 900);
  56.     }

  57.     @Override
  58.     public boolean equals(final Object obj) {
  59.         if (this == obj) {
  60.             return true;
  61.         }
  62.         if (obj == null) {
  63.             return false;
  64.         }
  65.         if (getClass() != obj.getClass()) {
  66.             return false;
  67.         }
  68.         final DumpArchiveSummary other = (DumpArchiveSummary) obj;
  69.         return Objects.equals(devname, other.devname) && dumpDate == other.dumpDate && Objects.equals(hostname, other.hostname);
  70.     }

  71.     /**
  72.      * Gets the device name, e.g., /dev/sda3 or /dev/mapper/vg0-home.
  73.      *
  74.      * @return device name
  75.      */
  76.     public String getDevname() {
  77.         return devname;
  78.     }

  79.     /**
  80.      * Gets the date of this dump.
  81.      *
  82.      * @return the date of this dump.
  83.      */
  84.     public Date getDumpDate() {
  85.         return new Date(dumpDate);
  86.     }

  87.     /**
  88.      * Gets the last mountpoint, e.g., /home.
  89.      *
  90.      * @return last mountpoint
  91.      */
  92.     public String getFilesystem() {
  93.         return filesys;
  94.     }

  95.     /**
  96.      * Gets the inode of the first record on this volume.
  97.      *
  98.      * @return inode of the first record on this volume.
  99.      */
  100.     public int getFirstRecord() {
  101.         return firstrec;
  102.     }

  103.     /**
  104.      * Gets the miscellaneous flags. See below.
  105.      *
  106.      * @return flags
  107.      */
  108.     public int getFlags() {
  109.         return flags;
  110.     }

  111.     /**
  112.      * Gets the hostname of the system where the dump was performed.
  113.      *
  114.      * @return hostname the host name
  115.      */
  116.     public String getHostname() {
  117.         return hostname;
  118.     }

  119.     /**
  120.      * Gets dump label. This may be autogenerated, or it may be specified by the user.
  121.      *
  122.      * @return dump label
  123.      */
  124.     public String getLabel() {
  125.         return label;
  126.     }

  127.     /**
  128.      * Gets 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'
  129.      * 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
  130.      * backups.
  131.      *
  132.      * @return dump level
  133.      */
  134.     public int getLevel() {
  135.         return level;
  136.     }

  137.     /**
  138.      * Gets the number of records per tape block. This is typically between 10 and 32.
  139.      *
  140.      * @return the number of records per tape block
  141.      */
  142.     public int getNTRec() {
  143.         return ntrec;
  144.     }

  145.     /**
  146.      * Gets the date of the previous dump at this level higher.
  147.      *
  148.      * @return dumpdate may be null
  149.      */
  150.     public Date getPreviousDumpDate() {
  151.         return new Date(previousDumpDate);
  152.     }

  153.     /**
  154.      * Gets volume (tape) number.
  155.      *
  156.      * @return volume (tape) number.
  157.      */
  158.     public int getVolume() {
  159.         return volume;
  160.     }

  161.     @Override
  162.     public int hashCode() {
  163.         return Objects.hash(devname, dumpDate, hostname);
  164.     }

  165.     /**
  166.      * Is this volume compressed? N.B., individual blocks may or may not be compressed. The first block is never compressed.
  167.      *
  168.      * @return true if volume is compressed
  169.      */
  170.     public boolean isCompressed() {
  171.         return (flags & 0x0080) == 0x0080;
  172.     }

  173.     /**
  174.      * Does this volume contain extended attributes.
  175.      *
  176.      * @return true if volume contains extended attributes.
  177.      */
  178.     public boolean isExtendedAttributes() {
  179.         return (flags & 0x8000) == 0x8000;
  180.     }

  181.     /**
  182.      * Does this volume only contain metadata?
  183.      *
  184.      * @return true if volume only contains meta-data
  185.      */
  186.     public boolean isMetaDataOnly() {
  187.         return (flags & 0x0100) == 0x0100;
  188.     }

  189.     /**
  190.      * Is this the new header format? (We do not currently support the old format.)
  191.      *
  192.      * @return true if using new header format
  193.      */
  194.     public boolean isNewHeader() {
  195.         return (flags & 0x0001) == 0x0001;
  196.     }

  197.     /**
  198.      * Is this the new inode format? (We do not currently support the old format.)
  199.      *
  200.      * @return true if using new inode format
  201.      */
  202.     public boolean isNewInode() {
  203.         return (flags & 0x0002) == 0x0002;
  204.     }

  205.     /**
  206.      * Sets the device name.
  207.      *
  208.      * @param devname the device name
  209.      */
  210.     public void setDevname(final String devname) {
  211.         this.devname = devname;
  212.     }

  213.     /**
  214.      * Sets dump date.
  215.      *
  216.      * @param dumpDate the dump date
  217.      */
  218.     public void setDumpDate(final Date dumpDate) {
  219.         this.dumpDate = dumpDate.getTime();
  220.     }

  221.     /**
  222.      * Sets the last mountpoint.
  223.      *
  224.      * @param fileSystem the last mountpoint
  225.      */
  226.     public void setFilesystem(final String fileSystem) {
  227.         this.filesys = fileSystem;
  228.     }

  229.     /**
  230.      * Sets the inode of the first record.
  231.      *
  232.      * @param firstrec the first record
  233.      */
  234.     public void setFirstRecord(final int firstrec) {
  235.         this.firstrec = firstrec;
  236.     }

  237.     /**
  238.      * Sets the miscellaneous flags.
  239.      *
  240.      * @param flags flags
  241.      */
  242.     public void setFlags(final int flags) {
  243.         this.flags = flags;
  244.     }

  245.     /**
  246.      * Sets the hostname.
  247.      *
  248.      * @param hostname the host name
  249.      */
  250.     public void setHostname(final String hostname) {
  251.         this.hostname = hostname;
  252.     }

  253.     /**
  254.      * Sets dump label.
  255.      *
  256.      * @param label the label
  257.      */
  258.     public void setLabel(final String label) {
  259.         this.label = label;
  260.     }

  261.     /**
  262.      * Sets level.
  263.      *
  264.      * @param level the level
  265.      */
  266.     public void setLevel(final int level) {
  267.         this.level = level;
  268.     }

  269.     /**
  270.      * Sets the number of records per tape block.
  271.      *
  272.      * @param ntrec the number of records per tape block
  273.      */
  274.     public void setNTRec(final int ntrec) {
  275.         this.ntrec = ntrec;
  276.     }

  277.     /**
  278.      * Sets previous dump date.
  279.      *
  280.      * @param previousDumpDate the previous dump dat
  281.      */
  282.     public void setPreviousDumpDate(final Date previousDumpDate) {
  283.         this.previousDumpDate = previousDumpDate.getTime();
  284.     }

  285.     /**
  286.      * Sets volume (tape) number.
  287.      *
  288.      * @param volume the volume number
  289.      */
  290.     public void setVolume(final int volume) {
  291.         this.volume = volume;
  292.     }
  293. }