-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinput.go
201 lines (172 loc) · 4.33 KB
/
input.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package ldifdiff
import (
"bufio"
"errors"
"os"
"sort"
"strings"
"sync"
)
/* Package only functions */
func convertLdifStr(ldifStr string, ignoreAttr []string) (entries, error) {
return importRecords(ldifStr, "", ignoreAttr)
}
func importLdifFile(file string, ignoreAttr []string) (entries, error) {
entries, err := importRecords("", file, ignoreAttr)
if err != nil {
err = errors.New(err.Error() + " [" + file + "]")
}
return entries, err
}
/* Internal functions */
func addLineToRecord(line *string, record *[]string, ignoreAttr []string, prevAttrSkipped *bool) error {
var err error
// Append continuation lines to previous line
if strings.HasPrefix(*line, " ") {
if *prevAttrSkipped { // but not lines from a skipped attribute
return nil
}
switch len(*record) > 0 {
case true:
prevIdx := len(*record) - 1
prevLine := strings.TrimSuffix((*record)[prevIdx], "\n") +
strings.TrimPrefix(*line, " ")
(*record)[prevIdx] = prevLine
return nil
case false:
err = errors.New("Invalid modifyStr line continuation: \"" + *line + "\"")
return err
}
}
// Regular line
if len(*line) != 0 {
for _, attrName := range ignoreAttr {
if strings.HasPrefix(*line, attrName+":") {
*prevAttrSkipped = true
return nil
}
}
*record = append(*record, *line)
*prevAttrSkipped = false
}
return nil
}
func importRecords(ldifStr, file string, ignoreAttr []string) (entries, error) {
var readErr, parseErr error
queue := make(chan []string, 10)
entries := make(map[string][]string)
// Read and Parse the file concurrently
var wg sync.WaitGroup
wg.Add(2) // 1 reader + 1 parser
switch file {
case "": // it's a ldifStr
go readStr(ldifStr, ignoreAttr, queue, &wg, &readErr)
default: // it's a file
go readFile(file, ignoreAttr, queue, &wg, &readErr)
}
go parse(entries, queue, &wg, &parseErr)
wg.Wait()
// Return values
switch {
case readErr != nil:
return entries, readErr
case parseErr != nil:
return entries, parseErr
default:
return entries, nil
}
}
func readFile(file string, ignoreAttr []string, queue chan<- []string, wg *sync.WaitGroup, err *error) {
defer wg.Done()
defer close(queue)
fh, osErr := os.Open(file)
if osErr != nil {
*err = osErr
return
}
defer fh.Close()
record := []string{}
scanner := bufio.NewScanner(fh)
var prevAttrSkipped bool // use to skip continuation lines of skipped attr
firstLine := true
for scanner.Scan() {
line := scanner.Text()
// Skip comments
if strings.HasPrefix(line, "#") {
continue
}
// Check if first line is a "version: *" line and skip it
if firstLine {
if strings.HasPrefix(line, "version: ") {
firstLine = false
continue
} else if line == "" {
continue
}
firstLine = false
}
// Import lines as records
*err = addLineToRecord(&line, &record, ignoreAttr, &prevAttrSkipped)
if *err != nil {
return
}
// Dispatch the record to buffer & reset record
if len(line) == 0 && len(record) != 0 {
queue <- record
record = []string{}
}
}
// Last record may be a leftover (no empty line)
if len(record) != 0 {
queue <- record
}
if *err == nil {
*err = scanner.Err()
}
}
func readStr(ldifStr string, ignoreAttr []string, queue chan<- []string, wg *sync.WaitGroup, err *error) {
defer wg.Done()
defer close(queue)
for idx, recordStr := range strings.Split(ldifStr, "\n\n") {
var prevAttrSkipped bool
record := []string{}
for _, line := range strings.Split(recordStr, "\n") {
// Skip comments
if strings.HasPrefix(line, "#") {
continue
}
if idx == 0 { // First record only
if strings.HasPrefix(line, "version: ") {
continue
}
}
*err = addLineToRecord(&line, &record, ignoreAttr, &prevAttrSkipped)
if *err != nil {
return
}
// Dispatch the record to buffer & reset record
if len(line) == 0 && len(record) != 0 {
queue <- record
record = []string{}
}
}
// Last record may be a leftover (no empty line)
if len(record) != 0 {
queue <- record
}
}
}
func parse(entries entries, queue <-chan []string, wg *sync.WaitGroup, err *error) {
defer wg.Done()
for record := range queue {
dn := record[0] // Find dn, should be the first line
if !strings.HasPrefix(dn, "dn:") {
*err = errors.New("No dn could be retrieved")
continue
}
// Sort the entries
attr := record[1:]
sort.Strings(attr)
entries[dn] = attr
}
}