Commit 679e5ae2 authored by Michael Tremer's avatar Michael Tremer

location-downloader: Do not change content of open database files

The database might be opened by another process. When modified,
it will return random results.

Fixes: #12420
Signed-off-by: default avatarMichael Tremer <michael.tremer@ipfire.org>
parent 6254dca6
......@@ -24,6 +24,7 @@ import lzma
import os
import random
import shutil
import stat
import sys
import tempfile
import time
......@@ -116,7 +117,7 @@ class Downloader(object):
return res
def download(self, url, public_key, timestamp=None, **kwargs):
def download(self, url, public_key, timestamp=None, tmpdir=None, **kwargs):
headers = {}
if timestamp:
......@@ -124,7 +125,7 @@ class Downloader(object):
"%a, %d %b %Y %H:%M:%S GMT",
)
t = tempfile.NamedTemporaryFile(delete=False)
t = tempfile.NamedTemporaryFile(dir=tmpdir, delete=False)
with t:
# Try all mirrors
for mirror in self.mirrors:
......@@ -175,6 +176,9 @@ class Downloader(object):
t.truncate()
continue
# Make the file readable for everyone
os.chmod(t.name, stat.S_IRUSR|stat.S_IRGRP|stat.S_IROTH)
# Return temporary file
return t
......@@ -296,10 +300,13 @@ class CLI(object):
except FileNotFoundError as e:
db = None
# Download the database into the correct directory
tmpdir = os.path.dirname(ns.database)
# Try downloading a new database
try:
t = self.downloader.download("%s/%s" % (self.version, DATABASE_FILENAME),
public_key=ns.public_key, timestamp=timestamp)
public_key=ns.public_key, timestamp=timestamp, tmpdir=tmpdir)
# If no file could be downloaded, log a message
except FileNotFoundError as e:
......@@ -310,11 +317,8 @@ class CLI(object):
if not t:
return 3
# Write temporary file to destination
shutil.copyfile(t.name, ns.database)
# Remove temporary file
os.unlink(t.name)
# Move temporary file to destination
shutil.move(t.name, ns.database)
return 0
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment