]> git.datanom.net - omvzfs.git/blob - src/Dataset.php
Introduce new helper function to further cleanup code
[omvzfs.git] / src / Dataset.php
1 <?php
2 require_once("Exception.php");
3 require_once("openmediavault/util.inc");
4
5 /**
6 * XXX detailed description
7 *
8 * @author XXX
9 * @version XXX
10 * @copyright XXX
11 */
12 class OMVModuleZFSDataset {
13 // Attributes
14 /**
15 * Name of Dataset
16 *
17 * @var string $name
18 * @access private
19 */
20 private $name;
21 /**
22 * Mountpoint of the Dataset
23 *
24 * @var string $mountPoint
25 * @access private
26 */
27 private $mountPoint;
28
29 /**
30 * Array with properties assigned to the Dataset
31 *
32 * @var array $properties
33 * @access private
34 */
35 private $properties;
36
37 // Associations
38 // Operations
39
40 /**
41 * Constructor
42 *
43 * @param string $name Name of the new Dataset
44 * @param array $properties An associative array with properties to set when creating the Dataset
45 *
46 */
47 public function __construct($name, array $properties = null) {
48 $cmd = "zfs create -p " . $name . " 2>&1";
49 $this->exec($cmd,$out,$res);
50 $this->name = $name;
51 $this->updateAllProperties();
52 $this->setProperties($properties);
53 $this->mountPoint = $this->properties["mountpoint"][0];
54 }
55
56 /**
57 * Return name of the Dataset
58 *
59 * @return string $name
60 * @access public
61 */
62 public function getName() {
63 return $this->name;
64 }
65
66 /**
67 * Get the mountpoint of the Dataset
68 *
69 * @return string $mountPoint
70 * @access public
71 */
72 public function getMountPoint() {
73 return $this->mountPoint;
74 }
75
76 /**
77 * Get a single property value associated with the Dataset
78 *
79 * @param string $property Name of the property to fetch
80 * @return array The returned array key 0=property value and key 1=property source.
81 * @access public
82 */
83 public function getProperty($property) {
84 return $this->properties["$property"];
85 }
86
87 /**
88 * Get an associative array of all properties associated with the Dataset.
89 *
90 * @return array $properties Each entry is an array where key 0=property value and key
91 * 1=property source.
92 *
93 * @access public
94 */
95 public function getProperties() {
96 return $this->properties;
97 }
98
99 /**
100 * Sets a number of Dataset properties. If a property is already set it will be updated with the new value.
101 *
102 * @param array $properties An associative array with properties to set
103 * @return void
104 * @access public
105 */
106 public function setProperties($properties) {
107 foreach ($properties as $newpropertyk => $newpropertyv) {
108 $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name;
109 $this->exec($cmd,$out,$res);
110 $this->updateProperty($newpropertyk);
111 }
112 }
113
114 /**
115 * Get all Dataset properties from commandline and update object properties attribute
116 *
117 * @return void
118 * @access private
119 */
120 private function updateAllProperties() {
121 $cmd = "zfs get -H all " . $this->name;
122 $this->exec($cmd,$out,$res);
123 unset($this->properties);
124 foreach ($out as $line) {
125 $tmpary = preg_split('/\t+/', $line);
126 $this->properties["$tmpary[1]"] = array($tmpary[2], $tmpary[3]);
127 }
128 }
129
130 /**
131 * Get single Datset property from commandline and update object property attribute
132 *
133 * @param string $property Name of the property to update
134 * @return void
135 * @access private
136 */
137 private function updateProperty($property) {
138 $cmd = "zfs get -H " . $property . " " . $this->name;
139 $this->exec($cmd,$out,$res);
140 $tmpary = preg_split('/\t+/', $out[0]);
141 $this->properties["$tmpary[1]"] = array($tmpary[2], $tmpary[3]);
142 }
143
144 /**
145 * Destroy the Dataset.
146 *
147 * @return void
148 * @access public
149 */
150 public function destroy() {
151 $cmd = "zfs destroy " . $this->name;
152 $this->exec($cmd,$out,$res);
153 }
154
155 /**
156 * Renames a Dataset
157 *
158 * @param string $newname New name of the Dataset
159 * @return void
160 * @access public
161 */
162 public function rename($newname) {
163 $cmd = "zfs rename -p " . $this->name . " " . $newname . " 2>&1";
164 $this->exec($cmd,$out,$res);
165 $this->name = $newname;
166 }
167
168 /**
169 * Clears a previously set proporty and specifies that it should be
170 * inherited from it's parent.
171 *
172 * @param string $property Name of the property to inherit.
173 * @return void
174 * @access public
175 */
176 public function inherit($property) {
177 $cmd = "zfs inherit " . $property . " " . $this->name . " 2>&1";
178 $this->exec($cmd,$out,$res);
179 $this->updateProperty($property);
180 }
181
182 /**
183 * Helper function to execute a command and throw an exception on error
184 * (requires stderr redirected to stdout for proper exception message).
185 *
186 * @param string $cmd Command to execute
187 * @param array &$out If provided will contain output in an array
188 * @param int &$res If provided will contain Exit status of the command
189 * @return string Last line of output when executing the command
190 * @throws OMVModuleZFSException
191 * @access private
192 */
193 private function exec($cmd, &$out = null, &$res = null) {
194 $tmp = OMVUtil::exec($cmd, $out, $res);
195 if ($res) {
196 throw new OMVModuleZFSException(implode("\n", $out));
197 }
198 return $tmp;
199 }
200
201 }
202
203 ?>
This page took 0.075798 seconds and 6 git commands to generate.