From e5dba5e66ae196c5658ba0b84b3ff5a0fb3dd396 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Wed, 18 Jan 2017 12:39:10 +0100 Subject: [PATCH] Fix bug with other directory of Qemu images Fix https://github.com/GNS3/gns3-gui/issues/1790 --- gns3server/compute/base_manager.py | 12 +++++++++--- tests/compute/test_manager.py | 13 ++++++++++++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/gns3server/compute/base_manager.py b/gns3server/compute/base_manager.py index bec1fce7..857c9c80 100644 --- a/gns3server/compute/base_manager.py +++ b/gns3server/compute/base_manager.py @@ -418,7 +418,7 @@ class BaseManager: # Not found we try the default directory s = os.path.split(orig_path) - path = force_unix_path(os.path.join(self.get_images_directory(), *s)) + path = force_unix_path(os.path.join(img_directory, *s)) if os.path.exists(path): return path raise ImageMissingError(orig_path) @@ -436,7 +436,7 @@ class BaseManager: if os.path.exists(path): return path raise ImageMissingError(orig_path) - raise NodeError("{} is not allowed on this remote server. Please use only a filename in {}.".format(path, self.get_images_directory())) + raise NodeError("{} is not allowed on this remote server. Please use only a filename in {}.".format(path, img_directory)) def _recursive_search_file_in_directory(self, directory, searched_file): """ @@ -468,9 +468,15 @@ class BaseManager: if not path: return "" path = force_unix_path(self.get_abs_image_path(path)) + + img_directory = self.get_images_directory() + for directory in images_directories(self._NODE_TYPE): if os.path.commonprefix([directory, path]) == directory: - return os.path.relpath(path, directory) + relpath = os.path.relpath(path, directory) + # We don't allow to recurse search from the top image directory just for image type directory (compatibility with old releases) + if os.sep not in relpath or directory == img_directory: + return relpath return path @asyncio.coroutine diff --git a/tests/compute/test_manager.py b/tests/compute/test_manager.py index 43cf48c2..72412ce9 100644 --- a/tests/compute/test_manager.py +++ b/tests/compute/test_manager.py @@ -186,6 +186,7 @@ def test_get_abs_image_recursive_ova(qemu, tmpdir, config): def test_get_relative_image_path(qemu, tmpdir, config): os.makedirs(str(tmpdir / "images1" / "QEMU")) + os.makedirs(str(tmpdir / "images1" / "VBOX")) path1 = force_unix_path(str(tmpdir / "images1" / "test1.bin")) open(path1, 'w+').close() @@ -196,9 +197,17 @@ def test_get_relative_image_path(qemu, tmpdir, config): path3 = force_unix_path(str(tmpdir / "images2" / "test3.bin")) open(path3, 'w+').close() + path4 = force_unix_path(str(tmpdir / "test4.bin")) + open(path4, 'w+').close() + + # The user use an image of another emulator we return the full path + path5 = force_unix_path(str(tmpdir / "images1" / "VBOX" / "test5.bin")) + open(path5, 'w+').close() + config.set_section_config("Server", { "images_path": str(tmpdir / "images1"), - "additional_images_path": str(tmpdir / "images2") + "additional_images_path": str(tmpdir / "images2"), + "local": True }) assert qemu.get_relative_image_path(path1) == "test1.bin" assert qemu.get_relative_image_path("test1.bin") == "test1.bin" @@ -206,6 +215,8 @@ def test_get_relative_image_path(qemu, tmpdir, config): assert qemu.get_relative_image_path("test2.bin") == "test2.bin" assert qemu.get_relative_image_path("../test1.bin") == "test1.bin" assert qemu.get_relative_image_path("test3.bin") == "test3.bin" + assert qemu.get_relative_image_path(path4) == path4 + assert qemu.get_relative_image_path(path5) == path5 def test_get_relative_image_path_ova(qemu, tmpdir, config):