Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an additional free disk space check before saving the datastore #1913

Merged
merged 1 commit into from
Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion application/bookmark/BookmarkIO.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use malkusch\lock\mutex\NoMutex;
use Shaarli\Bookmark\Exception\DatastoreNotInitializedException;
use Shaarli\Bookmark\Exception\EmptyDataStoreException;
use Shaarli\Bookmark\Exception\InvalidWritableDataException;
use Shaarli\Bookmark\Exception\NotEnoughSpaceException;
use Shaarli\Bookmark\Exception\NotWritableDataStoreException;
use Shaarli\Config\ConfigManager;

Expand Down Expand Up @@ -107,6 +109,7 @@ public function read()
* @param Bookmark[] $links
*
* @throws NotWritableDataStoreException the datastore is not writable
* @throws InvalidWritableDataException
*/
public function write($links)
{
Expand All @@ -118,9 +121,19 @@ public function write($links)
throw new NotWritableDataStoreException(dirname($this->datastore));
}

$data = self::$phpPrefix . base64_encode(gzdeflate(serialize($links))) . self::$phpSuffix;
$data = base64_encode(gzdeflate(serialize($links)));

if (empty($data)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would an empty $data happen?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If, for any kind of reason, PHP was not able to serialize or compress the bookmarks data.
Sometimes, an invalid UTF-8 character can mess up everything with PHP functions. Better be safe than sorry.

throw new InvalidWritableDataException();
}

$data = self::$phpPrefix . $data . self::$phpSuffix;

$this->synchronized(function () use ($data) {
if (!$this->checkDiskSpace($data)) {
throw new NotEnoughSpaceException();
}

file_put_contents(
$this->datastore,
$data
Expand All @@ -144,4 +157,17 @@ protected function synchronized(callable $function): void
$function();
}
}

/**
* Make sure that there is enough disk space available to save the current data store.
* We add an arbitrary margin of 500kB.
*
* @param string $data to be saved
*
* @return bool True if data can safely be saved
*/
public function checkDiskSpace(string $data): bool
{
return disk_free_space(dirname($this->datastore)) > (strlen($data) + 1024 * 500);
}
}
14 changes: 14 additions & 0 deletions application/bookmark/exception/InvalidWritableDataException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Shaarli\Bookmark\Exception;

class InvalidWritableDataException extends \Exception
{
/**
* InvalidWritableDataException constructor.
*/
public function __construct()
{
$this->message = 'Couldn\'t generate bookmark data to store in the datastore. Skipping file writing.';
}
}
14 changes: 14 additions & 0 deletions application/bookmark/exception/NotEnoughSpaceException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Shaarli\Bookmark\Exception;

class NotEnoughSpaceException extends \Exception
{
/**
* NotEnoughSpaceException constructor.
*/
public function __construct()
{
$this->message = 'Not enough available disk space to save the datastore.';
}
}