You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
5.4 KiB

  1. <?php
  2. /**
  3. * Copyright (c) 2014 Andreas Fischer <bantu@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC;
  9. /**
  10. * Helper class for large files on 32-bit platforms.
  11. */
  12. class LargeFileHelper {
  13. /**
  14. * pow(2, 53) as a base-10 string.
  15. * @var string
  16. */
  17. const POW_2_53 = '9007199254740992';
  18. /**
  19. * pow(2, 53) - 1 as a base-10 string.
  20. * @var string
  21. */
  22. const POW_2_53_MINUS_1 = '9007199254740991';
  23. /**
  24. * @brief Checks whether our assumptions hold on the PHP platform we are on.
  25. *
  26. * @throws \RunTimeException if our assumptions do not hold on the current
  27. * PHP platform.
  28. */
  29. public function __construct() {
  30. $pow_2_53 = floatval(self::POW_2_53_MINUS_1) + 1.0;
  31. if ($this->formatUnsignedInteger($pow_2_53) !== self::POW_2_53) {
  32. throw new \RunTimeException(
  33. 'This class assumes floats to be double precision or "better".'
  34. );
  35. }
  36. }
  37. /**
  38. * @brief Formats a signed integer or float as an unsigned integer base-10
  39. * string. Passed strings will be checked for being base-10.
  40. *
  41. * @param int|float|string $number Number containing unsigned integer data
  42. *
  43. * @throws \UnexpectedValueException if $number is not a float, not an int
  44. * and not a base-10 string.
  45. *
  46. * @return string Unsigned integer base-10 string
  47. */
  48. public function formatUnsignedInteger($number) {
  49. if (is_float($number)) {
  50. // Undo the effect of the php.ini setting 'precision'.
  51. return number_format($number, 0, '', '');
  52. } else if (is_string($number) && ctype_digit($number)) {
  53. return $number;
  54. } else if (is_int($number)) {
  55. // Interpret signed integer as unsigned integer.
  56. return sprintf('%u', $number);
  57. } else {
  58. throw new \UnexpectedValueException(
  59. 'Expected int, float or base-10 string'
  60. );
  61. }
  62. }
  63. /**
  64. * @brief Tries to get the size of a file via various workarounds that
  65. * even work for large files on 32-bit platforms.
  66. *
  67. * @param string $filename Path to the file.
  68. *
  69. * @return null|int|float Number of bytes as number (float or int) or
  70. * null on failure.
  71. */
  72. public function getFileSize($filename) {
  73. $fileSize = $this->getFileSizeViaCurl($filename);
  74. if (!is_null($fileSize)) {
  75. return $fileSize;
  76. }
  77. $fileSize = $this->getFileSizeViaCOM($filename);
  78. if (!is_null($fileSize)) {
  79. return $fileSize;
  80. }
  81. $fileSize = $this->getFileSizeViaExec($filename);
  82. if (!is_null($fileSize)) {
  83. return $fileSize;
  84. }
  85. return $this->getFileSizeNative($filename);
  86. }
  87. /**
  88. * @brief Tries to get the size of a file via a CURL HEAD request.
  89. *
  90. * @param string $filename Path to the file.
  91. *
  92. * @return null|int|float Number of bytes as number (float or int) or
  93. * null on failure.
  94. */
  95. public function getFileSizeViaCurl($filename) {
  96. if (function_exists('curl_init')) {
  97. $ch = curl_init("file://$filename");
  98. curl_setopt($ch, CURLOPT_NOBODY, true);
  99. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  100. curl_setopt($ch, CURLOPT_HEADER, true);
  101. $data = curl_exec($ch);
  102. curl_close($ch);
  103. if ($data !== false) {
  104. $matches = array();
  105. preg_match('/Content-Length: (\d+)/', $data, $matches);
  106. if (isset($matches[1])) {
  107. return 0 + $matches[1];
  108. }
  109. }
  110. }
  111. return null;
  112. }
  113. /**
  114. * @brief Tries to get the size of a file via the Windows DOM extension.
  115. *
  116. * @param string $filename Path to the file.
  117. *
  118. * @return null|int|float Number of bytes as number (float or int) or
  119. * null on failure.
  120. */
  121. public function getFileSizeViaCOM($filename) {
  122. if (class_exists('COM')) {
  123. $fsobj = new \COM("Scripting.FileSystemObject");
  124. $file = $fsobj->GetFile($filename);
  125. return 0 + $file->Size;
  126. }
  127. return null;
  128. }
  129. /**
  130. * @brief Tries to get the size of a file via an exec() call.
  131. *
  132. * @param string $filename Path to the file.
  133. *
  134. * @return null|int|float Number of bytes as number (float or int) or
  135. * null on failure.
  136. */
  137. public function getFileSizeViaExec($filename) {
  138. if (\OC_Helper::is_function_enabled('exec')) {
  139. $os = strtolower(php_uname('s'));
  140. $arg = escapeshellarg($filename);
  141. $result = '';
  142. if (strpos($os, 'linux') !== false) {
  143. $result = $this->exec("stat -c %s $arg");
  144. } else if (strpos($os, 'bsd') !== false) {
  145. $result = $this->exec("stat -f %z $arg");
  146. } else if (strpos($os, 'win') !== false) {
  147. $result = $this->exec("for %F in ($arg) do @echo %~zF");
  148. if (is_null($result)) {
  149. // PowerShell
  150. $result = $this->exec("(Get-Item $arg).length");
  151. }
  152. }
  153. return $result;
  154. }
  155. return null;
  156. }
  157. /**
  158. * @brief Gets the size of a file via a filesize() call and converts
  159. * negative signed int to positive float. As the result of filesize()
  160. * will wrap around after a file size of 2^32 bytes = 4 GiB, this
  161. * should only be used as a last resort.
  162. *
  163. * @param string $filename Path to the file.
  164. *
  165. * @return int|float Number of bytes as number (float or int).
  166. */
  167. public function getFileSizeNative($filename) {
  168. $result = filesize($filename);
  169. if ($result < 0) {
  170. // For file sizes between 2 GiB and 4 GiB, filesize() will return a
  171. // negative int, as the PHP data type int is signed. Interpret the
  172. // returned int as an unsigned integer and put it into a float.
  173. return (float) sprintf('%u', $result);
  174. }
  175. return $result;
  176. }
  177. protected function exec($cmd) {
  178. $result = trim(exec($cmd));
  179. return ctype_digit($result) ? 0 + $result : null;
  180. }
  181. }