From 0edbc24003ec73630b22d0ddbce916e87438b8e6 Mon Sep 17 00:00:00 2001 From: Cametendo Date: Tue, 21 Apr 2026 20:54:54 +0200 Subject: [PATCH] Added file and device verification --- FilePathAdd.java | 27 ++++++++++++++++++---- Flasher.java | 2 +- Main.java | 2 +- StorageDeviceLister.java | 49 +++++++++++++++++++++++++++++----------- 4 files changed, 61 insertions(+), 19 deletions(-) diff --git a/FilePathAdd.java b/FilePathAdd.java index 9176d3b..5d1f119 100644 --- a/FilePathAdd.java +++ b/FilePathAdd.java @@ -1,3 +1,6 @@ +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Scanner; public class FilePathAdd { @@ -5,9 +8,25 @@ public class FilePathAdd { public static String ImagePath = ""; protected static String filePath(Scanner UserInput) { - System.out.println("Please enter the FULL Path of your ISO / Image. ()"); - ImagePath = UserInput.nextLine(); - System.out.println("Using File: " + ImagePath); - return ImagePath; + fileQuestion(); + while (true) { + ImagePath = UserInput.nextLine(); + if (ImagePath.isBlank()) { + System.out.println("Oops.. You didn't specify a file, did you missclick?"); + continue; + } + Path path = Path.of(ImagePath); + try { + Files.readAttributes(path, "basic:size"); + System.out.println("Using File: " + ImagePath); + return ImagePath; + } catch (IOException e) { + System.out.println("Failed to access file, invalid path or no access to file! Please try again."); + } + } + } + + protected static void fileQuestion() { + System.out.println("Please enter the FULL Path of your ISO / Image. (No tab-complete)"); } } diff --git a/Flasher.java b/Flasher.java index 3cca041..e55d716 100644 --- a/Flasher.java +++ b/Flasher.java @@ -5,7 +5,7 @@ public class Flasher { String input = ""; - System.out.println("The programm wil use the following configuration, do you want to flash with this? (y/n)"); + System.out.println("The programm wil use the following configuration, do you want to flash with this? (Y/n)"); System.out.println(" - To be flashed device: " + StorageDeviceLister.fullPath); System.out.println(" - To be used path: " + FilePathAdd.ImagePath); System.out.println(" - To bed used blocksize: " + BlockSize.blockSizeString); diff --git a/Main.java b/Main.java index 58e37b8..4ecbaf8 100644 --- a/Main.java +++ b/Main.java @@ -13,7 +13,7 @@ public class Main { System.out.println("Canceling..."); System.exit(0); } - StorageDeviceLister.deviceList(UserInput); + StorageDeviceLister.deviceCheck(UserInput); FilePathAdd.filePath(UserInput); BlockSize.blockSize(UserInput); OflagHandler.handleOflag(UserInput); diff --git a/StorageDeviceLister.java b/StorageDeviceLister.java index 196a92f..39977cb 100644 --- a/StorageDeviceLister.java +++ b/StorageDeviceLister.java @@ -1,33 +1,56 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; +import java.nio.file.Path; import java.util.Scanner; public class StorageDeviceLister { public static String device = ""; public static String fullPath = ""; - - protected static String deviceList(Scanner UserInput) { + protected static String deviceCheck(Scanner UserInput) { + // 1. lsblk wird genau EINMAL aufgerufen + deviceList(); + + // 2. Die Abfrage-Schleife + while (true) { + device = UserInput.nextLine(); + if (device.isBlank()) { + System.out.println("Oops... Device name is empty. Did you missclick?"); + continue; + } + Path path = Path.of("/dev/" + device); + + try { + // Versuche, den echten Pfad zu finden + fullPath = path.toRealPath().toString(); + + // Wenn wir hier ankommen, war der Pfad gültig + System.out.println("Using device: " + fullPath); + return fullPath; + + } catch (IOException e) { + // Fehler-Output, danach springt die Schleife wieder nach oben + System.out.println("Failed to access device! Invalid path or no access. Please try again."); + } + } + } + + private static void deviceList() { try { ProcessBuilder pb = new ProcessBuilder("lsblk"); Process process = pb.start(); - - BufferedReader reader = new BufferedReader( - new InputStreamReader(process.getInputStream()) - ); - + BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } - } catch (IOException e) { + process.waitFor(); + System.out.println("Please enter the name of your device (without /dev/):"); + + } catch (IOException | InterruptedException e) { e.printStackTrace(); } - String device = UserInput.nextLine(); - System.out.println("Using device: " + "/dev/" + device); - fullPath = "/dev/" + device; - return fullPath; } -} +} \ No newline at end of file