2
0
mirror of https://github.com/carlostrub/sisyphus synced 2024-10-31 09:20:15 +00:00

Use path/filepath for cleaner and safer path generation

This commit is contained in:
Carlo Strub 2018-01-11 21:35:27 +01:00
parent a08ad36d6e
commit 3a95f5cf1b
No known key found for this signature in database
GPG Key ID: 34EF3FF33C29811A
4 changed files with 19 additions and 14 deletions

View File

@ -2,6 +2,7 @@ package sisyphus
import ( import (
"os" "os"
"path/filepath"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -173,7 +174,7 @@ func (m *Mail) Classify(db *bolt.DB, dir Maildir) (err error) {
// Move mail around if junk. // Move mail around if junk.
if junk { if junk {
err = os.Rename(string(dir)+"/new/"+m.Key, string(dir)+"/.Junk/cur/"+m.Key) err = os.Rename(filepath.Join(string(dir), "new", m.Key), filepath.Join(string(dir), ".Junk", "cur", m.Key))
if err != nil { if err != nil {
return err return err
} }

View File

@ -1,6 +1,8 @@
package sisyphus package sisyphus
import ( import (
"path/filepath"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/boltdb/bolt" "github.com/boltdb/bolt"
@ -14,7 +16,7 @@ func openDB(m Maildir) (db *bolt.DB, err error) {
}).Info("Loading database") }).Info("Loading database")
// Open the sisyphus.db data file in your current directory. // Open the sisyphus.db data file in your current directory.
// It will be created if it doesn't exist. // It will be created if it doesn't exist.
db, err = bolt.Open(string(m)+"/sisyphus.db", 0600, nil) db, err = bolt.Open(filepath.Join(string(m), "sisyphus.db"), 0600, nil)
if err != nil { if err != nil {
return db, err return db, err
} }
@ -76,7 +78,7 @@ func LoadDatabases(d []Maildir) (databases map[Maildir]*bolt.DB, err error) {
func LoadBackupDatabases(d []Maildir) (databases map[Maildir]*bolt.DB, err error) { func LoadBackupDatabases(d []Maildir) (databases map[Maildir]*bolt.DB, err error) {
databases = make(map[Maildir]*bolt.DB) databases = make(map[Maildir]*bolt.DB)
for _, val := range d { for _, val := range d {
databases[val], err = bolt.Open(string(val)+"/sisyphus.db.backup", 0600, nil) databases[val], err = bolt.Open(filepath.Join(string(val), "sisyphus.db.backup"), 0600, nil)
if err != nil { if err != nil {
return databases, err return databases, err
} }
@ -93,7 +95,7 @@ func CloseDatabases(databases map[Maildir]*bolt.DB) {
err := val.Close() err := val.Close()
if err != nil { if err != nil {
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"db": string(key) + "/sisyphus.db", "db": filepath.Join(string(key), "sisyphus.db"),
}).Error("Unable to close database") }).Error("Unable to close database")
} }
log.WithFields(log.Fields{ log.WithFields(log.Fields{

15
mail.go
View File

@ -8,6 +8,7 @@ import (
"mime/quotedprintable" "mime/quotedprintable"
"net/mail" "net/mail"
"os" "os"
"path/filepath"
"regexp" "regexp"
"strings" "strings"
"unicode/utf8" "unicode/utf8"
@ -37,15 +38,15 @@ func (d Maildir) CreateDirs() error {
"dir": dir, "dir": dir,
}).Info("Create missing directories") }).Info("Create missing directories")
err := os.MkdirAll(dir+"/.Junk/cur", 0700) err := os.MkdirAll(filepath.Join(dir, ".Junk", "cur"), 0700)
if err != nil { if err != nil {
return err return err
} }
err = os.MkdirAll(dir+"/new", 0700) err = os.MkdirAll(filepath.Join(dir, "new"), 0700)
if err != nil { if err != nil {
return err return err
} }
err = os.MkdirAll(dir+"/cur", 0700) err = os.MkdirAll(filepath.Join(dir, "cur"), 0700)
return err return err
} }
@ -59,7 +60,7 @@ func (d Maildir) Index() (m []*Mail, err error) {
"dir": dir, "dir": dir,
}).Info("Start indexing mails") }).Info("Start indexing mails")
dirs := []string{dir, dir + "/.Junk"} dirs := []string{dir, filepath.Join(dir, ".Junk")}
for _, val := range dirs { for _, val := range dirs {
j, err := maildir.Dir(val).Keys() j, err := maildir.Dir(val).Keys()
if err != nil { if err != nil {
@ -68,7 +69,7 @@ func (d Maildir) Index() (m []*Mail, err error) {
for _, v := range j { for _, v := range j {
var new Mail var new Mail
new.Key = v new.Key = v
if val == dir+"/.Junk" { if val == filepath.Join(dir, ".Junk") {
new.Junk = true new.Junk = true
} }
m = append(m, &new) m = append(m, &new)
@ -89,9 +90,9 @@ func (m *Mail) Load(dir Maildir) (err error) {
switch { switch {
case m.Junk: case m.Junk:
dir = dir + Maildir("/.Junk") dir = Maildir(filepath.Join(string(dir), ".Junk"))
case m.New: case m.New:
dir = dir + Maildir("/new") dir = Maildir(filepath.Join(string(dir), "new"))
} }
message, err = maildir.Dir(dir).Message(m.Key) message, err = maildir.Dir(dir).Message(m.Key)

View File

@ -4,6 +4,7 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"os" "os"
"path/filepath"
"strings" "strings"
"time" "time"
@ -202,11 +203,11 @@ COPYRIGHT:
}() }()
for _, val := range maildirs { for _, val := range maildirs {
err = watcher.Add(string(val) + "/new") err = watcher.Add(filepath.Join(string(val), "new"))
if err != nil { if err != nil {
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"err": err, "err": err,
"dir": val + "/new", "dir": filepath.Join(string(val), "new"),
}).Error("Cannot watch directory") }).Error("Cannot watch directory")
} }
} }
@ -282,7 +283,7 @@ func backup(maildirs []sisyphus.Maildir, dbs map[sisyphus.Maildir]*bolt.DB) {
for _, d := range maildirs { for _, d := range maildirs {
db := dbs[d] db := dbs[d]
backup, err := os.Create(string(d) + "/sisyphus.db.backup") backup, err := os.Create(filepath.Join(string(d), "sisyphus.db.backup"))
if err != nil { if err != nil {
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"err": err, "err": err,