Advertisement
Guest User

Untitled

a guest
Nov 27th, 2010
526
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 5.80 KB | None | 0 0
  1. Index: lib/filestorage/file_exceptions.php
  2. ===================================================================
  3. RCS file: /cvsroot/moodle/moodle/lib/filestorage/file_exceptions.php,v
  4. retrieving revision 1.2
  5. diff -u -r1.2 file_exceptions.php
  6. --- lib/filestorage/file_exceptions.php 21 Sep 2010 07:59:27 -0000  1.2
  7. +++ lib/filestorage/file_exceptions.php 27 Nov 2010 12:00:31 -0000
  8. @@ -83,8 +83,15 @@
  9.   * @copyright  2008 Petr Skoda (http://skodak.org)
  10.   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  11.   */
  12. -class file_pool_content_exception extends file_exception {
  13. -    function __construct($contenthash, $debuginfo = NULL) {
  14. -        parent::__construct('hashpoolproblem', $contenthash, $debuginfo);
  15. +class file_pool_put_exception extends file_exception {
  16. +    function __construct($contenthash, $content, $sourcefilepath, $debuginfo = null) {
  17. +        $a = new stdClass();
  18. +        $a->hash = $contenthash;
  19. +        if (!is_null($content)) {
  20. +            $a->source = 'string: ' . shorten_text($content);
  21. +        } else {
  22. +            $a->source = 'file: ' . $sourcefilepath;
  23. +        }
  24. +        parent::__construct('hashpoolproblem', $a, $debuginfo);
  25.      }
  26.  }
  27. Index: lib/filestorage/file_storage.php
  28. ===================================================================
  29. RCS file: /cvsroot/moodle/moodle/lib/filestorage/file_storage.php,v
  30. retrieving revision 1.10
  31. diff -u -r1.10 file_storage.php
  32. --- lib/filestorage/file_storage.php    11 Nov 2010 18:58:06 -0000  1.10
  33. +++ lib/filestorage/file_storage.php    27 Nov 2010 12:00:31 -0000
  34. @@ -1022,37 +1022,7 @@
  35.          }
  36.  
  37.          $filesize = filesize($pathname);
  38. -
  39. -        $hashpath = $this->path_from_hash($contenthash);
  40. -        $hashfile = "$hashpath/$contenthash";
  41. -
  42. -        if (file_exists($hashfile)) {
  43. -            if (filesize($hashfile) !== $filesize) {
  44. -                throw new file_pool_content_exception($contenthash);
  45. -            }
  46. -            $newfile = false;
  47. -
  48. -        } else {
  49. -            if (!is_dir($hashpath)) {
  50. -                if (!mkdir($hashpath, $this->dirpermissions, true)) {
  51. -                    throw new file_exception('storedfilecannotcreatefiledirs'); // permission trouble
  52. -                }
  53. -            }
  54. -            $newfile = true;
  55. -
  56. -            if (!copy($pathname, $hashfile)) {
  57. -                throw new file_exception('storedfilecannotread');
  58. -            }
  59. -
  60. -            if (filesize($hashfile) !== $filesize) {
  61. -                @unlink($hashfile);
  62. -                throw new file_pool_content_exception($contenthash);
  63. -            }
  64. -            chmod($hashfile, $this->filepermissions); // fix permissions if needed
  65. -        }
  66. -
  67. -
  68. -        return array($contenthash, $filesize, $newfile);
  69. +        return $this->put_file_in_pool($filesize, $contenthash, null, $pathname);
  70.      }
  71.  
  72.      /**
  73. @@ -1064,14 +1034,27 @@
  74.      public function add_string_to_pool($content) {
  75.          $contenthash = sha1($content);
  76.          $filesize = strlen($content); // binary length
  77. +        return $this->put_file_in_pool($filesize, $contenthash, $content, null);
  78. +    }
  79. +
  80. +    /**
  81. +     * Actually do the work of adding something to the sha1 pool. Used by
  82. +     * {@link add_string_to_pool()} and {@link add_file_to_pool()}
  83. +     *
  84. +     * @param string $content file content - binary string
  85. +     * @return array (contenthash, filesize, newfile)
  86. +     */
  87. +    protected function put_file_in_pool($filesize, $contenthash, $content, $sourcefilepath) {
  88. +        if (is_null($content) != is_null($sourcefilepath)) {
  89. +            throw new coding_exception('Exactly one of $content and $sourcefilepath must be non-null when calling put_file_in_pool.');
  90. +        }
  91.  
  92.          $hashpath = $this->path_from_hash($contenthash);
  93.          $hashfile = "$hashpath/$contenthash";
  94.  
  95. -
  96.          if (file_exists($hashfile)) {
  97.              if (filesize($hashfile) !== $filesize) {
  98. -                throw new file_pool_content_exception($contenthash);
  99. +                throw new file_pool_content_exception($contenthash, $content, $sourcefilepath);
  100.              }
  101.              $newfile = false;
  102.  
  103. @@ -1083,11 +1066,17 @@
  104.              }
  105.              $newfile = true;
  106.  
  107. -            file_put_contents($hashfile, $content);
  108. +            if (!is_null($content)) {
  109. +                file_put_contents($hashfile, $content);
  110. +            } else {
  111. +                if (!copy($pathname, $hashfile)) {
  112. +                    throw new file_exception('storedfilecannotread');
  113. +                }
  114. +            }
  115.  
  116.              if (filesize($hashfile) !== $filesize) {
  117.                  @unlink($hashfile);
  118. -                throw new file_pool_content_exception($contenthash);
  119. +                throw new file_pool_content_exception($contenthash, $content, $sourcefilepath);
  120.              }
  121.              chmod($hashfile, $this->filepermissions); // fix permissions if needed
  122.          }
  123. Index: lang/en/error.php
  124. ===================================================================
  125. RCS file: /cvsroot/moodle/moodle/lang/en/error.php,v
  126. retrieving revision 1.58
  127. diff -u -r1.58 error.php
  128. --- lang/en/error.php   19 Nov 2010 03:40:45 -0000  1.58
  129. +++ lang/en/error.php   27 Nov 2010 12:00:31 -0000
  130. @@ -244,7 +244,7 @@
  131.  $string['guestnorate'] = 'Guests are not allowed to rate entries';
  132.  $string['guestsarenotallowed'] = 'The guest user is not allowed to do this';
  133.  $string['hackdetected'] = 'Hack attack detected!';
  134. -$string['hashpoolproblem'] = 'Incorrect pool file content {$a}.';
  135. +$string['hashpoolproblem'] = 'Inconsistent data found in the file pool for hash {$a->hash} when trying to add a new file from {$a->source}.';
  136.  $string['headersent'] = 'Headers already sent';
  137.  $string['idnumbertaken'] = 'ID number is already used for another course';
  138.  $string['importformatnotimplement'] = 'Sorry, importing this format is not yet implemented!';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement