Skip to content

Commit

Permalink
prevent saving in case of low disk space fix #347
Browse files Browse the repository at this point in the history
the limit is hard coded to 256kio
  • Loading branch information
vincent-peugnet committed Dec 5, 2023
1 parent 811b90d commit f1beddf
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 80 deletions.
4 changes: 2 additions & 2 deletions app/class/Modelbookmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function add(Bookmark $bookmark)
}
$bookmarkdata = new Document($bookmark->dry());
$bookmarkdata->setId($bookmark->id());
$success = $this->repo->store($bookmarkdata);
$success = $this->storedoc($bookmarkdata);
if (!$success) {
throw new Databaseexception("Error while adding Bookmark to database");
}
Expand Down Expand Up @@ -186,7 +186,7 @@ public function update(Bookmark $bookmark): Bookmark

$bookmarkdata = new Document($bookmark->dry());
$bookmarkdata->setId($bookmark->id());
$success = $this->repo->store($bookmarkdata);
$success = $this->updatedoc($bookmarkdata);
if (!$success) {
throw new RuntimeException("Error while updating Bookmark to database");
}
Expand Down
52 changes: 52 additions & 0 deletions app/class/Modeldb.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Wcms;

use InvalidArgumentException;
use JamesMoss\Flywheel;
use JamesMoss\Flywheel\DocumentInterface;
use RuntimeException;
use Wcms\Flywheel\Formatter\JSON;
use Wcms\Flywheel\Query;
Expand All @@ -14,12 +16,62 @@ class Modeldb extends Model
/** @var Repository */
protected $repo;

/**
* Minimal disk space needed to authorize database writing.
* 2^18 o = 256 kio
*/
public const MINIMAL_DISK_SPACE = 2 ** 18;

public function __construct()
{
$this->dbinit();
}

/**
* Check if database directory have at least the minimal free disk space required left.
*
* @return bool True if enought space left, otherwise False
*/
protected function isdiskfree(): bool
{
try {
return (disk_free_space_ex(self::DATABASE_DIR) > self::MINIMAL_DISK_SPACE);
} catch (RuntimeException $e) {
throw new InvalidArgumentException($e->getMessage());
}
}

/**
* Store Document but only if there is enough space left on disk
*
* @param DocumentInterface $document Flywheel Document
* @return bool True in case of success, otherwise false
*
* @todo use exceptions to create a disctinction between differents possible problems
*/
protected function storedoc(DocumentInterface $document): bool {

Check failure on line 52 in app/class/Modeldb.php

View workflow job for this annotation

GitHub Actions / lint php

Opening brace should be on a new line
if (!$this->isdiskfree()) {
Logger::error("Not enough free space on disk to store datas in database");
return false;
}
return $this->repo->store($document);

Check failure on line 57 in app/class/Modeldb.php

View workflow job for this annotation

GitHub Actions / analyse php 8.1

Parameter #1 $document of method JamesMoss\Flywheel\Repository::store() expects JamesMoss\Flywheel\Document, JamesMoss\Flywheel\DocumentInterface given.

Check failure on line 57 in app/class/Modeldb.php

View workflow job for this annotation

GitHub Actions / lint php

Parameter #1 $document of method JamesMoss\Flywheel\Repository::store() expects JamesMoss\Flywheel\Document, JamesMoss\Flywheel\DocumentInterface given.
}

/**
* Update Document but only if there is enough space left on disk
*
* @param DocumentInterface $document Flywheel Document
* @return bool True in case of success, otherwise false
*
* @todo use exceptions to create a disctinction between differents possible problems
*/
protected function updatedoc(DocumentInterface $document): bool {

Check failure on line 68 in app/class/Modeldb.php

View workflow job for this annotation

GitHub Actions / lint php

Opening brace should be on a new line
if (!$this->isdiskfree()) {
Logger::error("Not enough free space on disk to update datas in database");
return false;
}
return $this->repo->update($document);

Check failure on line 73 in app/class/Modeldb.php

View workflow job for this annotation

GitHub Actions / analyse php 8.1

Parameter #1 $document of method JamesMoss\Flywheel\Repository::update() expects JamesMoss\Flywheel\Document, JamesMoss\Flywheel\DocumentInterface given.

Check failure on line 73 in app/class/Modeldb.php

View workflow job for this annotation

GitHub Actions / lint php

Parameter #1 $document of method JamesMoss\Flywheel\Repository::update() expects JamesMoss\Flywheel\Document, JamesMoss\Flywheel\DocumentInterface given.
}

