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:
parent
a08ad36d6e
commit
3a95f5cf1b
@ -2,6 +2,7 @@ package sisyphus
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
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.
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
package sisyphus
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/boltdb/bolt"
|
||||
@ -14,7 +16,7 @@ func openDB(m Maildir) (db *bolt.DB, err error) {
|
||||
}).Info("Loading database")
|
||||
// Open the sisyphus.db data file in your current directory.
|
||||
// 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 {
|
||||
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) {
|
||||
databases = make(map[Maildir]*bolt.DB)
|
||||
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 {
|
||||
return databases, err
|
||||
}
|
||||
@ -93,7 +95,7 @@ func CloseDatabases(databases map[Maildir]*bolt.DB) {
|
||||
err := val.Close()
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"db": string(key) + "/sisyphus.db",
|
||||
"db": filepath.Join(string(key), "sisyphus.db"),
|
||||
}).Error("Unable to close database")
|
||||
}
|
||||
log.WithFields(log.Fields{
|
||||
|
15
mail.go
15
mail.go
@ -8,6 +8,7 @@ import (
|
||||
"mime/quotedprintable"
|
||||
"net/mail"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
@ -37,15 +38,15 @@ func (d Maildir) CreateDirs() error {
|
||||
"dir": dir,
|
||||
}).Info("Create missing directories")
|
||||
|
||||
err := os.MkdirAll(dir+"/.Junk/cur", 0700)
|
||||
err := os.MkdirAll(filepath.Join(dir, ".Junk", "cur"), 0700)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = os.MkdirAll(dir+"/new", 0700)
|
||||
err = os.MkdirAll(filepath.Join(dir, "new"), 0700)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = os.MkdirAll(dir+"/cur", 0700)
|
||||
err = os.MkdirAll(filepath.Join(dir, "cur"), 0700)
|
||||
|
||||
return err
|
||||
}
|
||||
@ -59,7 +60,7 @@ func (d Maildir) Index() (m []*Mail, err error) {
|
||||
"dir": dir,
|
||||
}).Info("Start indexing mails")
|
||||
|
||||
dirs := []string{dir, dir + "/.Junk"}
|
||||
dirs := []string{dir, filepath.Join(dir, ".Junk")}
|
||||
for _, val := range dirs {
|
||||
j, err := maildir.Dir(val).Keys()
|
||||
if err != nil {
|
||||
@ -68,7 +69,7 @@ func (d Maildir) Index() (m []*Mail, err error) {
|
||||
for _, v := range j {
|
||||
var new Mail
|
||||
new.Key = v
|
||||
if val == dir+"/.Junk" {
|
||||
if val == filepath.Join(dir, ".Junk") {
|
||||
new.Junk = true
|
||||
}
|
||||
m = append(m, &new)
|
||||
@ -89,9 +90,9 @@ func (m *Mail) Load(dir Maildir) (err error) {
|
||||
|
||||
switch {
|
||||
case m.Junk:
|
||||
dir = dir + Maildir("/.Junk")
|
||||
dir = Maildir(filepath.Join(string(dir), ".Junk"))
|
||||
case m.New:
|
||||
dir = dir + Maildir("/new")
|
||||
dir = Maildir(filepath.Join(string(dir), "new"))
|
||||
}
|
||||
|
||||
message, err = maildir.Dir(dir).Message(m.Key)
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@ -202,11 +203,11 @@ COPYRIGHT:
|
||||
}()
|
||||
|
||||
for _, val := range maildirs {
|
||||
err = watcher.Add(string(val) + "/new")
|
||||
err = watcher.Add(filepath.Join(string(val), "new"))
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"err": err,
|
||||
"dir": val + "/new",
|
||||
"dir": filepath.Join(string(val), "new"),
|
||||
}).Error("Cannot watch directory")
|
||||
}
|
||||
}
|
||||
@ -282,7 +283,7 @@ func backup(maildirs []sisyphus.Maildir, dbs map[sisyphus.Maildir]*bolt.DB) {
|
||||
for _, d := range maildirs {
|
||||
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 {
|
||||
log.WithFields(log.Fields{
|
||||
"err": err,
|
||||
|
Loading…
Reference in New Issue
Block a user