LargeFileHelper.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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($number) {
  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 null|int|float Number of bytes as number (float or int) or
  93. * null on failure.
  94. */
  95. public function getFileSize($filename) {
  96. $fileSize = $this->getFileSizeViaCurl($filename);
  97. if (!is_null($fileSize)) {
  98. return $fileSize;
  99. }
  100. $fileSize = $this->getFileSizeViaExec($filename);
  101. if (!is_null($fileSize)) {
  102. return $fileSize;
  103. }
  104. return $this->getFileSizeNative($filename);
  105. }
  106. /**
  107. * @brief Tries to get the size of a file via a CURL HEAD request.
  108. *
  109. * @param string $fileName Path to the file.
  110. *
  111. * @return null|int|float Number of bytes as number (float or int) or
  112. * null on failure.
  113. */
  114. public function getFileSizeViaCurl($fileName) {
  115. if (\OC::$server->get(IniGetWrapper::class)->getString('open_basedir') === '') {
  116. $encodedFileName = rawurlencode($fileName);
  117. $ch = curl_init("file:///$encodedFileName");
  118. curl_setopt($ch, CURLOPT_NOBODY, true);
  119. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  120. curl_setopt($ch, CURLOPT_HEADER, true);
  121. $data = curl_exec($ch);
  122. curl_close($ch);
  123. if ($data !== false) {
  124. $matches = [];
  125. preg_match('/Content-Length: (\d+)/', $data, $matches);
  126. if (isset($matches[1])) {
  127. return 0 + $matches[1];
  128. }
  129. }
  130. }
  131. return null;
  132. }
  133. /**
  134. * @brief Tries to get the size of a file via an exec() call.
  135. *
  136. * @param string $filename Path to the file.
  137. *
  138. * @return null|int|float Number of bytes as number (float or int) or
  139. * null on failure.
  140. */
  141. public function getFileSizeViaExec($filename) {
  142. if (\OCP\Util::isFunctionEnabled('exec')) {
  143. $os = strtolower(php_uname('s'));
  144. $arg = escapeshellarg($filename);
  145. $result = null;
  146. if (strpos($os, 'linux') !== false) {
  147. $result = $this->exec("stat -c %s $arg");
  148. } elseif (strpos($os, 'bsd') !== false || strpos($os, 'darwin') !== false) {
  149. $result = $this->exec("stat -f %z $arg");
  150. }
  151. return $result;
  152. }
  153. return null;
  154. }
  155. /**
  156. * @brief Gets the size of a file via a filesize() call and converts
  157. * negative signed int to positive float. As the result of filesize()
  158. * will wrap around after a file size of 2^32 bytes = 4 GiB, this
  159. * should only be used as a last resort.
  160. *
  161. * @param string $filename Path to the file.
  162. *
  163. * @return int|float Number of bytes as number (float or int).
  164. */
  165. public function getFileSizeNative($filename) {
  166. $result = filesize($filename);
  167. if ($result < 0) {
  168. // For file sizes between 2 GiB and 4 GiB, filesize() will return a
  169. // negative int, as the PHP data type int is signed. Interpret the
  170. // returned int as an unsigned integer and put it into a float.
  171. return (float) sprintf('%u', $result);
  172. }
  173. return $result;
  174. }
  175. /**
  176. * Returns the current mtime for $fullPath
  177. *
  178. * @param string $fullPath
  179. * @return int
  180. */
  181. public function getFileMtime($fullPath) {
  182. try {
  183. $result = filemtime($fullPath);
  184. } catch (\Exception $e) {
  185. $result = - 1;
  186. }
  187. if ($result < 0) {
  188. if (\OCP\Util::isFunctionEnabled('exec')) {
  189. $os = strtolower(php_uname('s'));
  190. if (strpos($os, 'linux') !== false) {
  191. return $this->exec('stat -c %Y ' . escapeshellarg($fullPath));
  192. }
  193. }
  194. }
  195. return $result;
  196. }
  197. protected function exec($cmd) {
  198. $result = trim(exec($cmd));
  199. return ctype_digit($result) ? 0 + $result : null;
  200. }
  201. }