PluginProbe
ManageWP Worker / 4.9.29
ManageWP Worker v4.9.29
4.9.38 4.9.37 4.9.36 4.9.35 4.9.34 3.8.7 3.8.8 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.18 3.9.19 3.9.2 3.9.20 3.9.21 3.9.22 3.9.23 3.9.24 All 73 releases
worker / src / MWP / IncrementalBackup / HashComputer.php

HashComputer.php in ManageWP Worker 4.9.29, at src/MWP/IncrementalBackup/HashComputer.php

132 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * This file is part of the ManageWP Worker plugin.
4 *
5 * (c) ManageWP LLC <contact@managewp.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11 class MWP_IncrementalBackup_HashComputer
12 {
13
14 // 100kb default value
15 private $maxChunkByteSize = 102400;
16
17 /**
18 * @param int $maxChunkByteSize
19 */
20 public function setMaxChunkByteSize($maxChunkByteSize)
21 {
22 $this->maxChunkByteSize = $maxChunkByteSize;
23 }
24
25 /**
26 * @return int
27 */
28 public function getMaxChunkByteSize()
29 {
30 return $this->maxChunkByteSize;
31 }
32
33 /**
34 * Compute a full md5 file hash or compute a partial hash if $limit is present.
35 * Returns null on failure.
36 *
37 * @param string $realPath
38 * @param int $offset in bytes
39 * @param int $limit in bytes
40 * @param boolean $forcePartialHashing
41 *
42 * @return null|string
43 */
44 public function computeMd5Hash($realPath, $offset = 0, $limit = 0, $forcePartialHashing = false)
45 {
46 if ($limit === 0 && $offset === 0 && !$forcePartialHashing) {
47 // md5_file is always faster if we don't chunk the file
48 $hash = md5_file($realPath);
49
50 return $hash !== false ? $hash : null;
51 }
52
53 $ctx = hash_init('md5');
54 if (!$ctx) {
55 // Fail to initialize file hashing
56 return null;
57 }
58
59 // Calculate limit from file size and offset
60 if ($limit === 0) {
61 $limit = filesize($realPath) - $offset;
62 }
63
64 $fh = @fopen($realPath, "rb");
65 if ($fh === false) {
66 // Failed opening file, cleanup hash context
67 hash_final($ctx);
68
69 return null;
70 }
71
72 fseek($fh, $offset);
73
74 while ($limit > 0) {
75 // Limit chunk size to either our remaining chunk or max chunk size
76 $chunkSize = $limit < $this->maxChunkByteSize ? $limit : $this->maxChunkByteSize;
77 $limit -= $chunkSize;
78
79 $chunk = fread($fh, $chunkSize);
80 hash_update($ctx, $chunk);
81 }
82
83 fclose($fh);
84
85 return hash_final($ctx);
86 }
87
88 /**
89 * Run md5sum process and return file hash, or null in case of error
90 *
91 * @param $realPath
92 *
93 * @return string|null
94 */
95 public function computeUnixMd5Sum($realPath)
96 {
97 try {
98 $processBuilder = Symfony_Process_ProcessBuilder::create()
99 ->setPrefix('md5sum')
100 ->add($realPath);
101
102 if (!mwp_is_shell_available()) {
103 throw new MWP_Worker_Exception(MWP_Worker_Exception::SHELL_NOT_AVAILABLE, "Shell is not available");
104 }
105
106 $process = $processBuilder->getProcess();
107 $process->run();
108
109 if (!$process->isSuccessful()) {
110 throw new Symfony_Process_Exception_ProcessFailedException($process);
111 }
112
113 // Output is in the format of "md5hash filename"
114 $output = trim($process->getOutput());
115 $parts = explode(' ', $output);
116
117 // Return only the first part of the output
118 return trim($parts[0]);
119 } catch (Symfony_Process_Exception_ProcessFailedException $e) {
120 mwp_logger()->error('MD5 command line sum failed', array(
121 'process' => $e->getProcess(),
122 ));
123 } catch (Exception $e) {
124 mwp_logger()->error('MD5 command line sum failed', array(
125 'exception' => $e,
126 ));
127 }
128
129 return null;
130 }
131 }
132