{-# LANGUAGE CPP #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ViewPatterns #-}

module System.FSNotify.Linux.Util (
  canonicalizePath
  , canonicalizeRawDirPath
  , (<//>)
  , traverseAllDirs

  , boolToIsDirectory

  , fromRawFilePath
  , toRawFilePath

  , fromHinotifyPath

  , rawToHinotifyPath
  , rawFromHinotifyPath
  ) where

import Control.Exception.Safe as E
import Control.Monad
import qualified Data.ByteString as BS
import Data.Function
import Data.Monoid
import qualified GHC.Foreign as F
import GHC.IO.Encoding (getFileSystemEncoding)
import Prelude hiding (FilePath)
import System.Directory (canonicalizePath)
import System.FSNotify.Types
import System.FilePath (FilePath)
import System.Posix.ByteString (RawFilePath)
import System.Posix.Directory.ByteString (openDirStream, readDirStream, closeDirStream)
import System.Posix.Files (getSymbolicLinkStatus, isDirectory)


canonicalizeRawDirPath :: RawFilePath -> IO RawFilePath
canonicalizeRawDirPath :: ByteString -> IO ByteString
canonicalizeRawDirPath ByteString
p = ByteString -> IO FilePath
fromRawFilePath ByteString
p IO FilePath -> (FilePath -> IO FilePath) -> IO FilePath
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= FilePath -> IO FilePath
canonicalizePath IO FilePath -> (FilePath -> IO ByteString) -> IO ByteString
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= FilePath -> IO ByteString
toRawFilePath

-- | Same as </> but for RawFilePath
-- TODO: make sure this is correct or find in a library
(<//>) :: RawFilePath -> RawFilePath -> RawFilePath
ByteString
x <//> :: ByteString -> ByteString -> ByteString
<//> ByteString
y = ByteString
x ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"/" ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
y

traverseAllDirs :: RawFilePath -> (RawFilePath -> IO ()) -> IO ()
traverseAllDirs :: ByteString -> (ByteString -> IO ()) -> IO ()
traverseAllDirs ByteString
dir ByteString -> IO ()
cb = ByteString -> (ByteString -> IO Bool) -> IO ()
traverseAll ByteString
dir ((ByteString -> IO Bool) -> IO ())
-> (ByteString -> IO Bool) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ByteString
subPath ->
  -- TODO: wish we didn't need fromRawFilePath here
  -- TODO: should this follow symlinks? (What then about symlinks that escape the parent?)
  ByteString -> IO FilePath
fromRawFilePath ByteString
subPath IO FilePath -> (FilePath -> IO FileStatus) -> IO FileStatus
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= FilePath -> IO FileStatus
getSymbolicLinkStatus IO FileStatus -> (FileStatus -> IO Bool) -> IO Bool
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
    (FileStatus -> Bool
isDirectory -> Bool
True) -> ByteString -> IO ()
cb ByteString
subPath IO () -> IO Bool -> IO Bool
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Bool -> IO Bool
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
    FileStatus
_ -> Bool -> IO Bool
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False

traverseAll :: RawFilePath -> (RawFilePath -> IO Bool) -> IO ()
traverseAll :: ByteString -> (ByteString -> IO Bool) -> IO ()
traverseAll ByteString
dir ByteString -> IO Bool
cb = IO DirStream
-> (DirStream -> IO ()) -> (DirStream -> IO ()) -> IO ()
forall (m :: * -> *) a b c.
(HasCallStack, MonadMask m) =>
m a -> (a -> m b) -> (a -> m c) -> m c
bracket (ByteString -> IO DirStream
openDirStream ByteString
dir) DirStream -> IO ()
closeDirStream ((DirStream -> IO ()) -> IO ()) -> (DirStream -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \DirStream
dirStream ->
  (IO () -> IO ()) -> IO ()
forall a. (a -> a) -> a
fix ((IO () -> IO ()) -> IO ()) -> (IO () -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \IO ()
loop -> do
    DirStream -> IO ByteString
readDirStream DirStream
dirStream IO ByteString -> (ByteString -> IO ()) -> IO ()
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
      ByteString
x | ByteString -> Bool
BS.null ByteString
x -> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
      ByteString
"." -> IO ()
loop
      ByteString
".." -> IO ()
loop
      ByteString
subDir -> (IO () -> IO () -> IO ()) -> IO () -> IO () -> IO ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip IO () -> IO () -> IO ()
forall (m :: * -> *) a b.
(HasCallStack, MonadMask m) =>
m a -> m b -> m a
finally IO ()
loop (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
        -- TODO: canonicalize?
        let fullSubDir :: ByteString
fullSubDir = ByteString
dir ByteString -> ByteString -> ByteString
<//> ByteString
subDir
        shouldRecurse <- ByteString -> IO Bool
cb ByteString
fullSubDir
        when shouldRecurse $ traverseAll fullSubDir cb

boolToIsDirectory :: Bool -> EventIsDirectory
boolToIsDirectory :: Bool -> EventIsDirectory
boolToIsDirectory Bool
False = EventIsDirectory
IsFile
boolToIsDirectory Bool
True = EventIsDirectory
IsDirectory

toRawFilePath :: FilePath -> IO BS.ByteString
toRawFilePath :: FilePath -> IO ByteString
toRawFilePath FilePath
fp = do
  enc <- IO TextEncoding
getFileSystemEncoding
  F.withCString enc fp BS.packCString

fromRawFilePath :: BS.ByteString -> IO FilePath
fromRawFilePath :: ByteString -> IO FilePath
fromRawFilePath ByteString
bs = do
  enc <- IO TextEncoding
getFileSystemEncoding
  BS.useAsCString bs (F.peekCString enc)

#if MIN_VERSION_hinotify(0, 3, 10)
fromHinotifyPath :: BS.ByteString -> IO FilePath
fromHinotifyPath :: ByteString -> IO FilePath
fromHinotifyPath = ByteString -> IO FilePath
fromRawFilePath

rawToHinotifyPath :: BS.ByteString -> IO BS.ByteString
rawToHinotifyPath :: ByteString -> IO ByteString
rawToHinotifyPath = ByteString -> IO ByteString
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return

rawFromHinotifyPath :: BS.ByteString -> IO BS.ByteString
rawFromHinotifyPath :: ByteString -> IO ByteString
rawFromHinotifyPath = ByteString -> IO ByteString
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return
#else
fromHinotifyPath :: FilePath -> IO FilePath
fromHinotifyPath = return

rawToHinotifyPath :: BS.ByteString -> IO FilePath
rawToHinotifyPath = fromRawFilePath

rawFromHinotifyPath :: FilePath -> IO BS.ByteString
rawFromHinotifyPath = toRawFilePath
#endif