X-Git-Url: https://ruderich.org/simon/gitweb/?a=blobdiff_plain;f=group.go;h=214f91438d78740a331c487cbfe5ecab1355718c;hb=c6b898ea9ab372c58c59658c2acf17ea831d71db;hp=02502f62a147464eee533c8ce3a3ec1683fb0748;hpb=839f07d7b3130efc613d7d3fa8ed71a7d8d5fd7f;p=nsscash%2Fnsscash.git diff --git a/group.go b/group.go index 02502f6..214f914 100644 --- a/group.go +++ b/group.go @@ -39,11 +39,13 @@ type Group struct { Gid uint64 Members []string } + +// Go does not support slices in map keys; therefore, use this separate struct type GroupKey struct { Name string Passwd string Gid uint64 - Members string + Members string // "," separated } func toKey(g Group) GroupKey { @@ -56,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 { @@ -74,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 @@ -87,10 +98,6 @@ func ParseGroups(r io.Reader) ([]Group, error) { Members: members, }) } - err := s.Err() - if err != nil { - return nil, err - } return res, nil } @@ -112,10 +119,7 @@ func SerializeGroup(g Group) []byte { offPasswd := uint16(data.Len()) data.Write([]byte(g.Passwd)) data.WriteByte(0) - // Padding to align the following uint16 - if data.Len()%2 != 0 { - data.WriteByte(0) - } + alignBufferTo(&data, 2) // align the following uint16 offMemOff := uint16(data.Len()) // Offsets for group members offMem := offMemOff + 2*uint16(len(mems_off)) @@ -152,12 +156,7 @@ func SerializeGroup(g Group) []byte { res.Write(data.Bytes()) // We must pad each entry so that all uint64 at the beginning of the // struct are 8 byte aligned - l := res.Len() - if l%8 != 0 { - for i := 0; i < 8-l%8; i++ { - res.WriteByte(0) - } - } + alignBufferTo(&res, 8) return res.Bytes() } @@ -209,7 +208,8 @@ func SerializeGroups(w io.Writer, grs []Group) error { } // Sanity check - if indexOrig.Len() != indexId.Len() || + if len(grs)*8 != indexOrig.Len() || + indexOrig.Len() != indexId.Len() || indexId.Len() != indexName.Len() { return fmt.Errorf("indexes have inconsistent length") }