1
0

LargeFileHelper.php 6.1 KB

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