If configured directory is missing, archive will be created anyway, but this error will be logged.
This commit is contained in:
@@ -1,10 +1,12 @@
|
|||||||
package backer
|
package backer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"backer/internal/log"
|
backerlog "backer/internal/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// isExcluded checks if path matches any of the configured exclude patterns.
|
// 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.
|
// 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) {
|
func GetFilesFromDirectories(directories []string) ([]string, error) {
|
||||||
var files []string
|
var files []string
|
||||||
|
|
||||||
for _, dir := range directories {
|
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
|
err := filepath.WalkDir(dir, func(path string, de fs.DirEntry, err error) error { //nolint: revive
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warnf("Skipping %s: %v", path, err)
|
backerlog.Warnf("Skipping %s: %v", path, err)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if isExcluded(path) {
|
if isExcluded(path) {
|
||||||
log.Debugf("Excluding: %s", path)
|
backerlog.Debugf("Excluding: %s", path)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -45,7 +59,7 @@ func GetFilesFromDirectories(directories []string) ([]string, error) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warnf("Failed to walk directory %s: %v", dir, err)
|
backerlog.Warnf("Failed to walk directory %s: %v", dir, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -93,18 +93,39 @@ func TestGetFilesFromDirectoriesEmptyDir(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TestGetFilesFromDirectoriesNonExistent tests with non-existent directory.
|
// TestGetFilesFromDirectoriesNonExistent tests with non-existent directory.
|
||||||
|
// Should log error but not fail - returns empty list.
|
||||||
func TestGetFilesFromDirectoriesNonExistent(t *testing.T) {
|
func TestGetFilesFromDirectoriesNonExistent(t *testing.T) {
|
||||||
output, err := GetFilesFromDirectories([]string{"/nonexistent/path/that/does/not/exist"})
|
output, err := GetFilesFromDirectories([]string{"/nonexistent/path/that/does/not/exist"})
|
||||||
|
|
||||||
|
// Should not return error, but logs error
|
||||||
if err != nil {
|
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 {
|
if len(output) != 0 {
|
||||||
t.Errorf("Expected empty output for non-existent directory, got %d files", len(output))
|
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.
|
// TestGetFilesFromDirectoriesMultipleDirs tests multiple directories.
|
||||||
func TestGetFilesFromDirectoriesMultipleDirs(t *testing.T) {
|
func TestGetFilesFromDirectoriesMultipleDirs(t *testing.T) {
|
||||||
output, err := GetFilesFromDirectories([]string{
|
output, err := GetFilesFromDirectories([]string{
|
||||||
|
|||||||
@@ -107,6 +107,7 @@ func NewServer(configPath string) (*ServerWrapper, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Failed to get files: %v", err)
|
log.Errorf("Failed to get files: %v", err)
|
||||||
|
|
||||||
|
w.Header().Set("Server", "backer")
|
||||||
http.Error(w, "Failed to get files", http.StatusInternalServerError)
|
http.Error(w, "Failed to get files", http.StatusInternalServerError)
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user