protected function dbinit($dir = Model::DATABASE_DIR)
{
Expand Down
32 changes: 2 additions & 30 deletions app/class/Modelpage.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function add(Page $page): bool

$pagedata = new Document($page->dry());
$pagedata->setId($page->id());
return $this->repo->store($pagedata);
return $this->storedoc($pagedata);
}

/**
Expand Down Expand Up @@ -231,7 +231,6 @@ public function unlink(string $pageid)
/**
* Update a page in the database
*
* @todo Check if page already exist before updating ?
* @todo Use Exceptions instead of returning bool
*
* @param Page $page The page that is going to be updated
Expand All @@ -243,36 +242,9 @@ public function update(Page $page)
{
$pagedata = new Document($page->dry());
$pagedata->setId($page->id());
return $this->repo->store($pagedata);
return $this->updatedoc($pagedata);
}

public function combine(Page $pagea, Page $pageb)
{
$mergepage = $pagea;
$merge = [];
$diff = [];
foreach ($pagea::TABS as $element) {
if ($pagea->$element() !== $pageb->$element()) {
$merge[$element] = compare($pagea->$element(), $pageb->$element());
$diff[] = $element;
}
}
$mergepage->hydrate($merge);

return ['diff' => $diff, 'mergepage' => $mergepage];
}

// public function diffpageelement(Page $pagea, Page $pageb)
// {
// $diff = [];
// foreach ($pagea::TABS as $element) {
// if($pagea->$element() !== $pageb->$element()) {
// $diff[] = $element;
// }
// }
// return $diff;
// }

/**
* @param string[] $taglist list of tags
* @param Page[] $pagelist list of Page
Expand Down
4 changes: 3 additions & 1 deletion app/class/Modeluser.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,14 @@ public function passwordcheck(User $user, string $pass): bool
* @param User $user
*
* @return bool depending on success
*
* @todo throw error instead of returning boolean
*/
public function add(User $user): bool
{
$userdata = new Document($user->dry());
$userdata->setId($user->id());
return $this->repo->store($userdata);
return $this->storedoc($userdata);
}


Expand Down
65 changes: 18 additions & 47 deletions app/fn/fn.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,53 +143,6 @@ function changekey($array, $oldkey, $newkey)
return array_combine($keys, $array);
}



function compare($stringa, $stringb)
{
$arraya = explode("\n", $stringa);
$arrayb = explode("\n", $stringb);

$lnb = -1;
$commonlines = [];
foreach ($arraya as $na => $linea) {
$found = false;
foreach ($arrayb as $nb => $lineb) {
if ($linea === $lineb && $nb > $lnb && !$found && !empty($linea)) {
$commonlines[$na] = $nb;
$merge[] = $arrayb[$nb];
$lnb = $nb;
$found = true;
}
}
}


$merge = [];
$lnb = 0;
foreach ($arraya as $na => $linea) {
if (array_key_exists($na, $commonlines)) {
for ($j = $lnb; $j <= $commonlines[$na]; $j++) {
$merge[] = $arrayb[$j];
}
$lnb = $j;
} else {
$merge[] = $arraya[$na];
}
}
for ($k = $lnb;; $k++) {
if (array_key_exists($k, $arrayb)) {
$merge[] = $arrayb[$k];
} else {
break;
}
}

return implode("\n", $merge);
}



function findsize($file)
{
if (substr(PHP_OS, 0, 3) == "WIN") {
Expand Down Expand Up @@ -452,3 +405,21 @@ function insert_after(string $text, string $after, string $insert)
$pos = $afterpos + strlen($after);
return substr_replace($text, $insert, $pos, 0);
}

/**
* Returns available space on filesystem or disk partition in octets
*
* @param string $directory Directory to mesure
* @return float In bytes
*
* @throws RuntimeException In case of fail
*/
function disk_free_space_ex(string $directory): float
{
$dfs = disk_free_space($directory);
if (is_bool($dfs)) {
throw new RuntimeException("Error while calculating free space left on disk with directory `$directory`");
} else {
return $dfs;
}
}

0 comments on commit f1beddf

Please sign in to comment.