From: Joey Hess Date: Fri, 5 Sep 2025 16:05:13 +0000 (-0400) Subject: convert all bytestring readFile/writeFile to use Utility.FileIO X-Git-Tag: archive/raspbian/10.20251029-1+rpi1~1^2~3^2~139 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=6662b49d35b75066a51852f7a551f74b0b1cf1eb;p=git-annex.git convert all bytestring readFile/writeFile to use Utility.FileIO This is groundwork for setting the close-on-exec flag when opening files, which will be done in Utility.FileIO or a similar module using the same function names. The bytestring library does not set that flag, even though it could, and IMHO should. Note that there are many calls to the Prelude's readFile/writeFile/appendFile still in git-annex, and this does not address those. Sponsored-by: the NIH-funded NICEMAN (ReproNim TR&D3) project --- diff --git a/Build/Standalone.hs b/Build/Standalone.hs index b9a683fb0b..e0e9e56bee 100644 --- a/Build/Standalone.hs +++ b/Build/Standalone.hs @@ -15,7 +15,6 @@ import System.Environment (getArgs) import Control.Monad.IfElse import System.Posix.Files import Control.Monad -import qualified Data.ByteString.Lazy as L import qualified Data.Map as M import Utility.SafeCommand @@ -26,6 +25,7 @@ import Utility.Path.AbsRel import Utility.Directory import Utility.Env import Utility.SystemDirectory +import qualified Utility.FileIO as F import Build.BundledPrograms #ifdef darwin_HOST_OS import System.IO @@ -98,7 +98,7 @@ installGitLibs topdir = do let linktarget' = progDir topdir takeFileName linktarget unlessM (doesFileExist linktarget') $ do createDirectoryIfMissing True (takeDirectory linktarget') - L.readFile f' >>= L.writeFile (fromOsPath linktarget') + F.readFile f >>= F.writeFile linktarget' removeWhenExistsWith removeFile destf rellinktarget <- relPathDirToFile (takeDirectory destf) diff --git a/Remote/Directory/LegacyChunked.hs b/Remote/Directory/LegacyChunked.hs index 03dd7e398d..3773ed9172 100644 --- a/Remote/Directory/LegacyChunked.hs +++ b/Remote/Directory/LegacyChunked.hs @@ -52,7 +52,7 @@ storeLegacyChunked _ _ [] _ = error "bad storeLegacyChunked call" storeLegacyChunked meterupdate chunksize alldests@(firstdest:_) b | L.null b = do -- always write at least one file, even for empty - L.writeFile firstdest b + F.writeFile (toOsPath firstdest) b return [firstdest] | otherwise = storeLegacyChunked' meterupdate chunksize alldests (L.toChunks b) [] storeLegacyChunked' :: MeterUpdate -> ChunkSize -> [FilePath] -> [S.ByteString] -> [FilePath] -> IO [FilePath] @@ -103,7 +103,7 @@ retrieve locations d basek p _dest miv c = withOtherTmp $ \tmpdir -> do let go = \k sink -> do liftIO $ void $ withStoredFiles (fromOsPath d) (legacyLocations locations) k $ \fs -> do forM_ fs $ - F.appendFile' tmp <=< S.readFile + F.appendFile' tmp <=< F.readFile' . toOsPath return True b <- liftIO $ F.readFile tmp liftIO $ removeWhenExistsWith removeFile tmp diff --git a/Remote/Helper/Special.hs b/Remote/Helper/Special.hs index cc1fdf20a3..c74cb1dde1 100644 --- a/Remote/Helper/Special.hs +++ b/Remote/Helper/Special.hs @@ -96,7 +96,7 @@ mkRetrievalVerifiableKeysSecure gc fileStorer :: (Key -> OsPath -> MeterUpdate -> Annex ()) -> Storer fileStorer a k (FileContent f) m = a k f m fileStorer a k (ByteContent b) m = withTmp k $ \f -> do - liftIO $ L.writeFile (fromOsPath f) b + liftIO $ F.writeFile f b a k f m -- A Storer that expects to be provided with a L.ByteString of diff --git a/Utility/FileIO.hs b/Utility/FileIO.hs index f10cb20ffc..712877f0cb 100644 --- a/Utility/FileIO.hs +++ b/Utility/FileIO.hs @@ -1,4 +1,7 @@ -{- File IO on OsPaths. +{- This is a subset of the functions provided by file-io. + - + - When not building with file-io, this provides equvilant + - RawFilePath versions. - - Since Prelude exports many of these as well, this needs to be imported - qualified. @@ -41,7 +44,6 @@ import System.IO (IO, Handle, IOMode) import Prelude (return) import qualified System.File.OsPath as O import qualified Data.ByteString as B -import qualified Data.ByteString.Lazy as L import Control.Applicative withFile :: OsPath -> IOMode -> (Handle -> IO r) -> IO r