From c93d0d8d12fb47e234baeb09ec44def87749bea8 Mon Sep 17 00:00:00 2001 From: grossmj Date: Wed, 7 Mar 2018 16:39:04 +0700 Subject: [PATCH] Make sure we don't try to read when opening a file in binary more. Fixes #1301. --- gns3server/compute/base_manager.py | 2 +- gns3server/compute/dynamips/__init__.py | 2 +- gns3server/compute/iou/iou_vm.py | 4 ++-- gns3server/controller/drawing.py | 2 +- gns3server/controller/link.py | 23 +++++++++++-------- gns3server/controller/project.py | 11 +++++---- .../handlers/api/controller/symbol_handler.py | 16 ++++++++----- gns3server/run.py | 2 +- 8 files changed, 36 insertions(+), 26 deletions(-) diff --git a/gns3server/compute/base_manager.py b/gns3server/compute/base_manager.py index 4dd8ae36..f383a197 100644 --- a/gns3server/compute/base_manager.py +++ b/gns3server/compute/base_manager.py @@ -549,7 +549,7 @@ class BaseManager: # 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(tmp_path, 'wb+') as f: + with open(tmp_path, 'wb') as f: while True: packet = yield from stream.read(4096) if not packet: diff --git a/gns3server/compute/dynamips/__init__.py b/gns3server/compute/dynamips/__init__.py index c6fd1c05..0254d969 100644 --- a/gns3server/compute/dynamips/__init__.py +++ b/gns3server/compute/dynamips/__init__.py @@ -547,7 +547,7 @@ class Dynamips(BaseManager): content = content.replace('%h', vm.name) f.write(content.encode("utf-8")) except OSError as e: - raise DynamipsError("Could not create config file {}: {}".format(path, e)) + raise DynamipsError("Could not create config file '{}': {}".format(path, e)) return os.path.join("configs", os.path.basename(path)) diff --git a/gns3server/compute/iou/iou_vm.py b/gns3server/compute/iou/iou_vm.py index 68664501..c863d22e 100644 --- a/gns3server/compute/iou/iou_vm.py +++ b/gns3server/compute/iou/iou_vm.py @@ -348,14 +348,14 @@ class IOUVM(BaseNode): # reload path = os.path.join(os.path.expanduser("~/"), ".iourc") try: - with open(path, "wb+") as f: + with open(path, "wb") as f: f.write(value.encode("utf-8")) except OSError as e: raise IOUError("Could not write the iourc file {}: {}".format(path, e)) path = os.path.join(self.temporary_directory, "iourc") try: - with open(path, "wb+") as f: + with open(path, "wb") as f: f.write(value.encode("utf-8")) except OSError as e: raise IOUError("Could not write the iourc file {}: {}".format(path, e)) diff --git a/gns3server/controller/drawing.py b/gns3server/controller/drawing.py index 39a4d158..47c09d0e 100644 --- a/gns3server/controller/drawing.py +++ b/gns3server/controller/drawing.py @@ -118,7 +118,7 @@ class Drawing: file_path = os.path.join(self._project.pictures_directory, filename) if not os.path.exists(file_path): - with open(file_path, "wb+") as f: + with open(file_path, "wb") as f: f.write(data) value = filename diff --git a/gns3server/controller/link.py b/gns3server/controller/link.py index a5421150..edc01cec 100644 --- a/gns3server/controller/link.py +++ b/gns3server/controller/link.py @@ -315,16 +315,19 @@ class Link: self._project.controller.notification.emit("link.updated", self.__json__()) with stream_content as stream: - with open(self.capture_file_path, "wb+") as f: - while self._capturing: - # We read 1 bytes by 1 otherwise the remaining data is not read if the traffic stops - data = yield from stream.read(1) - if data: - f.write(data) - # Flush to disk otherwise the live is not really live - f.flush() - else: - break + try: + with open(self.capture_file_path, "wb") as f: + while self._capturing: + # We read 1 bytes by 1 otherwise the remaining data is not read if the traffic stops + data = yield from stream.read(1) + if data: + f.write(data) + # Flush to disk otherwise the live is not really live + f.flush() + else: + break + except OSError as e: + raise aiohttp.web.HTTPConflict(text="Could not write capture file '{}': {}".format(self.capture_file_path, e)) @asyncio.coroutine def stop_capture(self): diff --git a/gns3server/controller/project.py b/gns3server/controller/project.py index 7c8a11e0..46348083 100644 --- a/gns3server/controller/project.py +++ b/gns3server/controller/project.py @@ -627,9 +627,12 @@ class Project: with tempfile.TemporaryDirectory() as tmpdir: zipstream = yield from export_project(self, tmpdir, keep_compute_id=True, allow_all_nodes=True) - with open(snapshot.path, "wb+") as f: - for data in zipstream: - f.write(data) + try: + with open(snapshot.path, "wb") as f: + for data in zipstream: + f.write(data) + except OSError as e: + raise aiohttp.web.HTTPConflict(text="Could not write snapshot file '{}': {}".format(snapshot.path, e)) except OSError as e: raise aiohttp.web.HTTPInternalServerError(text="Could not create project directory: {}".format(e)) @@ -858,7 +861,7 @@ class Project: try: with tempfile.TemporaryDirectory() as tmpdir: zipstream = yield from export_project(self, tmpdir, keep_compute_id=True, allow_all_nodes=True) - with open(os.path.join(tmpdir, "project.gns3p"), "wb+") as f: + with open(os.path.join(tmpdir, "project.gns3p"), "wb") as f: for data in zipstream: f.write(data) with open(os.path.join(tmpdir, "project.gns3p"), "rb") as f: diff --git a/gns3server/handlers/api/controller/symbol_handler.py b/gns3server/handlers/api/controller/symbol_handler.py index 7122a2e3..b0c4600c 100644 --- a/gns3server/handlers/api/controller/symbol_handler.py +++ b/gns3server/handlers/api/controller/symbol_handler.py @@ -16,6 +16,7 @@ # along with this program. If not, see . import os +import aiohttp from gns3server.web.route import Route from gns3server.controller import Controller @@ -62,12 +63,15 @@ class SymbolHandler: def upload(request, response): controller = Controller.instance() path = os.path.join(controller.symbols.symbols_path(), os.path.basename(request.match_info["symbol_id"])) - with open(path, 'wb+') as f: - while True: - packet = yield from request.content.read(512) - if not packet: - break - f.write(packet) + try: + with open(path, 'wb') as f: + while True: + packet = yield from request.content.read(512) + if not packet: + break + f.write(packet) + except OSError as e: + raise aiohttp.web.HTTPConflict(text="Could not write symbol file '{}': {}".format(path, e)) # Reset the symbol list controller.symbols.list() response.set_status(204) diff --git a/gns3server/run.py b/gns3server/run.py index 23a74827..0d23b124 100644 --- a/gns3server/run.py +++ b/gns3server/run.py @@ -160,7 +160,7 @@ def pid_lock(path): with open(path) as f: try: pid = int(f.read()) - os.kill(pid, 0) # If the proces is not running kill return an error + os.kill(pid, 0) # kill returns an error if the process is not running except (OSError, SystemError, ValueError): pid = None except OSError as e: