Commons Compress
  1. Commons Compress
  2. COMPRESS-6

[compress] TarOutputStream.java long file name bug (and fix!)

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None
    • Environment:

      Operating System: other
      Platform: All

      Description

      'LongLink' entries written to a tar file header by
      TarOutputStream.putNextEntry() specify the wrong file name length and data,
      preventing GNU tar from properly reading long file name entries (in the current
      GNU 'tar' implementation, sometimes one gets lucky and the entry happens to be
      null-terminated so it works anyway). In particular, the file name
      length-stored as an octal string in each header entry-should include an extra
      byte for the null terminator that must also be written at the end of the file
      name in the header.

      Here's what the code in
      org.apache.commons.compress.tar.TarOutputStream.putNextEntry() currently looks
      like (starting around line 424):

      if( m_longFileMode == LONGFILE_GNU ) {
      // create a TarEntry for the LongLink, the contents
      // of which are the entry's name
      final TarEntry longLinkEntry =
      new TarEntry( TarConstants.GNU_LONGLINK,
      TarConstants.LF_GNUTYPE_LONGNAME );

      longLinkEntry.setSize( entry.getName().length() );
      putNextEntry( longLinkEntry );
      write( entry.getName().getBytes() );
      //write( 0 );
      closeEntry();
      }

      Here's what the code should have been:

      if( m_longFileMode == LONGFILE_GNU ) {
      // create a TarEntry for the LongLink, the contents
      // of which are the entry's name
      final TarEntry longLinkEntry =
      new TarEntry( TarConstants.GNU_LONGLINK,
      TarConstants.LF_GNUTYPE_LONGNAME );

      longLinkEntry.setSize( entry.getName().length() + 1 );
      putNextEntry( longLinkEntry );
      write( entry.getName().getBytes() );
      write( 0 );
      closeEntry();
      }

      1. patch-longfilename.txt
        6 kB
        Christian Grobmeier

        Activity

        Hide
        Christian Grobmeier added a comment -

        Long Filename Patch.

        Includes a testcase. Reduces the allowed char number to 99, write a 0 terminator after a filename

        Show
        Christian Grobmeier added a comment - Long Filename Patch. Includes a testcase. Reduces the allowed char number to 99, write a 0 terminator after a filename
        Hide
        Christian Grobmeier added a comment -

        According to:
        http://www.gnu.org/software/tar/manual/html_section/Standard.html#SEC166
        "The name, linkname, magic, uname, and gname are null-terminated character strings. All other fields are zero-filled octal numbers in ASCII. Each numeric field of width w contains w minus 1 digits, and a null. "

        TarArchiveEntry.NAMELEN should not be 100, it should be 99 as Derc pointed out.
        See also:
        http://www.gnu.org/software/tar/manual/html_section/Formats.html

        Format V7: "The maximum length of a file name is limited to 99 characters. "

        Show
        Christian Grobmeier added a comment - According to: http://www.gnu.org/software/tar/manual/html_section/Standard.html#SEC166 "The name, linkname, magic, uname, and gname are null-terminated character strings. All other fields are zero-filled octal numbers in ASCII. Each numeric field of width w contains w minus 1 digits, and a null. " TarArchiveEntry.NAMELEN should not be 100, it should be 99 as Derc pointed out. See also: http://www.gnu.org/software/tar/manual/html_section/Formats.html Format V7: "The maximum length of a file name is limited to 99 characters. "

          People

          • Assignee:
            Torsten Curdt
            Reporter:
            Derc Yamasaki
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development