9
9
package user
10
10
11
11
import (
12
- "bufio"
13
12
"bytes"
14
13
"errors"
15
14
"io"
@@ -27,80 +26,6 @@ func init() {
27
26
groupImplemented = false
28
27
}
29
28
30
- // lineFunc returns a value, an error, or (nil, nil) to skip the row.
31
- type lineFunc func (line []byte ) (v interface {}, err error )
32
-
33
- // readColonFile parses r as an /etc/group or /etc/passwd style file, running
34
- // fn for each row. readColonFile returns a value, an error, or (nil, nil) if
35
- // the end of the file is reached without a match.
36
- //
37
- // readCols is the minimum number of colon-separated fields that will be passed
38
- // to fn; in a long line additional fields may be silently discarded.
39
- func readColonFile (r io.Reader , fn lineFunc , readCols int ) (v interface {}, err error ) {
40
- rd := bufio .NewReader (r )
41
-
42
- // Read the file line-by-line.
43
- for {
44
- var isPrefix bool
45
- var wholeLine []byte
46
-
47
- // Read the next line. We do so in chunks (as much as reader's
48
- // buffer is able to keep), check if we read enough columns
49
- // already on each step and store final result in wholeLine.
50
- for {
51
- var line []byte
52
- line , isPrefix , err = rd .ReadLine ()
53
-
54
- if err != nil {
55
- // We should return (nil, nil) if EOF is reached
56
- // without a match.
57
- if err == io .EOF {
58
- err = nil
59
- }
60
- return nil , err
61
- }
62
-
63
- // Simple common case: line is short enough to fit in a
64
- // single reader's buffer.
65
- if ! isPrefix && len (wholeLine ) == 0 {
66
- wholeLine = line
67
- break
68
- }
69
-
70
- wholeLine = append (wholeLine , line ... )
71
-
72
- // Check if we read the whole line (or enough columns)
73
- // already.
74
- if ! isPrefix || bytes .Count (wholeLine , []byte {':' }) >= readCols {
75
- break
76
- }
77
- }
78
-
79
- // There's no spec for /etc/passwd or /etc/group, but we try to follow
80
- // the same rules as the glibc parser, which allows comments and blank
81
- // space at the beginning of a line.
82
- wholeLine = bytes .TrimSpace (wholeLine )
83
- if len (wholeLine ) == 0 || wholeLine [0 ] == '#' {
84
- continue
85
- }
86
- v , err = fn (wholeLine )
87
- if v != nil || err != nil {
88
- return
89
- }
90
-
91
- // If necessary, skip the rest of the line
92
- for ; isPrefix ; _ , isPrefix , err = rd .ReadLine () {
93
- if err != nil {
94
- // We should return (nil, nil) if EOF is reached without a match.
95
- if err == io .EOF {
96
- err = nil
97
- }
98
- return nil , err
99
- }
100
- }
101
- }
102
- }
103
-
104
29
func matchGroupIndexValue (value string , idx int ) lineFunc {
105
30
var leadColon string
106
31
if idx > 0 {
0 commit comments