Fix: Use atomic writes for all save processes

That includes uploaded files, profiles, caching files, settings and user
directories.
This commit is contained in:
Gina Häußge 2015-10-05 18:07:43 +02:00
parent abf073340f
commit f83d5aa89f
5 changed files with 11 additions and 5 deletions

View file

@ -7,6 +7,8 @@ __copyright__ = "Copyright (C) 2015 The OctoPrint Project - Released under terms
import io
from octoprint.util import atomic_write
class AbstractFileWrapper(object):
"""
Wrapper for file representations to save to storages.
@ -85,7 +87,7 @@ class StreamWrapper(AbstractFileWrapper):
"""
import shutil
with open(path, "wb") as dest:
with atomic_write(path, "wb") as dest:
with self.stream() as source:
shutil.copyfileobj(source, dest)

View file

@ -393,7 +393,7 @@ class CuraPlugin(octoprint.plugin.SlicerPlugin,
def _save_profile(self, path, profile, allow_overwrite=True):
import yaml
with open(path, "wb") as f:
with octoprint.util.atomic_write(path, "wb") as f:
yaml.safe_dump(profile, f, default_flow_style=False, indent=" ", allow_unicode=True)
def _convert_to_engine(self, profile_path, printer_profile, posX, posY):

View file

@ -536,7 +536,7 @@ class PluginManagerPlugin(octoprint.plugin.SimpleApiPlugin,
try:
import json
with open(self._repository_cache_path, "w+b") as f:
with octoprint.util.atomic_write(self._repository_cache_path, "wb") as f:
json.dump(repo_data, f)
except Exception as e:
self._logger.exception("Error while saving repository data to {}: {}".format(self._repository_cache_path, str(e)))

View file

@ -30,6 +30,8 @@ import logging
import re
import uuid
from octoprint.util import atomic_write
_APPNAME = "OctoPrint"
_instance = None
@ -1057,7 +1059,7 @@ class Settings(object):
path, _ = os.path.split(filename)
if not os.path.exists(path):
os.makedirs(path)
with open(filename, "w+") as f:
with atomic_write(filename, "wb") as f:
f.write(script)
def _default_basedir(applicationName):

View file

@ -17,6 +17,8 @@ import logging
from octoprint.settings import settings
from octoprint.util import atomic_write
class UserManager(object):
valid_roles = ["user", "admin"]
@ -217,7 +219,7 @@ class FilebasedUserManager(UserManager):
"settings": user._settings
}
with open(self._userfile, "wb") as f:
with atomic_write(self._userfile, "wb") as f:
yaml.safe_dump(data, f, default_flow_style=False, indent=" ", allow_unicode=True)
self._dirty = False
self._load()