Skip to content

Commit

Permalink
Merge pull request #7760 from drew2a/fix/7745
Browse files Browse the repository at this point in the history
Modify `check_free_space function` to verify available disk space
  • Loading branch information
drew2a committed Dec 8, 2023
2 parents 1484896 + b2bc372 commit da413e2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
14 changes: 10 additions & 4 deletions src/tribler/core/check_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import psutil

from tribler.core.utilities.path_util import Path
from tribler.core.utilities.utilities import show_system_popup

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -45,17 +46,22 @@ def check_environment():
check_read_write()


def check_free_space():
logger.info('Check free space')
def check_free_space(state_dir: Path):
""" Check if there is enough free space on the disk."""
logger.info(f'Checking free space for the folder: {state_dir}')
try:
free_space = psutil.disk_usage(".").free / (1024 * 1024.0)
if free_space < 100:
usage = psutil.disk_usage(str(state_dir.absolute()))
usage_mb = usage.free / (1024 * 1024)
if usage_mb < 100:
error_and_exit("Insufficient disk space",
"You have less than 100MB of usable disk space. " +
"Please free up some space and run Tribler again.")
logger.info(f'There is enough free space: {usage_mb:.0f} MB')
except ImportError as ie:
logger.error(ie)
error_and_exit("Import Error", f"Import error: {ie}")
except OSError as e:
logger.exception(e)


def set_process_priority(pid=None, priority_order=1):
Expand Down
31 changes: 30 additions & 1 deletion src/tribler/core/tests/test_check_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import psutil
import pytest

from tribler.core.check_os import enable_fault_handler, error_and_exit, set_process_priority
from tribler.core.check_os import check_free_space, enable_fault_handler, error_and_exit, set_process_priority
from tribler.core.utilities.patch_import import patch_import
from tribler.core.utilities.path_util import Path

DISK_USAGE = 'tribler.core.check_os.psutil.disk_usage'


# pylint: disable=import-outside-toplevel
Expand Down Expand Up @@ -72,3 +75,29 @@ def test_set_process_exception():
with patch.object(psutil.Process, 'nice', new=Mock(side_effect=FileNotFoundError)):
with pytest.raises(FileNotFoundError):
set_process_priority()


def test_check_free_space_sufficient():
# Test to ensure the function works correctly when there's sufficient disk space.
with patch(DISK_USAGE) as mock_usage:
mock_usage.return_value = MagicMock(free=1024 * 1024 * 200) # Simulating 200MB of free space
check_free_space(Path("/path/to/dir"))


def test_check_free_space_insufficient():
# Test to ensure the function raises an exception when there's insufficient disk space.
with patch(DISK_USAGE) as mock_usage, pytest.raises(SystemExit):
mock_usage.return_value = MagicMock(free=1024 * 1024 * 50) # Simulating 50MB of free space
check_free_space(Path("/path/to/dir"))


def test_check_free_space_import_error():
# Test to check the behavior when there's an ImportError.
with patch(DISK_USAGE, side_effect=ImportError("mock import error")), pytest.raises(SystemExit):
check_free_space(Path("/path/to/dir"))


def test_check_free_space_os_error():
# Test to check the behavior when there's an OSError.
with patch(DISK_USAGE, side_effect=OSError("mock os error")):
check_free_space(Path("/path/to/dir"))
2 changes: 1 addition & 1 deletion src/tribler/gui/start_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def run_gui(api_port: Optional[int], api_key: Optional[str], root_state_dir: Pat
enable_fault_handler(root_state_dir)
# Exit if we cant read/write files, etc.
check_environment()
check_free_space()
check_free_space(root_state_dir)

try:
app_name = os.environ.get('TRIBLER_APP_NAME', 'triblerapp')
Expand Down

0 comments on commit da413e2

Please sign in to comment.