Skip to content

Commit fa9e18f

Browse files
committed
Added support for max_size
1 parent 6f7dee1 commit fa9e18f

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

restic-index.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
$restic = Restic::Instance(Array(
66
"path" => "restic",
77
"append_only" => false,
8-
"private_repos" => false
8+
"private_repos" => false,
9+
"max_size" => 0
910
));
1011

1112
function page_404() {

restic-server.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ class Restic
77
private $append_only = false;
88
private $block_size = 8192;
99
private $basePath = "restic";
10+
private $currentSize = 0;
11+
private $maxRepoSize = 0;
1012
public $private_repos = false;
1113

1214

@@ -24,6 +26,9 @@ private function __construct($opts)
2426
if (array_key_exists("block_size", $opts)) {
2527
$this->block_size = $opts["block_size"];
2628
}
29+
if (array_key_exists("max_size", $opts)) {
30+
$this->maxRepoSize = $opts["max_size"];
31+
}
2732
}
2833
public static function Instance($opts = Array())
2934
{
@@ -51,6 +56,12 @@ public function sendStatus($status)
5156
case 404:
5257
header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found");
5358
break;
59+
case 411;
60+
header($_SERVER["SERVER_PROTOCOL"] . " 411 Length Required");
61+
break;
62+
case 413:
63+
header($_SERVER["SERVER_PROTOCOL"] . " 413 Request Entity Too Large");
64+
break;
5465
case 416:
5566
header($_SERVER["SERVER_PROTOCOL"] . " 416 Requested Range Not Satisfiable");
5667
break;
@@ -61,6 +72,28 @@ public function sendStatus($status)
6172
header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
6273
}
6374
}
75+
76+
private function tallySize($path, $firstrun = false)
77+
{
78+
$size = 0;
79+
$items = $firstrun
80+
? $this->validTypes
81+
: scandir($path);
82+
foreach($items as $i) {
83+
if ($i === "." || $i === "..") {
84+
continue;
85+
}
86+
$fullpath = $this->pathResolve($path, $i);
87+
if (is_dir($fullpath)) {
88+
$size += $this->tallySize($fullpath);
89+
} else {
90+
$st = stat($fullpath);
91+
$size += $st["size"];
92+
}
93+
}
94+
return $size;
95+
}
96+
6497
private function pathResolve()
6598
{
6699
$sep = DIRECTORY_SEPARATOR;
@@ -263,6 +296,24 @@ public function saveBlob($repo_name, $type, $name = "")
263296
$type = func_get_arg(0);
264297
$repo_name = ".";
265298
}
299+
300+
if ($this->maxRepoSize != 0) {
301+
// We never update currentSize after this, because the server will execute
302+
// an instance of the script per request anyway. The stat cache helps with the speed.
303+
$this->currentSize = $this->tallySize($this->pathResolve($this->basePath, $repo_name), true);
304+
305+
if (array_key_exists("CONTENT_LENGTH", $_SERVER) && $_SERVER["CONTENT_LENGTH"] != "") {
306+
$contentLen = intval($_SERVER["CONTENT_LENGTH"]);
307+
if (($this->currentSize + $contentLen) > $this->maxRepoSize) {
308+
$this->sendStatus(413); // payload too large
309+
exit;
310+
}
311+
} else {
312+
$this->sendStatus(411); // length required
313+
exit;
314+
}
315+
}
316+
266317
if ($this->isHashed($type)) {
267318
$path = $this->pathResolve($this->basePath, $repo_name, $type, substr($name, 0, 2), $name);
268319
} else {

0 commit comments

Comments
 (0)