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

Add allowSubgroups argument to readDescription.

This commit is contained in:
Juliusz Chroboczek 2024-04-08 23:52:29 +02:00
parent b7094fc373
commit 1666abf2f3
2 changed files with 15 additions and 11 deletions

View file

@ -205,17 +205,20 @@ func maxHistoryAge(desc *Description) time.Duration {
return DefaultMaxHistoryAge return DefaultMaxHistoryAge
} }
func getDescriptionFile[T any](name string, get func(string) (T, error)) (T, string, bool, error) { func getDescriptionFile[T any](name string, allowSubgroups bool, get func(string) (T, error)) (T, string, bool, error) {
isParent := false isSubgroup := false
for name != "" { for name != "" {
fileName := filepath.Join( fileName := filepath.Join(
Directory, path.Clean("/"+name)+".json", Directory, path.Clean("/"+name)+".json",
) )
r, err := get(fileName) r, err := get(fileName)
if !os.IsNotExist(err) { if !os.IsNotExist(err) {
return r, fileName, isParent, err return r, fileName, isSubgroup, err
} }
isParent = true if !allowSubgroups {
break
}
isSubgroup = true
name, _ = path.Split(name) name, _ = path.Split(name)
name = strings.TrimRight(name, "/") name = strings.TrimRight(name, "/")
} }
@ -239,7 +242,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 := getDescriptionFile(name, os.Stat) fi, fileName, _, err := getDescriptionFile(name, true, os.Stat)
if err != nil || fileName != desc.FileName { if err != nil || fileName != desc.FileName {
return false return false
} }
@ -259,12 +262,13 @@ func GetDescription(name string) (*Description, error) {
} }
} }
return readDescription(name) return readDescription(name, true)
} }
// 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, allowSubgroups bool) (*Description, error) {
r, fileName, isParent, err := getDescriptionFile(name, os.Open) r, fileName, isSubgroup, err :=
getDescriptionFile(name, allowSubgroups, os.Open)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -283,7 +287,7 @@ func readDescription(name string) (*Description, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if isParent { if isSubgroup {
if !desc.AutoSubgroups { if !desc.AutoSubgroups {
return nil, os.ErrNotExist return nil, os.ErrNotExist
} }

View file

@ -446,7 +446,7 @@ func add(name string, desc *Description) (*Group, []Client, error) {
g := groups.groups[name] g := groups.groups[name]
if g == nil { if g == nil {
if desc == nil { if desc == nil {
desc, err = readDescription(name) desc, err = readDescription(name, true)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
@ -471,7 +471,7 @@ func add(name string, desc *Description) (*Group, []Client, error) {
notify = true notify = true
} }
} else if !descriptionUnchanged(name, g.description) { } else if !descriptionUnchanged(name, g.description) {
desc, err = readDescription(name) desc, err = readDescription(name, true)
if err != nil { if err != nil {
if !os.IsNotExist(err) { if !os.IsNotExist(err) {
log.Printf("Reading group %v: %v", name, err) log.Printf("Reading group %v: %v", name, err)