s/log/glog/ and default to Mailder with trailing slash.

This should fix the problem of not recursing when Mailder is a symlink.
This commit is contained in:
Bill Thiede 2023-02-24 21:59:33 -08:00
parent 0dceaa33c2
commit 23a63911de

View File

@ -3,7 +3,6 @@ package main
import ( import (
"encoding/json" "encoding/json"
"flag" "flag"
"log"
"net/http" "net/http"
_ "net/http/pprof" _ "net/http/pprof"
"os" "os"
@ -11,10 +10,12 @@ import (
"sort" "sort"
"strings" "strings"
"time" "time"
"github.com/golang/glog"
) )
var ( var (
root = flag.String("root", filepath.Join(os.Getenv("HOME"), "Maildir"), root = flag.String("root", filepath.Join(os.Getenv("HOME"), "Maildir/"),
"root directory to watch") "root directory to watch")
poll = flag.Duration("poll", time.Second*time.Duration(10), poll = flag.Duration("poll", time.Second*time.Duration(10),
"poll interval between new file check") "poll interval between new file check")
@ -64,6 +65,7 @@ func isNewEmail(path string) bool {
// 1340520791.M843511P28536.sagan.sf.xinu.tv,S=1467,W=1503:2,RS // 1340520791.M843511P28536.sagan.sf.xinu.tv,S=1467,W=1503:2,RS
// Unseen: // Unseen:
// 1377995543.50996_2.sagan.sf.xinu.tv:2,Rb // 1377995543.50996_2.sagan.sf.xinu.tv:2,Rb
// 1677301126.3645317_2126.nixos-01,U=38925:2,
seen := false seen := false
i := strings.LastIndex(path, ":") i := strings.LastIndex(path, ":")
if i != -1 { if i != -1 {
@ -126,7 +128,7 @@ func (mc *mailCounter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
enc := json.NewEncoder(w) enc := json.NewEncoder(w)
err := enc.Encode(c) err := enc.Encode(c)
if err != nil { if err != nil {
log.Println("Error printing JSON:", err) glog.Infoln("Error printing JSON:", err)
} }
} }
@ -188,11 +190,12 @@ func skip(path string) bool {
func (mc *mailCounter) fillNeedUpdate() error { func (mc *mailCounter) fillNeedUpdate() error {
mc.needUpdate = mc.needUpdate[:0] mc.needUpdate = mc.needUpdate[:0]
// log.Println("BEGIN fillNeedUpdate", mc.needUpdate) // glog.Infoln("BEGIN fillNeedUpdate", mc.needUpdate)
// defer func() { // defer func() {
// log.Println("END fillNeedUpdate", mc.needUpdate) // glog.Infoln("END fillNeedUpdate", mc.needUpdate)
// }() // }()
return filepath.Walk(mc.root, func(path string, info os.FileInfo, err error) error { return filepath.Walk(mc.root, func(path string, info os.FileInfo, err error) error {
glog.Infof("path %s", path)
if err != nil { if err != nil {
return err return err
} }
@ -218,7 +221,7 @@ func (mc *mailCounter) watch(root string, poll time.Duration) error {
} }
modCnt := len(mc.needUpdate) modCnt := len(mc.needUpdate)
if modCnt > 0 { if modCnt > 0 {
log.Println(modCnt, "folders need update") glog.Infoln(modCnt, "folders need update")
update := map[string]struct{}{} update := map[string]struct{}{}
for _, dir := range mc.needUpdate { for _, dir := range mc.needUpdate {
// Store parent directory, which is the IMAP folder // Store parent directory, which is the IMAP folder
@ -283,7 +286,7 @@ func printCounts(counts map[string]int) {
sort.Sort(cl) sort.Sort(cl)
for _, c := range cl { for _, c := range cl {
if c.c > 0 { if c.c > 0 {
log.Printf("%s (%d)", c.f, c.c) glog.Infof("%s (%d)", c.f, c.c)
} }
} }
} }
@ -291,17 +294,18 @@ func printCounts(counts map[string]int) {
func main() { func main() {
flag.Parse() flag.Parse()
glog.Info("Starting")
mc := newMailCounter() mc := newMailCounter()
go func() { go func() {
http.Handle("/unread", mc) http.Handle("/unread", mc)
err := http.ListenAndServe(*addr, nil) err := http.ListenAndServe(*addr, nil)
if err != nil { if err != nil {
log.Fatal(err) glog.Fatal(err)
} }
}() }()
err := mc.watch(*root, *poll) err := mc.watch(*root, *poll)
if err != nil { if err != nil {
log.Fatal(err) glog.Fatal(err)
} }
} }