From 5ba6bbe0c26b3726e7112f1103e63aaa9961838b Mon Sep 17 00:00:00 2001 From: Daniel Mach Date: Mon, 6 Dec 2021 14:23:51 +0100 Subject: [PATCH] conf: Preserve oscrc symlink When a user creates a symlink pointing from ~/.config/osc/oscrc to a different location, don't overwrite the symlink but follow it when writing configuration on disk. --- osc/conf.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/osc/conf.py b/osc/conf.py index fa12ef60..8d741625 100644 --- a/osc/conf.py +++ b/osc/conf.py @@ -695,8 +695,20 @@ def write_config(fname, cp): if os.path.exists(fname) and not os.path.isfile(fname): # only write to a regular file return + + # config file is behind a symlink + # resolve the symlink and continue writing the config as usual + if os.path.islink(fname): + fname = os.readlink(fname) + + # create directories to the config file (if they don't exist already) if not os.path.exists(os.path.dirname(fname)): - os.makedirs(os.path.dirname(fname), mode=0o700) + try: + os.makedirs(os.path.dirname(fname), mode=0o700) + except OSError as e: + if e.errno != errno.EEXIST: + raise + with open(fname + '.new', 'w') as f: cp.write(f, comments=True) try: