Skip to content

Commit b69032e

Browse files
author
Mirko Brombin
committed
Using checksum to check files corruption
1 parent 4f5cd84 commit b69032e

File tree

1 file changed

+46
-21
lines changed

1 file changed

+46
-21
lines changed

src/runner.py

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# You should have received a copy of the GNU General Public License
1616
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717

18-
import os, logging, subprocess, urllib.request, json, tarfile, time, shutil, re
18+
import os, logging, subprocess, urllib.request, json, tarfile, time, shutil, re, hashlib
1919

2020
from glob import glob
2121
from threading import Thread
@@ -282,7 +282,7 @@ def extract_component(self, component, archive):
282282
'''
283283
Download a specific component release
284284
'''
285-
def download_component(self, component, tag, file, rename=False):
285+
def download_component(self, component, tag, file, rename=False, checksum=False):
286286
if component == "runner": repository = self.repository
287287
if component == "runner:proton": repository = self.proton_repository
288288
if component == "dxvk": repository = self.dxvk_repository
@@ -299,18 +299,39 @@ def download_component(self, component, tag, file, rename=False):
299299
file = rename if rename else file
300300
if os.path.isfile("%s/%s" % (self.temp_path, file)):
301301
logging.info("File `%s` already exists in temp, skipping." % file)
302-
return True
303-
304-
urllib.request.urlretrieve(download_url, "%s/%s" % (self.temp_path, file))
302+
else:
303+
urllib.request.urlretrieve(download_url, "%s/%s" % (self.temp_path, file))
305304

306305
'''
307306
The `rename` parameter mean that downloaded file should be
308307
renamed to another name
309308
'''
310309
if rename:
311310
logging.info("Renaming `%s` to `%s`." % (file, rename))
312-
os.rename("%s/%s" % (self.temp_path, file),
313-
"%s/%s" % (self.temp_path, rename))
311+
file_path = "%s/%s" % (self.temp_path, rename)
312+
os.rename("%s/%s" % (self.temp_path, file), file_path)
313+
else:
314+
file_path = "%s/%s" % (self.temp_path, file)
315+
316+
'''
317+
Compare checksums to check file corruptions
318+
'''
319+
if checksum:
320+
local_checksum = hashlib.md5()
321+
with open(file_path, "rb") as f:
322+
for chunk in iter(lambda: f.read(4096), b""):
323+
local_checksum.update(chunk)
324+
if local_checksum.hexdigest() != checksum:
325+
logging.info("Downloaded file `%s` looks corrupted." % file)
326+
logging.info("Source checksum: `%s` downloaded: `%s`" % (
327+
checksum, local_checksum.hexdigest()))
328+
self.window.send_notification(
329+
"Bottles",
330+
"Downloaded file `%s` looks corrupted. Try again." % file,
331+
"dialog-error-symbolic")
332+
os.remove(file_path)
333+
return False
334+
return True
314335

315336
'''
316337
Localy install a new component (runner, dxvk, ..) async
@@ -457,27 +478,31 @@ def async_install_dependency(self, args):
457478
'''
458479
if step[0] == "install_exe":
459480
step_data = step[1]
460-
self.download_component("dependency",
481+
download = self.download_component("dependency",
461482
step_data.get("url"),
462483
step_data.get("file_name"),
463-
step_data.get("rename"))
464-
if step_data.get("rename"):
465-
file = step_data.get("rename")
484+
step_data.get("rename"),
485+
checksum=step_data.get("file_checksum"))
486+
if download:
487+
if step_data.get("rename"):
488+
file = step_data.get("rename")
489+
else:
490+
file = step_data.get("file_name")
491+
self.run_command(configuration, "%s/%s /T C:\\windows\\Temp" % (
492+
self.temp_path, file))
466493
else:
467-
file = step_data.get("file_name")
468-
self.run_command(configuration, "%s/%s /T C:\\windows\\Temp" % (
469-
self.temp_path, file))
494+
widget.btn_install.set_sensitive(True)
495+
return False
470496

471497
'''
472498
Add dependency to the bottle configuration
473499
'''
474-
if configuration.get("Installed_Dependencies"):
475-
dependencies = configuration["Installed_Dependencies"]+[dependency[0]]
476-
else:
477-
dependencies = [dependency[0]]
478-
self.update_configuration(configuration,
479-
"Installed_Dependencies",
480-
dependencies)
500+
if dependency[0] not in configuration.get("Installed_Dependencies"):
501+
if configuration.get("Installed_Dependencies"):
502+
dependencies = configuration["Installed_Dependencies"]+[dependency[0]]
503+
else:
504+
dependencies = [dependency[0]]
505+
self.update_configuration(configuration,"Installed_Dependencies", dependencies)
481506

482507
'''
483508
Remove the entry from the download manager

0 commit comments

Comments
 (0)