1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-22 08:35:57 +01:00

Avoid code duplication in openDescriptionFile.

This commit is contained in:
Juliusz Chroboczek 2023-12-09 16:11:30 +01:00
parent 1f3b349ea2
commit dcde4562f5

View file

@ -1007,13 +1007,13 @@ func maxHistoryAge(desc *Description) time.Duration {
return DefaultMaxHistoryAge return DefaultMaxHistoryAge
} }
func openDescriptionFile(name string) (*os.File, string, bool, error) { func getDescriptionFile[T any](name string, get func(string) (T, error)) (T, string, bool, error) {
isParent := false isParent := false
for name != "" { for name != "" {
fileName := filepath.Join( fileName := filepath.Join(
Directory, path.Clean("/"+name)+".json", Directory, path.Clean("/"+name)+".json",
) )
r, err := os.Open(fileName) r, err := get(fileName)
if !os.IsNotExist(err) { if !os.IsNotExist(err) {
return r, fileName, isParent, err return r, fileName, isParent, err
} }
@ -1021,24 +1021,8 @@ func openDescriptionFile(name string) (*os.File, string, bool, error) {
name, _ = path.Split(name) name, _ = path.Split(name)
name = strings.TrimRight(name, "/") name = strings.TrimRight(name, "/")
} }
return nil, "", false, os.ErrNotExist var zero T
} return zero, "", false, os.ErrNotExist
func statDescriptionFile(name string) (os.FileInfo, string, bool, error) {
isParent := false
for name != "" {
fileName := filepath.Join(
Directory, path.Clean("/"+name)+".json",
)
fi, err := os.Stat(fileName)
if !os.IsNotExist(err) {
return fi, fileName, isParent, err
}
isParent = true
name, _ = path.Split(name)
name = strings.TrimRight(name, "/")
}
return nil, "", false, os.ErrNotExist
} }
// descriptionMatch returns true if the description hasn't changed between // descriptionMatch returns true if the description hasn't changed between
@ -1057,7 +1041,7 @@ func descriptionMatch(d1, d2 *Description) bool {
// descriptionUnchanged returns true if a group's description hasn't // descriptionUnchanged returns true if a group's description hasn't
// changed since it was last read. // changed since it was last read.
func descriptionUnchanged(name string, desc *Description) bool { func descriptionUnchanged(name string, desc *Description) bool {
fi, fileName, _, err := statDescriptionFile(name) fi, fileName, _, err := getDescriptionFile(name, os.Stat)
if err != nil || fileName != desc.FileName { if err != nil || fileName != desc.FileName {
return false return false
} }
@ -1082,7 +1066,7 @@ func GetDescription(name string) (*Description, error) {
// readDescription reads a group's description from disk // readDescription reads a group's description from disk
func readDescription(name string) (*Description, error) { func readDescription(name string) (*Description, error) {
r, fileName, isParent, err := openDescriptionFile(name) r, fileName, isParent, err := getDescriptionFile(name, os.Open)
if err != nil { if err != nil {
return nil, err return nil, err
} }