lnutils: add createdir util function

This utility function replaces repetitive logic patterns
throughout LND.
This commit is contained in:
Abdullahi Yunus committed 2025-02-20 17:08:21 +01:00
1 parent 0e87863481
commit 45a913ee91
2 files changed
+118

No files matched your search

+31
View File
@@ -0,0 +1,31 @@
package lnutils
import (
"errors"
"fmt"
"os"
)
// CreateDir creates a directory if it doesn't exist and also handles
// symlink-related errors with user-friendly messages. It creates all necessary
// parent directories with the specified permissions.
func CreateDir(dir string, perm os.FileMode) error {
err := os.MkdirAll(dir, perm)
if err == nil {
return nil
}
// Show a nicer error message if it's because a symlink
// is linked to a directory that does not exist
// (probably because it's not mounted).
var pathErr *os.PathError
if errors.As(err, &pathErr) && os.IsExist(err) {
link, lerr := os.Readlink(pathErr.Path)
if lerr == nil {
return fmt.Errorf("is symlink %s -> %s "+
"mounted?", pathErr.Path, link)
}
}
return fmt.Errorf("failed to create directory '%s': %w", dir, err)
}