하스 no 멍에 여기. 나는 하스켈의 적절한 사용과 관련하여 좀 더 근본적인 측면을 이끌어 낼 수있는 기존의 라이브러리를 사용하는 방법에 관한 질문을 가지고있다.Codec.Archive.Tar의 기능을 재정의하는 방법
저는 배울 때 하스켈을 배우며 작 업할 생각이 있습니다. 스크립트는 주어진 디렉토리에서 모든 tarball을 찾아서 병렬로 풀어야합니다. 이 시점에서, 나는 언 패킹의 기본 기능을 다루고 있습니다. 따라서 Codec.Archive.Tar 패키지를 사용하여 tarball과 관련된 동작을 정규화 된 경로로 재정의 할 수 있습니까? 내가 함께 포장 된 타르볼이있는 디렉토리에서이 프로그램을 실행할 때
module Main where
import qualified Codec.Archive.Tar as Tar
import qualified Codec.Compression.GZip as GZip
import Control.Monad (liftM, unless)
import qualified Data.ByteString.Lazy as BS
import System.Directory (doesDirectoryExist, getDirectoryContents)
import System.Exit (exitWith, ExitCode(..))
import System.FilePath.Posix (takeExtension)
searchPath = "/home/someuser/tarball/dir"
exit = exitWith ExitSuccess
die = exitWith (ExitFailure 1)
processFile :: String -> IO()
processFile file = do
putStrLn $ "Unpacking " ++ file ++ " to " ++ searchPath
Tar.unpack searchPath . Tar.read . GZip.decompress =<< BS.readFile filePath
where filePath = searchPath ++ "/" ++ file
main = do
dirExists <- doesDirectoryExist searchPath
unless dirExists $ (putStrLn $ "Error: Search path not found: " ++ searchPath) >> die
files <- targetFiles `liftM` getDirectoryContents searchPath
mapM_ processFile files
exit
where targetFiles = filter (\f -> f /= "." && f /= ".." && takeExtension f == ".tgz")
: 여기
몇 가지 예제 코드입니다tar czvPf myfile.tgz /tarball_testing/myfile
내가 얻을 다음과 같은 출력 :
Unpacking myfile.tgz to /tarball_testing
unpacker.hs: Absolute file name in tar archive: "/tarball_testing/myfile"
두 번째 줄이 문제입니다. 에 대한 문서 읽기이 기능을 사용하지 않도록 설정할 방법이 없습니다 (타르볼에서 전체 경로를 사용하려는 이유 또는 이와 관련된 보안 관련 의미에 대한 토론에 관심이 없음).
내가 생각하기에 가장 먼저 떠오르는 점은 어떻게 든 함수를 재정의해야한다는 것이지만 Haskeller가하는 것처럼 느껴지지 않는다는 것입니다. 올바른 방향으로 포인터를 가져올 수 있습니까?
: 걱정한다고 생각하지 않습니다 패키지] (http://hackage.haskell.org/package/tar) 파일을 추출하기 위해 제공된 유일한 인터페이스가 절대 경로를 거부하는 것처럼 보입니다. 그 패키지로 당신이 할 수있는 일은 아무것도 없습니다. – Carl
사용할 수있는 한 가지는 ['mapEntries'] (http://hackage.haskell.org/package/tar-0.3.1.0/docs/Codec-Archive-Tar.html#v:mapEntries)와 ['Codec.Archive.Tar.Entry'] (http://hackage.haskell.org/package/tar-0.3.1.0/docs/Codec-Archive-Tar-Entry.html)와'System.FilePath'는 추출 전에 상대 경로 항목. – duplode