forked from carlostrub/sisyphus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
105 lines (88 loc) · 2.56 KB
/
database.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package sisyphus
import (
"path/filepath"
log "github.com/sirupsen/logrus"
"github.com/boltdb/bolt"
)
// openDB creates and opens a new database and its respective buckets (if required)
func openDB(m Maildir) (db *bolt.DB, err error) {
log.WithFields(log.Fields{
"dir": string(m),
}).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(filepath.Join(string(m), "sisyphus.db"), 0600, nil)
if err != nil {
return db, err
}
// Create DB bucket for the map of processed e-mail IDs
err = db.Update(func(tx *bolt.Tx) error {
_, err = tx.CreateBucketIfNotExists([]byte("Statistics"))
return err
})
if err != nil {
return db, err
}
// Create DB bucket for word lists
err = db.Update(func(tx *bolt.Tx) error {
_, err = tx.CreateBucketIfNotExists([]byte("Wordlists"))
return err
})
if err != nil {
return db, err
}
// Create DB bucket for Junk inside bucket Wordlists
err = db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("Wordlists"))
_, err = b.CreateBucketIfNotExists([]byte("Junk"))
return err
})
if err != nil {
return db, err
}
// Create DB bucket for Good inside bucket Wordlists
err = db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("Wordlists"))
_, err = b.CreateBucketIfNotExists([]byte("Good"))
return err
})
return db, err
}
// LoadDatabases loads all databases from a given slice of Maildirs
func LoadDatabases(d []Maildir) (databases map[Maildir]*bolt.DB, err error) {
databases = make(map[Maildir]*bolt.DB)
for _, val := range d {
databases[val], err = openDB(val)
if err != nil {
return databases, err
}
}
log.Info("All databases loaded")
return databases, nil
}
// LoadBackupDatabases loads all backup databases from a given slice of Maildirs
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(filepath.Join(string(val), "sisyphus.db.backup"), 0600, nil)
if err != nil {
return databases, err
}
}
log.Info("All databases loaded")
return databases, nil
}
// CloseDatabases closes all databases from a given slice of Maildirs
func CloseDatabases(databases map[Maildir]*bolt.DB) {
for key, val := range databases {
err := val.Close()
if err != nil {
log.WithFields(log.Fields{
"db": filepath.Join(string(key), "sisyphus.db"),
}).Error("Unable to close database")
}
log.WithFields(log.Fields{
"maildir": string(key),
}).Info("Database closed")
}
}