From 78b018989e84cf7d2198ae5ea95f5fcd4eee19bf Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Thu, 12 Sep 2019 14:36:45 -0700 Subject: [PATCH] export: Refuse to overwrite an existing directory or tarball. Previously, incorrectly passing an existing directory to the `manage.py export --output` option would remove its contents without warning. Abort instead. Signed-off-by: Anders Kaseorg --- zerver/management/commands/export.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/zerver/management/commands/export.py b/zerver/management/commands/export.py index b41da4e2d7..e5ae775faf 100644 --- a/zerver/management/commands/export.py +++ b/zerver/management/commands/export.py @@ -1,6 +1,5 @@ import os import tempfile -import shutil from argparse import ArgumentParser from typing import Any @@ -118,9 +117,21 @@ class Command(ZulipBaseCommand): output_dir = tempfile.mkdtemp(prefix="zulip-export-") else: output_dir = os.path.realpath(os.path.expanduser(output_dir)) - if os.path.exists(output_dir): - shutil.rmtree(output_dir) - os.makedirs(output_dir) + if os.path.exists(output_dir): + if os.listdir(output_dir): + raise CommandError( + "Refusing to overwrite nonempty directory: %s. Aborting..." + % (output_dir,) + ) + else: + os.makedirs(output_dir) + + tarball_path = output_dir.rstrip("/") + ".tar.gz" + try: + os.close(os.open(tarball_path, os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o666)) + except FileExistsError: + raise CommandError("Refusing to overwrite existing tarball: %s. Aborting..." % (tarball_path,)) + print("\033[94mExporting realm\033[0m: %s" % (realm.string_id,)) num_threads = int(options['threads'])