]> git.datanom.net - omvzfs.git/blob - src/Snapshot.php
afd189c2f082055ddfd31fa72b473d304494c1ed
[omvzfs.git] / src / Snapshot.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 OMVModuleZFSSnapshot {
13 // Attributes
14 /**
15 * Name of the Snapshot
16 *
17 * @var string $name
18 * @access private
19 */
20 private $name;
21
22 /**
23 * Properties associated with the Snaphost
24 *
25 * @var array $properties
26 * @access private
27 */
28 private $properties;
29
30 // Associations
31 // Operations
32
33 /**
34 * Constructor. If the Snapshot already exists in the system the object will be updated with all
35 * associated properties from commandline.
36 *
37 * @param string $name Name of the new Snapshot
38 * @return void
39 * @access public
40 */
41 public function __construct($name) {
42 $this->name = $name;
43 $cmd = "zfs list -H -t snapshot " .$name . " 2>&1";
44 try {
45 $this->exec($cmd, $out, $res);
46 $this->updateAllProperties();
47 }
48 catch (OMVModuleZFSException $e) {
49 }
50 }
51
52 /**
53 * Return name of the Snapshot
54 *
55 * @return string Nameof the Snapshot
56 * @access public
57 */
58 public function getName() {
59 return $this->name;
60 }
61
62 /**
63 * Get a single property value associated with the Snapshot
64 *
65 * @param string $property Name of the property to fetch
66 * @return array The returned array with the property. The property is an associative array with
67 * two elements, <value> and <source>.
68 * @access public
69 */
70 public function getProperty($property) {
71 return $this->properties["$property"];
72 }
73
74 /**
75 * Get an associative array of all properties associated with the Snapshot
76 *
77 * @return array $properties Each entry is an associative array with two elements
78 * <value> and <source>
79 * @access public
80 */
81 public function getProperties() {
82 return $this->properties;
83 }
84
85 /**
86 * Sets a number of Snapshot properties. If a property is already set it will be updated with the new value.
87 *
88 * @param array $properties An associative array with properties to set
89 * @return void
90 * @access public
91 */
92 public function setProperties($properties) {
93 foreach ($properties as $newpropertyk => $newpropertyv) {
94 $cmd = "zfs set " . $newpropertyk . "=" . $newpropertyv . " " . $this->name . " 2>&1";
95 $this->exec($cmd,$out,$res);
96 $this->updateProperty($newpropertyk);
97 }
98 }
99
100 /**
101 * Get single Snapshot property from commandline and update object property attribute
102 *
103 * @param string $property Name of the property to update
104 * @return void
105 * @access private
106 */
107 private function updateProperty($property) {
108 $cmd = "zfs get -H " . $property . " " . $this->name . " 2>&1";
109 $this->exec($cmd,$out,$res);
110 $tmpary = preg_split('/\t+/', $out[0]);
111 $this->properties["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]);
112 }
113
114 /**
115 * Get all Snapshot 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 . " 2>&1";
122 $this->exec($cmd,$out,$res);
123 unset($this->properties);
124 foreach ($out as $line) {
125 $tmpary = preg_split('/\t/', $line);
126 if (strlen($tmpary[2] == 0)) {
127 $this->properties["$tmpary[1]"] = array("value" => "-", "source" => $tmpary[3]);
128 } else {
129 $this->properties["$tmpary[1]"] = array("value" => $tmpary[2], "source" => $tmpary[3]);
130 }
131 }
132 }
133
134 /**
135 * Craete a Snapshot on commandline. Optionally provide a number of properties to set.
136 *
137 * @param array $properties Properties to set when creating the dataset.
138 * @return void
139 * @access public
140 */
141 public function create(array $properties = null) {
142 $cmd = "zfs snapshot " . $this->name . " 2>&1";
143 $this->exec($cmd,$out,$res);
144 $this->updateAllProperties();
145 $this->setProperties($properties);
146 }
147
148 /**
149 * Destroy a Snapshot on commandline.
150 *
151 * @return void
152 * @access public
153 */
154 public function destroy() {
155 $cmd = "zfs destroy " . $this->name . " 2>&1";
156 $this->exec($cmd,$out,$res);
157 }
158
159 /**
160 * Helper function to execute a command and throw an exception on error
161 * (requires stderr redirected to stdout for proper exception message).
162 *
163 * @param string $cmd Command to execute
164 * @param array &$out If provided will contain output in an array
165 * @param int &$res If provided will contain Exit status of the command
166 * @return string Last line of output when executing the command
167 * @throws OMVModuleZFSException
168 * @access private
169 */
170 private function exec($cmd, &$out = null, &$res = null) {
171 $tmp = OMVUtil::exec($cmd, $out, $res);
172 if ($res) {
173 throw new OMVModuleZFSException(implode("\n", $out));
174 }
175 return $tmp;
176 }
177 }
178
179 ?>
This page took 0.073513 seconds and 4 git commands to generate.