NativeSeekDriver   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 5
c 3
b 0
f 0
lcom 0
cbo 2
dl 0
loc 44
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B getFileSize() 0 33 5
1
<?php
2
3
namespace BigFileTools\Driver;
4
5
use BigFileTools\Utils;
6
use Brick\Math\BigInteger;
7
8
class NativeSeekDriver implements ISizeDriver
9
{
10
	/**
11
	 * Returns file size by seeking at the end of file
12
	 * @see http://www.php.net/manual/en/function.filesize.php#79023
13
	 * @see http://www.php.net/manual/en/function.filesize.php#102135
14
	 * @param string $path Full path to file
15
	 * @return BigInteger
16
	 * @throws Exception
17
	 */
18
	public function getFileSize($path)
19
	{
20
		// This should work for large files on 64bit platforms and for small files everywhere
21
		$fp = fopen($path, "rb");
22
		if (!$fp) {
23
			throw new Exception("Cannot open specified file for reading.");
24
		}
25
26
		$flockResult = flock($fp, LOCK_SH);
27
		$seekResult = fseek($fp, 0, SEEK_END);
28
		$position = ftell($fp);
29
		flock($fp, LOCK_UN);
30
		fclose($fp);
31
32
		if($flockResult === false) {
33
			throw new Exception("Couldn't get file lock. Operation abandoned.");
34
		}
35
36
		if($seekResult !== 0) {
37
			throw new Exception("Seeking to end of file failed");
38
		}
39
40
		if($position === false) {
41
			throw new Exception("Cannot determine position in file. ftell() failed.");
42
		}
43
44
		// PHP uses internally (in C) UNSIGNED integer for file size.
45
		// PHP uses signed implicitly
46
		// convert signed (max val +2^31) -> unsigned integer will extend range for 32-bit to (+2^32)
47
		return BigInteger::of(
48
			sprintf("%u", $position)
49
		);
50
	}
51
}