If configured directory is missing, archive will be created anyway, but this error will be logged.

This commit is contained in:
eleksir
2026-04-04 22:45:00 +03:00
parent 3c22b2e1a0
commit 6e56e74df4
3 changed files with 41 additions and 5 deletions

View File

@@ -1,10 +1,12 @@
package backer
import (
"errors"
"io/fs"
"os"
"path/filepath"
"backer/internal/log"
backerlog "backer/internal/log"
)
// isExcluded checks if path matches any of the configured exclude patterns.
@@ -22,19 +24,31 @@ func isExcluded(path string) bool {
}
// GetFilesFromDirectories makes a file list of given directories.
// Logs errors for directories that don't exist or can't be accessed but continues with valid directories.
func GetFilesFromDirectories(directories []string) ([]string, error) {
var files []string
for _, dir := range directories {
// Check directory exists before walking.
if _, err := os.Stat(dir); err != nil {
if errors.Is(err, os.ErrNotExist) {
backerlog.Errorf("Configured backup directory not found: %s", dir)
} else {
backerlog.Errorf("Failed to access configured directory %s: %v", dir, err)
}
continue
}
err := filepath.WalkDir(dir, func(path string, de fs.DirEntry, err error) error { //nolint: revive
if err != nil {
log.Warnf("Skipping %s: %v", path, err)
backerlog.Warnf("Skipping %s: %v", path, err)
return nil
}
if isExcluded(path) {
log.Debugf("Excluding: %s", path)
backerlog.Debugf("Excluding: %s", path)
return nil
}
@@ -45,7 +59,7 @@ func GetFilesFromDirectories(directories []string) ([]string, error) {
})
if err != nil {
log.Warnf("Failed to walk directory %s: %v", dir, err)
backerlog.Warnf("Failed to walk directory %s: %v", dir, err)
}
}

View File

@@ -93,18 +93,39 @@ func TestGetFilesFromDirectoriesEmptyDir(t *testing.T) {
}
// TestGetFilesFromDirectoriesNonExistent tests with non-existent directory.
// Should log error but not fail - returns empty list.
func TestGetFilesFromDirectoriesNonExistent(t *testing.T) {
output, err := GetFilesFromDirectories([]string{"/nonexistent/path/that/does/not/exist"})
// Should not return error, but logs error
if err != nil {
t.Errorf("Expected no error (logs warning instead), got %v", err)
t.Errorf("Expected no error (logs error instead), got %v", err)
}
// Should return empty list
if len(output) != 0 {
t.Errorf("Expected empty output for non-existent directory, got %d files", len(output))
}
}
// TestGetFilesFromDirectoriesPartialFailure tests with mix of valid and invalid directories.
func TestGetFilesFromDirectoriesPartialFailure(t *testing.T) {
// Create a temp directory that exists
tmpDir := t.TempDir()
output, err := GetFilesFromDirectories([]string{tmpDir, "/nonexistent/path/that/does/not/exist"})
// Should not return error, but logs error
if err != nil {
t.Errorf("Expected no error (logs error instead), got %v", err)
}
// Should return files from valid directory
if len(output) == 0 {
t.Error("Expected files from valid directory")
}
}
// TestGetFilesFromDirectoriesMultipleDirs tests multiple directories.
func TestGetFilesFromDirectoriesMultipleDirs(t *testing.T) {
output, err := GetFilesFromDirectories([]string{

View File

@@ -107,6 +107,7 @@ func NewServer(configPath string) (*ServerWrapper, error) {
if err != nil {
log.Errorf("Failed to get files: %v", err)
w.Header().Set("Server", "backer")
http.Error(w, "Failed to get files", http.StatusInternalServerError)
return