mirror of
https://git.deluge-torrent.org/deluge
synced 2025-08-05 16:08:40 +00:00
[Config] Prevent symlinked config files being overwritten
If a user keeps the deluge config file under source control and symlinks to the config files in deluge config dir then when deluge saves config files it will replace the symlink with actual file. Using realpath will resolve these symlinks and file will be updated in the correct location. Use a temporary file for new config new before moving it to the resolved location. Co-authored-by: Calum Lind <calumlind+deluge@gmail.com>
This commit is contained in:
parent
3645feb486
commit
c01679de1f
1 changed files with 11 additions and 4 deletions
|
@ -47,6 +47,7 @@ import os
|
||||||
import shutil
|
import shutil
|
||||||
from codecs import getwriter
|
from codecs import getwriter
|
||||||
from io import open
|
from io import open
|
||||||
|
from tempfile import NamedTemporaryFile
|
||||||
|
|
||||||
import six.moves.cPickle as pickle
|
import six.moves.cPickle as pickle
|
||||||
|
|
||||||
|
@ -475,8 +476,11 @@ class Config(object):
|
||||||
|
|
||||||
# Save the new config and make sure it's written to disk
|
# Save the new config and make sure it's written to disk
|
||||||
try:
|
try:
|
||||||
log.debug('Saving new config file %s', filename + '.new')
|
with NamedTemporaryFile(
|
||||||
with open(filename + '.new', 'wb') as _file:
|
prefix=os.path.basename(filename) + '.', delete=False
|
||||||
|
) as _file:
|
||||||
|
filename_tmp = _file.name
|
||||||
|
log.debug('Saving new config file %s', filename_tmp)
|
||||||
json.dump(self.__version, getwriter('utf8')(_file), **JSON_FORMAT)
|
json.dump(self.__version, getwriter('utf8')(_file), **JSON_FORMAT)
|
||||||
json.dump(self.__config, getwriter('utf8')(_file), **JSON_FORMAT)
|
json.dump(self.__config, getwriter('utf8')(_file), **JSON_FORMAT)
|
||||||
_file.flush()
|
_file.flush()
|
||||||
|
@ -485,6 +489,9 @@ class Config(object):
|
||||||
log.error('Error writing new config file: %s', ex)
|
log.error('Error writing new config file: %s', ex)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
# Resolve symlinked config files before backing up and saving.
|
||||||
|
filename = os.path.realpath(filename)
|
||||||
|
|
||||||
# Make a backup of the old config
|
# Make a backup of the old config
|
||||||
try:
|
try:
|
||||||
log.debug('Backing up old config file to %s.bak', filename)
|
log.debug('Backing up old config file to %s.bak', filename)
|
||||||
|
@ -495,8 +502,8 @@ class Config(object):
|
||||||
# The new config file has been written successfully, so let's move it over
|
# The new config file has been written successfully, so let's move it over
|
||||||
# the existing one.
|
# the existing one.
|
||||||
try:
|
try:
|
||||||
log.debug('Moving new config file %s to %s..', filename + '.new', filename)
|
log.debug('Moving new config file %s to %s', filename_tmp, filename)
|
||||||
shutil.move(filename + '.new', filename)
|
shutil.move(filename_tmp, filename)
|
||||||
except IOError as ex:
|
except IOError as ex:
|
||||||
log.error('Error moving new config file: %s', ex)
|
log.error('Error moving new config file: %s', ex)
|
||||||
return False
|
return False
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue