Make sure an image is not partially uploaded

Fix #505
This commit is contained in:
Julien Duponchelle 2016-05-02 17:25:46 +02:00
parent d952718f30
commit e9fb7f4981
No known key found for this signature in database
GPG Key ID: CE8B29639E07F5E8
4 changed files with 9 additions and 14 deletions

View File

@ -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))

View File

@ -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)

View File

@ -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

View File

@ -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)