You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

56 lines
1.8 KiB

  1. module System.Posix.Directory.Foreign where
  2. import Data.Bits
  3. import Data.List (foldl')
  4. import Foreign.C.Types
  5. #include <limits.h>
  6. #include <stdlib.h>
  7. #include <dirent.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <fcntl.h>
  11. newtype DirType = DirType Int deriving (Eq, Show)
  12. data Flags = Flags Int | UnsupportedFlag String deriving (Eq, Show)
  13. unFlags :: Flags -> Int
  14. unFlags (Flags i) = i
  15. unFlags (UnsupportedFlag name) = error (name ++ " is not supported on this platform")
  16. -- |Returns @True@ if posix-paths was compiled with support for the provided
  17. -- flag. (As of this writing, the only flag for which this check may be
  18. -- necessary is 'oCloexec'; all other flags will always yield @True@.)
  19. isSupported :: Flags -> Bool
  20. isSupported (Flags _) = True
  21. isSupported _ = False
  22. -- |@O_CLOEXEC@ is not supported on every POSIX platform. Use
  23. -- @'isSupported' oCloexec@ to determine if support for @O_CLOEXEC@ was
  24. -- compiled into your version of posix-paths. (If not, using @oCloexec@ will
  25. -- throw an exception.)
  26. oCloexec :: Flags
  27. #ifdef O_CLOEXEC
  28. oCloexec = Flags #{const O_CLOEXEC}
  29. #else
  30. {-# WARNING oCloexec
  31. "This version of posix-paths was compiled without @O_CLOEXEC@ support." #-}
  32. oCloexec = UnsupportedFlag "O_CLOEXEC"
  33. #endif
  34. -- If these enum declarations occur earlier in the file, haddock
  35. -- gets royally confused about the above doc comments.
  36. -- Probably http://trac.haskell.org/haddock/ticket/138
  37. #{enum DirType, DirType, DT_BLK, DT_CHR, DT_DIR, DT_FIFO, DT_LNK, DT_REG, DT_SOCK, DT_UNKNOWN}
  38. #{enum Flags, Flags, O_APPEND, O_ASYNC, O_CREAT, O_DIRECTORY, O_EXCL, O_NOCTTY, O_NOFOLLOW, O_NONBLOCK, O_RDONLY, O_WRONLY, O_RDWR, O_SYNC, O_TRUNC}
  39. pathMax :: Int
  40. pathMax = #{const PATH_MAX}
  41. unionFlags :: [Flags] -> CInt
  42. unionFlags = fromIntegral . foldl' ((. unFlags) . (.|.)) 0