Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: NoSuchFileException if configs\db\backup is not present on first start #2665

Merged
merged 1 commit into from
Jan 11, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -74,6 +75,8 @@ public List<FileInfo> getBackupList() {
List<FileInfo> backupFiles = new ArrayList<>();

if (isH2Database()) {
createBackupDirectory();

Path backupPath = Paths.get(BACKUP_DIR);

try (DirectoryStream<Path> stream =
Expand Down Expand Up @@ -110,6 +113,18 @@ public List<FileInfo> 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.");
Expand Down Expand Up @@ -255,6 +270,7 @@ public boolean deleteBackupFile(String fileName) throws IOException {
* @return the <code>Path</code> 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");
Expand Down
Loading