Index: includes/file.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/file.inc,v
retrieving revision 1.214
diff -u -p -r1.214 file.inc
--- includes/file.inc	20 Jun 2010 23:53:52 -0000	1.214
+++ includes/file.inc	21 Jun 2010 23:02:25 -0000
@@ -676,8 +676,34 @@ function file_unmanaged_copy($source, $d
   }
   // Make sure the .htaccess files are present.
   file_ensure_htaccess();
+
+    // Perform the replace operation. copy() is not atomic but rename() is.
+    // Since there could be mutiple processes writing to the same file the best
+    // option is to create a temp file in the same directory and then rename it
+    // to the final filename.
+    $result = FALSE;
+    if ($replace == FILE_EXISTS_REPLACE) {
+      // Get temp filename
+      $temp_name = uniqid(realpath($destinationm), TRUE) . '.tmp';
+      // Place new contents in temp file
+      if (@copy($source, $temp_name)) {
+        // Try the rename opperation
+        if (!$result = @rename($temp_name, $dest)) {
+          // Unlink and try again for windows since rename on windows does not
+          // replace the file if it already exists.
+          @unlink($dest);
+          $result = @rename($temp_name, $dest);
+        }
+      }
+      else {
+        $result = FALSE;
+      }
+    }
   // Perform the copy operation.
-  if (!@copy($source, $destination)) {
+  else {
+    $result = @copy($source, $destination);
+  }
+  if ($result === FALSE) {
     drupal_set_message(t('The specified file %file could not be copied.', array('%file' => $source)), 'error');
     return FALSE;
   }
