Skip to content

Commit

Permalink
Don't store received backups in a TempDir
Browse files Browse the repository at this point in the history
  • Loading branch information
emontnemery committed Dec 4, 2024
1 parent 6e1fd59 commit a9f238f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
18 changes: 6 additions & 12 deletions homeassistant/components/backup/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from pathlib import Path
import shutil
import tarfile
from tempfile import TemporaryDirectory
import time
from typing import TYPE_CHECKING, Any, Protocol

Expand Down Expand Up @@ -382,24 +381,19 @@ async def async_receive_backup(
contents: aiohttp.BodyPartReader,
) -> None:
"""Receive and store a backup file from upload."""
temp_dir_handler = await self.hass.async_add_executor_job(TemporaryDirectory)
target_temp_file = Path(
temp_dir_handler.name, contents.filename or "backup.tar"
)
target_temp_file = Path(self.temp_backup_dir, contents.filename or "backup.tar")

await receive_file(self.hass, contents, target_temp_file)

def _copy_and_cleanup(
local_file_paths: list[Path], backup: AgentBackup
) -> Path:
if local_file_paths:
tar_file_path = local_file_paths[0]
else:
tar_file_path = self.temp_backup_dir / f"{backup.backup_id}.tar"
for local_path in local_file_paths:
shutil.copy(target_temp_file, local_path)
temp_dir_handler.cleanup()
return tar_file_path
for local_path in local_file_paths:
shutil.copy(target_temp_file, local_path)
return local_file_paths[0]

return target_temp_file

try:
backup = await self.hass.async_add_executor_job(
Expand Down
1 change: 1 addition & 0 deletions tests/components/hassio/test_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ async def test_agent_upload(

supervisor_client.backups.reload.assert_not_called()
with (
patch("pathlib.Path.open"),
patch(
"homeassistant.components.backup.manager.BackupManager.async_get_backup",
) as fetch_backup,
Expand Down

0 comments on commit a9f238f

Please sign in to comment.