diff --git a/internal/backer/getFilesFromDirectories.go b/internal/backer/getFilesFromDirectories.go index bd93cd1..cf9330e 100644 --- a/internal/backer/getFilesFromDirectories.go +++ b/internal/backer/getFilesFromDirectories.go @@ -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) } } diff --git a/internal/backer/getFilesFromDirectories_test.go b/internal/backer/getFilesFromDirectories_test.go index c9a52a0..4962bbb 100644 --- a/internal/backer/getFilesFromDirectories_test.go +++ b/internal/backer/getFilesFromDirectories_test.go @@ -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{ diff --git a/internal/backer/newServer.go b/internal/backer/newServer.go index 3c2070c..54b977a 100644 --- a/internal/backer/newServer.go +++ b/internal/backer/newServer.go @@ -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