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.  *   https://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 || getClass() != obj.getClass()) {
  63.             return false;
  64.         }
  65.         final DumpArchiveSummary other = (DumpArchiveSummary) obj;
  66.         return Objects.equals(devname, other.devname) && dumpDate == other.dumpDate && Objects.equals(hostname, other.hostname);
  67.     }

  68.     /**
  69.      * Gets the device name, for example, /dev/sda3 or /dev/mapper/vg0-home.
  70.      *
  71.      * @return device name
  72.      */
  73.     public String getDevname() {
  74.         return devname;
  75.     }

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

  84.     /**
  85.      * Gets the last mountpoint, for example, /home.
  86.      *
  87.      * @return last mountpoint
  88.      */
  89.     public String getFilesystem() {
  90.         return filesys;
  91.     }

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

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

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

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

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

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

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

  150.     /**
  151.      * Gets volume (tape) number.
  152.      *
  153.      * @return volume (tape) number.
  154.      */
  155.     public int getVolume() {
  156.         return volume;
  157.     }

  158.     @Override
  159.     public int hashCode() {
  160.         return Objects.hash(devname, dumpDate, hostname);
  161.     }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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