git-annex/Utility/FileIO/String.hs
Joey Hess 867110e9ee
add readFileString, writeFileString, appendFileString
Exported by Common, so they will be available everywhere.
These are the same as readFile, writeFile, appendFile.
But have two benefits:

* They take OsPath, so using them avoids converting back and forth
  unncessarily.
* They use the close-on-exec flag so can't leak FDs to child processes.
  Unlike the standard Haskell versions which unfortunately have that
  wart currently. (I do hope the standard versions get fixed
  eventually.)

Sponsored-by: the NIH-funded NICEMAN (ReproNim TR&D3) project
2025-09-05 14:54:36 -04:00

38 lines
967 B
Haskell

{- Functions that operate on OsPath, but treat the contents of files as
- Strings.
-
- These functions all set the close-on-exec flag to True, unlike
- the Prelude versions.
-
- Copyright 2025 Joey Hess <id@joeyh.name>
-
- License: BSD-2-clause
-}
{-# OPTIONS_GHC -fno-warn-tabs #-}
{-# LANGUAGE CPP #-}
module Utility.FileIO.String
(
#ifdef WITH_OSPATH
readFileString,
writeFileString,
appendFileString,
#endif
) where
#ifdef WITH_OSPATH
import qualified Utility.FileIO.CloseOnExec as I
import Utility.OsPath (OsPath)
import Prelude (String, IO, (>>=))
import System.IO (IOMode(..), hGetContents, hPutStr)
readFileString :: OsPath -> IO String
readFileString f = I.openFile f ReadMode >>= hGetContents
writeFileString :: OsPath -> String -> IO ()
writeFileString f txt = I.withFile f WriteMode (\hdl -> hPutStr hdl txt)
appendFileString :: OsPath -> String -> IO ()
appendFileString f txt = I.withFile f AppendMode (\hdl -> hPutStr hdl txt)
#endif