diff --git a/core/lib/Drupal/Component/FileSystem/Filesystem.php b/core/lib/Drupal/Component/FileSystem/Filesystem.php
new file mode 100644
index 0000000..feaee47
--- /dev/null
+++ b/core/lib/Drupal/Component/FileSystem/Filesystem.php
@@ -0,0 +1,79 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\Component\FileSystem\Filesystem.
+ */
+
+namespace Drupal\Component\FileSystem;
+
+/**
+ * Provides a compatiblity layer that is platform aware
+ * for common file functions.
+ */
+class Filesystem {
+
+  /**
+   * Renames a file or directory.
+   *
+   * @param string $oldname
+   *   The old name. The wrapper used in oldname must match the wrapper used in newname.
+   * @param string $newname
+   *   The new name.
+   * @param bool $overwrite
+   *   Wether to overwrite the destination file if it already exists.
+   */
+  public static function rename ($oldname , $newname, $overwrite = false) {
+    $exists = self::isReadable($newname);
+    if (!$overwrite && $exists) {
+      throw new \Exception(sprintf('Cannot rename because the target "%s" already exists.', $newname), 0, null, $newname);
+    }
+    if ($exists && !self::isWritable($newname)) {
+      chmod($newname, 0755);
+    }
+    return rename($oldname, $newname);
+  }
+
+  /**
+   * Tells whether a file exists and is readable.
+   *
+   * @param string $filename
+   *   Path to the file.
+   * @throws \Exception
+   *   When windows path is longer than 258 characters
+   * @return bool
+   */
+  private static function isReadable($filename) {
+    if (self::isWindows() && strlen($filename) > 258) {
+      throw new \Exception('Could not check if file is readable because path length exceeds 258 characters.', 0, null, $filename);
+    }
+
+    return is_readable($filename);
+  }
+
+  /**
+   * Tells whether a file exists and is writable.
+   *
+   * @param string $filename
+   *   Path to the file.
+   * @throws \Exception
+   *   When windows path is longer than 258 characters
+   * @return bool
+   */
+  private static function isWritable($filename) {
+    if (self::isWindows() && strlen($filename) > 258) {
+      throw new \Exception('Could not check if file is readable because path length exceeds 258 characters.', 0, null, $filename);
+    }
+
+    return is_writable($filename);
+  }
+
+  /**
+   * Check if this is a Windows system.
+   *
+   * @return bool
+   */
+  public static function isWindows() {
+    return '\\' === DIRECTORY_SEPARATOR;
+  }
+}
diff --git a/core/lib/Drupal/Component/PhpStorage/MTimeProtectedFastFileStorage.php b/core/lib/Drupal/Component/PhpStorage/MTimeProtectedFastFileStorage.php
index 87d9d2a..9abecf9 100644
--- a/core/lib/Drupal/Component/PhpStorage/MTimeProtectedFastFileStorage.php
+++ b/core/lib/Drupal/Component/PhpStorage/MTimeProtectedFastFileStorage.php
@@ -95,7 +95,7 @@ public function save($name, $data) {
     $directory = $this->getContainingDirectoryFullPath($name);
     $this->ensureDirectory($directory);
     $full_path = $this->getFullPath($name, $directory, $mtime);
-    $result = rename($temporary_path, $full_path);
+    $result = \Drupal\Component\FileSystem\Filesystem::rename($temporary_path, $full_path, TRUE);
 
     // Finally reset the modification time of the directory to match the one of
     // the newly created file. In order to prevent the creation of a file if the
diff --git a/core/lib/Drupal/Core/Template/TwigPhpStorageCache.php b/core/lib/Drupal/Core/Template/TwigPhpStorageCache.php
index 4d07643..e1f0ea2 100644
--- a/core/lib/Drupal/Core/Template/TwigPhpStorageCache.php
+++ b/core/lib/Drupal/Core/Template/TwigPhpStorageCache.php
@@ -106,10 +106,11 @@ public function load($key) {
    * {@inheritdoc}
    */
   public function write($key, $content) {
-    $this->storage()->save($key, $content);
-    // Save the last mtime.
-    $cid = 'twig:' . $key;
-    $this->cache->set($cid, REQUEST_TIME);
+    if($this->storage()->save($key, $content)) {
+      // Save the last mtime.
+      $cid = 'twig:' . $key;
+      $this->cache->set($cid, REQUEST_TIME);
+    }
   }
 
   /**
