mirror of
https://github.com/mjl-/mox.git
synced 2025-07-12 17:44:35 +03:00
update to latest bstore (with support for an index on a []string: Message.DKIMDomains), and cyclic data types (to be used for Message.Part soon); also adds a context.Context to database operations.
This commit is contained in:
@ -61,13 +61,13 @@ type TLSReportRecord struct {
|
||||
Report tlsrpt.Report
|
||||
}
|
||||
|
||||
func database() (rdb *bstore.DB, rerr error) {
|
||||
func database(ctx context.Context) (rdb *bstore.DB, rerr error) {
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
if tlsrptDB == nil {
|
||||
p := mox.DataDirPath("tlsrpt.db")
|
||||
os.MkdirAll(filepath.Dir(p), 0770)
|
||||
db, err := bstore.Open(p, &bstore.Options{Timeout: 5 * time.Second, Perm: 0660}, TLSReportRecord{})
|
||||
db, err := bstore.Open(ctx, p, &bstore.Options{Timeout: 5 * time.Second, Perm: 0660}, TLSReportRecord{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -78,7 +78,7 @@ func database() (rdb *bstore.DB, rerr error) {
|
||||
|
||||
// Init opens and possibly initializes the database.
|
||||
func Init() error {
|
||||
_, err := database()
|
||||
_, err := database(mox.Shutdown)
|
||||
return err
|
||||
}
|
||||
|
||||
@ -106,7 +106,7 @@ func Close() {
|
||||
func AddReport(ctx context.Context, verifiedFromDomain dns.Domain, mailFrom string, r *tlsrpt.Report) error {
|
||||
log := xlog.WithContext(ctx)
|
||||
|
||||
db, err := database()
|
||||
db, err := database(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -149,39 +149,39 @@ func AddReport(ctx context.Context, verifiedFromDomain dns.Domain, mailFrom stri
|
||||
}
|
||||
}
|
||||
record.Domain = reportdom.Name()
|
||||
return db.Insert(&record)
|
||||
return db.Insert(ctx, &record)
|
||||
}
|
||||
|
||||
// Records returns all TLS reports in the database.
|
||||
func Records(ctx context.Context) ([]TLSReportRecord, error) {
|
||||
db, err := database()
|
||||
db, err := database(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return bstore.QueryDB[TLSReportRecord](db).List()
|
||||
return bstore.QueryDB[TLSReportRecord](ctx, db).List()
|
||||
}
|
||||
|
||||
// RecordID returns the report for the ID.
|
||||
func RecordID(ctx context.Context, id int64) (TLSReportRecord, error) {
|
||||
db, err := database()
|
||||
db, err := database(ctx)
|
||||
if err != nil {
|
||||
return TLSReportRecord{}, err
|
||||
}
|
||||
|
||||
e := TLSReportRecord{ID: id}
|
||||
err = db.Get(&e)
|
||||
err = db.Get(ctx, &e)
|
||||
return e, err
|
||||
}
|
||||
|
||||
// RecordsPeriodDomain returns the reports overlapping start and end, for the given
|
||||
// domain. If domain is empty, all records match for domain.
|
||||
func RecordsPeriodDomain(ctx context.Context, start, end time.Time, domain string) ([]TLSReportRecord, error) {
|
||||
db, err := database()
|
||||
db, err := database(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
q := bstore.QueryDB[TLSReportRecord](db)
|
||||
q := bstore.QueryDB[TLSReportRecord](ctx, db)
|
||||
if domain != "" {
|
||||
q.FilterNonzero(TLSReportRecord{Domain: domain})
|
||||
}
|
||||
|
Reference in New Issue
Block a user