X-Git-Url: https://ruderich.org/simon/gitweb/?a=blobdiff_plain;f=passwd.go;h=54da63b3f30252f65e5c1717318b683686a037c1;hb=7adc7f83eddd37aa372aa10159128b6d16c4320a;hp=90fed68a6b9f1d959f6ab9b03e4ebf89a081ec49;hpb=1d028880cfb17a7c8a9e3b1f54cf1e980baca63d;p=nsscash%2Fnsscash.git diff --git a/passwd.go b/passwd.go index 90fed68..54da63b 100644 --- a/passwd.go +++ b/passwd.go @@ -44,13 +44,19 @@ type Passwd struct { } // ParsePasswds parses a file in the format of /etc/passwd and returns all -// entries as Passwd structs. +// entries as slice of Passwd structs. func ParsePasswds(r io.Reader) ([]Passwd, error) { var res []Passwd - 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) != 7 { @@ -73,18 +79,14 @@ func ParsePasswds(r io.Reader) ([]Passwd, error) { Gid: gid, Gecos: x[4], Dir: x[5], - Shell: x[6], + // ReadString() contains the delimiter + Shell: strings.TrimSuffix(x[6], "\n"), }) } - err := s.Err() - if err != nil { - return nil, err - } - return res, nil } -func SerializePasswd(p Passwd) []byte { +func SerializePasswd(p Passwd) ([]byte, error) { // Concatenate all (NUL-terminated) strings and store the offsets var data bytes.Buffer data.Write([]byte(p.Name)) @@ -136,7 +138,7 @@ func SerializePasswd(p Passwd) []byte { // struct are 8 byte aligned alignBufferTo(&res, 8) - return res.Bytes() + return res.Bytes(), nil } func SerializePasswds(w io.Writer, pws []Passwd) error { @@ -146,7 +148,11 @@ func SerializePasswds(w io.Writer, pws []Passwd) error { for _, p := range pws { // TODO: warn about duplicate entries offsets[p] = uint64(data.Len()) - data.Write(SerializePasswd(p)) + x, err := SerializePasswd(p) + if err != nil { + return err + } + data.Write(x) } // Copy to prevent sorting from modifying the argument