10 "github.com/mjl-/bstore"
12 "github.com/mjl-/mox/mlog"
13 "github.com/mjl-/mox/mox-"
17 ReportDBTypes = []any{Record{}}
20 // Accessed directly by tlsrptsend.
21 ResultDBTypes = []any{TLSResult{}, SuppressAddress{}}
25// Init opens and possibly initializes the databases.
27 if ReportDB != nil || ResultDB != nil {
28 return fmt.Errorf("already initialized")
31 log := mlog.New("tlsrptdb", nil)
34 ReportDB, err = openReportDB(mox.Shutdown, log)
36 return fmt.Errorf("opening report db: %v", err)
38 ResultDB, err = openResultDB(mox.Shutdown, log)
40 return fmt.Errorf("opening result db: %v", err)
45func openReportDB(ctx context.Context, log mlog.Log) (*bstore.DB, error) {
46 p := mox.DataDirPath("tlsrpt.db")
47 os.MkdirAll(filepath.Dir(p), 0770)
48 opts := bstore.Options{Timeout: 5 * time.Second, Perm: 0660, RegisterLogger: log.Logger}
49 return bstore.Open(ctx, p, &opts, ReportDBTypes...)
52func openResultDB(ctx context.Context, log mlog.Log) (*bstore.DB, error) {
53 p := mox.DataDirPath("tlsrptresult.db")
54 os.MkdirAll(filepath.Dir(p), 0770)
55 opts := bstore.Options{Timeout: 5 * time.Second, Perm: 0660, RegisterLogger: log.Logger}
56 return bstore.Open(ctx, p, &opts, ResultDBTypes...)
59// Close closes the database connections.
61 if err := ResultDB.Close(); err != nil {
62 return fmt.Errorf("closing result db: %w", err)
66 if err := ReportDB.Close(); err != nil {
67 return fmt.Errorf("closing report db: %w", err)