[project @ 2001-08-21 08:53:22 by simonmar]
authorsimonmar <unknown>
Tue, 21 Aug 2001 08:53:22 +0000 (08:53 +0000)
committersimonmar <unknown>
Tue, 21 Aug 2001 08:53:22 +0000 (08:53 +0000)
Allow renameFile to rename dangling symlinks.  We use lstat, but check
for HAVE_LSTAT first.

ghc/lib/std/Directory.hsc

index ee457cd..c03407d 100644 (file)
@@ -1,5 +1,5 @@
 -- -----------------------------------------------------------------------------
--- $Id: Directory.hsc,v 1.14 2001/08/10 13:48:06 simonmar Exp $
+-- $Id: Directory.hsc,v 1.15 2001/08/21 08:53:22 simonmar Exp $
 --
 -- (c) The University of Glasgow, 1994-2000
 --
@@ -300,7 +300,7 @@ Either path refers to an existing directory.
 
 renameFile :: FilePath -> FilePath -> IO ()
 renameFile opath npath =
-   withFileStatus opath $ \st -> do
+   withFileOrSymlinkStatus opath $ \st -> do
    is_dir <- isDirectory st
    if is_dir
        then ioException (IOError Nothing InappropriateType "renameFile"
@@ -504,6 +504,17 @@ withFileStatus name f = do
         throwErrnoIfMinus1Retry_ "withFileStatus" (stat s p)
        f p
 
+withFileOrSymlinkStatus :: FilePath -> (Ptr CStat -> IO a) -> IO a
+#ifdef HAVE_LSTAT
+withFileOrSymlinkStatus name f = do
+    allocaBytes (#const sizeof(struct stat)) $ \p ->
+      withCString name $ \s -> do
+        throwErrnoIfMinus1Retry_ "withFileOrSymlinkStatus" (lstat s p)
+       f p
+#else
+withFileOrSymlinkStatus = withFileStatus
+#endif
+
 modificationTime :: Ptr CStat -> IO ClockTime
 modificationTime stat = do
     mtime <- (#peek struct stat, st_mtime) stat
@@ -551,4 +562,8 @@ foreign import ccall unsafe closedir :: Ptr CDir -> IO CInt
 
 foreign import ccall unsafe stat     :: CString -> Ptr CStat -> IO CInt
 
+#ifdef HAVE_LSTAT
+foreign import ccall unsafe lstat    :: CString -> Ptr CStat -> IO CInt
+#endif
+
 type CDirent = ()