Remove Data.FiniteMap, add Control.Applicative, Data.Traversable, and
[haskell-directory.git] / System / Directory.hs
index 5e87c21..e5e988f 100644 (file)
@@ -507,26 +507,47 @@ renameFile opath npath =
 
 {- |@'copyFile' old new@ copies the existing file from /old/ to /new/.
 If the /new/ file already exists, it is atomically replaced by the /old/ file.
-Neither path may refer to an existing directory.
+Neither path may refer to an existing directory.  The permissions of /old/ are
+copied to /new/, if possible.
+-}
+
+{- NOTES:
+
+It's tempting to try to remove the target file before opening it for
+writing.  This could be useful: for example if the target file is an
+executable that is in use, writing will fail, but unlinking first
+would succeed.
+
+However, it certainly isn't always what you want.
+
+ * if the target file is hardlinked, removing it would break
+   the hard link, but just opening would preserve it.
+
+ * opening and truncating will preserve permissions and
+   ACLs on the target.
+
+ * If the destination file is read-only in a writable directory,
+   we might want copyFile to fail.  Removing the target first
+   would succeed, however.
+
+ * If the destination file is special (eg. /dev/null), removing
+   it is probably not the right thing.  Copying to /dev/null
+   should leave /dev/null intact, not replace it with a plain
+   file.
+
+ * There's a small race condition between removing the target and
+   opening it for writing during which time someone might
+   create it again.
 -}
 copyFile :: FilePath -> FilePath -> IO ()
-copyFile fromFPath toFPath = do
-       -- We try removing the target file before opening it for
-       -- writing.  In the event that the target file is locked or in
-       -- use, this allows us to replace it safely.  However, it
-       -- leaves a race condition: someone else might create the file
-       -- after we delete it, but there isn't much we can do about
-       -- that.
+copyFile fromFPath toFPath =
 #if (!(defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ > 600))
-       contents <- readFile fromFPath
-       try (removeFile toFPath)
-       writeFile toFPath contents
-       try (copyPermissions fromFPath toFPath)
-       return ()
+       do readFile fromFPath >>= writeFile toFPath
+          try (copyPermissions fromFPath toFPath)
+          return ()
 #else
-       (bracket (openBinaryFile fromFPath ReadMode) hClose $ \hFrom -> do
-        try (removeFile toFPath)
-        bracket (openBinaryFile toFPath WriteMode) hClose $ \hTo -> do
+       (bracket (openBinaryFile fromFPath ReadMode) hClose $ \hFrom ->
+        bracket (openBinaryFile toFPath WriteMode) hClose $ \hTo ->
         allocaBytes bufferSize $ \buffer -> do
                copyContents hFrom hTo buffer
                try (copyPermissions fromFPath toFPath)