Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NBHF4R9EAejDJUMdwr6C68
40 lines
1.1 KiB
Go
40 lines
1.1 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"io/fs"
|
|
"os"
|
|
)
|
|
|
|
// CheckFileMode bricht ab, wenn die Datei mehr Rechte trägt als maxMode.
|
|
func CheckFileMode(path string, maxMode fs.FileMode) error {
|
|
info, err := os.Stat(path)
|
|
if err != nil {
|
|
return fmt.Errorf("Datei %s nicht lesbar: %w", path, err)
|
|
}
|
|
if info.IsDir() {
|
|
return fmt.Errorf("%s ist ein Verzeichnis, erwartet wurde eine Datei", path)
|
|
}
|
|
return checkMode(path, info.Mode().Perm(), maxMode)
|
|
}
|
|
|
|
// CheckDirMode bricht ab, wenn das Verzeichnis mehr Rechte trägt als maxMode.
|
|
func CheckDirMode(path string, maxMode fs.FileMode) error {
|
|
info, err := os.Stat(path)
|
|
if err != nil {
|
|
return fmt.Errorf("Verzeichnis %s nicht lesbar: %w", path, err)
|
|
}
|
|
if !info.IsDir() {
|
|
return fmt.Errorf("%s ist kein Verzeichnis", path)
|
|
}
|
|
return checkMode(path, info.Mode().Perm(), maxMode)
|
|
}
|
|
|
|
func checkMode(path string, actual, maxMode fs.FileMode) error {
|
|
if extra := actual &^ maxMode; extra != 0 {
|
|
return fmt.Errorf(
|
|
"%s hat zu weite Dateirechte %#o (erlaubt höchstens %#o). Beheben mit: chmod %#o %s",
|
|
path, actual, maxMode, maxMode, path)
|
|
}
|
|
return nil
|
|
}
|