Index: lib/ezfile/classes/ezatomicfile.php
===================================================================
--- lib/ezfile/classes/ezatomicfile.php	(revision 0)
+++ lib/ezfile/classes/ezatomicfile.php	(revision 0)
@@ -0,0 +1,102 @@
+<?php
+//
+// Definition of eZAtomicFile class
+//
+// Created on: <09-May-2007 13:43:12 oh>
+//
+// ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
+// SOFTWARE NAME: eZ publish
+// SOFTWARE RELEASE: 3.8.x
+// COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
+// SOFTWARE LICENSE: GNU General Public License v2.0
+// NOTICE: >
+//   This program is free software; you can redistribute it and/or
+//   modify it under the terms of version 2.0  of the GNU General
+//   Public License as published by the Free Software Foundation.
+//
+//   This program is distributed in the hope that it will be useful,
+//   but WITHOUT ANY WARRANTY; without even the implied warranty of
+//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//   GNU General Public License for more details.
+//
+//   You should have received a copy of version 2.0 of the GNU General
+//   Public License along with this program; if not, write to the Free
+//   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+//   MA 02110-1301, USA.
+//
+//
+// ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
+//
+
+/*! \file ezatomicfile.php
+*/
+
+include_once( "lib/ezfile/classes/ezfile.php" );
+include_once( 'lib/ezfile/classes/ezdir.php' );
+
+class eZAtomicFile
+{
+    function eZAtomicFile( $filename, $permission = false )
+    {
+        // Use default permissions if non was provided
+        if ( !$permission )
+        {
+            $ini =& eZINI::instance();
+            $this->Permission = octdec( $ini->variable( 'FileSettings', 'StorageDirPermissions' ) );            
+        }
+        else
+        {
+            $this->Permission = $permission;
+        }
+
+        // Create a unique hash which we'll append to the file name.
+        $uniqueHash = md5( uniqid( "ezp-atmoic-file". getmypid(), true ) );
+        $this->TempFileName = $filename . $uniqueHash;
+        $this->FileName = $filename;
+
+        // Make sure the directory the file will be stored in exists.
+        $directory = dirname( $filename );
+        if ( !file_exists( $directory ) )
+        {
+            $result = eZDir::mkdir( $directory, $this->Permission, true );
+            if ( !$result )
+            {
+                eZDebug::writeError( "Could not create directory '$directory', perhaps wrong permissions" );
+            }
+        }
+
+        // Open our temporary file
+        $this->FileHandler = fopen( $this->TempFileName, "w" );
+        if ( !$this->FileHandler )
+        {
+            eZDebug::writeError( "Could not open file '$filename' for writing, perhaps wrong permissions" );
+        }
+    }
+    
+    function write( $content )
+    {
+        fwrite( $this->FileHandler, $content );
+    }
+    
+    function close()
+    {
+        fclose( $this->FileHandler );
+        
+        // Only set permissions if the file didn't exists.
+        $doChmod = false;
+        if ( !file_exists( $this->FileName ) )
+            $doChmod = true;
+
+        // The renaming is the key which makes this class atomic.
+        eZFile::rename( $this->TempFileName, $this->FileName );
+        
+        if ( $doChmod )
+            chmod( $this->FileName, $this->Permission );
+        
+    }
+    
+    var $FileHandler;
+    var $FileName;
+    var $TempFileName;
+    var $Permission;
+}
\ No newline at end of file
Index: kernel/classes/eznodeviewfunctions.php
===================================================================
--- kernel/classes/eznodeviewfunctions.php	(revision 17063)
+++ kernel/classes/eznodeviewfunctions.php	(working copy)
@@ -157,31 +157,11 @@
         {
             $serializeString = serialize( $Result );
 
-            if ( !file_exists( $cacheDir ) )
-            {
-                include_once( 'lib/ezfile/classes/ezdir.php' );
-                $ini =& eZINI::instance();
-                $perm = octdec( $ini->variable( 'FileSettings', 'StorageDirPermissions' ) );
-                eZDir::mkdir( $cacheDir, $perm, true );
-            }
-            $oldumask = umask( 0 );
-            $pathExisted = file_exists( $cachePath );
-            $ini =& eZINI::instance();
-            $perm = octdec( $ini->variable( 'FileSettings', 'StorageFilePermissions' ) );
-            $fp = @fopen( $cachePath, "w" );
-            if ( !$fp )
-                eZDebug::writeError( "Could not open file '$cachePath' for writing, perhaps wrong permissions" );
-            if ( $fp and
-                 !$pathExisted )
-                chmod( $cachePath, $perm );
-            umask( $oldumask );
+            include_once( "lib/ezfile/classes/ezatomicfile.php" );
+            $cacheFile = new eZAtomicFile( $cachePath );
+            $cacheFile->write( $serializeString );
+            $cacheFile->close();
 
-            if ( $fp )
-            {
-                fwrite( $fp, $serializeString );
-                fclose( $fp );
-            }
-
             // VS-DBFILE
 
             require_once( 'kernel/classes/ezclusterfilehandler.php' );
