]> git.ipfire.org Git - thirdparty/wireguard-apple.git/commitdiff
Refactoring: Consolidate file deletion into a separate function
authorRoopesh Chander <roop@roopc.net>
Wed, 5 Dec 2018 09:53:55 +0000 (15:23 +0530)
committerRoopesh Chander <roop@roopc.net>
Fri, 7 Dec 2018 07:06:19 +0000 (12:36 +0530)
Signed-off-by: Roopesh Chander <roop@roopc.net>
WireGuard/Shared/FileManager+Extension.swift
WireGuard/WireGuard/UI/iOS/AppDelegate.swift
WireGuard/WireGuard/UI/iOS/SettingsTableViewController.swift

index 7b25ffbe187ca84f3845ad6879027e5721ef1e86..4d9bf4f777fd44d25b9c150dd4b77245b6e87dfa 100644 (file)
@@ -16,4 +16,14 @@ extension FileManager {
         }
         return sharedFolderURL.appendingPathComponent("lastActivatedTunnelLog.txt")
     }
+
+    static func deleteFile(at url: URL) -> Bool {
+        do {
+            try FileManager.default.removeItem(at: url)
+        } catch(let e) {
+            os_log("Failed to delete file '%{public}@': %{public}@", log: OSLog.default, type: .debug, url.absoluteString, e.localizedDescription)
+            return false
+        }
+        return true
+    }
 }
index 7ba77e4aeb6ad86dfbb6f9d40492345c3cd6ba8b..3b414dd1254228a74fedec2e3d3b5c1351ea9ec0 100644 (file)
@@ -27,14 +27,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
     }
 
     func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
-        defer {
-            do {
-                try FileManager.default.removeItem(at: url)
-            } catch {
-                os_log("Failed to remove item from Inbox: %{public}@", log: OSLog.default, type: .debug, url.absoluteString)
-            }
-        }
         mainVC?.tunnelsListVC?.importFromFile(url: url)
+        _ = FileManager.deleteFile(at: url)
         return true
     }
 
index 6f8410eda055b2dc9382faa69d0b3ea11d1a676d..7beb2e91ee98ec51f1825066f7c45138e77a369f 100644 (file)
@@ -69,12 +69,9 @@ class SettingsTableViewController: UITableViewController {
         guard let destinationDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else {
             return
         }
+
         let destinationURL = destinationDir.appendingPathComponent("wireguard-export.zip")
-        do {
-            try FileManager.default.removeItem(at: destinationURL)
-        } catch {
-            os_log("Failed to delete file: %{public}@ : %{public}@", log: OSLog.default, type: .error, destinationURL.absoluteString, error.localizedDescription)
-        }
+        _ = FileManager.deleteFile(at: destinationURL)
 
         let count = tunnelsManager.numberOfTunnels()
         let tunnelConfigurations = (0 ..< count).compactMap { tunnelsManager.tunnel(at: $0).tunnelConfiguration() }
@@ -102,10 +99,8 @@ class SettingsTableViewController: UITableViewController {
         let destinationURL = destinationDir.appendingPathComponent("WireGuard_iOS_log_\(timeStampString).txt")
 
         if (FileManager.default.fileExists(atPath: destinationURL.path)) {
-            do {
-                try FileManager.default.removeItem(at: destinationURL)
-            } catch {
-                os_log("Failed to delete file: %{public}@ : %{public}@", log: OSLog.default, type: .error, destinationURL.absoluteString, error.localizedDescription)
+            let isDeleted = FileManager.deleteFile(at: destinationURL)
+            if (!isDeleted) {
                 showErrorAlert(title: "No log available", message: "The pre-existing log could not be cleared")
                 return
             }
@@ -131,7 +126,7 @@ class SettingsTableViewController: UITableViewController {
         activityVC.popoverPresentationController?.sourceRect = sourceView.bounds
         activityVC.completionWithItemsHandler = { (_, _, _, _) in
             // Remove the exported log file after the activity has completed
-            try? FileManager.default.removeItem(at: destinationURL)
+            _ = FileManager.deleteFile(at: destinationURL)
         }
         self.present(activityVC, animated: true)
     }