]> ruderich.org/simon Gitweb - nsscash/nsscash.git/blobdiff - group.go
nsscash: support longer lines in passwd/group files
[nsscash/nsscash.git] / group.go
index 4513758df41c4a8d22eb32f8fac5653e773ca3dd..214f91438d78740a331c487cbfe5ecab1355718c 100644 (file)
--- a/group.go
+++ b/group.go
@@ -45,7 +45,7 @@ type GroupKey struct {
        Name    string
        Passwd  string
        Gid     uint64
-       Members string
+       Members string // "," separated
 }
 
 func toKey(g Group) GroupKey {
@@ -58,13 +58,19 @@ func toKey(g Group) GroupKey {
 }
 
 // ParseGroups parses a file in the format of /etc/group and returns all
-// entries as Group structs.
+// entries as slice of Group structs.
 func ParseGroups(r io.Reader) ([]Group, error) {
        var res []Group
 
-       s := bufio.NewScanner(r)
-       for s.Scan() {
-               t := s.Text()
+       s := bufio.NewReader(r)
+       for {
+               t, err := s.ReadString('\n')
+               if err != nil {
+                       if err == io.EOF {
+                               break
+                       }
+                       return nil, err
+               }
 
                x := strings.Split(t, ":")
                if len(x) != 4 {
@@ -76,6 +82,9 @@ func ParseGroups(r io.Reader) ([]Group, error) {
                        return nil, errors.Wrapf(err, "invalid gid in line %q", t)
                }
 
+               // ReadString() contains the delimiter
+               x[3] = strings.TrimSuffix(x[3], "\n")
+
                var members []string
                // No members must result in empty slice, not slice with the
                // empty string
@@ -89,10 +98,6 @@ func ParseGroups(r io.Reader) ([]Group, error) {
                        Members: members,
                })
        }
-       err := s.Err()
-       if err != nil {
-               return nil, err
-       }
 
        return res, nil
 }