From f5b99a6e341f86aee1fe5463b49efed00ad63c76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Mon, 18 Jun 2018 14:24:04 +0200 Subject: [PATCH] Fix symlink test util That was resolving the path completely before checking whether the symlink is broken or not. --- src/octoprint/server/api/__init__.py | 19 +++++++++++-------- .../static/js/app/viewmodels/settings.js | 4 +++- src/octoprint/util/paths.py | 6 ++++-- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/octoprint/server/api/__init__.py b/src/octoprint/server/api/__init__.py index 7a6f12498..89945c661 100644 --- a/src/octoprint/server/api/__init__.py +++ b/src/octoprint/server/api/__init__.py @@ -268,9 +268,12 @@ def _test_path(data): import os from octoprint.util.paths import normalize - path = normalize(data["path"]) + path = normalize(data["path"], real=False) if not path: - return jsonify(path=path, exists=False, typeok=False, access=False, result=False) + return jsonify(path=path, exists=False, typeok=False, broken_symlink=False, access=False, result=False) + + unreal_path = path + path = os.path.realpath(path) check_type = None check_access = [] @@ -293,17 +296,17 @@ def _test_path(data): # check if path exists exists = os.path.exists(path) if not exists: - if os.path.islink(path): + if os.path.islink(unreal_path): # broken symlink, see #2644 - logging.getLogger(__name__).error("{} is a broken symlink") - return jsonify(path=path, exists=False, typeok=False, access=False, result=False) + logging.getLogger(__name__).error("{} is a broken symlink pointing at non existing {}".format(unreal_path, path)) + return jsonify(path=unreal_path, exists=False, typeok=False, broken_symlink=True, access=False, result=False) elif check_type == "dir" and allow_create_dir: try: os.makedirs(path) except: logging.getLogger(__name__).exception("Error while trying to create {}".format(path)) - return jsonify(path=path, exists=False, typeok=False, access=False, result=False) + return jsonify(path=path, exists=False, typeok=False, broken_symlink=False, access=False, result=False) else: exists = True @@ -332,9 +335,9 @@ def _test_path(data): os.remove(test_path) except: logging.getLogger(__name__).exception("Error while testing if {} is really writable".format(path)) - return jsonify(path=path, exists=exists, typeok=typeok, access=False, result=False) + return jsonify(path=path, exists=exists, typeok=typeok, broken_symlink=False, access=False, result=False) - return jsonify(path=path, exists=exists, typeok=typeok, access=access, result=exists and typeok and access) + return jsonify(path=path, exists=exists, typeok=typeok, broken_symlink=False, access=access, result=exists and typeok and access) def _test_url(data): diff --git a/src/octoprint/static/js/app/viewmodels/settings.js b/src/octoprint/static/js/app/viewmodels/settings.js index e209c7a27..9f45e6174 100644 --- a/src/octoprint/static/js/app/viewmodels/settings.js +++ b/src/octoprint/static/js/app/viewmodels/settings.js @@ -473,7 +473,9 @@ $(function() { OctoPrint.util.testPath(path, opts) .done(function(response) { if (!response.result) { - if (!response.exists) { + if (response.broken_symlink) { + self.testFolderConfigText[folder](gettext("The path is a broken symlink.")); + } else if (!response.exists) { self.testFolderConfigText[folder](gettext("The path does not exist and cannot be created.")); } else if (!response.typeok) { self.testFolderConfigText[folder](gettext("The path is not a folder.")); diff --git a/src/octoprint/util/paths.py b/src/octoprint/util/paths.py index 8efb127f5..017d67bc3 100644 --- a/src/octoprint/util/paths.py +++ b/src/octoprint/util/paths.py @@ -5,7 +5,7 @@ __author__ = "Gina Häußge " __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html' __copyright__ = "Copyright (C) 2015 The OctoPrint Project - Released under terms of the AGPLv3 License" -def normalize(path, expand_user=True, expand_vars=True, **kwargs): +def normalize(path, expand_user=True, expand_vars=True, real=True, **kwargs): import os if path is None: @@ -15,5 +15,7 @@ def normalize(path, expand_user=True, expand_vars=True, **kwargs): path = os.path.expanduser(path) if expand_vars: path = os.path.expandvars(path) - path = os.path.realpath(os.path.abspath(path)) + path = os.path.abspath(path) + if real: + path = os.path.realpath(path) return path