Skip to content

Commit 8c616e2

Browse files
committed
Improve temp dir creation
Use more pythonic way to create secure temp dir and avoid race condition
1 parent 6a59a7f commit 8c616e2

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

zypperoni

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,21 @@ import sys
2121
import time
2222
import logging
2323
import asyncio
24+
import tempfile
2425
import subprocess
2526
from uuid import uuid4
2627
from shlex import quote
2728
import xml.etree.ElementTree as ET
2829

2930
# Constants
30-
ZYPPERONI_VERSION = "0.3.0"
31-
ZYPPERONI_TMP_DIR = "/tmp/zypperoni"
31+
ZYPPERONI_VERSION = "0.3.1"
3232
ZYPPER_PID_FILE = "/run/zypp.pid"
3333
VALID_CMD = ["ref", "force-ref", "in", "in-download", "dup", "dup-download", "inr", "inr-download"]
3434
VALID_OPT = ["--debug", "--help", "--version", "--no-confirm", "--max-jobs"]
3535

36+
# Create secure temp dir
37+
ZYPPERONI_TMP_DIR = tempfile.mkdtemp(dir="/tmp", prefix="zypperoni_")
38+
3639
# Command help/usage info
3740
help_text = """
3841
Usage: zypperoni [options] command
@@ -443,15 +446,6 @@ if os.path.isfile(ZYPPER_PID_FILE):
443446
logging.error(msg)
444447
sys.exit(5)
445448

446-
# Create and secure temp dir
447-
if os.path.exists(ZYPPERONI_TMP_DIR):
448-
os.rename(ZYPPERONI_TMP_DIR, f"{ZYPPERONI_TMP_DIR}.{int(time.time())}.broken")
449-
commands = f"""
450-
mkdir -p {ZYPPERONI_TMP_DIR};
451-
chmod 700 {ZYPPERONI_TMP_DIR};
452-
"""
453-
shell_exec(commands)
454-
455449
# Handle commands: ref, force-ref
456450
if COMMAND in ["ref", "force-ref"]:
457451
# get all enabled repos

0 commit comments

Comments
 (0)