on master help sign in
Add Absolute Path to Windows and Linux Events
1ce52c2Ranjeet Kumar Ranjan
src/Streamly/Internal/FileSystem/Event/Linux.hs+19−1
@@ -112,6 +112,7 @@ module Streamly.Internal.FileSystem.Event.Linux
112 , Event(..)
112 , Event(..)
113 , getRoot
113 , getRoot
114 , getRelPath
114 , getRelPath
115+ , getAbsPath
115 , getCookie
116 , getCookie
116
117
117 -- ** Exception Conditions
118 -- ** Exception Conditions
@@ -172,6 +173,7 @@ import Streamly.Prelude (SerialT)
172import Streamly.Internal.Data.Parser (Parser)
173import Streamly.Internal.Data.Parser (Parser)
173import Streamly.Internal.Data.Array.Storable.Foreign.Types (Array(..))
174import Streamly.Internal.Data.Array.Storable.Foreign.Types (Array(..))
174import System.IO (Handle, hClose, IOMode(ReadMode))
175import System.IO (Handle, hClose, IOMode(ReadMode))
176+import System.IO.Unsafe (unsafePerformIO)
175#if !MIN_VERSION_base(4,10,0)
177#if !MIN_VERSION_base(4,10,0)
176import Control.Concurrent.MVar (readMVar)
178import Control.Concurrent.MVar (readMVar)
177import Data.Typeable (cast)
179import Data.Typeable (cast)
@@ -949,6 +951,21 @@ getRoot Event{..} =
949getRelPath :: Event -> Array Word8
951getRelPath :: Event -> Array Word8
950getRelPath Event{..} = eventRelPath
952getRelPath Event{..} = eventRelPath
951
953
954+
955+-- | Get the absolute file system object path for which the event is generated.
956+-- The path is a "/" separated array of bytes.
957+--
958+-- /Internal/
959+--
960+getAbsPath :: Event -> Array Word8
961+getAbsPath ev0 = go ev0
962+
963+ where
964+
965+ go ev = unsafePerformIO
966+ $ A.fromStream
967+ $ (A.toStream (getRoot ev) <> A.toStream (getRelPath ev))
968+
952-- XXX should we use a Maybe?
969-- XXX should we use a Maybe?
953-- | Cookie is set when a rename occurs. The cookie value can be used to
970-- | Cookie is set when a rename occurs. The cookie value can be used to
954-- connect the 'isMovedFrom' and 'isMovedTo' events, if both the events belong
971-- connect the 'isMovedFrom' and 'isMovedTo' events, if both the events belong
@@ -1188,7 +1205,8 @@ showEvent ev@Event{..} =
1188 "--------------------------"
1205 "--------------------------"
1189 ++ "\nWd = " ++ show eventWd
1206 ++ "\nWd = " ++ show eventWd
1190 ++ "\nRoot = " ++ show (utf8ToString $ getRoot ev)
1207 ++ "\nRoot = " ++ show (utf8ToString $ getRoot ev)
1191− ++ "\nPath = " ++ show (utf8ToString $ getRelPath ev)
1208+ ++ "\nRelative Path = " ++ show (utf8ToString $ getRelPath ev)
1209+ ++ "\nAbsolute Path = " ++ show (utf8ToString $ getAbsPath ev)
1192 ++ "\nCookie = " ++ show (getCookie ev)
1210 ++ "\nCookie = " ++ show (getCookie ev)
1193 ++ "\nFlags " ++ show eventFlags
1211 ++ "\nFlags " ++ show eventFlags
1194
1212
src/Streamly/Internal/FileSystem/Event/Windows.hs+27−2
@@ -74,6 +74,7 @@ module Streamly.Internal.FileSystem.Event.Windows
74 , watchTreesWith
74 , watchTreesWith
75
75
76 -- * Handling Events
76 -- * Handling Events
77+ , getAbsPath
77 , getRelPath
78 , getRelPath
78 , getRoot
79 , getRoot
79
80
@@ -463,6 +464,19 @@ watchTreesWith f paths =
463watchTrees :: NonEmpty (Array Word8) -> SerialT IO Event
464watchTrees :: NonEmpty (Array Word8) -> SerialT IO Event
464watchTrees = watchTreesWith id
465watchTrees = watchTreesWith id
465
466
467+-- | Add a trailing "\" at the end of the path if there is none. Do not add a
468+-- "\" if the path is empty.
469+--
470+ensureTrailingSlash :: String -> String
471+ensureTrailingSlash path =
472+ if null path
473+ then path
474+ else
475+ let x = last path
476+ in if x /= '\\' && x /= '/'
477+ then path ++ "\\"
478+ else path
479+
466getFlag :: DWORD -> Event -> Bool
480getFlag :: DWORD -> Event -> Bool
467getFlag mask Event{..} = eventFlags == mask
481getFlag mask Event{..} = eventFlags == mask
468
482
@@ -483,7 +497,17 @@ getRelPath Event{..} = eventRelPath
483-- /Internal/
497-- /Internal/
484--
498--
485getRoot :: Event -> String
499getRoot :: Event -> String
486−getRoot Event{..} = eventRootPath
500+getRoot Event{..} = ensureTrailingSlash eventRootPath
501+
502+-- XXX Change the type to Array Word8 to make it compatible with other APIs.
503+--
504+-- | Get the absolute file system object path for which the event is generated.
505+-- The path is a UTF-8 encoded array of bytes.
506+--
507+-- /Internal/
508+--
509+getAbsPath :: Event -> String
510+getAbsPath ev = getRoot ev <> getRelPath ev
487
511
488-- XXX need to document the exact semantics of these.
512-- XXX need to document the exact semantics of these.
489--
513--
@@ -545,7 +569,8 @@ showEvent :: Event -> String
545showEvent ev@Event{..} =
569showEvent ev@Event{..} =
546 "--------------------------"
570 "--------------------------"
547 ++ "\nRoot = " ++ show (getRoot ev)
571 ++ "\nRoot = " ++ show (getRoot ev)
548− ++ "\nPath = " ++ show (getRelPath ev)
572+ ++ "\nRelative Path = " ++ show (getRelPath ev)
573+ ++ "\nAbsolute Path = " ++ show (getAbsPath ev)
549 ++ "\nFlags " ++ show eventFlags
574 ++ "\nFlags " ++ show eventFlags
550 ++ showev isOverflow "Overflow"
575 ++ showev isOverflow "Overflow"
551 ++ showev isCreated "Created"
576 ++ showev isCreated "Created"