LargeFileHelper.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC;
  8. use bantu\IniGetWrapper\IniGetWrapper;
  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. public const POW_2_53 = '9007199254740992';
  18. /**
  19. * pow(2, 53) - 1 as a base-10 string.
  20. * @var string
  21. */
  22. public 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 = (float)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(int|float|string $number): string {
  49. if (is_float($number)) {
  50. // Undo the effect of the php.ini setting 'precision'.
  51. return number_format($number, 0, '', '');
  52. } elseif (is_string($number) && ctype_digit($number)) {
  53. return $number;
  54. } elseif (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 int|float Number of bytes as number (float or int)
  70. */
  71. public function getFileSize(string $filename): int|float {
  72. $fileSize = $this->getFileSizeViaCurl($filename);
  73. if (!is_null($fileSize)) {
  74. return $fileSize;
  75. }
  76. $fileSize = $this->getFileSizeViaExec($filename);
  77. if (!is_null($fileSize)) {
  78. return $fileSize;
  79. }
  80. return $this->getFileSizeNative($filename);
  81. }
  82. /**
  83. * @brief Tries to get the size of a file via a CURL HEAD request.
  84. *
  85. * @param string $fileName Path to the file.
  86. *
  87. * @return null|int|float Number of bytes as number (float or int) or
  88. * null on failure.
  89. */
  90. public function getFileSizeViaCurl(string $fileName): null|int|float {
  91. if (\OC::$server->get(IniGetWrapper::class)->getString('open_basedir') === '') {
  92. $encodedFileName = rawurlencode($fileName);
  93. $ch = curl_init("file:///$encodedFileName");
  94. curl_setopt($ch, CURLOPT_NOBODY, true);
  95. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  96. curl_setopt($ch, CURLOPT_HEADER, true);
  97. $data = curl_exec($ch);
  98. curl_close($ch);
  99. if ($data !== false) {
  100. $matches = [];
  101. preg_match('/Content-Length: (\d+)/', $data, $matches);
  102. if (isset($matches[1])) {
  103. return 0 + $matches[1];
  104. }
  105. }
  106. }
  107. return null;
  108. }
  109. /**
  110. * @brief Tries to get the size of a file via an exec() call.
  111. *
  112. * @param string $filename Path to the file.
  113. *
  114. * @return null|int|float Number of bytes as number (float or int) or
  115. * null on failure.
  116. */
  117. public function getFileSizeViaExec(string $filename): null|int|float {
  118. if (\OCP\Util::isFunctionEnabled('exec')) {
  119. $os = strtolower(php_uname('s'));
  120. $arg = escapeshellarg($filename);
  121. $result = null;
  122. if (str_contains($os, 'linux')) {
  123. $result = $this->exec("stat -c %s $arg");
  124. } elseif (str_contains($os, 'bsd') || str_contains($os, 'darwin')) {
  125. $result = $this->exec("stat -f %z $arg");
  126. }
  127. return $result;
  128. }
  129. return null;
  130. }
  131. /**
  132. * @brief Gets the size of a file via a filesize() call and converts
  133. * negative signed int to positive float. As the result of filesize()
  134. * will wrap around after a file size of 2^32 bytes = 4 GiB, this
  135. * should only be used as a last resort.
  136. *
  137. * @param string $filename Path to the file.
  138. *
  139. * @return int|float Number of bytes as number (float or int).
  140. */
  141. public function getFileSizeNative(string $filename): int|float {
  142. $result = filesize($filename);
  143. if ($result < 0) {
  144. // For file sizes between 2 GiB and 4 GiB, filesize() will return a
  145. // negative int, as the PHP data type int is signed. Interpret the
  146. // returned int as an unsigned integer and put it into a float.
  147. return (float) sprintf('%u', $result);
  148. }
  149. return $result;
  150. }
  151. /**
  152. * Returns the current mtime for $fullPath
  153. */
  154. public function getFileMtime(string $fullPath): int {
  155. try {
  156. $result = filemtime($fullPath) ?: -1;
  157. } catch (\Exception $e) {
  158. $result = - 1;
  159. }
  160. if ($result < 0) {
  161. if (\OCP\Util::isFunctionEnabled('exec')) {
  162. $os = strtolower(php_uname('s'));
  163. if (str_contains($os, 'linux')) {
  164. return (int)($this->exec('stat -c %Y ' . escapeshellarg($fullPath)) ?? -1);
  165. }
  166. }
  167. }
  168. return $result;
  169. }
  170. protected function exec(string $cmd): null|int|float {
  171. $result = trim(exec($cmd));
  172. return ctype_digit($result) ? 0 + $result : null;
  173. }
  174. }