]> ruderich.org/simon Gitweb - nsscash/nsscash.git/blobdiff - passwd.go
nsscash: support longer lines in passwd/group files
[nsscash/nsscash.git] / passwd.go
index 1b4939c63ac6a1a9b2c634bdd0a74a2def5f0419..2698765212e0975738ebc8d8273140daee528816 100644 (file)
--- 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,14 +79,10 @@ 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
 }
 
@@ -134,12 +136,7 @@ func SerializePasswd(p Passwd) []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.Write([]byte{'0'})
-               }
-       }
+       alignBufferTo(&res, 8)
 
        return res.Bytes()
 }