@@ -33,22 +33,20 @@ func New(prefix string) *Finder {
33
33
// We exclude non-files, and ignore $path/tmp/.
34
34
func (f * Finder ) Messages (path string ) []string {
35
35
36
- // TODO use a struct
37
- // filename string
38
- // info os.FileInfo
36
+ // We want to sort the files by age.
39
37
//
40
- //Then we can sort them like so:
41
- //
42
- // sort.Slice(files, func(i,j int) bool{
43
- // return files[i].ModTime().Unix() < files[j].ModTime().Unix()
44
- // })
45
- //
46
- // Doing this is better than getting all the info a second
47
- // time, once we've found the filenames
38
+ // Calling stat is expensive, but the appropriate
39
+ // details are already retrieved by our filesystem
40
+ // walker - so we store them alongside the names
41
+ // as we receive them.
48
42
//
43
+ type MessageInfo struct {
44
+ path string
45
+ info os.FileInfo
46
+ }
49
47
50
- // Build the list of message filenames here .
51
- var files []string
48
+ // The holder for the messages we find .
49
+ var files []MessageInfo
52
50
53
51
// Directories we examine beneath the maildir
54
52
dirs := []string {"cur" , "new" }
@@ -65,14 +63,26 @@ func (f *Finder) Messages(path string) []string {
65
63
// We only care about files
66
64
mode := f .Mode ()
67
65
if mode .IsRegular () {
68
- files = append (files , path )
66
+ files = append (files , MessageInfo { path : path , info : f } )
69
67
}
70
68
71
69
return nil
72
70
})
73
71
}
74
72
75
- return files
73
+ // Sort the files
74
+ sort .Slice (files , func (i , j int ) bool {
75
+ return files [i ].info .ModTime ().Unix () < files [j ].info .ModTime ().Unix ()
76
+ })
77
+
78
+ // Now build up a return value of just the filenames,
79
+ // which have been sorted by modification time.
80
+ ret := make ([]string , len (files ))
81
+ for i , e := range files {
82
+ ret [i ] = e .path
83
+ }
84
+
85
+ return ret
76
86
}
77
87
78
88
// Maildirs returns the list of Maildir folders beneath our prefix.
0 commit comments