Browse Source

implement fix tool

main 1.1.0
ghost 1 year ago
parent
commit
555ceee2b2
  1. 12
      README.md
  2. 192
      cli/fix.php

12
README.md

@ -15,6 +15,18 @@ php cli/put.php processor filename [length] [delay] @@ -15,6 +15,18 @@ php cli/put.php processor filename [length] [delay]
* `length` - optional split size, `3072` bytes [max](https://kevacoin.org/faq.html)
* `delay` - optional seconds of parts sending delay to prevent `too-long-mempool-chain` reject, default `60`
### fix
check and fix FS object in blockchain
```
php cli/put.php processor filename [delay]
```
* `processor` - path to `kevacoin-cli`
* `filename` - local file path to store
* `delay` - optional seconds of parts sending delay to prevent `too-long-mempool-chain` reject, default `60`
### get
import from blockchain to FS location

192
cli/fix.php

@ -1,3 +1,193 @@ @@ -1,3 +1,193 @@
<?php
// @TODO #1
// Init helper
function _exec(
string $processor,
string $command
): mixed
{
if (false !== exec(sprintf('%s %s', $processor, $command), $output))
{
$rows = [];
foreach($output as $row)
{
$rows[] = $row;
}
if ($result = @json_decode(implode(PHP_EOL, $rows)))
{
return $result;
}
}
return false;
}
// Check file exits
if (!file_exists($argv[2]))
{
exit('filename does not exist!' . PHP_EOL);
}
// Get file hash sum
$md5file = md5_file(
$argv[2]
);
// Search namespace by md5file
echo 'Get namespaces list...' . PHP_EOL;
$namespaces = _exec(
$argv[1],
'keva_list_namespaces'
);
// Get _CLITOR_IS_ meta
foreach ((array) $namespaces as $namespace)
{
echo sprintf(
'Search for _CLITOR_IS_ match file MD5 %s ...' . PHP_EOL,
$md5file
);
$meta = _exec(
$argv[1],
sprintf(
"%s '%s' '%s'",
'keva_get',
$namespace->namespaceId,
'_CLITOR_IS_'
)
);
if ($value = @json_decode($meta->value))
{
if (
isset($value->pieces) &&
isset($value->pieces->total) &&
isset($value->pieces->size) &&
isset($value->file) &&
isset($value->file->md5) &&
$value->file->md5 === $md5file
)
{
// Meta found
echo sprintf(
'_CLITOR_IS_ found for this file with namespace %s' . PHP_EOL,
$namespace->namespaceId
);
// Split content to smaller parts using _CLITOR_IS_ size defined before
$pieces = str_split(
base64_encode(
file_get_contents(
$argv[2]
)
),
$value->pieces->size
);
// Count total pieces
$total = count(
$pieces
);
// Validate pieces count
if ($value->pieces->total !== $total)
{
echo '_CLITOR_IS_ have another pieces quantity' . PHP_EOL;
exit;
}
// Begin pieces saving
foreach ($pieces as $key => $value)
{
// Check piece stored is valid
$piece = _exec(
$argv[1],
sprintf(
"%s '%s' '%s'",
'keva_get',
$namespace->namespaceId,
$key
)
);
// Piece value not found
if (empty($piece->value))
{
echo sprintf(
'Piece %s value not found, creating...' . PHP_EOL,
$key
);
}
// Piece value invalid, begin blockchain record
else if ($piece->value !== $value)
{
echo sprintf(
'Piece %s value invalid (%s <> %s), rewriting...' . PHP_EOL,
$key,
md5(
$piece->value
),
md5(
$value
),
);
}
// Piece valid
else
{
echo sprintf(
'Piece %s - OK' . PHP_EOL,
$key
);
continue;
}
print_r(
_exec(
$argv[1],
sprintf(
"%s '%s' '%s' '%s'",
'keva_put',
$namespace->namespaceId,
$key,
$value
)
)
);
// Apply delays to prevent too-long-mempool-chain reject
$delay = isset($argv[3]) && $argv[3] > 0 ? (int) $argv[3] : 60;
echo sprintf(
'Piece %s sent, waiting %s seconds...' . PHP_EOL,
$key,
$total,
$delay
);
sleep($delay);
}
// Print result
echo sprintf(
'done! run to extract: php %s/get.php %s %s' . PHP_EOL,
__DIR__,
$argv[1],
$namespace->namespaceId
);
break;
}
}
}
echo 'Could not recover this file!' . PHP_EOL;
Loading…
Cancel
Save