From fbcdcb3e54b097fe08bc54a5187385027e5732f8 Mon Sep 17 00:00:00 2001 From: Ludy87 Date: Sat, 11 Jan 2025 21:13:37 +0100 Subject: [PATCH] Fix: `NoSuchFileException` if `configs\db\backup` is not present on first start --- .../security/database/DatabaseService.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/main/java/stirling/software/SPDF/config/security/database/DatabaseService.java b/src/main/java/stirling/software/SPDF/config/security/database/DatabaseService.java index f11acda3290..7d413947054 100644 --- a/src/main/java/stirling/software/SPDF/config/security/database/DatabaseService.java +++ b/src/main/java/stirling/software/SPDF/config/security/database/DatabaseService.java @@ -55,6 +55,7 @@ public DatabaseService(ApplicationProperties applicationProperties, DataSource d */ @Override public boolean hasBackup() { + createBackupDirectory(); Path filePath = Paths.get(BACKUP_DIR); if (Files.exists(filePath)) { @@ -74,6 +75,8 @@ public List getBackupList() { List backupFiles = new ArrayList<>(); if (isH2Database()) { + createBackupDirectory(); + Path backupPath = Paths.get(BACKUP_DIR); try (DirectoryStream stream = @@ -110,6 +113,18 @@ public List getBackupList() { return backupFiles; } + private void createBackupDirectory() { + Path backupPath = Paths.get(BACKUP_DIR); + if (!Files.exists(backupPath)) { + try { + Files.createDirectories(backupPath); + log.debug("create backup directory: {}", BACKUP_DIR); + } catch (IOException e) { + log.error("Error create backup directory: {}", e.getMessage(), e); + } + } + } + @Override public void importDatabase() { if (!hasBackup()) throw new BackupNotFoundException("No backup scripts were found."); @@ -255,6 +270,7 @@ public boolean deleteBackupFile(String fileName) throws IOException { * @return the Path object for the given file name */ public Path getBackupFilePath(String fileName) { + createBackupDirectory(); Path filePath = Paths.get(BACKUP_DIR, fileName).normalize(); if (!filePath.startsWith(BACKUP_DIR)) { throw new SecurityException("Path traversal detected");