aboutsummaryrefslogtreecommitdiffhomepage
path: root/System
diff options
context:
space:
mode:
authorGravatar Simon Marlow <marlowsd@gmail.com>2008-08-21 14:47:54 +0000
committerGravatar Simon Marlow <marlowsd@gmail.com>2008-08-21 14:47:54 +0000
commitc1180fec9f1121323b519ea86fd730b29f1b2f6d (patch)
tree9ce4b4527c5d6428eaa08078077254dc5ab303fd /System
parent1e118d9a64e928a07f9c7c3a64b4b22e5fca821c (diff)
move some stuff here from System.Directory, now the dependencies are reversed
Diffstat (limited to 'System')
-rw-r--r--System/Posix/Directory.hsc42
-rw-r--r--System/Posix/Files.hsc3
2 files changed, 42 insertions, 3 deletions
diff --git a/System/Posix/Directory.hsc b/System/Posix/Directory.hsc
index 6e220f5..a4bdf6d 100644
--- a/System/Posix/Directory.hsc
+++ b/System/Posix/Directory.hsc
@@ -33,10 +33,11 @@ module System.Posix.Directory (
changeWorkingDirectoryFd,
) where
+import System.IO.Error
import System.Posix.Error
import System.Posix.Types
import System.Posix.Internals
-import System.Directory hiding (createDirectory)
+--import System.Directory hiding (createDirectory)
import Foreign
import Foreign.C
@@ -124,12 +125,47 @@ foreign import ccall unsafe "telldir"
-- | @getWorkingDirectory@ calls @getcwd@ to obtain the name
-- of the current working directory.
getWorkingDirectory :: IO FilePath
-getWorkingDirectory = getCurrentDirectory
+getWorkingDirectory = do
+ p <- mallocBytes long_path_size
+ go p long_path_size
+ where go p bytes = do
+ p' <- c_getcwd p (fromIntegral bytes)
+ if p' /= nullPtr
+ then do s <- peekCString p'
+ free p'
+ return s
+ else do errno <- getErrno
+ if errno == eRANGE
+ then do let bytes' = bytes * 2
+ p'' <- reallocBytes p bytes'
+ go p'' bytes'
+ else throwErrno "getCurrentDirectory"
+
+foreign import ccall unsafe "getcwd"
+ c_getcwd :: Ptr CChar -> CSize -> IO (Ptr CChar)
+
+foreign import ccall unsafe "__hsunix_long_path_size"
+ long_path_size :: Int
-- | @changeWorkingDirectory dir@ calls @chdir@ to change
-- the current working directory to @dir@.
changeWorkingDirectory :: FilePath -> IO ()
-changeWorkingDirectory name = setCurrentDirectory name
+changeWorkingDirectory path =
+ modifyIOError (`ioeSetFileName` path) $
+ withCString path $ \s ->
+ throwErrnoIfMinus1Retry_ "changeWorkingDirectory" (c_chdir s)
+
+foreign import ccall unsafe "chdir"
+ c_chdir :: CString -> IO CInt
+
+removeDirectory :: FilePath -> IO ()
+removeDirectory path =
+ modifyIOError (`ioeSetFileName` path) $
+ withCString path $ \s ->
+ throwErrnoIfMinus1Retry_ "removeDirectory" (c_rmdir s)
+
+foreign import ccall unsafe "rmdir"
+ c_rmdir :: CString -> IO CInt
changeWorkingDirectoryFd :: Fd -> IO ()
changeWorkingDirectoryFd (Fd fd) =
diff --git a/System/Posix/Files.hsc b/System/Posix/Files.hsc
index c15c92c..bc61255 100644
--- a/System/Posix/Files.hsc
+++ b/System/Posix/Files.hsc
@@ -502,6 +502,9 @@ rename name1 name2 =
withCString name2 $ \s2 ->
throwErrnoPathIfMinus1_ "rename" name1 (c_rename s1 s2)
+foreign import ccall unsafe "rename"
+ c_rename :: CString -> CString -> IO CInt
+
-- -----------------------------------------------------------------------------
-- chown()