]> git.datanom.net - omvzfs.git/blob - src/Dataset.php
Changed properties attribute to an associative array to get cleaner 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 * @throws OMVModuleZFSException
46 *
47 */
48 public function __construct($name, array $properties = null) {
49 $cmd = "zfs create " . $name . " 2>&1";
50 OMVUtil::exec($cmd,$out,$res);
51 if ($res) {
52 throw new OMVModuleZFSException(implode("\n", $out));
53 }
54 $this->name = $name;
55 $this->setProperties($properties);
56 $this->mountPoint = $this->properties["mountpoint"];
57 }
58
59 /**
60 * Return name of the Dataset
61 *
62 * @return string $name
63 * @access public
64 */
65 public function getName() {
66 return $this->name;
67 }
68
69 /**
70 * Get the mountpoint of the Dataset
71 *
72 * @return string $mountPoint
73 * @access public
74 */
75 public function getMountPoint() {
76 return $this->mountPoint;
77 }
78
79 /**
80 * Get a single property value associated with the Dataset
81 *
82 * @param string $property Name of the property to fetch
83 * @return string
84 * @access public
85 */
86 public function getProperty($property) {
87 return $this->properties["$property"];
88 }
89
90 /**
91 * Get an array of all properties associated with the Dataset
92 *
93 * @return array $properties
94 * @access public
95 */
96 public function getProperties() {
97 return $this->properties;
98 }
99
100 /**
101 * Sets a number of Dataset properties. If a property is already set it will be updated with the new value.
102 *
103 * @param array $properties An associative array with properties to set
104 * @return void
105 * @access public
106 */
107 public function setProperties($properties) {
108 foreach ($properties as $newpropertyk => $newpropertyv) {
109 $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name;
110 OMVUtil::exec($cmd,$out,$res);
111 if ($res) {
112 throw new OMVModuleZFSException(implode("\n", $out));
113 }
114 $this->properties["$newpropertyk"] = $newpropertyv;
115 }
116 $this->updateAllProperties();
117 }
118
119 /**
120 * Get all Dataset properties from commandline and update object properties attribute
121 *
122 * @throws OMVModuleZFSException
123 * @access private
124 */
125 private function updateAllProperties() {
126 $cmd = "zfs get -H all " . $this->name;
127 OMVUtil::exec($cmd,$out,$res);
128 if ($res) {
129 throw new OMVModuleZFSException(implode("\n", $out));
130 }
131 unset($this->properties);
132 foreach ($out as $line) {
133 $tmpary = preg_split('/\t+/', $line);
134 $this->properties["$tmpary[1]"] = $tmpary[2];
135 }
136 }
137
138 /**
139 * Destroy the Dataset.
140 *
141 * @throws OMVModuleZFSException
142 * @access public
143 */
144 public function destroy() {
145 $cmd = "zfs destroy " . $this->name;
146 OMVUtil::exec($cmd,$out,$res);
147 if ($res) {
148 throw new OMVModuleZFSException(implode("\n", $out));
149 }
150 }
151
152 }
153
154 ?>
This page took 0.068779 seconds and 6 git commands to generate.