MDL-58297 core: Update to use new hashing functions

This commit is contained in:
Andrew Nicols 2017-03-17 15:21:59 +08:00
parent a30a04fa01
commit e6a4780452
6 changed files with 18 additions and 14 deletions

View file

@ -1379,7 +1379,7 @@ class moodle1_file_manager implements loggable {
protected function make_file_record(array $fileinfo) { protected function make_file_record(array $fileinfo) {
$defaultrecord = array( $defaultrecord = array(
'contenthash' => 'da39a3ee5e6b4b0d3255bfef95601890afd80709', // sha1 of an empty file 'contenthash' => file_storage::hash_from_string(''),
'contextid' => $this->contextid, 'contextid' => $this->contextid,
'component' => $this->component, 'component' => $this->component,
'filearea' => $this->filearea, 'filearea' => $this->filearea,
@ -1422,7 +1422,7 @@ class moodle1_file_manager implements loggable {
throw new moodle1_convert_exception('file_not_readable'); throw new moodle1_convert_exception('file_not_readable');
} }
$contenthash = sha1_file($pathname); $contenthash = file_storage::hash_from_path($pathname);
$filesize = filesize($pathname); $filesize = filesize($pathname);
$hashpath = $this->converter->get_workdir_path().'/files/'.substr($contenthash, 0, 2); $hashpath = $this->converter->get_workdir_path().'/files/'.substr($contenthash, 0, 2);
$hashfile = "$hashpath/$contenthash"; $hashfile = "$hashpath/$contenthash";

View file

@ -64,7 +64,7 @@ class core_backup_moodle1_converter_testcase extends advanced_testcase {
"$CFG->dirroot/backup/converter/moodle1/tests/fixtures/icon.gif", "$CFG->dirroot/backup/converter/moodle1/tests/fixtures/icon.gif",
"$CFG->tempdir/backup/$this->tempdir/moddata/unittest/4/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( copy(
"$CFG->dirroot/backup/converter/moodle1/tests/fixtures/icon.gif", "$CFG->dirroot/backup/converter/moodle1/tests/fixtures/icon.gif",
"$CFG->tempdir/backup/$this->tempdir/moddata/unittest/4/7/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['/file1.gif']));
$this->assertEquals('array', gettype($files['/sub1/.'])); $this->assertEquals('array', gettype($files['/sub1/.']));
$this->assertEquals('array', gettype($files['/sub1/file2.gif'])); $this->assertEquals('array', gettype($files['/sub1/file2.gif']));
$this->assertEquals(sha1(''), $files['/.']['contenthash']); $this->assertEquals(file_storage::hash_from_string(''), $files['/.']['contenthash']);
$this->assertEquals(sha1(''), $files['/sub1/.']['contenthash']); $this->assertEquals(file_storage::hash_from_string(''), $files['/sub1/.']['contenthash']);
$this->assertEquals($this->iconhash, $files['/file1.gif']['contenthash']); $this->assertEquals($this->iconhash, $files['/file1.gif']['contenthash']);
$this->assertEquals($this->iconhash, $files['/sub1/file2.gif']['contenthash']); $this->assertEquals($this->iconhash, $files['/sub1/file2.gif']['contenthash']);

View file

@ -2065,7 +2065,10 @@ function upgrade_fix_missing_root_folders_draft() {
'filename' => '.', 'filename' => '.',
'userid' => 0, // Don't rely on any particular user for these system records. 'userid' => 0, // Don't rely on any particular user for these system records.
'filesize' => 0, '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) { foreach ($rs as $r) {
$r->pathnamehash = sha1("/$r->contextid/user/draft/$r->itemid/."); $r->pathnamehash = sha1("/$r->contextid/user/draft/$r->itemid/.");
$DB->insert_record('files', (array)$r + $defaults); $DB->insert_record('files', (array)$r + $defaults);

View file

@ -118,7 +118,7 @@ class assignfeedback_file_zip_importer {
$contenthash = ''; $contenthash = '';
if (is_array($file)) { if (is_array($file)) {
$content = reset($file); $content = reset($file);
$contenthash = sha1($content); $contenthash = file_storage::hash_from_string($content);
} else { } else {
$contenthash = $file->get_contenthash(); $contenthash = $file->get_contenthash();
} }

View file

@ -571,7 +571,7 @@ class repository_filesystem extends repository {
$fs = get_file_storage(); $fs = get_file_storage();
$issyncing = true; $issyncing = true;
if (file_extension_in_typegroup($filepath, 'web_image')) { if (file_extension_in_typegroup($filepath, 'web_image')) {
$contenthash = sha1_file($filepath); $contenthash = file_storage::hash_from_path($filepath);
if ($file->get_contenthash() == $contenthash) { if ($file->get_contenthash() == $contenthash) {
// File did not change since the last synchronisation. // File did not change since the last synchronisation.
$filesize = filesize($filepath); $filesize = filesize($filepath);
@ -581,9 +581,10 @@ class repository_filesystem extends repository {
} }
} else { } else {
// Update only file size so file will NOT be copied into moodle filepool. // Update only file size so file will NOT be copied into moodle filepool.
$emptyfile = $contenthash = sha1(''); if ($file->compare_to_string('') || !$file->compare_to_path($filepath)) {
$currentcontenthash = $file->get_contenthash(); // File is not synchronized or the file has changed.
if ($currentcontenthash !== $emptyfile && $currentcontenthash === sha1_file($filepath)) { $contenthash = file_storage::hash_from_string('');
} else {
// File content was synchronised and has not changed since then, leave it. // File content was synchronised and has not changed since then, leave it.
$contenthash = null; $contenthash = null;
} }
@ -701,7 +702,7 @@ class repository_filesystem extends repository {
// File is not found or is not readable. // File is not found or is not readable.
return null; return null;
} }
$filename = sha1($filecontents); $filename = file_storage::hash_from_string($filecontents);
// Try to get generated thumbnail for this file. // Try to get generated thumbnail for this file.
$fs = get_file_storage(); $fs = get_file_storage();
@ -751,7 +752,7 @@ class repository_filesystem extends repository {
foreach ($files as $filepath => $filesinpath) { foreach ($files as $filepath => $filesinpath) {
if ($filecontents = @file_get_contents($this->get_rootpath() . trim($filepath, '/'))) { if ($filecontents = @file_get_contents($this->get_rootpath() . trim($filepath, '/'))) {
// The 'filename' in Moodle file storage is contenthash of the file in filesystem repository. // 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) { foreach ($filesinpath as $file) {
if ($file->get_filename() !== $filename && $file->get_filename() !== '.') { if ($file->get_filename() !== $filename && $file->get_filename() !== '.') {
// Contenthash does not match, this is an old thumbnail. // Contenthash does not match, this is an old thumbnail.

View file

@ -1722,7 +1722,7 @@ abstract class repository implements cacheable_object {
// size, and a contenthash which does not related to empty content. // 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 // If thereis no file size, or the contenthash is for an empty file, then the file has
// yet to be successfully downloaded. // 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) { if (!$file->get_status() && $contentexists) {
// we already have the content in moodle filepool and it was synchronised recently. // we already have the content in moodle filepool and it was synchronised recently.