Merge branch 'w08_MDL-38154_m25_delperf' of git://github.com/skodak/moodle
authorDan Poltawski <dan@moodle.com>
Tue, 26 Feb 2013 03:08:52 +0000 (11:08 +0800)
committerDan Poltawski <dan@moodle.com>
Tue, 26 Feb 2013 03:08:52 +0000 (11:08 +0800)
lib/filestorage/file_storage.php
lib/filestorage/stored_file.php

index dfb70d8..8a40431 100644 (file)
@@ -1718,6 +1718,11 @@ class file_storage {
     public function deleted_file_cleanup($contenthash) {
         global $DB;
 
+        if ($contenthash === sha1('')) {
+            // No need to delete empty content file with sha1('') content hash.
+            return;
+        }
+
         //Note: this section is critical - in theory file could be reused at the same
         //      time, if this happens we can still recover the file from trash
         if ($DB->record_exists('files', array('contenthash'=>$contenthash))) {
index fee7bfa..84d9510 100644 (file)
@@ -275,26 +275,32 @@ class stored_file {
     public function delete() {
         global $DB;
 
-        $transaction = $DB->start_delegated_transaction();
+        if ($this->is_directory()) {
+            // Directories can not be referenced, just delete the record.
+            $DB->delete_records('files', array('id'=>$this->file_record->id));
 
-        // If there are other files referring to this file, convert them to copies.
-        if ($files = $this->fs->get_references_by_storedfile($this)) {
-            foreach ($files as $file) {
-                $this->fs->import_external_file($file);
+        } else {
+            $transaction = $DB->start_delegated_transaction();
+
+            // If there are other files referring to this file, convert them to copies.
+            if ($files = $this->fs->get_references_by_storedfile($this)) {
+                foreach ($files as $file) {
+                    $this->fs->import_external_file($file);
+                }
             }
-        }
 
-        // If this file is a reference (alias) to another file, unlink it first.
-        if ($this->is_external_file()) {
-            $this->delete_reference();
-        }
+            // If this file is a reference (alias) to another file, unlink it first.
+            if ($this->is_external_file()) {
+                $this->delete_reference();
+            }
 
-        // Now delete the file record.
-        $DB->delete_records('files', array('id'=>$this->file_record->id));
+            // Now delete the file record.
+            $DB->delete_records('files', array('id'=>$this->file_record->id));
 
-        $transaction->allow_commit();
+            $transaction->allow_commit();
+        }
 
-        // moves pool file to trash if content not needed any more
+        // Move pool file to trash if content not needed any more.
         $this->fs->deleted_file_cleanup($this->file_record->contenthash);
         return true; // BC only
     }