mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-07 03:38:53 -05:00
[V3] Set r/w access before deleting files (#1412)
* [V3] set access before deleting * [V3] move+rename do_delete and use in repo removal in downloader
This commit is contained in:
parent
2e9a0de4a1
commit
eb3b6346bb
@ -13,6 +13,7 @@ from discord.ext import commands
|
|||||||
|
|
||||||
from redbot.core import Config
|
from redbot.core import Config
|
||||||
from redbot.core import data_manager
|
from redbot.core import data_manager
|
||||||
|
from redbot.core.utils import safe_delete
|
||||||
from .errors import *
|
from .errors import *
|
||||||
from .installable import Installable, InstallableType
|
from .installable import Installable, InstallableType
|
||||||
from .json_mixins import RepoJSONMixin
|
from .json_mixins import RepoJSONMixin
|
||||||
@ -614,7 +615,7 @@ class RepoManager:
|
|||||||
if repo is None:
|
if repo is None:
|
||||||
raise MissingGitRepo("There is no repo with the name {}".format(name))
|
raise MissingGitRepo("There is no repo with the name {}".format(name))
|
||||||
|
|
||||||
shutil.rmtree(str(repo.folder_path))
|
safe_delete(repo.folder_path)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
del self._repos[name]
|
del self._repos[name]
|
||||||
|
|||||||
@ -1,4 +1,8 @@
|
|||||||
__all__ = ['TYPE_CHECKING', 'NewType']
|
__all__ = ['TYPE_CHECKING', 'NewType', 'safe_delete']
|
||||||
|
|
||||||
|
from pathlib import Path
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
@ -10,3 +14,12 @@ try:
|
|||||||
except ImportError:
|
except ImportError:
|
||||||
def NewType(name, tp):
|
def NewType(name, tp):
|
||||||
return type(name, (tp,), {})
|
return type(name, (tp,), {})
|
||||||
|
|
||||||
|
|
||||||
|
def safe_delete(pth: Path):
|
||||||
|
if pth.exists():
|
||||||
|
for root, dirs, files in os.walk(str(pth)):
|
||||||
|
os.chmod(root, 0o755)
|
||||||
|
for d in dirs:
|
||||||
|
os.chmod(os.path.join(root, d), 0o755)
|
||||||
|
shutil.rmtree(str(pth), ignore_errors=True)
|
||||||
|
|||||||
@ -15,6 +15,7 @@ import appdirs
|
|||||||
from redbot.core.cli import confirm
|
from redbot.core.cli import confirm
|
||||||
from redbot.core.data_manager import basic_config_default
|
from redbot.core.data_manager import basic_config_default
|
||||||
from redbot.core.json_io import JsonIO
|
from redbot.core.json_io import JsonIO
|
||||||
|
from redbot.core.utils import safe_delete
|
||||||
|
|
||||||
config_dir = None
|
config_dir = None
|
||||||
appdir = appdirs.AppDirs("Red-DiscordBot")
|
appdir = appdirs.AppDirs("Red-DiscordBot")
|
||||||
@ -310,31 +311,26 @@ def remove_instance():
|
|||||||
selected, dt.utcnow().strftime("%Y-%m-%d %H-%M-%S")
|
selected, dt.utcnow().strftime("%Y-%m-%d %H-%M-%S")
|
||||||
)
|
)
|
||||||
pth = Path(instance_data["DATA_PATH"])
|
pth = Path(instance_data["DATA_PATH"])
|
||||||
home = pth.home()
|
if pth.exists():
|
||||||
backup_file = home / backup_filename
|
home = pth.home()
|
||||||
os.chdir(str(pth.parent)) # str is used here because 3.5 support
|
backup_file = home / backup_filename
|
||||||
with tarfile.open(str(backup_file), "w:gz") as tar:
|
os.chdir(str(pth.parent)) # str is used here because 3.5 support
|
||||||
tar.add(pth.stem) # add all files in that directory
|
with tarfile.open(str(backup_file), "w:gz") as tar:
|
||||||
print(
|
tar.add(pth.stem) # add all files in that directory
|
||||||
"A backup of {} has been made. It is at {}".format(
|
print(
|
||||||
selected, backup_file
|
"A backup of {} has been made. It is at {}".format(
|
||||||
|
selected, backup_file
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
print("Removing the instance...")
|
||||||
print("Removing the instance...")
|
safe_delete(pth)
|
||||||
try:
|
|
||||||
shutil.rmtree(str(pth))
|
|
||||||
except FileNotFoundError:
|
|
||||||
pass # data dir was removed manually
|
|
||||||
save_config(selected, {}, remove=True)
|
save_config(selected, {}, remove=True)
|
||||||
print("The instance has been removed")
|
print("The instance has been removed")
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
pth = Path(instance_data["DATA_PATH"])
|
pth = Path(instance_data["DATA_PATH"])
|
||||||
print("Removing the instance...")
|
print("Removing the instance...")
|
||||||
try:
|
safe_delete(pth)
|
||||||
shutil.rmtree(str(pth))
|
|
||||||
except FileNotFoundError:
|
|
||||||
pass # data dir was removed manually
|
|
||||||
save_config(selected, {}, remove=True)
|
save_config(selected, {}, remove=True)
|
||||||
print("The instance has been removed")
|
print("The instance has been removed")
|
||||||
return
|
return
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user