replace removeLink with removeFile

removeFile calls unlink so removes anything not a directory. So these
are replaceable in order to convert to OsPath.
This commit is contained in:
Joey Hess 2025-02-02 14:16:58 -04:00
parent 8af91a4c92
commit 5cc8d9d03b
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
9 changed files with 39 additions and 41 deletions

View file

@ -19,12 +19,10 @@ module Utility.Tmp (
) where
import System.IO
import System.Directory
import Control.Monad.IO.Class
import System.IO.Error
import Data.Char
import qualified Data.ByteString as B
import qualified System.FilePath.ByteString as P
import Utility.Exception
import Utility.FileSystemEncoding
@ -32,6 +30,7 @@ import Utility.FileMode
import qualified Utility.RawFilePath as R
import qualified Utility.FileIO as F
import Utility.OsPath
import Utility.SystemDirectory
type Template = OsString
@ -58,14 +57,14 @@ openTmpFileIn dir template = F.openTempFile dir template
viaTmp :: (MonadMask m, MonadIO m) => (OsPath -> v -> m ()) -> OsPath -> v -> m ()
viaTmp a file content = bracketIO setup cleanup use
where
(dir, base) = P.splitFileName (fromOsPath file)
template = relatedTemplate (base <> ".tmp")
(dir, base) = splitFileName file
template = relatedTemplate (fromOsPath base <> ".tmp")
setup = do
createDirectoryIfMissing True (fromRawFilePath dir)
openTmpFileIn (toOsPath dir) template
createDirectoryIfMissing True dir
openTmpFileIn dir template
cleanup (tmpfile, h) = do
_ <- tryIO $ hClose h
tryIO $ R.removeLink (fromOsPath tmpfile)
tryIO $ removeFile tmpfile
use (tmpfile, h) = do
let tmpfile' = fromOsPath tmpfile
-- Make mode the same as if the file were created usually,
@ -83,8 +82,8 @@ viaTmp a file content = bracketIO setup cleanup use
- (or in "." if there is none) then removes the file. -}
withTmpFile :: (MonadIO m, MonadMask m) => Template -> (OsPath -> Handle -> m a) -> m a
withTmpFile template a = do
tmpdir <- liftIO $ catchDefaultIO "." getTemporaryDirectory
withTmpFileIn (toOsPath (toRawFilePath tmpdir)) template a
tmpdir <- liftIO $ catchDefaultIO (literalOsPath ".") getTemporaryDirectory
withTmpFileIn tmpdir template a
{- Runs an action with a tmp file located in the specified directory,
- then removes the file.
@ -98,7 +97,7 @@ withTmpFileIn tmpdir template a = bracket create remove use
create = liftIO $ openTmpFileIn tmpdir template
remove (name, h) = liftIO $ do
hClose h
tryIO $ R.removeLink (fromOsPath name)
tryIO $ removeFile name
use (name, h) = a name h
{- It's not safe to use a FilePath of an existing file as the template