diff --git a/gns3server/modules/base_manager.py b/gns3server/modules/base_manager.py index b8e2512a..110f2c12 100644 --- a/gns3server/modules/base_manager.py +++ b/gns3server/modules/base_manager.py @@ -486,14 +486,17 @@ class BaseManager: log.info("Writting image file %s", path) try: remove_checksum(path) + # We store the file under his final name only when the upload is finished + tmp_path = path + ".tmp" os.makedirs(os.path.dirname(path), exist_ok=True) - with open(path, 'wb+') as f: + with open(tmp_path, 'wb+') as f: while True: packet = yield from stream.read(512) if not packet: break f.write(packet) - os.chmod(path, stat.S_IWRITE | stat.S_IREAD | stat.S_IEXEC) + os.chmod(tmp_path, stat.S_IWRITE | stat.S_IREAD | stat.S_IEXEC) + shutil.move(tmp_path, path) md5sum(path) except OSError as e: raise aiohttp.web.HTTPConflict(text="Could not write image: {} because {}".format(filename, e)) diff --git a/tests/handlers/api/test_dynamips.py b/tests/handlers/api/test_dynamips.py index ba652b5f..fa201970 100644 --- a/tests/handlers/api/test_dynamips.py +++ b/tests/handlers/api/test_dynamips.py @@ -173,9 +173,9 @@ def test_upload_vm(server, tmpdir): def test_upload_vm_permission_denied(server, tmpdir): - with open(str(tmpdir / "test2"), "w+") as f: + with open(str(tmpdir / "test2.tmp"), "w+") as f: f.write("") - os.chmod(str(tmpdir / "test2"), 0) + os.chmod(str(tmpdir / "test2.tmp"), 0) with patch("gns3server.modules.Dynamips.get_images_directory", return_value=str(tmpdir),): response = server.post("/dynamips/vms/test2", body="TEST", raw=True) diff --git a/tests/handlers/api/test_iou.py b/tests/handlers/api/test_iou.py index 60c39d88..2dbb4011 100644 --- a/tests/handlers/api/test_iou.py +++ b/tests/handlers/api/test_iou.py @@ -349,11 +349,3 @@ def test_upload_vm(server, tmpdir): assert checksum == "033bd94b1168d7e4f0d644c3c95e35bf" -def test_upload_vm_permission_denied(server, tmpdir): - with open(str(tmpdir / "test2"), "w+") as f: - f.write("") - os.chmod(str(tmpdir / "test2"), 0) - - with patch("gns3server.modules.IOU.get_images_directory", return_value=str(tmpdir),): - response = server.post("/iou/vms/test2", body="TEST", raw=True) - assert response.status == 409 diff --git a/tests/handlers/api/test_qemu.py b/tests/handlers/api/test_qemu.py index 07073e60..49cbae62 100644 --- a/tests/handlers/api/test_qemu.py +++ b/tests/handlers/api/test_qemu.py @@ -263,9 +263,9 @@ def test_upload_vm_forbiden_location(server, tmpdir): def test_upload_vm_permission_denied(server, tmpdir): - with open(str(tmpdir / "test2"), "w+") as f: + with open(str(tmpdir / "test2.tmp"), "w+") as f: f.write("") - os.chmod(str(tmpdir / "test2"), 0) + os.chmod(str(tmpdir / "test2.tmp"), 0) with patch("gns3server.modules.Qemu.get_images_directory", return_value=str(tmpdir),): response = server.post("/qemu/vms/test2", body="TEST", raw=True)