Skip to content

Commit

Permalink
Use ByteString for FilePaths.
Browse files Browse the repository at this point in the history
Instead of String.
The linux file systems deal with raw bytes and doesn't care about encodings.
GTK always uses UTF-8, QT uses the user's locale.
By using ByteString, we let the libraries' users decide what they want to use.
  • Loading branch information
kolmodin committed May 1, 2016
1 parent edf41b8 commit 11c4d31
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 22 deletions.
2 changes: 1 addition & 1 deletion hinotify.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ source-repository head
location: git://github.com/kolmodin/hinotify.git

library
build-depends: base >= 4.5.0.0 && < 5, containers, directory, unix
build-depends: base >= 4.5.0.0 && < 5, bytestring, containers, directory, unix
extensions: ForeignFunctionInterface

exposed-modules:
Expand Down
31 changes: 14 additions & 17 deletions src/System/INotify.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ import Prelude hiding (init)
import Control.Monad
import Control.Concurrent
import Control.Exception as E (bracket, catch, mask_, SomeException)
import qualified Data.ByteString as B
import Data.Maybe
import Data.Map (Map)
import qualified Data.Map as Map
import Foreign.C hiding (withCString, peekCString)
import Foreign.C
import Foreign.Marshal hiding (void)
import Foreign.Ptr
import Foreign.Storable
Expand All @@ -51,8 +52,6 @@ import GHC.IO.Handle.FD (fdToHandle')
import GHC.IO.Device (IODeviceType(Stream))

import System.Posix.Files
import GHC.IO.Encoding (getFileSystemEncoding)
import GHC.Foreign (withCString, peekCString)

import System.INotify.Masks

Expand All @@ -71,45 +70,45 @@ instance Eq INotify where

newtype Cookie = Cookie CUInt deriving (Eq,Ord)

data FDEvent = FDEvent WD Masks CUInt{-Cookie-} (Maybe String) deriving (Eq, Show)
data FDEvent = FDEvent WD Masks CUInt{-Cookie-} (Maybe B.ByteString) deriving (Eq, Show)

data Event =
-- | A file was accessed. @Accessed isDirectory file@
Accessed
{ isDirectory :: Bool
, maybeFilePath :: Maybe FilePath
, maybeFilePath :: Maybe B.ByteString
}
-- | A file was modified. @Modified isDirectory file@
| Modified
{ isDirectory :: Bool
, maybeFilePath :: Maybe FilePath
, maybeFilePath :: Maybe B.ByteString
}
-- | A files attributes where changed. @Attributes isDirectory file@
| Attributes
{ isDirectory :: Bool
, maybeFilePath :: Maybe FilePath
, maybeFilePath :: Maybe B.ByteString
}
-- | A file was closed. @Closed isDirectory file wasWriteable@
| Closed
{ isDirectory :: Bool
, maybeFilePath :: Maybe FilePath
, maybeFilePath :: Maybe B.ByteString
, wasWriteable :: Bool
}
-- | A file was opened. @Opened isDirectory maybeFilePath@
| Opened
{ isDirectory :: Bool
, maybeFilePath :: Maybe FilePath
, maybeFilePath :: Maybe B.ByteString
}
-- | A file was moved away from the watched dir. @MovedFrom isDirectory from cookie@
| MovedOut
{ isDirectory :: Bool
, filePath :: FilePath
, filePath :: B.ByteString
, moveCookie :: Cookie
}
-- | A file was moved into the watched dir. @MovedTo isDirectory to cookie@
| MovedIn
{ isDirectory :: Bool
, filePath :: FilePath
, filePath :: B.ByteString
, moveCookie :: Cookie
}
-- | The watched file was moved. @MovedSelf isDirectory@
Expand All @@ -119,12 +118,12 @@ data Event =
-- | A file was created. @Created isDirectory file@
| Created
{ isDirectory :: Bool
, filePath :: FilePath
, filePath :: B.ByteString
}
-- | A file was deleted. @Deleted isDirectory file@
| Deleted
{ isDirectory :: Bool
, filePath :: FilePath
, filePath :: B.ByteString
}
-- | The file watched was deleted.
| DeletedSelf
Expand Down Expand Up @@ -188,8 +187,7 @@ addWatch inotify@(INotify _ fd em _ _) masks fp cb = do
Nothing
(Just fp)
let mask = joinMasks (map eventVarietyToMask masks)
enc <- getFileSystemEncoding
wd <- withCString enc fp $ \fp_c ->
wd <- withCString fp $ \fp_c ->
throwErrnoIfMinus1 "addWatch" $
c_inotify_add_watch (fromIntegral fd) fp_c mask
let event = \e -> ignore_failure $ do
Expand Down Expand Up @@ -261,8 +259,7 @@ read_events h =
nameM <- if len == 0
then return Nothing
else do
enc <- getFileSystemEncoding
fmap Just $ peekCString enc ((#ptr struct inotify_event, name) ptr)
fmap Just $ B.packCString ((#ptr struct inotify_event, name) ptr)
let event_size = (#size struct inotify_event) + (fromIntegral len)
event = cEvent2Haskell (FDEvent wd mask cookie nameM)
rest <- read_events' (ptr `plusPtr` event_size) (r - event_size)
Expand Down
1 change: 1 addition & 0 deletions tests/test002-writefile.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE OverloadedStrings #-}
module Main where

import Control.Monad
Expand Down
5 changes: 4 additions & 1 deletion tests/test003-removefile.hs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
{-# LANGUAGE OverloadedStrings #-}
module Main where

import Control.Monad

import Data.String

import System.Directory

import System.INotify as INotify

import Utils

file :: String
file :: IsString s => s
file = "hello"

write :: String -> IO ()
Expand Down
5 changes: 4 additions & 1 deletion tests/test004-modify-file.hs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
{-# LANGUAGE OverloadedStrings #-}
module Main where

import Control.Exception
import Control.Monad

import Data.String

import System.Directory
import System.IO

import System.INotify as INotify

import Utils

file :: String
file :: IsString s => s
file = "hello"

write :: String -> IO ()
Expand Down
8 changes: 6 additions & 2 deletions tests/test005-move-file.hs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
{-# LANGUAGE OverloadedStrings #-}
module Main where

import Control.Monad

import Data.String

import System.Directory

import System.INotify as INotify

import Utils

file, file2 :: String
file, file2 :: IsString s => s
file = "hello"
file2 = file ++ "2"
file2 = "hello2"

write :: String -> IO ()
write path = do
Expand Down

0 comments on commit 11c4d31

Please sign in to comment.