Skip to content

Commit

Permalink
feature #11857 [Filesystem] Check number of bytes copied. (skigun)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.6-dev branch.

Discussion
----------

[Filesystem] Check number of bytes copied.

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #10838
| License       | MIT
| Doc PR        | n/a

This only test local files (because of `filesize`), wonder if we should include the remote files using `get_headers` in order to get the Content-length for example. However, it will perform an additional request...

Here's a little benchmark for 500 copy from a remote origin file :
- Standard without check -> Time: 47.34 seconds, Memory: 3.75Mb
- Check with `get_headers` ->Time: 1.32 minutes, Memory: 3.75Mb

Commits
-------

81eca38 [Filesystem] Check number of bytes copied.
  • Loading branch information
fabpot committed Sep 22, 2014
2 parents a1f8d99 + 81eca38 commit e5e6f4d
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Symfony/Component/Filesystem/Filesystem.php
Expand Up @@ -60,14 +60,18 @@ public function copy($originFile, $targetFile, $override = false)
throw new IOException(sprintf('Failed to copy "%s" to "%s" because target file could not be opened for writing.', $originFile, $targetFile), 0, null, $originFile);
}

stream_copy_to_stream($source, $target);
$bytesCopied = stream_copy_to_stream($source, $target);
fclose($source);
fclose($target);
unset($source, $target);

if (!is_file($targetFile)) {
throw new IOException(sprintf('Failed to copy "%s" to "%s".', $originFile, $targetFile), 0, null, $originFile);
}

if (stream_is_local($originFile) && $bytesCopied !== filesize($originFile)) {
throw new IOException(sprintf('Failed to copy the whole content of "%s" to "%s %g bytes copied".', $originFile, $targetFile, $bytesCopied), 0, null, $originFile);
}
}
}

Expand Down

0 comments on commit e5e6f4d

Please sign in to comment.