From 869b75ad274091503b146ad4fe55e0b0526143e5 Mon Sep 17 00:00:00 2001 From: sezanzeb Date: Mon, 15 Feb 2021 18:27:25 +0100 Subject: [PATCH] safer chown --- keymapper/paths.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/keymapper/paths.py b/keymapper/paths.py index a45e4f73..ed47e15e 100644 --- a/keymapper/paths.py +++ b/keymapper/paths.py @@ -66,6 +66,14 @@ USER = get_user() CONFIG_PATH = os.path.join('/home', USER, '.config/key-mapper') +def chown(path): + try: + shutil.chown(path, user=USER, group=USER) + except LookupError: + # the users group was missing in one case for whatever reason + shutil.chown(path, user=USER) + + def touch(path, log=True): """Create an empty file and all its parent dirs, give it to the user.""" if path.endswith('/'): @@ -80,7 +88,7 @@ def touch(path, log=True): mkdir(os.path.dirname(path), log=False) os.mknod(path) - shutil.chown(path, USER, USER) + chown(path) def mkdir(path, log=True): @@ -97,7 +105,7 @@ def mkdir(path, log=True): mkdir(base, log=False) os.makedirs(path) - shutil.chown(path, USER, USER) + chown(path) def get_preset_path(device=None, preset=None):