/
symlinks.inc
181 lines (174 loc) · 7.19 KB
/
symlinks.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
/**
* @license http://www.gnu.org/licenses/gpl.html GPL Version 3
* @author Volker Theile <volker.theile@openmediavault.org>
* @copyright Copyright (c) 2009-2013 Volker Theile
* @copyright Copyright (c) 2013-2024 openmediavault plugin developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class OMVRpcServiceSymlinks extends \OMV\Rpc\ServiceAbstract
{
public function getName()
{
return "Symlinks";
}
public function initialize()
{
$this->registerMethod("getSymlinkList");
$this->registerMethod("getSymlink");
$this->registerMethod("setSymlink");
$this->registerMethod("deleteSymlink");
$this->registerMethod("enumerateSymlinks");
$this->registerMethod("importSymlink");
}
public function getSymlinkList($params, $context)
{
// Validate the RPC caller context.
$this->validateMethodContext($context, ["role" => OMV_ROLE_ADMINISTRATOR]);
// Validate the parameters of the RPC service method.
$this->validateMethodParams($params, "rpc.common.getlist");
// Get the configuration object.
$db = \OMV\Config\Database::getInstance();
$objects = $db->getAssoc("conf.service.symlinks.symlink");
// Filter the result.
return $this->applyFilter($objects, $params['start'], $params['limit'],
$params['sortfield'], $params['sortdir']);
}
public function getSymlink($params, $context)
{
// Validate the RPC caller context.
$this->validateMethodContext($context, ["role" => OMV_ROLE_ADMINISTRATOR]);
// Validate the parameters of the RPC service method.
$this->validateMethodParams($params, "rpc.common.objectuuid");
// Get the configuration object.
$db = \OMV\Config\Database::getInstance();
return $db->getAssoc("conf.service.symlinks.symlink", $params['uuid']);
}
public function setSymlink($params, $context)
{
// Validate the RPC caller context.
$this->validateMethodContext($context, ["role" => OMV_ROLE_ADMINISTRATOR]);
// Validate the parameters of the RPC service method.
$this->validateMethodParams($params, "rpc.symlinks.setsymlink");
if (substr($params['source'],0,1) != '/') {
$params['source'] = '/' . $params['source'];
}
if (substr($params['destination'],0,1) != '/') {
$params['destination'] = '/' . $params['destination'];
}
// Prepare the configuration object.
$object = new \OMV\Config\ConfigObject("conf.service.symlinks.symlink");
$object->setAssoc($params);
// Set the configuration object.
$isNew = $object->isNew();
$db = \OMV\Config\Database::getInstance();
// remove old symbolic link if modifying existing symlink
if ($isNew === false) {
$oldObject = $db->getAssoc("conf.service.symlinks.symlink", $params['uuid']);
if (is_link($oldObject['destination'])) {
unlink($oldObject['destination']);
}
}
if (($object->get('enable') === true) && (is_link($dest) !== true)) {
$dest = $object->get('destination');
if (file_exists($dest)) {
throw new \OMV\Exception(
sprintf("Folder or directory already exists! %s", $dest)
);
}
// Create symbolic link
$success = false;
$success = symlink($object->get('source'), $dest);
if (false === $success) {
throw new \OMV\Exception(
sprintf("Failed to create symbolic link at %s", $dest)
);
}
}
$db->set($object);
// Return the configuration object.
return $object->getAssoc();
}
public function deleteSymlink($params, $context)
{
// Validate the RPC caller context.
$this->validateMethodContext($context, ["role" => OMV_ROLE_ADMINISTRATOR]);
// Validate the parameters of the RPC service method.
$this->validateMethodParams($params, "rpc.common.objectuuid");
// Get the configuration object.
$db = \OMV\Config\Database::getInstance();
$object = $db->get("conf.service.symlinks.symlink", $params['uuid']);
// remove old symbolic link
if (is_link($object->get('destination'))) {
unlink($object->get('destination'));
}
// Delete the configuration object.
$db->delete($object);
// Return the deleted configuration object.
return $object->getAssoc();
}
public function enumerateSymlinks($params, $context)
{
// Validate the RPC caller context.
$this->validateMethodContext($context, ['role' => OMV_ROLE_ADMINISTRATOR]);
$cmdArgs = [];
$cmdArgs[] = "/";
$cmdArgs[] = "-maxdepth 4";
$cmdArgs[] = "-type l";
$cmdArgs[] = "-regextype posix-egrep";
$cmdArgs[] = "-not";
$cmdArgs[] = "-regex '/(bin|boot|dev|etc|initrd|lib|lib(32|64)|proc|run|sbin|sys|usr|var/(run|lock|spool)|vmlinuz)/?.*'";
$cmd = new \OMV\System\Process('find', $cmdArgs);
$cmd->setQuiet(true);
$cmd->setRedirect2toFile("/dev/null");
$output = [];
$cmd->execute($output, $exitStatus);
$objects = [];
$count = count($output);
for ($i = 0; $i < $count; $i++) {
if (strlen($output[$i]) < 2) {
continue;
}
if (is_file($output[$i])) {
continue;
}
$objects[] = ['link' => trim($output[$i])];
}
return ($objects);
}
public function importSymlink($params, $context)
{
// Validate the RPC caller context.
$this->validateMethodContext($context, ['role' => OMV_ROLE_ADMINISTRATOR]);
// Validate the parameters of the RPC service method.
//$this->validateMethodParams($params, "rpc.symlinks.setsymlink");
// get source
$output = [];
$cmd = new \OMV\System\Process('readlink', $params['link']);
$cmd->execute($output, $exitStatus);
$source = $output[0];
// Prepare the configuration object.
$object = new \OMV\Config\ConfigObject("conf.service.symlinks.symlink");
$object->set('enable', true);
$object->set('source', $source);
$object->set('destination', $params['link']);
// Set the configuration object.
$db = \OMV\Config\Database::getInstance();
$db->assertIsUnique($object, "destination");
$db->set($object);
// Return the configuration object.
return $object->getAssoc();
}
}