diff options
author | Simon Marlow <marlowsd@gmail.com> | 2008-08-21 14:47:54 +0000 |
---|---|---|
committer | Simon Marlow <marlowsd@gmail.com> | 2008-08-21 14:47:54 +0000 |
commit | c1180fec9f1121323b519ea86fd730b29f1b2f6d (patch) | |
tree | 9ce4b4527c5d6428eaa08078077254dc5ab303fd | |
parent | 1e118d9a64e928a07f9c7c3a64b4b22e5fca821c (diff) |
move some stuff here from System.Directory, now the dependencies are reversed
-rw-r--r-- | System/Posix/Directory.hsc | 42 | ||||
-rw-r--r-- | System/Posix/Files.hsc | 3 | ||||
-rw-r--r-- | cbits/HsUnix.c | 1 | ||||
-rw-r--r-- | include/HsUnix.h | 13 | ||||
-rw-r--r-- | unix.cabal | 2 |
5 files changed, 57 insertions, 4 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() diff --git a/cbits/HsUnix.c b/cbits/HsUnix.c index ebaeed3..5b62798 100644 --- a/cbits/HsUnix.c +++ b/cbits/HsUnix.c @@ -10,3 +10,4 @@ // Out-of-line versions of all the inline functions from HsUnix.h #define INLINE /* nothing */ #include "HsUnix.h" + diff --git a/include/HsUnix.h b/include/HsUnix.h index 4afd51a..5acdb74 100644 --- a/include/HsUnix.h +++ b/include/HsUnix.h @@ -10,6 +10,7 @@ #define HSUNIX_H #include "HsUnixConfig.h" +#include "HsFFI.h" /* ultra-evil... */ #undef PACKAGE_BUGREPORT @@ -200,4 +201,16 @@ INLINE int __hsunix_unsetenv(const char *name) #endif } +/* A size that will contain many path names, but not necessarily all + * (PATH_MAX is not defined on systems with unlimited path length, + * e.g. the Hurd). + */ +INLINE HsInt __hsunix_long_path_size() { +#ifdef PATH_MAX + return PATH_MAX; +#else + return 4096; +#endif +} + #endif @@ -41,7 +41,7 @@ extra-source-files: extra-tmp-files: config.log config.status autom4te.cache unix.buildinfo include/HsUnixConfig.h -build-depends: base, directory +build-depends: base extensions: CPP, ForeignFunctionInterface include-dirs: include includes: HsUnix.h execvpe.h |