Skip to content

Commit

Permalink
TOSQUASH
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisdral committed May 31, 2024
1 parent 9298640 commit 05945d9
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 41 deletions.
7 changes: 6 additions & 1 deletion fs-api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
`HasFS`. As a result, `ioHasFS` now requires that `PrimState IO ~ PrimState m`.
* Rename `Util.CallStack` and `Util.Condense` to `System.FS.CallStack` and
`System.FS.Condense` respectively.
* Make modules in the `System.FS.IO.Internal` hierarchy truly internal.
* Make modules in the `System.FS.IO.Internal` hierarchy public, inspired by
"Internal convention is a mistake". The following modules are moved/renamed:
* `System.FS.IO.Internal` is moved to `System.FS.IO.Unix` on Linux and MacOS
systems, and moved to `System.FS.IO.Windows` on Windows systems.
* `System.FS.IO.Internal.Handle` is moved to `System.FS.IO.Handle`.
* Make the `fs-api` package only buildable on Linux, MacOS and Windows systems.

### Non-breaking

Expand Down
21 changes: 13 additions & 8 deletions fs-api/fs-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ library
System.FS.Condense
System.FS.CRC
System.FS.IO

other-modules:
System.FS.IO.Internal
System.FS.IO.Internal.Handle
System.FS.IO.Handle

default-language: Haskell2010
build-depends:
Expand All @@ -54,15 +51,23 @@ library
, text >=1.2 && <2.2

if os(windows)
hs-source-dirs: src-win32
build-depends: Win32 >=2.6.1.0
hs-source-dirs: src-win32
exposed-modules: System.FS.IO.Win32
build-depends: Win32 >=2.6.1.0

else
hs-source-dirs: src-unix
elif (os(linux) || os(osx))
hs-source-dirs: src-unix
exposed-modules: System.FS.IO.Unix
build-depends:
, unix
, unix-bytestring >=0.4.0

else
-- The package currently only supports Linux, MacOS and Windows
-- explicitly.
build-depends: unbuildable <0
buildable: False

ghc-options:
-Wall -Wcompat -Wincomplete-uni-patterns
-Wincomplete-record-updates -Wpartial-fields -Widentities
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE PackageImports #-}

-- | This is meant to be used for the implementation of HasFS instances and not
-- directly by client code.
module System.FS.IO.Internal (
-- | This module is mainly meant to be used for the 'IO' implementation of
-- 'System.FS.API.HasFS'.
module System.FS.IO.Unix (
FHandle
, close
, getSize
Expand All @@ -29,7 +29,7 @@ import Data.Word (Word32, Word64, Word8)
import Foreign (Ptr)
import System.FS.API.Types (AllowExisting (..), OpenMode (..),
SeekMode (..))
import System.FS.IO.Internal.Handle
import System.FS.IO.Handle
import qualified System.Posix as Posix
import System.Posix (ByteCount, Fd (..), FileOffset)
import qualified System.Posix.IO.ByteString.Ext as Posix (fdPreadBuf,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{-# OPTIONS_GHC -Wno-dodgy-imports #-}

-- | This is meant to be used for the implementation of HasFS instances and not
-- directly by client code.
module System.FS.IO.Internal (
-- | This module is mainly meant to be used for the 'IO' implementation of
-- 'System.FS.API.HasFS'.
module System.FS.IO.Windows (
FHandle
, close
, getSize
Expand All @@ -28,7 +28,7 @@ import Data.Word (Word32, Word64, Word8)
import Foreign (Int64, Ptr)
import System.FS.API.Types (AllowExisting (..), FsError (..),
FsErrorType (..), OpenMode (..), SeekMode (..))
import System.FS.IO.Internal.Handle
import System.FS.IO.Handle
import System.Posix.Types
import System.Win32

Expand Down
43 changes: 22 additions & 21 deletions fs-api/src/System/FS/IO.hs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}

-- | IO implementation of the 'HasFS' interface
-- | 'IO' implementation of the 'HasFS' interface.
module System.FS.IO (
-- * IO implementation & monad
HandleIO
, ioHasFS
) where
Expand All @@ -19,8 +19,12 @@ import qualified Foreign
import GHC.Stack
import qualified System.Directory as Dir
import System.FS.API
import qualified System.FS.IO.Internal as F
import qualified System.FS.IO.Internal.Handle as H
#ifdef windows_HOST_OS
import qualified System.FS.IO.Windows as F
#else
import qualified System.FS.IO.Unix as F
#endif
import qualified System.FS.IO.Handle as H

{-------------------------------------------------------------------------------
I/O implementation of HasFS
Expand All @@ -31,6 +35,10 @@ import qualified System.FS.IO.Internal.Handle as H
-- We store the path the handle points to for better error messages
type HandleIO = F.FHandle

-- | 'IO' implementation of the 'HasFS' interface using the /real/ file system.
--
-- The concrete implementation depends on the OS distribution, but behaviour
-- should be similar across distributions.
ioHasFS :: (MonadIO m, PrimState IO ~ PrimState m) => MountPoint -> HasFS m HandleIO
ioHasFS mount = HasFS {
-- TODO(adn) Might be useful to implement this properly by reading all
Expand Down Expand Up @@ -94,21 +102,14 @@ ioHasFS mount = HasFS {
root = fsToFilePath mount

rethrowFsError :: HasCallStack => FsPath -> IO a -> IO a
rethrowFsError = _rethrowFsError mount

{-# INLINE _rethrowFsError #-}
-- | Catch IO exceptions and rethrow them as 'FsError'
--
-- See comments for 'ioToFsError'
_rethrowFsError :: HasCallStack => MountPoint -> FsPath -> IO a -> IO a
_rethrowFsError mount fp action = do
res <- E.try action
case res of
Left err -> handleError err
Right a -> return a
where
handleError :: HasCallStack => IOError -> IO a
handleError ioErr = E.throwIO $ ioToFsError errorPath ioErr
rethrowFsError fp action = do
res <- E.try action
case res of
Left err -> handleError err
Right a -> return a
where
handleError :: HasCallStack => IOError -> IO a
handleError ioErr = E.throwIO $ ioToFsError errorPath ioErr

errorPath :: FsErrorPath
errorPath = fsToFsErrorPath mount fp
errorPath :: FsErrorPath
errorPath = fsToFsErrorPath mount fp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{-# LANGUAGE LambdaCase #-}

-- | This is meant to be used for the implementation of HasFS instances and not
-- directly by client code.
module System.FS.IO.Internal.Handle (
-- | This module is mainly meant to be used for the 'IO' implementation of
-- 'System.FS.API.HasFS'.
module System.FS.IO.Handle (
HandleOS (..)
, closeHandleOS
, isHandleClosedException
Expand Down

0 comments on commit 05945d9

Please sign in to comment.