X-Git-Url: https://ruderich.org/simon/gitweb/?p=nsscash%2Fnsscash.git;a=blobdiff_plain;f=group.go;h=0ad86711431d068ff0922ceace8c39cdba01a1a0;hp=7cc084187d15f613d140f251eef5cfdfea0d4751;hb=13b90749eb1b3547bbfbb47fe142cb96a17234b5;hpb=d7eb5ee1a910303f38904e604aea9cf0b1372d7b diff --git a/group.go b/group.go index 7cc0841..0ad8671 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,7 +58,7 @@ 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 @@ -112,10 +114,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 +151,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() }