|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright (c) 2011-present Mediasift Ltd |
|
5
|
|
|
* All rights reserved. |
|
6
|
|
|
* |
|
7
|
|
|
* Redistribution and use in source and binary forms, with or without |
|
8
|
|
|
* modification, are permitted provided that the following conditions |
|
9
|
|
|
* are met: |
|
10
|
|
|
* |
|
11
|
|
|
* * Redistributions of source code must retain the above copyright |
|
12
|
|
|
* notice, this list of conditions and the following disclaimer. |
|
13
|
|
|
* |
|
14
|
|
|
* * Redistributions in binary form must reproduce the above copyright |
|
15
|
|
|
* notice, this list of conditions and the following disclaimer in |
|
16
|
|
|
* the documentation and/or other materials provided with the |
|
17
|
|
|
* distribution. |
|
18
|
|
|
* |
|
19
|
|
|
* * Neither the names of the copyright holders nor the names of his |
|
20
|
|
|
* contributors may be used to endorse or promote products derived |
|
21
|
|
|
* from this software without specific prior written permission. |
|
22
|
|
|
* |
|
23
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
24
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
25
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
|
26
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
|
27
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
|
28
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|
29
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
30
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|
31
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
32
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
|
33
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
34
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
|
35
|
|
|
* |
|
36
|
|
|
* @category Libraries |
|
37
|
|
|
* @package Storyplayer/Modules/Filesystem |
|
38
|
|
|
* @author Stuart Herbert <[email protected]> |
|
39
|
|
|
* @copyright 2011-present Mediasift Ltd www.datasift.com |
|
40
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
41
|
|
|
* @link http://datasift.github.io/storyplayer |
|
42
|
|
|
*/ |
|
43
|
|
|
|
|
44
|
|
|
namespace Storyplayer\SPv2\Modules\Filesystem; |
|
45
|
|
|
|
|
46
|
|
|
use Storyplayer\SPv2\Modules\Exceptions; |
|
47
|
|
|
use Storyplayer\SPv2\Modules\Filesystem; |
|
48
|
|
|
use Storyplayer\SPv2\Modules\Host\HostAwareModule; |
|
49
|
|
|
use Storyplayer\SPv2\Modules\Log; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* |
|
53
|
|
|
* test the state of a (possibly remote) computer |
|
54
|
|
|
* |
|
55
|
|
|
* @category Libraries |
|
56
|
|
|
* @package Storyplayer/Modules/Host |
|
57
|
|
|
* @author Stuart Herbert <[email protected]> |
|
58
|
|
|
* @copyright 2011-present Mediasift Ltd www.datasift.com |
|
59
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
60
|
|
|
* @link http://datasift.github.io/storyplayer |
|
61
|
|
|
*/ |
|
62
|
|
|
class ExpectsFilesystem extends HostAwareModule |
|
63
|
|
|
{ |
|
64
|
|
View Code Duplication |
public function hasFileWithPermissions($filename, $owner, $group, $mode) |
|
|
|
|
|
|
65
|
|
|
{ |
|
66
|
|
|
// shorthand |
|
67
|
|
|
$octMode = decoct($mode); |
|
68
|
|
|
|
|
69
|
|
|
// what are we doing? |
|
70
|
|
|
$log = Log::usingLog()->startAction("make sure file '{$filename}' exists on host '{$this->args[0]}' with permissions '{$octMode}' owned by '{$owner}:{$group}'"); |
|
71
|
|
|
|
|
72
|
|
|
// make sure we have valid host details |
|
73
|
|
|
$hostDetails = $this->getHostDetails(); |
|
74
|
|
|
|
|
75
|
|
|
// get the file details |
|
76
|
|
|
$details = Filesystem::fromHost($hostDetails->hostId)->getFileDetails($filename); |
|
77
|
|
|
|
|
78
|
|
|
// validate the details |
|
79
|
|
|
if ($details === null) { |
|
80
|
|
|
throw Exceptions::newExpectFailedException(__METHOD__, "'{$filename}' exists", "'{$filename}' does not exist"); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if ($details->type != 'file') { |
|
84
|
|
|
throw Exceptions::newExpectFailedException(__METHOD__, "'{$filename}' is a file", "'{$filename}' is type '{$details->type}'"); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
if ($details->mode != $mode) { |
|
88
|
|
|
$theirOctMode = decoct($details->mode); |
|
89
|
|
|
throw Exceptions::newExpectFailedException(__METHOD__, "'{$filename}' has permissions '{$octMode}'", "'{$filename}' has permissions '{$theirOctMode}'"); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if ($details->user != $owner || $details->group != $group) { |
|
93
|
|
|
throw Exceptions::newExpectFailedException(__METHOD__, "'{$filename}' has ownership '{$owner}:{$group}'", "'{$filename}' has ownership '{$details->user}:{$details->group}'"); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
// if we get here, then all is good |
|
97
|
|
|
$log->endAction(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
View Code Duplication |
public function hasFolderWithPermissions($folder, $owner, $group, $mode) |
|
|
|
|
|
|
101
|
|
|
{ |
|
102
|
|
|
// shorthand |
|
103
|
|
|
$octMode = decoct($mode); |
|
104
|
|
|
|
|
105
|
|
|
// what are we doing? |
|
106
|
|
|
$log = Log::usingLog()->startAction("make sure folder '{$folder}' exists on host '{$this->args[0]}' with permissions '{$octMode}' owned by '{$owner}:{$group}'"); |
|
107
|
|
|
|
|
108
|
|
|
// make sure we have valid host details |
|
109
|
|
|
$hostDetails = $this->getHostDetails(); |
|
110
|
|
|
|
|
111
|
|
|
// get the file details |
|
112
|
|
|
$details = Filesystem::fromHost($hostDetails->hostId)->getFileDetails($folder); |
|
113
|
|
|
|
|
114
|
|
|
// validate the details |
|
115
|
|
|
if ($details === null) { |
|
116
|
|
|
throw Exceptions::newExpectFailedException(__METHOD__, "'{$folder}' exists", "'{$folder}' does not exist"); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
if ($details->type != 'dir') { |
|
120
|
|
|
throw Exceptions::newExpectFailedException(__METHOD__, "'{$folder}' is a file", "'{$folder}' is type '{$details->type}'"); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
if ($details->mode != $mode) { |
|
124
|
|
|
$theirOctMode = decoct($details->mode); |
|
125
|
|
|
throw Exceptions::newExpectFailedException(__METHOD__, "'{$folder}' has permissions '{$octMode}'", "'{$folder}' has permissions '{$theirOctMode}'"); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
if ($details->user != $owner || $details->group != $group) { |
|
129
|
|
|
throw Exceptions::newExpectFailedException(__METHOD__, "'{$folder}' has ownership '{$owner}:{$group}'", "'{$folder}' has ownership '{$details->user}:{$details->group}'"); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
// if we get here, then all is good |
|
133
|
|
|
$log->endAction(); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.