Make sure we don't try to read when opening a file in binary more. Fixes #1301.

This commit is contained in:
grossmj 2018-03-07 16:39:04 +07:00
parent 419797dd92
commit c93d0d8d12
8 changed files with 36 additions and 26 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -16,6 +16,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
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)

View File

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