From 5a2b4a9f05eb70789b451fcedd24bcc989f97842 Mon Sep 17 00:00:00 2001 From: Phil Ruffwind Date: Mon, 20 May 2024 01:05:02 -0700 Subject: [PATCH] Migrate file I/O to file-io --- System/Directory/Internal/Common.hs | 9 --------- System/Directory/Internal/Posix.hsc | 24 +++--------------------- System/Directory/Internal/Windows.hsc | 22 ---------------------- System/Directory/OsPath.hs | 3 ++- changelog.md | 4 ++++ directory.cabal | 7 ++++--- 6 files changed, 13 insertions(+), 56 deletions(-) diff --git a/System/Directory/Internal/Common.hs b/System/Directory/Internal/Common.hs index bbf8a9b8..a8a61323 100644 --- a/System/Directory/Internal/Common.hs +++ b/System/Directory/Internal/Common.hs @@ -8,7 +8,6 @@ import System.Directory.Internal.Prelude import GHC.IO.Encoding.Failure (CodingFailureMode(TransliterateCodingFailure)) import GHC.IO.Encoding.UTF16 (mkUTF16le) import GHC.IO.Encoding.UTF8 (mkUTF8) -import System.IO (hSetBinaryMode) import System.OsPath ( OsPath , OsString @@ -243,14 +242,6 @@ data Permissions , searchable :: Bool } deriving (Eq, Ord, Read, Show) -withBinaryHandle :: IO Handle -> (Handle -> IO r) -> IO r -withBinaryHandle open = bracket openBinary hClose - where - openBinary = do - h <- open - hSetBinaryMode h True - pure h - -- | Copy data from one handle to another until end of file. copyHandleData :: Handle -- ^ Source handle -> Handle -- ^ Destination handle diff --git a/System/Directory/Internal/Posix.hsc b/System/Directory/Internal/Posix.hsc index 27e7f92d..38e97482 100644 --- a/System/Directory/Internal/Posix.hsc +++ b/System/Directory/Internal/Posix.hsc @@ -13,6 +13,7 @@ import System.Directory.Internal.Common import System.Directory.Internal.Config (exeExtension) import Data.Time (UTCTime) import Data.Time.Clock.POSIX (POSIXTime) +import System.File.OsPath (withBinaryFile) import System.OsPath ((), isRelative, splitSearchPath) import System.OsString.Internal.Types (OsString(OsString, getOsString)) import qualified Data.Time.Clock.POSIX as POSIXTime @@ -20,7 +21,6 @@ import qualified System.OsPath.Internal as OsPath import qualified System.Posix.Directory.PosixPath as Posix import qualified System.Posix.Env.PosixString as Posix import qualified System.Posix.Files.PosixString as Posix -import qualified System.Posix.IO.PosixString as Posix import qualified System.Posix.PosixPath.FilePath as Posix import qualified System.Posix.Types as Posix import qualified System.Posix.User.ByteString as Posix @@ -244,24 +244,6 @@ tryCopyOwnerAndGroupFromStatus st dst = do ignoreIOExceptions (copyOwnerFromStatus st dst) ignoreIOExceptions (copyGroupFromStatus st dst) -defaultFlags :: Posix.OpenFileFlags -defaultFlags = - Posix.defaultFileFlags - { Posix.noctty = True - , Posix.nonBlock = True - , Posix.cloexec = True - } - -openFileForRead :: OsPath -> IO Handle -openFileForRead (OsString p) = - Posix.fdToHandle =<< Posix.openFd p Posix.ReadOnly defaultFlags - -openFileForWrite :: OsPath -> IO Handle -openFileForWrite (OsString p) = - Posix.fdToHandle =<< - Posix.openFd p Posix.WriteOnly - defaultFlags { Posix.creat = Just 0o666, Posix.trunc = True } - -- | Truncate the destination file and then copy the contents of the source -- file to the destination file. If the destination file already exists, its -- attributes shall remain unchanged. Otherwise, its attributes are reset to @@ -271,8 +253,8 @@ copyFileContents :: OsPath -- ^ Source filename -> IO () copyFileContents fromFPath toFPath = (`ioeAddLocation` "copyFileContents") `modifyIOError` do - withBinaryHandle (openFileForWrite toFPath) $ \ hTo -> do - withBinaryHandle (openFileForRead fromFPath) $ \ hFrom -> do + withBinaryFile toFPath WriteMode $ \ hTo -> do + withBinaryFile fromFPath ReadMode $ \ hFrom -> do copyHandleData hFrom hTo copyFileWithMetadataInternal :: (Metadata -> OsPath -> IO ()) diff --git a/System/Directory/Internal/Windows.hsc b/System/Directory/Internal/Windows.hsc index 8c55861c..1c723fb4 100644 --- a/System/Directory/Internal/Windows.hsc +++ b/System/Directory/Internal/Windows.hsc @@ -105,28 +105,6 @@ maxShareMode = Win32.fILE_SHARE_READ .|. Win32.fILE_SHARE_WRITE -openFileForRead :: OsPath -> IO Handle -openFileForRead (OsString path) = - bracketOnError - (Win32.createFile - path - Win32.gENERIC_READ - maxShareMode - Nothing - Win32.oPEN_EXISTING - (Win32.fILE_ATTRIBUTE_NORMAL .|. possiblyOverlapped) - Nothing) - Win32.closeHandle - Win32.hANDLEToHandle - -possiblyOverlapped :: Win32.FileAttributeOrFlag -#ifdef __IO_MANAGER_WINIO__ -possiblyOverlapped | ioSubSystem == IoNative = Win32.fILE_FLAG_OVERLAPPED - | otherwise = 0 -#else -possiblyOverlapped = 0 -#endif - win32_getFinalPathNameByHandle :: Win32.HANDLE -> Win32.DWORD -> IO WindowsPath #ifdef HAVE_GETFINALPATHNAMEBYHANDLEW win32_getFinalPathNameByHandle h flags = do diff --git a/System/Directory/OsPath.hs b/System/Directory/OsPath.hs index fc3492e4..8f3744dc 100644 --- a/System/Directory/OsPath.hs +++ b/System/Directory/OsPath.hs @@ -105,6 +105,7 @@ module System.Directory.OsPath import Prelude () import System.Directory.Internal import System.Directory.Internal.Prelude +import System.File.OsPath (withBinaryFile) import System.OsPath ( (<.>) , () @@ -728,7 +729,7 @@ copyFileToHandle :: OsPath -- ^ Source file -> IO () copyFileToHandle fromFPath hTo = (`ioeAddLocation` "copyFileToHandle") `modifyIOError` do - withBinaryHandle (openFileForRead fromFPath) $ \ hFrom -> + withBinaryFile fromFPath ReadMode $ \ hFrom -> copyHandleData hFrom hTo -- | Copy the contents of a source file to a destination file, replacing the diff --git a/changelog.md b/changelog.md index d87a2dde..5f8ee0b9 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,10 @@ Changelog for the [`directory`][1] package ========================================== +## 1.3.8.5 (unreleased) + + * Rely on `file-io` for file I/O. + ## 1.3.8.5 (May 2024) * Fix regression that causes copying of nonexistent files to create empty diff --git a/directory.cabal b/directory.cabal index 7c080242..23f8fea3 100644 --- a/directory.cabal +++ b/directory.cabal @@ -1,6 +1,6 @@ cabal-version: 2.2 name: directory -version: 1.3.8.5 +version: 1.3.9.0 license: BSD-3-Clause license-file: LICENSE maintainer: libraries@haskell.org @@ -60,8 +60,9 @@ Library include-dirs: . build-depends: - base >= 4.11.0 && < 4.21, - time >= 1.8.0 && < 1.15 + base >= 4.12.0 && < 4.21, + file-io >= 0.1.1 && < 0.2, + time >= 1.8.0 && < 1.15, if os(windows) build-depends: Win32 >= 2.13.3 && < 2.15 else