Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

76 строки
2.1 KiB

  1. -- |
  2. -- Module : System.Posix.FD
  3. -- Copyright : © 2016 Julian Ospald
  4. -- License : BSD3
  5. --
  6. -- Maintainer : Julian Ospald <hasufell@posteo.de>
  7. -- Stability : experimental
  8. -- Portability : portable
  9. --
  10. -- Provides an alternative for `System.Posix.IO.ByteString.openFd`
  11. -- which gives us more control on what status flags to pass to the
  12. -- low-level @open(2)@ call, in contrast to the unix package.
  13. {-# LANGUAGE ForeignFunctionInterface #-}
  14. {-# LANGUAGE OverloadedStrings #-}
  15. {-# LANGUAGE TupleSections #-}
  16. {-# OPTIONS_GHC -Wall #-}
  17. module System.Posix.FD (
  18. openFd
  19. ) where
  20. import Foreign.C.String
  21. import Foreign.C.Types
  22. import System.Posix.Foreign
  23. import qualified System.Posix as Posix
  24. import System.Posix.ByteString.FilePath
  25. foreign import ccall unsafe "open"
  26. c_open :: CString -> CInt -> Posix.CMode -> IO CInt
  27. open_ :: CString
  28. -> Posix.OpenMode
  29. -> [Flags]
  30. -> Maybe Posix.FileMode
  31. -> IO Posix.Fd
  32. open_ str how optional_flags maybe_mode = do
  33. fd <- c_open str all_flags mode_w
  34. return (Posix.Fd fd)
  35. where
  36. all_flags = unionFlags $ optional_flags ++ [open_mode] ++ creat
  37. (creat, mode_w) = case maybe_mode of
  38. Nothing -> ([],0)
  39. Just x -> ([oCreat], x)
  40. open_mode = case how of
  41. Posix.ReadOnly -> oRdonly
  42. Posix.WriteOnly -> oWronly
  43. Posix.ReadWrite -> oRdwr
  44. -- |Open and optionally create this file. See 'System.Posix.Files'
  45. -- for information on how to use the 'FileMode' type.
  46. --
  47. -- Note that passing @Just x@ as the 4th argument triggers the
  48. -- `oCreat` status flag, which must be set when you pass in `oExcl`
  49. -- to the status flags. Also see the manpage for @open(2)@.
  50. openFd :: RawFilePath
  51. -> Posix.OpenMode
  52. -> [Flags] -- ^ status flags of @open(2)@
  53. -> Maybe Posix.FileMode -- ^ @Just x@ => creates the file with the given modes, Nothing => the file must exist.
  54. -> IO Posix.Fd
  55. openFd name how optional_flags maybe_mode =
  56. withFilePath name $ \str ->
  57. throwErrnoPathIfMinus1Retry "openFd" name $
  58. open_ str how optional_flags maybe_mode