Moodle
  1. Moodle
  2. MDL-27125

Bug in repository->get_file causing data corruption for downloaded files

    Details

    • Testing Instructions:
      Hide

      Regression testing:

      1. Enable the 'URL downloader' repository
      2. Download a file to my private files
      3. Check the file in private files is not corrupt.
      Show
      Regression testing: Enable the 'URL downloader' repository Download a file to my private files Check the file in private files is not corrupt.
    • Difficulty:
      Easy
    • Affected Branches:
      MOODLE_21_STABLE, MOODLE_22_STABLE
    • Fixed Branches:
      MOODLE_21_STABLE, MOODLE_22_STABLE, MOODLE_23_STABLE
    • Pull from Repository:
      git@github.com:dongsheng/moodle.git
    • Pull Master Branch:
      MDL-27125_master_2
    • Rank:
      16765

      Description

      Here is the code for get_file() method for repository class in repository/lib.php:

         public function get_file($url, $filename = '') {
              global $CFG;
              $path = $this->prepare_file($filename);
              $fp = fopen($path, 'w');
              $c = new curl;
              $c->download(array(array('url'=>$url, 'file'=>$fp)));
              return array('path'=>$path, 'url'=>$url);
          }
      

      Notice that the file is opened but not closed in this function. This will cause that within the request, after calling this function not whole file may be flushed to the disk. We have tested it on linux and were getting different results, but very often a partial file would be copied from temp/downloads area into the filestorage. The fix is very simple - adding fclose($fp) fixes the issue:

          public function get_file($url, $filename = '') {
              global $CFG;
              $path = $this->prepare_file($filename);
              $fp = fopen($path, 'w');
              $c = new curl;
              $c->download(array(array('url'=>$url, 'file'=>$fp)));
              fclose($fp);
              return array('path'=>$path, 'url'=>$url);
          }
      
      1. MDL-27125_jh1.diff
        0.6 kB
        Jonathan Harker
      2. MDL-27125-patch-1.diff
        1 kB
        Jonathan Harker

        Issue Links

          Activity

            People

            • Votes:
              1 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: