Skip to content

Commit

Permalink
Merge branch 'MDL-58297-master' of git://github.com/andrewnicols/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
snake committed Apr 11, 2017
2 parents f669ad5 + e6a4780 commit 84fb93a
Show file tree
Hide file tree
Showing 14 changed files with 115 additions and 72 deletions.
4 changes: 2 additions & 2 deletions backup/converter/moodle1/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1379,7 +1379,7 @@ public function log($message, $level, $a = null, $depth = null, $display = false
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,
Expand Down Expand Up @@ -1422,7 +1422,7 @@ protected function add_file_to_pool($pathname) {
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";
Expand Down
6 changes: 3 additions & 3 deletions backup/converter/moodle1/tests/moodle1_converter_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected function setUp() {
"$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"
Expand Down Expand Up @@ -351,8 +351,8 @@ public function test_migrate_directory() {
$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']);

Expand Down
21 changes: 20 additions & 1 deletion lib/filestorage/file_storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,7 @@ public function create_directory($contextid, $component, $filearea, $itemid, $fi
static $contenthash = null;
if (!$contenthash) {
$this->add_string_to_pool('');
$contenthash = sha1('');
$contenthash = self::hash_from_string('');
}

$now = time();
Expand Down Expand Up @@ -2324,4 +2324,23 @@ public function update_references($referencefileid, $lastsync, $lifetime, $conte
$DB->update_record('files_reference', (object)$data);
}

/**
* Calculate and return the contenthash of the supplied file.
*
* @param string $filepath The path to the file on disk
* @return string The file's content hash
*/
public static function hash_from_path($filepath) {
return sha1_file($filepath);
}

/**
* Calculate and return the contenthash of the supplied content.
*
* @param string $content The file content
* @return string The file's content hash
*/
public static function hash_from_string($content) {
return sha1($content);
}
}
8 changes: 4 additions & 4 deletions lib/filestorage/file_system.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public function is_file_readable_remotely_by_storedfile(stored_file $file) {
* @return bool
*/
public function is_file_readable_locally_by_hash($contenthash, $fetchifnotfound = false) {
if ($contenthash === sha1('')) {
if ($contenthash === file_storage::hash_from_string('')) {
// Files with empty size are either directories or empty.
// We handle these virtually.
return true;
Expand All @@ -203,7 +203,7 @@ public function is_file_readable_locally_by_hash($contenthash, $fetchifnotfound
* @return bool
*/
public function is_file_readable_remotely_by_hash($contenthash) {
if ($contenthash === sha1('')) {
if ($contenthash === file_storage::hash_from_string('')) {
// Files with empty size are either directories or empty.
// We handle these virtually.
return true;
Expand Down Expand Up @@ -247,8 +247,8 @@ abstract public function remove_file($contenthash);
protected static function is_file_removable($contenthash) {
global $DB;

if ($contenthash === sha1('')) {
// No need to delete empty content file with sha1('') content hash.
if ($contenthash === file_storage::hash_from_string('')) {
// No need to delete files without content.
return false;
}

Expand Down
26 changes: 13 additions & 13 deletions lib/filestorage/file_system_filedir.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ protected function recover_file(stored_file $file) {
$trashfile = $alttrashfile;
}

if (filesize($trashfile) != $file->get_filesize() or sha1_file($trashfile) != $contenthash) {
if (filesize($trashfile) != $file->get_filesize() or file_storage::hash_from_path($trashfile) != $contenthash) {
// The files are different. Leave this one in trash - something seems to be wrong with it.
return false;
}
Expand Down Expand Up @@ -356,9 +356,9 @@ public function add_file_from_path($pathname, $contenthash = null) {
}

if (is_null($contenthash)) {
$contenthash = sha1_file($pathname);
$contenthash = file_storage::hash_from_path($pathname);
} else if ($CFG->debugdeveloper) {
$filehash = sha1_file($pathname);
$filehash = file_storage::hash_from_path($pathname);
if ($filehash === false) {
throw new file_exception('storedfilecannotread', '', $pathname);
}
Expand All @@ -372,16 +372,16 @@ public function add_file_from_path($pathname, $contenthash = null) {
throw new file_exception('storedfilecannotread', '', $pathname);
}

if ($filesize > 0 and $contenthash === sha1('')) {
// Did the file change or is sha1_file() borked for this file?
if ($filesize > 0 and $contenthash === file_storage::hash_from_string('')) {
// Did the file change or is file_storage::hash_from_path() borked for this file?
clearstatcache();
$contenthash = sha1_file($pathname);
$contenthash = file_storage::hash_from_path($pathname);
$filesize = filesize($pathname);

if ($contenthash === false or $filesize === false) {
throw new file_exception('storedfilecannotread', '', $pathname);
}
if ($filesize > 0 and $contenthash === sha1('')) {
if ($filesize > 0 and $contenthash === file_storage::hash_from_string('')) {
// This is very weird...
throw new file_exception('storedfilecannotread', '', $pathname);
}
Expand All @@ -396,8 +396,8 @@ public function add_file_from_path($pathname, $contenthash = null) {
if (filesize($hashfile) === $filesize) {
return array($contenthash, $filesize, false);
}
if (sha1_file($hashfile) === $contenthash) {
// Jackpot! We have a sha1 collision.
if (file_storage::hash_from_path($hashfile) === $contenthash) {
// Jackpot! We have a hash collision.
mkdir("$this->filedir/jackpot/", $this->dirpermissions, true);
copy($pathname, "$this->filedir/jackpot/{$contenthash}_1");
copy($hashfile, "$this->filedir/jackpot/{$contenthash}_2");
Expand Down Expand Up @@ -425,7 +425,7 @@ public function add_file_from_path($pathname, $contenthash = null) {
ignore_user_abort($prev);
throw new file_exception('storedfilecannotcreatefile');
}
if (sha1_file($hashfile.'.tmp') !== $contenthash) {
if (file_storage::hash_from_path($hashfile.'.tmp') !== $contenthash) {
// Highly unlikely edge case, but this can happen on an NFS volume with no space remaining.
@unlink($hashfile.'.tmp');
ignore_user_abort($prev);
Expand All @@ -452,7 +452,7 @@ public function add_file_from_path($pathname, $contenthash = null) {
public function add_file_from_string($content) {
global $CFG;

$contenthash = sha1($content);
$contenthash = file_storage::hash_from_string($content);
// Binary length.
$filesize = strlen($content);

Expand All @@ -465,8 +465,8 @@ public function add_file_from_string($content) {
if (filesize($hashfile) === $filesize) {
return array($contenthash, $filesize, false);
}
if (sha1_file($hashfile) === $contenthash) {
// Jackpot! We have a sha1 collision.
if (file_storage::hash_from_path($hashfile) === $contenthash) {
// Jackpot! We have a hash collision.
mkdir("$this->filedir/jackpot/", $this->dirpermissions, true);
copy($hashfile, "$this->filedir/jackpot/{$contenthash}_1");
file_put_contents("$this->filedir/jackpot/{$contenthash}_2", $content);
Expand Down
20 changes: 20 additions & 0 deletions lib/filestorage/stored_file.php
Original file line number Diff line number Diff line change
Expand Up @@ -1016,4 +1016,24 @@ public function resize_image($width, $height) {
// Generate the resized image.
return resize_image_from_image($original, $imageinfo, $width, $height);
}

/**
* Check whether the supplied file is the same as this file.
*
* @param string $path The path to the file on disk
* @return boolean
*/
public function compare_to_path($path) {
return $this->get_contenthash() === file_storage::hash_from_path($path);
}

/**
* Check whether the supplied content is the same as this file.
*
* @param string $content The file content
* @return boolean
*/
public function compare_to_string($content) {
return $this->get_contenthash() === file_storage::hash_from_string($content);
}
}
4 changes: 2 additions & 2 deletions lib/filestorage/tests/file_storage_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function test_create_file_from_string() {
$file = $fs->create_file_from_string($filerecord, $content);

$this->assertInstanceOf('stored_file', $file);
$this->assertSame(sha1($content), $file->get_contenthash());
$this->assertTrue($file->compare_to_string($content));
$this->assertSame($pathhash, $file->get_pathnamehash());

$this->assertTrue($DB->record_exists('files', array('pathnamehash'=>$pathhash)));
Expand Down Expand Up @@ -132,7 +132,7 @@ public function test_create_file_from_pathname() {
$file = $fs->create_file_from_pathname($filerecord, $filepath);

$this->assertInstanceOf('stored_file', $file);
$this->assertSame(sha1_file($filepath), $file->get_contenthash());
$this->assertTrue($file->compare_to_path($filepath));

$this->assertTrue($DB->record_exists('files', array('pathnamehash'=>$pathhash)));

Expand Down
Loading

0 comments on commit 84fb93a

Please sign in to comment.