mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-07-08 17:56:33 +00:00
Use find to delete files recursively (#4732)
* Use find to delete files recursively Instead of using rm -rf use find to delete files recursively. This has the added benefit that we do not need to rely on shell expansion. In particular, shell expansion caused the --one-file-system flag to not work as intended: The idea was that the content of a (left-over) bind mounted directory would not get deleted. However, since shell expansion passed the directory to rm, rm happily deleted also files in that bind mounted directory. * Pass arguments correctly * Fix argument order and stderr output * Improve error handling Log with exception level if there is an OS level error. Decode the stderr output correctly. * Remove unnecessary newline
This commit is contained in:
parent
cb03d039f4
commit
3d5bd2adef
@ -15,7 +15,7 @@ WORKDIR /usr/src
|
|||||||
RUN \
|
RUN \
|
||||||
set -x \
|
set -x \
|
||||||
&& apk add --no-cache \
|
&& apk add --no-cache \
|
||||||
coreutils \
|
findutils \
|
||||||
eudev \
|
eudev \
|
||||||
eudev-libs \
|
eudev-libs \
|
||||||
git \
|
git \
|
||||||
|
@ -105,30 +105,36 @@ async def remove_folder(
|
|||||||
if any(item.match(exclude) for exclude in excludes):
|
if any(item.match(exclude) for exclude in excludes):
|
||||||
moved_files.append(item.rename(temp_path / item.name))
|
moved_files.append(item.rename(temp_path / item.name))
|
||||||
|
|
||||||
del_folder = f"{folder}" + "/{,.[!.],..?}*" if content_only else f"{folder}"
|
find_args = []
|
||||||
|
if content_only:
|
||||||
|
find_args.extend(["-mindepth", "1"])
|
||||||
try:
|
try:
|
||||||
proc = await asyncio.create_subprocess_exec(
|
proc = await asyncio.create_subprocess_exec(
|
||||||
"bash",
|
"/usr/bin/find",
|
||||||
"-c",
|
folder,
|
||||||
f"rm -rf --one-file-system {del_folder}",
|
"-xdev",
|
||||||
|
*find_args,
|
||||||
|
"-delete",
|
||||||
stdout=asyncio.subprocess.DEVNULL,
|
stdout=asyncio.subprocess.DEVNULL,
|
||||||
|
stderr=asyncio.subprocess.PIPE,
|
||||||
env=clean_env(),
|
env=clean_env(),
|
||||||
)
|
)
|
||||||
|
|
||||||
_, error_msg = await proc.communicate()
|
_, error_msg = await proc.communicate()
|
||||||
except OSError as err:
|
except OSError as err:
|
||||||
error_msg = str(err)
|
_LOGGER.exception("Can't remove folder %s: %s", folder, err)
|
||||||
else:
|
else:
|
||||||
if proc.returncode == 0:
|
if proc.returncode == 0:
|
||||||
return
|
return
|
||||||
|
_LOGGER.error(
|
||||||
|
"Can't remove folder %s: %s", folder, error_msg.decode("utf-8").strip()
|
||||||
|
)
|
||||||
finally:
|
finally:
|
||||||
if excludes:
|
if excludes:
|
||||||
for item in moved_files:
|
for item in moved_files:
|
||||||
item.rename(folder / item.name)
|
item.rename(folder / item.name)
|
||||||
temp.cleanup()
|
temp.cleanup()
|
||||||
|
|
||||||
_LOGGER.error("Can't remove folder %s: %s", folder, error_msg)
|
|
||||||
|
|
||||||
|
|
||||||
def clean_env() -> dict[str, str]:
|
def clean_env() -> dict[str, str]:
|
||||||
"""Return a clean env from system."""
|
"""Return a clean env from system."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user