Example #1
0
  /** TarHeaders can be cloned. */
  public Object clone() {
    TarHeader hdr = null;

    try {
      hdr = (TarHeader) super.clone();

      hdr.name = (this.name == null) ? null : new StringBuffer(this.name.toString());
      hdr.mode = this.mode;
      hdr.userId = this.userId;
      hdr.groupId = this.groupId;
      hdr.size = this.size;
      hdr.modTime = this.modTime;
      hdr.checkSum = this.checkSum;
      hdr.linkFlag = this.linkFlag;
      hdr.linkName = (this.linkName == null) ? null : new StringBuffer(this.linkName.toString());
      hdr.magic = (this.magic == null) ? null : new StringBuffer(this.magic.toString());
      hdr.userName = (this.userName == null) ? null : new StringBuffer(this.userName.toString());
      hdr.groupName = (this.groupName == null) ? null : new StringBuffer(this.groupName.toString());
      hdr.devMajor = this.devMajor;
      hdr.devMinor = this.devMinor;
    } catch (CloneNotSupportedException ex) {
      ex.printStackTrace(System.err);
    }

    return hdr;
  }
Example #2
0
  /**
   * Parse an entry's TarHeader information from a header buffer.
   *
   * <p>Old unix-style code contributed by David Mehringer <*****@*****.**>.
   *
   * @param hdr The TarHeader to fill in from the buffer information.
   * @param header The tar entry header buffer to get information from.
   */
  public void parseTarHeader(TarHeader hdr, byte[] headerBuf) throws InvalidHeaderException {
    int offset = 0;

    //
    // NOTE Recognize archive header format.
    //
    if (headerBuf[257] == 0
        && headerBuf[258] == 0
        && headerBuf[259] == 0
        && headerBuf[260] == 0
        && headerBuf[261] == 0) {
      this.unixFormat = true;
      this.ustarFormat = false;
      this.gnuFormat = false;
    } else if (headerBuf[257] == 'u'
        && headerBuf[258] == 's'
        && headerBuf[259] == 't'
        && headerBuf[260] == 'a'
        && headerBuf[261] == 'r'
        && headerBuf[262] == 0) {
      this.ustarFormat = true;
      this.gnuFormat = false;
      this.unixFormat = false;
    } else if (headerBuf[257] == 'u'
        && headerBuf[258] == 's'
        && headerBuf[259] == 't'
        && headerBuf[260] == 'a'
        && headerBuf[261] == 'r'
        && headerBuf[262] != 0
        && headerBuf[263] != 0) {
      // REVIEW
      this.gnuFormat = true;
      this.unixFormat = false;
      this.ustarFormat = false;
    } else {
      StringBuffer buf = new StringBuffer(128);

      buf.append("header magic is not 'ustar' or unix-style zeros, it is '");
      buf.append(headerBuf[257]);
      buf.append(headerBuf[258]);
      buf.append(headerBuf[259]);
      buf.append(headerBuf[260]);
      buf.append(headerBuf[261]);
      buf.append(headerBuf[262]);
      buf.append(headerBuf[263]);
      buf.append("', or (dec) ");
      buf.append((int) headerBuf[257]);
      buf.append(", ");
      buf.append((int) headerBuf[258]);
      buf.append(", ");
      buf.append((int) headerBuf[259]);
      buf.append(", ");
      buf.append((int) headerBuf[260]);
      buf.append(", ");
      buf.append((int) headerBuf[261]);
      buf.append(", ");
      buf.append((int) headerBuf[262]);
      buf.append(", ");
      buf.append((int) headerBuf[263]);

      throw new InvalidHeaderException(buf.toString());
    }

    hdr.name = TarHeader.parseFileName(headerBuf);

    offset = TarHeader.NAMELEN;

    hdr.mode = (int) TarHeader.parseOctal(headerBuf, offset, TarHeader.MODELEN);

    offset += TarHeader.MODELEN;

    hdr.userId = (int) TarHeader.parseOctal(headerBuf, offset, TarHeader.UIDLEN);

    offset += TarHeader.UIDLEN;

    hdr.groupId = (int) TarHeader.parseOctal(headerBuf, offset, TarHeader.GIDLEN);

    offset += TarHeader.GIDLEN;

    hdr.size = TarHeader.parseOctal(headerBuf, offset, TarHeader.SIZELEN);

    offset += TarHeader.SIZELEN;

    hdr.modTime = TarHeader.parseOctal(headerBuf, offset, TarHeader.MODTIMELEN);

    offset += TarHeader.MODTIMELEN;

    hdr.checkSum = (int) TarHeader.parseOctal(headerBuf, offset, TarHeader.CHKSUMLEN);

    offset += TarHeader.CHKSUMLEN;

    hdr.linkFlag = headerBuf[offset++];

    hdr.linkName = TarHeader.parseName(headerBuf, offset, TarHeader.NAMELEN);

    offset += TarHeader.NAMELEN;

    if (this.ustarFormat) {
      hdr.magic = TarHeader.parseName(headerBuf, offset, TarHeader.MAGICLEN);

      offset += TarHeader.MAGICLEN;

      hdr.userName = TarHeader.parseName(headerBuf, offset, TarHeader.UNAMELEN);

      offset += TarHeader.UNAMELEN;

      hdr.groupName = TarHeader.parseName(headerBuf, offset, TarHeader.GNAMELEN);

      offset += TarHeader.GNAMELEN;

      hdr.devMajor = (int) TarHeader.parseOctal(headerBuf, offset, TarHeader.DEVLEN);

      offset += TarHeader.DEVLEN;

      hdr.devMinor = (int) TarHeader.parseOctal(headerBuf, offset, TarHeader.DEVLEN);
    } else {
      hdr.devMajor = 0;
      hdr.devMinor = 0;
      hdr.magic = new StringBuffer("");
      hdr.userName = new StringBuffer("");
      hdr.groupName = new StringBuffer("");
    }
  }