-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathSymlinkPlugin.php
More file actions
169 lines (153 loc) · 6.19 KB
/
SymlinkPlugin.php
File metadata and controls
169 lines (153 loc) · 6.19 KB
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
<?php
declare(strict_types=1);
/*
* @copyright Copyright (c) 2023 Tamino Bauknecht <dev@tb6.eu>
*
* @author Tamino Bauknecht <dev@tb6.eu>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\DAV\Upload;
use OC\Files\SymlinkManager;
use Psr\Log\LoggerInterface;
use Sabre\DAV\Server;
use Sabre\DAV\ServerPlugin;
use Sabre\HTTP\RequestInterface;
use Sabre\HTTP\ResponseInterface;
class SymlinkPlugin extends ServerPlugin {
/**
* @var Server
*
* @psalm-suppress PropertyNotSetInConstructor
*/
private $server;
/** @var SymlinkManager */
private $symlinkManager;
/** @var LoggerInterface */
private $logger;
public function __construct(LoggerInterface $logger) {
$this->symlinkManager = new SymlinkManager();
$this->logger = $logger;
}
/**
* @inheritdoc
*/
public function initialize(Server $server): void {
$server->on('method:PUT', [$this, 'httpPut']);
$server->on('method:DELETE', [$this, 'httpDelete']);
$server->on('afterMove', [$this, 'afterMove']);
$server->on('afterCopy', [$this, 'afterCopy']);
$this->server = $server;
}
public function httpPut(RequestInterface $request, ResponseInterface $response): bool {
if ($request->hasHeader('OC-File-Type') && $request->getHeader('OC-File-Type') == 1) {
$symlinkPath = $request->getPath();
$symlinkName = basename($symlinkPath);
$symlinkTarget = $request->getBodyAsString();
$parentPath = dirname($symlinkPath);
$parentNode = $this->server->tree->getNodeForPath($parentPath);
if (!$parentNode instanceof \Sabre\DAV\ICollection) {
throw new \Sabre\DAV\Exception\Forbidden("Directory does not allow creation of files - failed to upload '$symlinkName'");
}
$etag = $parentNode->createFile($symlinkName);
$symlinkNode = $parentNode->getChild($symlinkName);
if (!$symlinkNode instanceof \OCA\DAV\Connector\Sabre\File) {
throw new \Sabre\DAV\Exception\NotFound("Failed to get newly created file '$symlinkName'");
}
$symlinkNode->put($symlinkTarget);
$this->symlinkManager->storeSymlink($symlinkNode->getFileInfo());
if ($etag) {
$response->setHeader('OC-ETag', $etag);
}
$response->setStatus(201);
return false; // this request was handled already
} elseif ($this->server->tree->nodeExists($request->getPath())) {
$node = $this->server->tree->getNodeForPath($request->getPath());
if (!$node instanceof \OCA\DAV\Connector\Sabre\File) {
// cannot check if file was symlink before - let's hope it's not
$this->logger->warning('Unable to check if there was a symlink
before at the same location');
return true;
}
// if the newly uploaded file is not a symlink,
// but there was a symlink at the same path before
if ($this->symlinkManager->isSymlink($node->getFileInfo())) {
$this->symlinkManager->deleteSymlink($node->getFileInfo());
}
}
return true; // continue handling this request
}
public function httpDelete(RequestInterface $request, ResponseInterface $response): bool {
$path = $request->getPath();
$node = $this->server->tree->getNodeForPath($path);
if (!$node instanceof \OCA\DAV\Connector\Sabre\Node) {
return true;
}
$info = $node->getFileInfo();
if ($this->symlinkManager->isSymlink($info)) {
if (!$this->symlinkManager->deleteSymlink($info)) {
$symlinkName = $info->getName();
throw new \Sabre\DAV\Exception\NotFound("Unable to delete symlink '$symlinkName'!");
}
}
// always propagate to trigger deletion of regular file representing symlink in filesystem
return true;
}
public function afterMove(string $source, string $destination): void {
// source node does not exist anymore, thus use still existing parent
$sourceParentNode = dirname($source);
$sourceParentNode = $this->server->tree->getNodeForPath($sourceParentNode);
if (!$sourceParentNode instanceof \OCA\DAV\Connector\Sabre\Node) {
throw new \Sabre\DAV\Exception\NotImplemented('Unable to check if moved file is a symlink!');
}
$destinationNode = $this->server->tree->getNodeForPath($destination);
if (!$destinationNode instanceof \OCA\DAV\Connector\Sabre\Node) {
throw new \Sabre\DAV\Exception\NotImplemented('Unable to set symlink information on move destination!');
}
$sourceInfo = new \OC\Files\FileInfo(
$source,
$sourceParentNode->getFileInfo()->getStorage(),
$sourceParentNode->getInternalPath() . '/' . basename($source),
[],
$sourceParentNode->getFileInfo()->getMountPoint());
$destinationInfo = $destinationNode->getFileInfo();
if ($this->symlinkManager->isSymlink($sourceInfo)) {
$this->symlinkManager->deleteSymlink($sourceInfo);
$this->symlinkManager->storeSymlink($destinationInfo);
} elseif ($this->symlinkManager->isSymlink($destinationInfo)) {
// source was not a symlink, but destination was a symlink before
$this->symlinkManager->deleteSymlink($destinationInfo);
}
}
public function afterCopy(string $source, string $destination): void {
$sourceNode = $this->server->tree->getNodeForPath($source);
if (!$sourceNode instanceof \OCA\DAV\Connector\Sabre\Node) {
throw new \Sabre\DAV\Exception\NotImplemented(
'Unable to check if copied file is a symlink!');
}
$destinationNode = $this->server->tree->getNodeForPath($destination);
if (!$destinationNode instanceof \OCA\DAV\Connector\Sabre\Node) {
throw new \Sabre\DAV\Exception\NotImplemented(
'Unable to set symlink information on copy destination!');
}
$sourceInfo = $sourceNode->getFileInfo();
$destinationInfo = $destinationNode->getFileInfo();
if ($this->symlinkManager->isSymlink($sourceInfo)) {
$this->symlinkManager->storeSymlink($destinationInfo);
}
}
}