8 | 8 |
import (
|
9 | 9 |
"crypto/rand"
|
10 | 10 |
"encoding/hex"
|
|
11 |
"fmt"
|
11 | 12 |
"io"
|
12 | 13 |
"os"
|
13 | 14 |
"path/filepath"
|
|
35 | 36 |
}
|
36 | 37 |
|
37 | 38 |
func (e *KeyError) Error() string {
|
38 | |
return "maildir: key " + e.Key + " matches " + strconv.Itoa(e.N) + " files."
|
|
39 |
return fmt.Sprintf("maildir: key %v matches %v files, expected exactly one", e.Key, e.N)
|
39 | 40 |
}
|
40 | 41 |
|
41 | 42 |
// A FlagError occurs when a non-standard info section is encountered.
|
|
122 | 123 |
return c, nil
|
123 | 124 |
}
|
124 | 125 |
|
|
126 |
func parseKey(filename string) (string, error) {
|
|
127 |
split := strings.FieldsFunc(filename, func(r rune) bool {
|
|
128 |
return r == separator
|
|
129 |
})
|
|
130 |
|
|
131 |
if len(split) == 0 {
|
|
132 |
return "", fmt.Errorf("Cannot parse key from filename %s", filename)
|
|
133 |
}
|
|
134 |
|
|
135 |
return split[0], nil
|
|
136 |
}
|
|
137 |
|
|
138 |
// Key returns the key for the given file path.
|
|
139 |
func (d Dir) Key(path string) (string, error) {
|
|
140 |
if filepath.Dir(path) != string(d) {
|
|
141 |
return "", fmt.Errorf("Filepath %s belongs to a different Maildir", path)
|
|
142 |
}
|
|
143 |
|
|
144 |
filename := filepath.Base(path)
|
|
145 |
return parseKey(filename)
|
|
146 |
}
|
|
147 |
|
125 | 148 |
// Keys returns a slice of valid keys to access messages by.
|
126 | 149 |
func (d Dir) Keys() ([]string, error) {
|
127 | 150 |
f, err := os.Open(filepath.Join(string(d), "cur"))
|
|
136 | 159 |
var keys []string
|
137 | 160 |
for _, n := range names {
|
138 | 161 |
if n[0] != '.' {
|
139 | |
split := strings.FieldsFunc(n, func(r rune) bool {
|
140 | |
return r == separator
|
141 | |
})
|
142 | |
keys = append(keys, split[0])
|
|
162 |
key, err := parseKey(n)
|
|
163 |
if err != nil {
|
|
164 |
return nil, err
|
|
165 |
}
|
|
166 |
keys = append(keys, key)
|
143 | 167 |
}
|
144 | 168 |
}
|
145 | 169 |
return keys, nil
|