Skip to content

Can't extract tar file: inappropriate type (Is a directory) #27

Closed

Description

@bitc

I found a .tar file that this library chokes on: https://nodejs.org/dist/v8.9.3/node-v8.9.3-linux-x64.tar.xz

I extracted it with the "unxz" program and got "node-v8.9.3-linux-x64.tar". This file extracts just fine using the regular "tar" program. But when using this library (extract function) I get this error:

*** Exception: ./node-v8.9.3-linux-x64/lib/node_modules/npm/node_modules/readable-stream/node_modules/util-deprecate/: openBinaryFile: inappropriate type

I managed to print the failing Tar Entry:

Entry
  { entryTarPath = "node-v8.9.3-linux-x64/lib/node_modules/npm/node_modules/readable-stream/node_modules/util-deprecate/"
  , entryContent = NormalFile "\n1.0.2 / 2015-10-07\n==================\n\n  * use try/catch when checking `localStorage` (#3, @kumavis)\n\n1.0.1 / 2014-11-25\n==================\n\n  * browser: use `console.warn()` for deprecation calls\n  * browser: more jsdocs\n\n1.0.0 / 2014-04-30\n==================\n\n  * initial commit\n" 282
  , entryPermissions = 436
  , entryOwnership = Ownership
      { ownerName = "iojs"
      , groupName = "iojs"
      , ownerId = 500
      , groupId = 500
      }
  , entryTime = 1496058403
  , entryFormat = GnuFormat
  }

Looking at the "entryContent", the "entryTarPath" is incorrect, and is supposed to be the file: node-v8.9.3-linux-x64/lib/node_modules/npm/node_modules/readable-stream/node_modules/util-deprecate/History.md

Activity

  1. bitc commented on Dec 18, 2017

    @bitc
    Author

    Here is the "History.md" file, this is the Tar Entry just before the above failing one:

    Entry
      { entryTarPath = "././@LongLink"
      , entryContent = OtherEntryType 'L' "node-v9.3.0-linux-x64/lib/node_modules/npm/node_modules/readable-stream/node_modules/util-deprecate/History.md\NUL" 111
      , entryPermissions = 0
      , entryOwnership = Ownership
          { ownerName = "root"
          , groupName = "root"
          , ownerId = 0
          , groupId = 0
          }
      , entryTime = 0
      , entryFormat = GnuFormat
      }
  2. vmchale commented on Feb 5, 2019

    @vmchale

    I suspect that this could be resolved by adding nicer support for GNU/old GNU/POSIX tar archives. According to this, GNU tar archives support unlimited file names, but according to a comment here, there doesn't seem to be any attempt to support this.

  3. bitc commented on Feb 8, 2019

    @bitc
    Author

    I have some code that can extract PAX tar archives pretty well (note: these are different from GNU tar archives that I originally reported)

    WARNING: This might have security issues (overwriting files outside the destination directory)

    unpackTar :: FilePath -> Tar.Entries Tar.FormatError -> IO ()
    unpackTar dir x =
        (loop Nothing Nothing . (checkEntries checkEntrySecurity)) x
      where
        loop :: (Maybe FilePath) -> (Maybe FilePath) -> Tar.Entries (Either Tar.FormatError Tar.FileNameError) -> IO ()
        loop _ _ Tar.Done = return ()
        loop _ _ (Tar.Fail e) = either throwIO throwIO e
        loop pathOverride linkOverride (Tar.Next e es) = case Tar.entryContent e of
            Tar.Directory -> loop Nothing Nothing es
            Tar.NormalFile lbs _ -> do
                let fp = dir </> fromMaybe (Tar.entryPath e) pathOverride
                saveFile fp lbs (Tar.entryPermissions e)
                loop Nothing Nothing es
            Tar.SymbolicLink target -> do
                let fp = dir </> fromMaybe (Tar.entryPath e) pathOverride
                let link = fromMaybe (Tar.fromLinkTarget target) linkOverride
                saveSymbolicLink fp link
                loop Nothing Nothing es
            Tar.OtherEntryType 'x' pax _ -> do
                case parsePaxExtended pax of
                    Left err -> fail err
                    Right overrides -> do
                        let pathOverride' = lookup (L.pack "path") overrides
                            linkOverride' = lookup (L.pack "linkpath") overrides
                        loop (fmap L.unpack pathOverride') (fmap L.unpack linkOverride') es
            _ -> do
                loop Nothing Nothing es
    
    saveFile :: FilePath -> L.ByteString -> Tar.Permissions -> IO ()
    saveFile fp lbs perms = do
        createDirectoryIfMissing True $ F.takeDirectory fp
        runConduitRes $ mapM_ yield (L.toChunks lbs) .| sinkFile fp
        when ((perms .&. (ownerExecuteMode .|. groupExecuteMode .|. otherExecuteMode)) /= 0 ) $ do
            stat <- getFileStatus fp
            setFileMode fp (fileMode stat .|. (ownerExecuteMode .|. groupExecuteMode .|. otherExecuteMode))
    
    saveSymbolicLink :: FilePath -> FilePath -> IO ()
    saveSymbolicLink fp target = do
        createDirectoryIfMissing True $ F.takeDirectory fp
        createSymbolicLink target fp
    
    checkEntries :: (Tar.Entry -> Maybe e') -> Tar.Entries e -> Tar.Entries (Either e e')
    checkEntries checkEntry =
        Tar.mapEntries (\entry -> maybe (Right entry) Left (checkEntry entry))
    
    checkEntrySecurity :: Tar.Entry -> Maybe Tar.FileNameError
    checkEntrySecurity _ = Nothing
    
    parsePaxExtended :: L.ByteString -> Either String [(L.ByteString, L.ByteString)]
    parsePaxExtended = next []
        where
        next :: [(L.ByteString, L.ByteString)] -> L.ByteString -> Either String [(L.ByteString, L.ByteString)]
        next accum str =
            let (lenStr, _) = L.break (==' ') str
            in case L.readInt lenStr of
                Nothing -> Left "Invalid PAX"
                Just (len, _) ->
                    let (seg, rest) = L.splitAt (fromIntegral len) str
                        seg' = L.drop 1 (L.dropWhile (/=' ') seg)
                        seg'' = L.take (L.length seg' - 1) seg'
                        (key, val) = L.break (=='=') seg''
                        newPair = (key, L.drop 1 val)
                    in if L.null rest then Right (newPair:accum)
                        else next (newPair:accum) rest
  4. hasufell commented on Mar 1, 2020

    @hasufell
    Member

    #50 seems related

  5. Bodigrim commented on Nov 19, 2023

    @Bodigrim
    Contributor

    It works better wrt long file names after #77, but still fails later on:

    $ cabal run htar -- --extract -f node-v8.9.3-linux-x64.tar
    htar: Invalid file name in tar archive: "../lib/node_modules/npm/bin/npx-cli.js"
    
  6. hasufell commented on Dec 7, 2023

    @hasufell
    Member

    @Bodigrim I presume it works now?

  7. Bodigrim commented on Dec 7, 2023

    @Bodigrim
    Contributor

    @hasufell yes, it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions