Skip to content

Commit

Permalink
Don't store received backups in a TempDir (#132272)
Browse files Browse the repository at this point in the history
* Don't store received backups in a TempDir

* Fix tests

* Make sure backup directory exists

* Address review comments

* Fix tests
  • Loading branch information
emontnemery authored Dec 5, 2024
1 parent 2f35e2f commit 4fdb965
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
34 changes: 14 additions & 20 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 @@ -389,25 +388,11 @@ 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 self.hass.async_add_executor_job(make_backup_dir, self.temp_backup_dir)

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

try:
backup = await self.hass.async_add_executor_job(
read_backup, target_temp_file
Expand All @@ -421,9 +406,18 @@ def _copy_and_cleanup(
for agent_id in agent_ids
if agent_id in self.local_backup_agents
]
tar_file_path = await self.hass.async_add_executor_job(
_copy_and_cleanup, local_file_paths, backup
)
if local_file_paths:

def _copy_to_local_agents(local_file_paths: list[Path]) -> Path:
for local_path in local_file_paths:
shutil.copy(target_temp_file, local_path)
return local_file_paths[0]

tar_file_path = await self.hass.async_add_executor_job(
_copy_to_local_agents, local_file_paths
)
else:
tar_file_path = target_temp_file
await self._async_upload_backup(
backup=backup, agent_ids=agent_ids, path=tar_file_path
)
Expand Down
1 change: 1 addition & 0 deletions tests/components/cloud/test_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,7 @@ async def test_agents_upload_not_protected(
size=0.0,
)
with (
patch("pathlib.Path.open"),
patch(
"homeassistant.components.backup.manager.read_backup",
return_value=test_backup,
Expand Down
2 changes: 2 additions & 0 deletions tests/components/hassio/test_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ async def test_agent_upload(

supervisor_client.backups.reload.assert_not_called()
with (
patch("pathlib.Path.mkdir"),
patch("pathlib.Path.open"),
patch(
"homeassistant.components.backup.manager.BackupManager.async_get_backup",
) as fetch_backup,
Expand Down
1 change: 1 addition & 0 deletions tests/components/kitchen_sink/test_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ async def test_agents_upload(
)

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

0 comments on commit 4fdb965

Please sign in to comment.