LargeFileHelper.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. * @copyright Copyright (c) 2016, Lukas Reschke <lukas@statuscode.ch>
  5. *
  6. * @author Andreas Fischer <bantu@owncloud.com>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author marco44 <cousinmarc@gmail.com>
  11. * @author Michael Roitzsch <reactorcontrol@icloud.com>
  12. * @author Morris Jobke <hey@morrisjobke.de>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OC;
  31. use bantu\IniGetWrapper\IniGetWrapper;
  32. /**
  33. * Helper class for large files on 32-bit platforms.
  34. */
  35. class LargeFileHelper {
  36. /**
  37. * pow(2, 53) as a base-10 string.
  38. * @var string
  39. */
  40. public const POW_2_53 = '9007199254740992';
  41. /**
  42. * pow(2, 53) - 1 as a base-10 string.
  43. * @var string
  44. */
  45. public const POW_2_53_MINUS_1 = '9007199254740991';
  46. /**
  47. * @brief Checks whether our assumptions hold on the PHP platform we are on.
  48. *
  49. * @throws \RuntimeException if our assumptions do not hold on the current
  50. * PHP platform.
  51. */
  52. public function __construct() {
  53. $pow_2_53 = (float)self::POW_2_53_MINUS_1 + 1.0;
  54. if ($this->formatUnsignedInteger($pow_2_53) !== self::POW_2_53) {
  55. throw new \RuntimeException(
  56. 'This class assumes floats to be double precision or "better".'
  57. );
  58. }
  59. }
  60. /**
  61. * @brief Formats a signed integer or float as an unsigned integer base-10
  62. * string. Passed strings will be checked for being base-10.
  63. *
  64. * @param int|float|string $number Number containing unsigned integer data
  65. *
  66. * @throws \UnexpectedValueException if $number is not a float, not an int
  67. * and not a base-10 string.
  68. *
  69. * @return string Unsigned integer base-10 string
  70. */
  71. public function formatUnsignedInteger(int|float|string $number): string {
  72. if (is_float($number)) {
  73. // Undo the effect of the php.ini setting 'precision'.
  74. return number_format($number, 0, '', '');
  75. } elseif (is_string($number) && ctype_digit($number)) {
  76. return $number;
  77. } elseif (is_int($number)) {
  78. // Interpret signed integer as unsigned integer.
  79. return sprintf('%u', $number);
  80. } else {
  81. throw new \UnexpectedValueException(
  82. 'Expected int, float or base-10 string'
  83. );
  84. }
  85. }
  86. /**
  87. * @brief Tries to get the size of a file via various workarounds that
  88. * even work for large files on 32-bit platforms.
  89. *
  90. * @param string $filename Path to the file.
  91. *
  92. * @return int|float Number of bytes as number (float or int)
  93. */
  94. public function getFileSize(string $filename): int|float {
  95. $fileSize = $this->getFileSizeViaCurl($filename);
  96. if (!is_null($fileSize)) {
  97. return $fileSize;
  98. }
  99. $fileSize = $this->getFileSizeViaExec($filename);
  100. if (!is_null($fileSize)) {
  101. return $fileSize;
  102. }
  103. return $this->getFileSizeNative($filename);
  104. }
  105. /**
  106. * @brief Tries to get the size of a file via a CURL HEAD request.
  107. *
  108. * @param string $fileName Path to the file.
  109. *
  110. * @return null|int|float Number of bytes as number (float or int) or
  111. * null on failure.
  112. */
  113. public function getFileSizeViaCurl(string $fileName): null|int|float {
  114. if (\OC::$server->get(IniGetWrapper::class)->getString('open_basedir') === '') {
  115. $encodedFileName = rawurlencode($fileName);
  116. $ch = curl_init("file:///$encodedFileName");
  117. curl_setopt($ch, CURLOPT_NOBODY, true);
  118. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  119. curl_setopt($ch, CURLOPT_HEADER, true);
  120. $data = curl_exec($ch);
  121. curl_close($ch);
  122. if ($data !== false) {
  123. $matches = [];
  124. preg_match('/Content-Length: (\d+)/', $data, $matches);
  125. if (isset($matches[1])) {
  126. return 0 + $matches[1];
  127. }
  128. }
  129. }
  130. return null;
  131. }
  132. /**
  133. * @brief Tries to get the size of a file via an exec() call.
  134. *
  135. * @param string $filename Path to the file.
  136. *
  137. * @return null|int|float Number of bytes as number (float or int) or
  138. * null on failure.
  139. */
  140. public function getFileSizeViaExec(string $filename): null|int|float {
  141. if (\OCP\Util::isFunctionEnabled('exec')) {
  142. $os = strtolower(php_uname('s'));
  143. $arg = escapeshellarg($filename);
  144. $result = null;
  145. if (str_contains($os, 'linux')) {
  146. $result = $this->exec("stat -c %s $arg");
  147. } elseif (str_contains($os, 'bsd') || str_contains($os, 'darwin')) {
  148. $result = $this->exec("stat -f %z $arg");
  149. }
  150. return $result;
  151. }
  152. return null;
  153. }
  154. /**
  155. * @brief Gets the size of a file via a filesize() call and converts
  156. * negative signed int to positive float. As the result of filesize()
  157. * will wrap around after a file size of 2^32 bytes = 4 GiB, this
  158. * should only be used as a last resort.
  159. *
  160. * @param string $filename Path to the file.
  161. *
  162. * @return int|float Number of bytes as number (float or int).
  163. */
  164. public function getFileSizeNative(string $filename): int|float {
  165. $result = filesize($filename);
  166. if ($result < 0) {
  167. // For file sizes between 2 GiB and 4 GiB, filesize() will return a
  168. // negative int, as the PHP data type int is signed. Interpret the
  169. // returned int as an unsigned integer and put it into a float.
  170. return (float) sprintf('%u', $result);
  171. }
  172. return $result;
  173. }
  174. /**
  175. * Returns the current mtime for $fullPath
  176. */
  177. public function getFileMtime(string $fullPath): int {
  178. try {
  179. $result = filemtime($fullPath) ?: -1;
  180. } catch (\Exception $e) {
  181. $result = - 1;
  182. }
  183. if ($result < 0) {
  184. if (\OCP\Util::isFunctionEnabled('exec')) {
  185. $os = strtolower(php_uname('s'));
  186. if (str_contains($os, 'linux')) {
  187. return (int)($this->exec('stat -c %Y ' . escapeshellarg($fullPath)) ?? -1);
  188. }
  189. }
  190. }
  191. return $result;
  192. }
  193. protected function exec(string $cmd): null|int|float {
  194. $result = trim(exec($cmd));
  195. return ctype_digit($result) ? 0 + $result : null;
  196. }
  197. }