MDL-58297 core: Update to use new hashing functions
authorAndrew Nicols <andrew@nicols.co.uk>
Fri, 17 Mar 2017 07:21:59 +0000 (15:21 +0800)
committerAndrew Nicols <andrew@nicols.co.uk>
Fri, 7 Apr 2017 02:40:23 +0000 (10:40 +0800)
backup/converter/moodle1/lib.php
backup/converter/moodle1/tests/moodle1_converter_test.php
lib/upgradelib.php
mod/assign/feedback/file/importziplib.php
repository/filesystem/lib.php
repository/lib.php

index 6710afd..6e8382a 100644 (file)
@@ -1379,7 +1379,7 @@ class moodle1_file_manager implements loggable {
     protected function make_file_record(array $fileinfo) {
 
         $defaultrecord = array(
-            'contenthash'   => 'da39a3ee5e6b4b0d3255bfef95601890afd80709',  // sha1 of an empty file
+            'contenthash'   => file_storage::hash_from_string(''),
             'contextid'     => $this->contextid,
             'component'     => $this->component,
             'filearea'      => $this->filearea,
@@ -1422,7 +1422,7 @@ class moodle1_file_manager implements loggable {
             throw new moodle1_convert_exception('file_not_readable');
         }
 
-        $contenthash = sha1_file($pathname);
+        $contenthash = file_storage::hash_from_path($pathname);
         $filesize    = filesize($pathname);
         $hashpath    = $this->converter->get_workdir_path().'/files/'.substr($contenthash, 0, 2);
         $hashfile    = "$hashpath/$contenthash";
index 88681b8..6502ef3 100644 (file)
@@ -64,7 +64,7 @@ class core_backup_moodle1_converter_testcase extends advanced_testcase {
             "$CFG->dirroot/backup/converter/moodle1/tests/fixtures/icon.gif",
             "$CFG->tempdir/backup/$this->tempdir/moddata/unittest/4/icon.gif"
         );
-        $this->iconhash = sha1_file($CFG->tempdir.'/backup/'.$this->tempdir.'/moddata/unittest/4/icon.gif');
+        $this->iconhash = file_storage::hash_from_path($CFG->tempdir.'/backup/'.$this->tempdir.'/moddata/unittest/4/icon.gif');
         copy(
             "$CFG->dirroot/backup/converter/moodle1/tests/fixtures/icon.gif",
             "$CFG->tempdir/backup/$this->tempdir/moddata/unittest/4/7/icon.gif"
@@ -351,8 +351,8 @@ class core_backup_moodle1_converter_testcase extends advanced_testcase {
         $this->assertEquals('array', gettype($files['/file1.gif']));
         $this->assertEquals('array', gettype($files['/sub1/.']));
         $this->assertEquals('array', gettype($files['/sub1/file2.gif']));
-        $this->assertEquals(sha1(''), $files['/.']['contenthash']);
-        $this->assertEquals(sha1(''), $files['/sub1/.']['contenthash']);
+        $this->assertEquals(file_storage::hash_from_string(''), $files['/.']['contenthash']);
+        $this->assertEquals(file_storage::hash_from_string(''), $files['/sub1/.']['contenthash']);
         $this->assertEquals($this->iconhash, $files['/file1.gif']['contenthash']);
         $this->assertEquals($this->iconhash, $files['/sub1/file2.gif']['contenthash']);
 
index 7fe339b..2cf235f 100644 (file)
@@ -2065,7 +2065,10 @@ function upgrade_fix_missing_root_folders_draft() {
         'filename' => '.',
         'userid' => 0, // Don't rely on any particular user for these system records.
         'filesize' => 0,
-        'contenthash' => sha1(''));
+        // Note: This does not use the file_storage API's hash calculator
+        // because access to core APIs is not allowed during upgrade.
+        'contenthash' => sha1(''),
+    );
     foreach ($rs as $r) {
         $r->pathnamehash = sha1("/$r->contextid/user/draft/$r->itemid/.");
         $DB->insert_record('files', (array)$r + $defaults);
index a5466b1..01d9a33 100644 (file)
@@ -118,7 +118,7 @@ class assignfeedback_file_zip_importer {
                 $contenthash = '';
                 if (is_array($file)) {
                     $content = reset($file);
-                    $contenthash = sha1($content);
+                    $contenthash = file_storage::hash_from_string($content);
                 } else {
                     $contenthash = $file->get_contenthash();
                 }
index a72c342..a41e637 100644 (file)
@@ -571,7 +571,7 @@ class repository_filesystem extends repository {
             $fs = get_file_storage();
             $issyncing = true;
             if (file_extension_in_typegroup($filepath, 'web_image')) {
-                $contenthash = sha1_file($filepath);
+                $contenthash = file_storage::hash_from_path($filepath);
                 if ($file->get_contenthash() == $contenthash) {
                     // File did not change since the last synchronisation.
                     $filesize = filesize($filepath);
@@ -581,9 +581,10 @@ class repository_filesystem extends repository {
                 }
             } else {
                 // Update only file size so file will NOT be copied into moodle filepool.
-                $emptyfile = $contenthash = sha1('');
-                $currentcontenthash = $file->get_contenthash();
-                if ($currentcontenthash !== $emptyfile && $currentcontenthash === sha1_file($filepath)) {
+                if ($file->compare_to_string('') || !$file->compare_to_path($filepath)) {
+                    // File is not synchronized or the file has changed.
+                    $contenthash = file_storage::hash_from_string('');
+                } else {
                     // File content was synchronised and has not changed since then, leave it.
                     $contenthash = null;
                 }
@@ -701,7 +702,7 @@ class repository_filesystem extends repository {
             // File is not found or is not readable.
             return null;
         }
-        $filename = sha1($filecontents);
+        $filename = file_storage::hash_from_string($filecontents);
 
         // Try to get generated thumbnail for this file.
         $fs = get_file_storage();
@@ -751,7 +752,7 @@ class repository_filesystem extends repository {
         foreach ($files as $filepath => $filesinpath) {
             if ($filecontents = @file_get_contents($this->get_rootpath() . trim($filepath, '/'))) {
                 // The 'filename' in Moodle file storage is contenthash of the file in filesystem repository.
-                $filename = sha1($filecontents);
+                $filename = file_storage::hash_from_string($filecontents);
                 foreach ($filesinpath as $file) {
                     if ($file->get_filename() !== $filename && $file->get_filename() !== '.') {
                         // Contenthash does not match, this is an old thumbnail.
index c11f4cd..627cfae 100644 (file)
@@ -1722,7 +1722,7 @@ abstract class repository implements cacheable_object {
             // size, and a contenthash which does not related to empty content.
             // If thereis no file size, or the contenthash is for an empty file, then the file has
             // yet to be successfully downloaded.
-            $contentexists = $file->get_filesize() && $file->get_contenthash() !== sha1('');
+            $contentexists = $file->get_filesize() && !$file->compare_to_string('');
 
             if (!$file->get_status() && $contentexists) {
                 // we already have the content in moodle filepool and it was synchronised recently.