3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Haskell でテンポラリディレクトリを扱う方法

Last updated at Posted at 2015-04-17

テンポラリファイルを扱う方法として、System.IO.Temp を使う方法と System.Unix.Directory を使う方法があります。

サンプルを作ってみたところ、大体同じ使い方であることがわかりました。

テンポラリファイルを扱う方法の検証は以下を参照してください。

Haskell でテンポラリファイルを扱う方法 - Qiita

System.IO.Temp

SystemIoTempTest1.hs
import qualified System.Directory as SD
import qualified System.IO.Temp as SIT 

tempFile :: String -> String
tempFile path = path ++ "/" ++ "hoge.txt"

withSystemTempDir :: FilePath -> IO String
withSystemTempDir path = do
  putStrLn $ "path: '" ++ path ++ "'" 
  isDirExist <- SD.doesDirectoryExist path
  putStrLn $ "isDirExist: " ++ show isDirExist
  writeFile (tempFile path) "hogehoge"
  isFileExist <- SD.doesFileExist $ tempFile path
  putStrLn $ "doesFileExist1: " ++ show isFileExist
  return path

main :: IO ()
main = do
  path <- SIT.withSystemTempDirectory "hoge" withSystemTempDir
  isFileExist <- SD.doesFileExist $ tempFile path
  putStrLn $ "doesFileExist2: " ++ show isFileExist

withSystemTempDirectory(FilePath -> IO a) を渡し、その中でテンポラリファイルの操作を完結させる必要があります。関数を抜けると、作成されたテンポラリディレクトリごと削除されます。

以下、実行結果です。

$ runhaskell SystemIOTempTest.hs 
path: '/var/folders/lc/46fljbjd7m39wszcjqsjwf500000gn/T/hoge23608'
isDirExist: True
doesFileExist1: True
doesFileExist2: False

System.Unix.Directory

SystemUnixDirectoryTest.hs
import qualified System.Directory as SD
import qualified System.Unix.Directory as SUD

tempFile :: String -> String
tempFile path = path ++ "/" ++ "hoge.txt"

withTempDir :: FilePath -> IO String
withTempDir path = do
  putStrLn $ "path: '" ++ path ++ "'"
  isDirExist <- SD.doesDirectoryExist path
  putStrLn $ "doesDirectoryExist: " ++ show isDirExist
  writeFile (tempFile path) "hogehoge"
  isFileExist <- SD.doesFileExist $ tempFile path
  putStrLn $ "doesFileExist1: " ++ show isFileExist
  return path

main :: IO ()
main = do
  path <- SUD.withTemporaryDirectory "hoge" withTempDir
  isFileExist <- SD.doesFileExist $ tempFile path
  putStrLn $ "doesFileExist2: " ++ show isFileExist

System.IO.Temp と同様、withTemporaryDirectory(FilePath -> IO a) を渡し、その中でテンポラリファイルの操作を完結させる必要があります。また同様に、関数を抜けると、作成されたテンポラリディレクトリごと削除されます。

以下、実行結果です。

$ runhaskell SystemUnixDirectoryTest.hs 
path: '/var/folders/lc/46fljbjd7m39wszcjqsjwf500000gn/T/hoge8rQDhY'
doesDirectoryExist: True
doesFileExist1: True
doesFileExist2: False

まとめ

オーソドックスな使い方は大体同じでしょうか。

System.IO.Temp は依存関係が簡潔、System.Unix.Directory は unixutils というユーティリティの機能の一部で依存関係が複雑であり、Unix と名前が付いていることからプラットフォームを選ぶ可能性があるため、前者が良いかもしれません。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?