From 3db48864bcdb3ab1aa085a77877effa40e782b26 Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Thu, 15 Sep 2016 06:23:10 +0200 Subject: [PATCH] Trying to rename downloaded files more than once. This could help against aggressive anti virus tools. --- .../fabianonline/telegram_backup/Config.java | 3 +++ .../telegram_backup/DownloadManager.java | 18 +++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/fabianonline/telegram_backup/Config.java b/src/main/java/de/fabianonline/telegram_backup/Config.java index 0339e02..c621ccf 100644 --- a/src/main/java/de/fabianonline/telegram_backup/Config.java +++ b/src/main/java/de/fabianonline/telegram_backup/Config.java @@ -40,6 +40,9 @@ public class Config { public static int DELAY_AFTER_GET_MESSAGES = 100; public static int DELAY_AFTER_GET_FILE = 100; + public static int RENAMING_MAX_TRIES = 5; + public static int RENAMING_DELAY = 1000; + public static final String SECRET_GMAPS = "AIzaSyBEtUDhCQKEH6i2Mn1GAiQ9M_tLN0vxHIs"; static { diff --git a/src/main/java/de/fabianonline/telegram_backup/DownloadManager.java b/src/main/java/de/fabianonline/telegram_backup/DownloadManager.java index 59a5827..6a2ad68 100644 --- a/src/main/java/de/fabianonline/telegram_backup/DownloadManager.java +++ b/src/main/java/de/fabianonline/telegram_backup/DownloadManager.java @@ -311,7 +311,23 @@ public class DownloadManager { System.exit(1); } logger.trace("Renaming {} to {}", temp_filename, target); - Files.move(new File(temp_filename).toPath(), new File(target).toPath(), StandardCopyOption.REPLACE_EXISTING); + int rename_tries = 0; + IOException last_exception = null; + while (rename_tries <= Config.RENAMING_MAX_TRIES) { + rename_tries++; + try { + Files.move(new File(temp_filename).toPath(), new File(target).toPath(), StandardCopyOption.REPLACE_EXISTING); + last_exception = null; + break; + } catch (IOException e) { + logger.debug("Exception during move. rename_tries: {}. Exception: {}", rename_tries, e); + last_exception = e; + try { Thread.sleep(Config.RENAMING_DELAY); } catch (InterruptedException e2) {} + } + } + if (last_exception != null) { + throw last_exception; + } last_download_succeeded = true; return true; } catch (java.io.IOException ex) {