From ae0bde37d4fc6705f8beab381906cc220f71aaa2 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Thu, 5 Feb 2015 16:56:19 -0600
Subject: [PATCH] files: import: Don't call flush() if write() was not called

---
 setup/cli/modules/file.php | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/setup/cli/modules/file.php b/setup/cli/modules/file.php
index 2761ac468..2432a2937 100644
--- a/setup/cli/modules/file.php
+++ b/setup/cli/modules/file.php
@@ -319,6 +319,7 @@ class FileManager extends Module {
                 // Write file contents to the backend
                 $md5 = hash_init('md5');
                 $sha1 = hash_init('sha1');
+                $written = 0;
 
                 // Handle exceptions by dropping imported file contents and
                 // then returning the error to the error output stream.
@@ -348,8 +349,11 @@ class FileManager extends Module {
                         hash_update($md5, $contents);
                         hash_update($sha1, $contents);
                         $dlen -= strlen($contents);
+                        $written += strlen($contents);
                     }
-                    if (!$bk->flush())
+                    // Some backends cannot handle flush() without a
+                    // corresponding write() call.
+                    if ($written && !$bk->flush())
                         throw new Exception(
                             'Unable to commit file contents to backend');
 
-- 
GitLab