-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add LargeFileHelper / Add CURL filesize workaround / Fix some 32-bit filesize issues #5365
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
6195f13
Use CURL to get filesize on 32bit systems.
3f8f802
Cast to numeric instead of float, i.e. use an integer if possible.
c8fa1fd
Refactor Large File handling code.
68cc0ba
Unit Tests for LargeFileHelper.
626e87a
Output validation for exec() method.
df29eec
Windows exec() implementation.
fb7ec2b
Only call $this->filesize() for files.
82e1715
Rename: LargeFileHelper -> LargeFileHelperGetFilesize
2c36a4b
Add helper method for turning int|float into base-10 unsigned integer…
a9b2832
Add LargeFileHelper::__construct() verifying that our assumptions hold.
a34aa19
Cast to number instead of integer in OC\Files\Cache\Cache
0bae680
Cast to number instead of integer in OC\Files\Cache\HomeCache
fb45560
Cast '{DAV:}getcontentlength' to number instead of int.
0417e52
Increase file size limit from 2 GiB to 4 GiB when workarounds are una…
ea246d0
Use "file size" instead of "filesize", then also apply camel case.
2929d19
Document exceptions thrown by \OC\LargeFileHelper.
129d809
Typo: getFileSizeViaDOM -> getFileSizeViaCOM
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
<?php | ||
/** | ||
* Copyright (c) 2014 Andreas Fischer <[email protected]> | ||
* This file is licensed under the Affero General Public License version 3 or | ||
* later. | ||
* See the COPYING-README file. | ||
*/ | ||
|
||
namespace OC; | ||
|
||
/** | ||
* Helper class for large files on 32-bit platforms. | ||
*/ | ||
class LargeFileHelper { | ||
/** | ||
* pow(2, 53) as a base-10 string. | ||
* @var string | ||
*/ | ||
const POW_2_53 = '9007199254740992'; | ||
|
||
/** | ||
* pow(2, 53) - 1 as a base-10 string. | ||
* @var string | ||
*/ | ||
const POW_2_53_MINUS_1 = '9007199254740991'; | ||
|
||
/** | ||
* @brief Checks whether our assumptions hold on the PHP platform we are on. | ||
* | ||
* @throws \RunTimeException if our assumptions do not hold on the current | ||
* PHP platform. | ||
*/ | ||
public function __construct() { | ||
$pow_2_53 = floatval(self::POW_2_53_MINUS_1) + 1.0; | ||
if ($this->formatUnsignedInteger($pow_2_53) !== self::POW_2_53) { | ||
throw new \RunTimeException( | ||
'This class assumes floats to be double precision or "better".' | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @brief Formats a signed integer or float as an unsigned integer base-10 | ||
* string. Passed strings will be checked for being base-10. | ||
* | ||
* @param int|float|string $number Number containing unsigned integer data | ||
* | ||
* @throws \UnexpectedValueException if $number is not a float, not an int | ||
* and not a base-10 string. | ||
* | ||
* @return string Unsigned integer base-10 string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
*/ | ||
public function formatUnsignedInteger($number) { | ||
if (is_float($number)) { | ||
// Undo the effect of the php.ini setting 'precision'. | ||
return number_format($number, 0, '', ''); | ||
} else if (is_string($number) && ctype_digit($number)) { | ||
return $number; | ||
} else if (is_int($number)) { | ||
// Interpret signed integer as unsigned integer. | ||
return sprintf('%u', $number); | ||
} else { | ||
throw new \UnexpectedValueException( | ||
'Expected int, float or base-10 string' | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @brief Tries to get the size of a file via various workarounds that | ||
* even work for large files on 32-bit platforms. | ||
* | ||
* @param string $filename Path to the file. | ||
* | ||
* @return null|int|float Number of bytes as number (float or int) or | ||
* null on failure. | ||
*/ | ||
public function getFileSize($filename) { | ||
$fileSize = $this->getFileSizeViaCurl($filename); | ||
if (!is_null($fileSize)) { | ||
return $fileSize; | ||
} | ||
$fileSize = $this->getFileSizeViaCOM($filename); | ||
if (!is_null($fileSize)) { | ||
return $fileSize; | ||
} | ||
$fileSize = $this->getFileSizeViaExec($filename); | ||
if (!is_null($fileSize)) { | ||
return $fileSize; | ||
} | ||
return $this->getFileSizeNative($filename); | ||
} | ||
|
||
/** | ||
* @brief Tries to get the size of a file via a CURL HEAD request. | ||
* | ||
* @param string $filename Path to the file. | ||
* | ||
* @return null|int|float Number of bytes as number (float or int) or | ||
* null on failure. | ||
*/ | ||
public function getFileSizeViaCurl($filename) { | ||
if (function_exists('curl_init')) { | ||
$ch = curl_init("file://$filename"); | ||
curl_setopt($ch, CURLOPT_NOBODY, true); | ||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
curl_setopt($ch, CURLOPT_HEADER, true); | ||
$data = curl_exec($ch); | ||
curl_close($ch); | ||
if ($data !== false) { | ||
$matches = array(); | ||
preg_match('/Content-Length: (\d+)/', $data, $matches); | ||
if (isset($matches[1])) { | ||
return 0 + $matches[1]; | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* @brief Tries to get the size of a file via the Windows DOM extension. | ||
* | ||
* @param string $filename Path to the file. | ||
* | ||
* @return null|int|float Number of bytes as number (float or int) or | ||
* null on failure. | ||
*/ | ||
public function getFileSizeViaCOM($filename) { | ||
if (class_exists('COM')) { | ||
$fsobj = new \COM("Scripting.FileSystemObject"); | ||
$file = $fsobj->GetFile($filename); | ||
return 0 + $file->Size; | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* @brief Tries to get the size of a file via an exec() call. | ||
* | ||
* @param string $filename Path to the file. | ||
* | ||
* @return null|int|float Number of bytes as number (float or int) or | ||
* null on failure. | ||
*/ | ||
public function getFileSizeViaExec($filename) { | ||
if (\OC_Helper::is_function_enabled('exec')) { | ||
$os = strtolower(php_uname('s')); | ||
$arg = escapeshellarg($filename); | ||
$result = ''; | ||
if (strpos($os, 'linux') !== false) { | ||
$result = $this->exec("stat -c %s $arg"); | ||
} else if (strpos($os, 'bsd') !== false) { | ||
$result = $this->exec("stat -f %z $arg"); | ||
} else if (strpos($os, 'win') !== false) { | ||
$result = $this->exec("for %F in ($arg) do @echo %~zF"); | ||
if (is_null($result)) { | ||
// PowerShell | ||
$result = $this->exec("(Get-Item $arg).length"); | ||
} | ||
} | ||
return $result; | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* @brief Gets the size of a file via a filesize() call and converts | ||
* negative signed int to positive float. As the result of filesize() | ||
* will wrap around after a file size of 2^32 bytes = 4 GiB, this | ||
* should only be used as a last resort. | ||
* | ||
* @param string $filename Path to the file. | ||
* | ||
* @return int|float Number of bytes as number (float or int). | ||
*/ | ||
public function getFileSizeNative($filename) { | ||
$result = filesize($filename); | ||
if ($result < 0) { | ||
// For file sizes between 2 GiB and 4 GiB, filesize() will return a | ||
// negative int, as the PHP data type int is signed. Interpret the | ||
// returned int as an unsigned integer and put it into a float. | ||
return (float) sprintf('%u', $result); | ||
} | ||
return $result; | ||
} | ||
|
||
protected function exec($cmd) { | ||
$result = trim(exec($cmd)); | ||
return ctype_digit($result) ? 0 + $result : null; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no
()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do coding guidelines require these?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You know - I'm an old C++ hacker with gray beard .... for me 'new' is always related to a ctor call and a function/method call has these
()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not changing this unless you insist.