This repository has been archived by the owner on Dec 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathSia.php
381 lines (315 loc) · 8.56 KB
/
Sia.php
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<?php
/**
* @copyright Copyright (c) 2016, Nebulous, Inc.
*
* @author Johnathan Howell <me@johnathanhowell.com>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Files_External_Sia\Storage;
set_include_path(get_include_path() . PATH_SEPARATOR .
\OC_App::getAppPath('files_external_sia') . '/sia-php');
require_once 'sia.php';
use OCP\Files\ForbiddenException;
use Icewind\Streams\IteratorDirectory;
use Icewind\Streams\CallbackWrapper;
class Sia extends \OC\Files\Storage\Common {
private $client;
private $apiaddr;
private $datadir;
public function __construct($arguments) {
if (!isset($arguments['apiaddr']) || !is_string($arguments['apiaddr'])) {
throw new \InvalidArgumentException('no api address set for Sia');
}
$this->client = new \Sia\Client($arguments['apiaddr']);
$this->apiaddr = $arguments['apiaddr'];
$this->datadir = realpath($arguments['datadir']);
}
// parsePaths takes an array of siafiles and a path and returns an array of
// siafiles that contain $path at the beginning of their siapath.
private function parsePaths($siafiles, $path) {
$ret = array();
foreach($siafiles as $siafile) {
if ($path === "/") {
array_push($ret, $siafile);
} else if (strpos($siafile, $path) === 0) {
$components = explode($path, $siafile, 2);
array_push($ret, $components[count($components)-1]);
}
}
return $ret;
}
// ls takes an array of siafiles and a path and returns an array of names of
// directories or files in that path.
private function ls($siafiles, $path) {
$ret = array();
$paths = $this->parsePaths($siafiles, $path . "/");
foreach($paths as $siafile) {
$filename = $siafile;
$pathComponents = explode('/', $siafile);
$isDir = count($pathComponents) > 1;
if ($isDir) {
$filename = $pathComponents[0];
}
$ret[$filename] = $filename;
}
return array_values($ret);
}
// localFile takes a $siapath and returns the location of the file on disk.
// Temporary files ('.octransfer*') will have the same hash as their final
// forms.
private function localFile($siapath) {
$cleanpath = $siapath;
if (strpos($siapath, '.ocTransferId') !== false) {
$cleanpath = explode('.ocTransferId', $siapath)[0];
}
return $this->datadir . '/' . hash('sha256', $cleanpath);
}
public function __destruct() {
}
public function getId() {
return 'sia::' . $this->apiaddr;
}
public function mkdir($path) {
$tmpFile = \OCP\Files::tmpFile();
$f = fopen($tmpFile, 'r+');
fwrite($f, '0x4A');
fclose($f);
$this->client->upload($path . '/.dirinfo', $tmpFile);
return true;
}
public function rmdir($path) {
if ($path == "") {
return false;
}
$files = $this->client->renterFiles();
foreach ($files as $file) {
if (strpos($file->siapath, $path) === 0) {
$this->client->delete($file->siapath);
}
}
return true;
}
public function opendir($path) {
$siafiles = $this->client->renterFiles();
$siapaths = array();
foreach($siafiles as $siafile) {
array_push($siapaths, $siafile->siapath);
}
return IteratorDirectory::wrap($this->ls($siapaths, $path));
}
// test the node's upload capability by verifying that the node has some
// contracts and the renter data directory is writeable.
public function test() {
try {
$contracts = $this->client->renterContracts();
if (count($contracts) === 0) {
return false;
}
if (!is_writeable($this->datadir)) {
return false;
}
return true;
} catch (\Exception $e) {
return false;
}
}
// directorySize returns the aggregate filesize of every file in the directory supplied by $path.
private function directorySize($path) {
$files = $this->client->renterFiles();
$sz = 0;
foreach($files as $file) {
if (strpos($file->siapath, $path) !== false) {
$sz += $file->filesize;
}
}
return $sz;
}
public function stat($path) {
clearstatcache();
$ret = array();
if ($this->is_dir($path)) {
$ret['size'] = $this->directorySize($path);
return $ret;
}
$files = $this->client->renterFiles();
foreach($files as $file) {
if (strpos($file->siapath, $path) !== false) {
$ret['size'] = $file->filesize;
return $ret;
}
}
return false;
}
public function filetype($path) {
if ($this->is_dir($path)) {
return 'dir';
}
return 'file';
}
public function is_dir($path) {
if ($path === "") {
return true;
}
if ($this->is_file($path)) {
return false;
}
$files = $this->client->renterFiles();
foreach($files as $file) {
if (strpos($file->siapath, $path) === 0) {
return true;
}
}
return false;
}
public function is_file($path) {
$file = $this->fileAtSiapath($path);
if (!$file) {
return false;
}
return true;
}
// filesize returns the file size in bytes of the file at $path.
public function filesize($path) {
if ($this->is_dir($path)) {
return $this->directorySize($path);
}
$sz = 0;
$file = $this->fileAtSiapath($path);
if (!$file) {
return false;
}
return $file->filesize;
}
public function isDeletable($path) {
if ($path === '') {
return false;
}
if ($this->file_exists($path)) {
return true;
}
return false;
}
// file_exists checks for the existence of the file at $path on the Sia node.
public function file_exists($path) {
return $this->is_dir($path) || $this->is_file($path);
}
public function isCreatable($path) {
return true;
}
public function filemtime($path) {
return false;
}
public function touch($path, $mtime = null) {
return true;
}
public function unlink($path) {
try {
$this->client->delete($path);
if (file_exists($this->localFile($path))) {
unlink($this->localFile($path));
}
} catch (\Exception $e) {
return false;
}
return true;
}
public function getPermissions($path) {
return \OCP\Constants::PERMISSION_ALL;
}
public function rename($path1, $path2) {
try {
$this->client->rename($path1, $path2);
} catch (\Exception $e) {
return false;
}
return true;
}
// isFileInDownloads returns a bool true if a file exists in the Sia node's
// downloads, otherwise it returns false.
private function isFileInDownloads($siapath) {
$exists = false;
$downloads = $this->client->downloads();
foreach($downloads as $download) {
if ($download->siapath == $siapath && $download->error == "") {
$exists = true;
}
}
return $exists;
}
// fileAtSiapath returns the file object at the requested siapath, if it
// exists.
private function fileAtSiapath($siapath) {
$files = $this->client->renterFiles();
foreach ($files as $file) {
if ($file->siapath == $siapath) {
return $file;
}
}
return false;
}
// nodeHasFile returns true if this node has a valid copy of the supplied
// siapath.
private function nodeHasFile($siapath) {
$file = $this->fileAtSiapath($siapath);
if (!$file) {
return false;
}
if (file_exists($this->localFile($siapath))) {
return filesize($this->localFile($siapath)) == $file->filesize;
}
return false;
}
public function fopen($path, $mode) {
switch ($mode) {
case 'r':
case 'rb':
// if this file is on disk, just return a handle to it
if ($this->nodeHasFile($path)) {
return fopen($this->localFile($path), 'r');
}
// if this file hasn't been downloaded already, download it and return
// a file informing the user what is happening.
if (!$this->isFileInDownloads($path)) {
$this->client->download($path, $this->localFile($path));
}
return fopen('data://text/plain,' . $path . ' is currently being downloaded from Sia.', $mode);
case 'w':
case 'wb':
case 'a':
case 'ab':
case 'r+':
case 'w+':
case 'wb+':
case 'a+':
case 'x':
case 'x+':
case 'c':
case 'c+':
$localfile = $this->localFile($path);
$handle = fopen($localfile, $mode);
return CallbackWrapper::wrap($handle, null, null, function () use ($path, $localfile) {
$this->client->upload($path, $localfile);
});
}
}
public function hasUpdated($path, $time) {
return true;
}
public function isReadable($path) {
return true;
}
}