Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

export_single_user: Refuse to overwrite a nonempty directory.

Anders Kaseorg0daf32310ecd
diff --git a/zerver/management/commands/export_single_user.py b/zerver/management/commands/export_single_user.py
index 89fa29b0220..87851a08037 100644
--- a/zerver/management/commands/export_single_user.py
+++ b/zerver/management/commands/export_single_user.py
@@ -1,10 +1,11 @@
import os
-import shutil
import subprocess
import tempfile
from argparse import ArgumentParser
from typing import Any
+from django.core.management.base import CommandError
+
from zerver.lib.export import do_export_user
from zerver.lib.management import ZulipBaseCommand
@@ -32,9 +33,14 @@ class Command(ZulipBaseCommand):
output_dir = options["output_dir"]
if output_dir is None:
output_dir = tempfile.mkdtemp(prefix="zulip-export-")
- if os.path.exists(output_dir):
- shutil.rmtree(output_dir)
- os.makedirs(output_dir)
+ else:
+ if os.path.exists(output_dir) and os.listdir(output_dir):
+ raise CommandError(
+ f"Refusing to overwrite nonempty directory: {output_dir}. Aborting...",
+ )
+ else:
+ os.makedirs(output_dir)
+
print(f"Exporting user {user_profile.delivery_email}")
do_export_user(user_profile, output_dir)
print(f"Finished exporting to {output_dir}; tarring")