]> git.datanom.net - omvzfs.git/blame - src/Dataset.php
Changed properties attribute to an associative array to get cleaner code
[omvzfs.git] / src / Dataset.php
CommitLineData
f891182f 1<?php
ba6cf545 2require_once("Exception.php");
eb034e47 3require_once("openmediavault/util.inc");
f891182f
MR
4
5/**
6 * XXX detailed description
7 *
8 * @author XXX
9 * @version XXX
10 * @copyright XXX
11 */
63617ac2 12class OMVModuleZFSDataset {
f891182f
MR
13 // Attributes
14 /**
a4056e15 15 * Name of Dataset
f891182f
MR
16 *
17 * @var string $name
18 * @access private
19 */
a4056e15 20 private $name;
f891182f 21 /**
ba6cf545 22 * Mountpoint of the Dataset
f891182f
MR
23 *
24 * @var string $mountPoint
25 * @access private
26 */
ba6cf545 27 private $mountPoint;
f891182f
MR
28
29 /**
60a2cc94 30 * Array with properties assigned to the Dataset
f891182f 31 *
31fb83f4 32 * @var array $properties
f891182f
MR
33 * @access private
34 */
31fb83f4 35 private $properties;
f891182f 36
ba6cf545
NB
37 // Associations
38 // Operations
f891182f 39
ba6cf545
NB
40 /**
41 * Constructor
eb034e47 42 *
ba6cf545 43 * @param string $name Name of the new Dataset
60a2cc94 44 * @param array $properties An associative array with properties to set when creating the Dataset
0b156fc3 45 * @throws OMVModuleZFSException
ba6cf545
NB
46 *
47 */
31fb83f4 48 public function __construct($name, array $properties = null) {
60a2cc94 49 $cmd = "zfs create " . $name . " 2>&1";
eb034e47 50 OMVUtil::exec($cmd,$out,$res);
31fb83f4 51 if ($res) {
ba6cf545
NB
52 throw new OMVModuleZFSException(implode("\n", $out));
53 }
ba6cf545 54 $this->name = $name;
60a2cc94
NB
55 $this->setProperties($properties);
56 $this->mountPoint = $this->properties["mountpoint"];
ba6cf545 57 }
f891182f 58
ba6cf545
NB
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 }
f891182f 68
ba6cf545
NB
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 /**
60a2cc94
NB
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
ba6cf545 92 *
31fb83f4 93 * @return array $properties
ba6cf545
NB
94 * @access public
95 */
31fb83f4
NB
96 public function getProperties() {
97 return $this->properties;
ba6cf545
NB
98 }
99
100 /**
0b156fc3 101 * Sets a number of Dataset properties. If a property is already set it will be updated with the new value.
ba6cf545 102 *
60a2cc94 103 * @param array $properties An associative array with properties to set
eb034e47 104 * @return void
ba6cf545
NB
105 * @access public
106 */
31fb83f4 107 public function setProperties($properties) {
60a2cc94
NB
108 foreach ($properties as $newpropertyk => $newpropertyv) {
109 $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name;
eb034e47 110 OMVUtil::exec($cmd,$out,$res);
31fb83f4 111 if ($res) {
0b156fc3
NB
112 throw new OMVModuleZFSException(implode("\n", $out));
113 }
60a2cc94
NB
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];
0b156fc3
NB
135 }
136 }
137
138 /**
139 * Destroy the Dataset.
140 *
60a2cc94
NB
141 * @throws OMVModuleZFSException
142 * @access public
0b156fc3
NB
143 */
144 public function destroy() {
145 $cmd = "zfs destroy " . $this->name;
eb034e47 146 OMVUtil::exec($cmd,$out,$res);
60a2cc94 147 if ($res) {
0b156fc3
NB
148 throw new OMVModuleZFSException(implode("\n", $out));
149 }
ba6cf545 150 }
f891182f
MR
151
152}
153
154?>
This page took 0.053355 seconds and 5 git commands to generate.