Skip to content
Permalink
Browse files

Braces.

  • Loading branch information...
kohler committed Nov 6, 2019
1 parent dff08cb commit c42c7f8d56dad7492c47272e7e7f2e57fa92f8bc
Showing with 113 additions and 65 deletions.
  1. +22 −13 batch/cleandocstore.php
  2. +2 −1 lib/curls3document.php
  3. +26 −15 lib/s3document.php
  4. +21 −14 lib/zipdocument.php
  5. +11 −5 src/conference.php
  6. +25 −13 src/documentfiletree.php
  7. +6 −4 test/test02.php
@@ -8,8 +8,9 @@
if (isset($arg[$s]) && !isset($arg[$l]))
$arg[$l] = $arg[$s];
}
if (isset($arg["silent"]))
if (isset($arg["silent"])) {
$arg["quiet"] = false;
}
if (isset($arg["h"]) || isset($arg["help"])) {
fwrite(STDOUT, "Usage: php batch/cleandocstore.php [-c COUNT] [-V] [-m MATCH]\n"
. " [-d|--dry-run] [-u USAGELIMIT]\n");
@@ -51,8 +52,9 @@
}
$want_fs = $ts * (1 - (float) $arg["max-usage"]);
$usage_threshold = $want_fs - $fs;
if (!isset($arg["count"]))
if (!isset($arg["count"])) {
$count = 5000;
}
}
@@ -61,7 +63,8 @@
$ndone = $nsuccess = $bytesremoved = 0;
while ($count > 0 && ($usage_threshold === null || $bytesremoved < $usage_threshold)) {
while ($count > 0
&& ($usage_threshold === null || $bytesremoved < $usage_threshold)) {
$x = [];
for ($i = 0; $x ? count($x) < 5 && $i < 10 : $i < 10000; ++$i) {
$fm = $fparts->random_match();
@@ -84,18 +87,20 @@
$fm = $x[0];
$fparts->hide($fm);
$doc = new DocumentInfo(["sha1" => $fm->algohash,
"mimetype" => Mimetype::type($fm->extension)], $Conf);
$doc = new DocumentInfo([
"sha1" => $fm->algohash,
"mimetype" => Mimetype::type($fm->extension)
], $Conf);
$hashalg = $doc->hash_algorithm();
$ok = false;
$size = 0;
if ($hashalg === false)
if ($hashalg === false) {
fwrite(STDERR, "{$fm->fname}: unknown hash\n");
else if (($chash = hash_file($hashalg, $fm->fname, true)) === false)
} else if (($chash = hash_file($hashalg, $fm->fname, true)) === false) {
fwrite(STDERR, "{$fm->fname}: is unreadable\n");
else if ($chash !== $doc->binary_hash_data())
} else if ($chash !== $doc->binary_hash_data()) {
fwrite(STDERR, "{$fm->fname}: incorrect hash\n");
else if ($doc->check_s3()) {
} else if ($doc->check_s3()) {
$size = filesize($fm->fname);
if ($dry_run) {
if ($verbose)
@@ -105,10 +110,12 @@
if ($verbose)
fwrite(STDOUT, "{$fm->fname}: removed\n");
$ok = true;
} else
} else {
fwrite(STDERR, "{$fm->fname}: cannot remove\n");
} else
}
} else {
fwrite(STDERR, "{$fm->fname}: not on S3\n");
}
--$count;
++$ndone;
if ($ok) {
@@ -117,8 +124,10 @@
}
}
if ($verbose && $usage_threshold !== null && $bytesremoved >= $usage_threshold)
if ($verbose && $usage_threshold !== null && $bytesremoved >= $usage_threshold) {
fwrite(STDOUT, $usage_directory . ": free space above threshold\n");
if (!isset($arg["quiet"]))
}
if (!isset($arg["quiet"])) {
fwrite(STDOUT, $usage_directory . ": " . ($dry_run ? "would remove " : "removed ") . plural($nsuccess, "file") . ", " . plural($bytesremoved, "byte") . "\n");
}
exit($nsuccess && $nsuccess == $ndone ? 0 : 1);
@@ -80,8 +80,9 @@ function run() {
while (true) {
$this->prepare();
$this->exec();
if ($this->parse_result())
if ($this->parse_result()) {
return;
}
$timeout = 0.005 * (1 << $this->runindex);
S3Document::$retry_timeout_allowance -= $timeout;
usleep(1000000 * $timeout);
@@ -9,9 +9,9 @@ class S3Result {
public $user_data;
function check_skey($skey) {
if ((string) $skey !== "")
if ((string) $skey !== "") {
return true;
else {
} else {
$this->status = 404;
$this->status_text = "Filename missing";
$this->response_headers = $this->user_data = [];
@@ -27,12 +27,13 @@ function parse_response_lines($w) {
$this->status = (int) $m[1];
$this->status_text = $m[2];
}
for ($i = 1; $i != count($w); ++$i)
for ($i = 1; $i != count($w); ++$i) {
if (preg_match('{\A(.*?):\s*(.*)\z}', $w[$i], $m)) {
$this->response_headers[strtolower($m[1])] = $m[2];
if (substr($m[1], 0, 11) == "x-amz-meta-")
$this->user_data[substr($m[1], 11)] = $m[2];
}
}
}
}
@@ -112,23 +113,26 @@ function signature($method, $url, $hdr, $content = null) {
if (($query = $m[3]) !== "") {
$a = [];
foreach (explode("&", $query) as $x)
foreach (explode("&", $query) as $x) {
if (($pos = strpos($x, "=")) !== false) {
$k = substr($x, 0, $pos);
$v = rawurlencode(urldecode(substr($x, $pos + 1)));
$a[$k] = "$k=$v";
} else
} else {
$a[$x] = "$x=";
}
}
ksort($a);
$query = join("&", $a);
}
$chdr = ["Host" => $host];
foreach ($hdr as $k => $v)
foreach ($hdr as $k => $v) {
if (strcasecmp($k, "host")) {
$v = trim($v);
$chdr[$k] = $v;
}
}
if (!isset($chdr["x-amz-content-sha256"])) {
if ($content !== false && $content !== "" && $content !== null)
$h = hash("sha256", $content);
@@ -165,15 +169,17 @@ function signature($method, $url, $hdr, $content = null) {
. hash("sha256", $canonical_request);
$hdrarr = [];
foreach ($chdr as $k => $v)
foreach ($chdr as $k => $v) {
$hdrarr[] = $k . ": " . $v;
}
$signature = hash_hmac("sha256", $signable, $signing_key);
$hdrarr[] = "Authorization: AWS4-HMAC-SHA256 Credential="
. $this->s3_key . "/" . $scope
. ",SignedHeaders=" . substr($chk, 1)
. ",Signature=" . $signature;
if (self::$verbose)
if (self::$verbose) {
error_log(var_export($hdrarr, true));
}
return ["headers" => $hdrarr, "signature" => $signature];
}
@@ -188,12 +194,13 @@ private function signed_headers($skey, $method, $args) {
$xkey = "x-amz-meta-$xkey";
$hdr[$xkey] = $xvalue;
}
} else if ($key === "content")
} else if ($key === "content") {
$content = $value;
else if ($key === "content_type")
} else if ($key === "content_type") {
$content_type = $value;
else
} else {
$hdr[$key] = $value;
}
}
$sig = $this->signature($method, $url, $hdr, $content);
return [$url, $sig["headers"], $content, $content_type];
@@ -228,8 +235,9 @@ private function run_stream_once($skey, $method, $args) {
}
private function run($skey, $method, $args) {
if (!$this->check_skey($skey))
if (!$this->check_skey($skey)) {
return;
}
if (isset($args["content"])
&& strlen($args["content"]) > 10000000
&& strlen($args["content"]) * 2.5 > ini_get_bytes("memory_limit")
@@ -239,8 +247,9 @@ private function run($skey, $method, $args) {
for ($i = 1; true; ++$i) {
$this->clear_result();
$this->run_stream_once($skey, $method, $args);
if ($this->status !== null && $this->status !== 500)
if ($this->status !== null && $this->status !== 500) {
return;
}
if (self::$retry_timeout_allowance <= 0 || $i >= 5) {
trigger_error("S3 error: $method $skey: failed", E_USER_WARNING);
return;
@@ -254,10 +263,11 @@ private function run($skey, $method, $args) {
function save($skey, $content, $content_type, $user_data = null) {
$this->run($skey, "HEAD", []);
if ($this->status != 200
|| get($this->response_headers, "content-length") != strlen($content))
|| get($this->response_headers, "content-length") != strlen($content)) {
$this->run($skey, "PUT", ["content" => $content,
"content_type" => $content_type,
"user_data" => $user_data]);
}
return $this->status == 200;
}
@@ -297,9 +307,10 @@ function copy($src_skey, $dst_skey) {
function ls($prefix, $args = []) {
$suffix = "?list-type=2&prefix=" . urlencode($prefix);
foreach (["max-keys", "start-after", "continuation-token"] as $k)
foreach (["max-keys", "start-after", "continuation-token"] as $k) {
if (isset($args[$k]))
$suffix .= "&" . $k . "=" . urlencode($args[$k]);
}
$this->run($suffix, "GET", []);
return get($this->response_headers, "content");
}
@@ -40,15 +40,17 @@ function clean() {
private function tmpdir() {
if ($this->_tmpdir === null
&& ($this->_tmpdir = tempdir()) === false)
&& ($this->_tmpdir = tempdir()) === false) {
$this->warnings[] = "Could not create temporary directory.";
}
return $this->_tmpdir;
}
private function _save_one() {
assert($this->_saveindex < count($this->_docs));
if (!($tmpdir = $this->tmpdir()))
if (!($tmpdir = $this->tmpdir())) {
return false;
}
$doc = $this->_docs[$this->_saveindex];
++$this->_saveindex;
// create parent directories
@@ -64,9 +66,9 @@ private function _save_one() {
// store file in temporary directory
$zfn = $tmpdir . "/" . $fn;
if (($path = $doc->available_content_file())
&& @symlink($path, $zfn))
/* OK */;
else {
&& @symlink($path, $zfn)) {
// OK
} else {
$content = $doc->content();
$trylen = file_put_contents($zfn, $content);
if ($trylen !== strlen($content)) {
@@ -91,8 +93,9 @@ private function _add($doc, $filename, $check_filename) {
$doc = new DocumentInfo(["content" => $doc], $this->conf);
assert($doc instanceof DocumentInfo);
if ($filename == "" && $doc->filename != "")
if ($filename == "" && $doc->filename != "") {
$filename = $doc->filename;
}
// maybe this is a warning container
if ($doc->error) {
@@ -131,14 +134,17 @@ private function _add($doc, $filename, $check_filename) {
// add document to list
$doc = clone $doc;
$doc->filename = $filename;
if ($doc->available_content_file() && $doc->content !== null)
if ($doc->available_content_file() && $doc->content !== null) {
$doc->content = null;
}
$this->_docs[] = $doc;
if ($doc->content !== null)
if ($doc->content !== null) {
$this->_docmem += strlen($doc->content);
}
while ($this->_saveindex < count($this->_docs)
&& $this->_docmem > 4000000)
&& $this->_docmem > 4000000) {
$this->_save_one();
}
return true;
}
@@ -160,9 +166,9 @@ function download_headers() {
}
private function _make_document($error_html = false) {
if (!$error_html)
if (!$error_html) {
return new DocumentInfo(["filename" => $this->filename, "mimetype" => $this->mimetype, "content_file" => $this->filestore], $this->conf);
else {
} else {
$this->filestore = false;
return new DocumentInfo(["error" => true, "error_html" => $error_html], $this->conf);
}
@@ -190,8 +196,9 @@ function create() {
if (Filer::prepare_docstore($dstore_prefix, $zfn)) {
$this->filestore = $zfn;
if (file_exists($zfn)) {
if (@filemtime($zfn) < $Now - 21600)
if (@filemtime($zfn) < $Now - 21600) {
@touch($zfn);
}
return $this->_make_document();
}
}
@@ -207,8 +214,8 @@ function create() {
$this->_save_one();
if (!$this->filestore) {
for ($n = 0; isset($this->_files["_hotcrp$n.zip"]); ++$n)
/* skip */;
for ($n = 0; isset($this->_files["_hotcrp$n.zip"]); ++$n) {
}
$this->filestore = $tmpdir . "/_hotcrp$n.zip";
}
$topfiles = array_filter(array_keys($this->_files), function ($f) {
@@ -312,21 +312,27 @@ private function crosscheck_settings() {
$this->crosscheck_round_settings();
// S3 settings
foreach (array("s3_bucket", "s3_key", "s3_secret") as $k)
foreach (array("s3_bucket", "s3_key", "s3_secret") as $k) {
if (!get($this->settingTexts, $k) && ($x = get($this->opt, $k)))
$this->settingTexts[$k] = $x;
}
if (!get($this->settingTexts, "s3_key")
|| !get($this->settingTexts, "s3_secret")
|| !get($this->settingTexts, "s3_bucket"))
|| !get($this->settingTexts, "s3_bucket")) {
unset($this->settingTexts["s3_key"], $this->settingTexts["s3_secret"],
$this->settingTexts["s3_bucket"]);
if (get($this->opt, "dbNoPapers") && !get($this->opt, "docstore")
&& !get($this->opt, "filestore") && !get($this->settingTexts, "s3_bucket"))
}
if (get($this->opt, "dbNoPapers")
&& !get($this->opt, "docstore")
&& !get($this->opt, "filestore")
&& !get($this->settingTexts, "s3_bucket")) {
unset($this->opt["dbNoPapers"]);
}
if ($this->_s3_document
&& (!isset($this->settingTexts["s3_bucket"])
|| !$this->_s3_document->check_key_secret_bucket($this->settingTexts["s3_key"], $this->settingTexts["s3_secret"], $this->settingTexts["s3_bucket"])))
|| !$this->_s3_document->check_key_secret_bucket($this->settingTexts["s3_key"], $this->settingTexts["s3_secret"], $this->settingTexts["s3_bucket"]))) {
$this->_s3_document = false;
}
// tracks settings
$this->_tracks = $this->_track_tags = null;

0 comments on commit c42c7f8

Please sign in to comment.
You can’t perform that action at this time.