From 2d89051867c6e47b32cfa1052304ce8133565a2e Mon Sep 17 00:00:00 2001 From: Nils <> Date: Thu, 23 Apr 2020 21:50:54 +0200 Subject: [PATCH] Protect against corner case where rename happens into a subdir of the session --- engine/nsmservercontrol.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/engine/nsmservercontrol.py b/engine/nsmservercontrol.py index 07a981c..ed7a815 100644 --- a/engine/nsmservercontrol.py +++ b/engine/nsmservercontrol.py @@ -37,6 +37,7 @@ import subprocess import atexit import pathlib import json +from uuid import uuid4 from datetime import datetime from sys import exit as sysexit @@ -1362,8 +1363,11 @@ class NsmServerControl(object): logger.warning(f"Can't rename {nsmSessionName} to {newName}. {newName} already exists.") return False else: - logger.info(f"Renaming {nsmSessionName} to {newName}.") - oldPath.rename(newPath) + logger.info(f"Renaming {nsmSessionName} to {newName}.") + tmp = pathlib.Path(oldPath.name+str(uuid4())) #Can't move itself into a subdir in itself. move to temp first. We don't use tempdir because that could be on another partition. we already know we can write here. + oldPath.rename(tmp) + pathlib.Path(newPath).mkdir(parents=True, exist_ok=True) + tmp.rename(newPath) assert newPath.exists() def copySession(self, nsmSessionName:str, newName:str):