Codebase list golang-github-emersion-go-maildir / 2531326
Import upstream version 0.2.0+git20201109.1.ced1977 Debian Janitor 2 years ago
2 changed file(s) with 31 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
88 import (
99 "crypto/rand"
1010 "encoding/hex"
11 "fmt"
1112 "io"
1213 "os"
1314 "path/filepath"
3536 }
3637
3738 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)
3940 }
4041
4142 // A FlagError occurs when a non-standard info section is encountered.
122123 return c, nil
123124 }
124125
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
125148 // Keys returns a slice of valid keys to access messages by.
126149 func (d Dir) Keys() ([]string, error) {
127150 f, err := os.Open(filepath.Join(string(d), "cur"))
136159 var keys []string
137160 for _, n := range names {
138161 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)
143167 }
144168 }
145169 return keys, nil
6161 func TestInit(t *testing.T) {
6262 t.Parallel()
6363
64 var d Dir = "test_create"
64 var d Dir = "test_init"
6565 err := d.Init()
6666 if err != nil {
6767 t.Fatal(err)
6868 }
6969
70 f, err := os.Open("test_create")
70 f, err := os.Open("test_init")
7171 if err != nil {
7272 t.Fatal(err)
7373 }