| 1 |
//go:build notmuch |
| 2 |
// +build notmuch |
| 3 |
|
| 4 |
package notmuch |
| 5 |
|
| 6 |
/* |
| 7 |
#cgo LDFLAGS: -lnotmuch |
| 8 |
|
| 9 |
#include <stdlib.h> |
| 10 |
#include <notmuch.h> |
| 11 |
|
| 12 |
*/ |
| 13 |
import "C" |
| 14 |
|
| 15 |
import ( |
| 16 |
"errors" |
| 17 |
"fmt" |
| 18 |
"unsafe" |
| 19 |
) |
| 20 |
|
| 21 |
type Mode int |
| 22 |
|
| 23 |
const ( |
| 24 |
MODE_READ_ONLY Mode = C.NOTMUCH_DATABASE_MODE_READ_ONLY |
| 25 |
MODE_READ_WRITE Mode = C.NOTMUCH_DATABASE_MODE_READ_WRITE |
| 26 |
) |
| 27 |
|
| 28 |
type Database struct { |
| 29 |
// The profile to use. If it is the empty string, the profile will be |
| 30 |
// determined from the environment variable NOTMUCH_PROFILE or the |
| 31 |
// default profile will be used instead. |
| 32 |
Profile string |
| 33 |
|
| 34 |
db *C.notmuch_database_t |
| 35 |
open bool |
| 36 |
} |
| 37 |
|
| 38 |
// Open opens the database with the given mode. Caller must call Close when done |
| 39 |
// to commit changes and free resources |
| 40 |
func (db *Database) Open(mode Mode) error { |
| 41 |
var ( |
| 42 |
cProfile *C.char |
| 43 |
cErr *C.char |
| 44 |
) |
| 45 |
defer C.free(unsafe.Pointer(cProfile)) |
| 46 |
defer C.free(unsafe.Pointer(cErr)) |
| 47 |
|
| 48 |
if db.Profile != "" { |
| 49 |
cProfile = C.CString(db.Profile) |
| 50 |
} |
| 51 |
cmode := C.notmuch_database_mode_t(mode) |
| 52 |
|
| 53 |
var cdb *C.notmuch_database_t |
| 54 |
|
| 55 |
// gocritic:dupSubExpr throws an issue here no matter how we call this |
| 56 |
// function |
| 57 |
err := errorWrap( |
| 58 |
C.notmuch_database_open_with_config( |
| 59 |
nil, cmode, nil, cProfile, &cdb, &cErr, //nolint:gocritic // see above |
| 60 |
), |
| 61 |
) |
| 62 |
if err != nil { |
| 63 |
return err |
| 64 |
} |
| 65 |
db.db = cdb |
| 66 |
db.open = true |
| 67 |
return nil |
| 68 |
} |
| 69 |
|
| 70 |
func (db *Database) MailRoot() string { |
| 71 |
// notmuch_config_get() returns a `const char *`. No need to free(). |
| 72 |
root := C.notmuch_config_get(db.db, C.NOTMUCH_CONFIG_MAIL_ROOT) |
| 73 |
return C.GoString(root) |
| 74 |
} |
| 75 |
|
| 76 |
// Reopen an open notmuch database, usually with a different mode |
| 77 |
func (db *Database) Reopen(mode Mode) error { |
| 78 |
cmode := C.notmuch_database_mode_t(mode) |
| 79 |
return errorWrap(C.notmuch_database_reopen(db.db, cmode)) |
| 80 |
} |
| 81 |
|
| 82 |
// Close commits changes and closes the database, freeing any resources |
| 83 |
// associated with it |
| 84 |
func (db *Database) Close() error { |
| 85 |
if !db.open { |
| 86 |
return nil |
| 87 |
} |
| 88 |
err := errorWrap(C.notmuch_database_close(db.db)) |
| 89 |
if err != nil { |
| 90 |
return err |
| 91 |
} |
| 92 |
err = errorWrap(C.notmuch_database_destroy(db.db)) |
| 93 |
if err != nil { |
| 94 |
return err |
| 95 |
} |
| 96 |
db.open = false |
| 97 |
return nil |
| 98 |
} |
| 99 |
|
| 100 |
// LastStatus returns the last status string for the database |
| 101 |
func (db *Database) LastStatus() string { |
| 102 |
cStatus := C.notmuch_database_status_string(db.db) |
| 103 |
defer C.free(unsafe.Pointer(cStatus)) |
| 104 |
return C.GoString(cStatus) |
| 105 |
} |
| 106 |
|
| 107 |
func (db *Database) Compact(backupPath string) error { |
| 108 |
if backupPath == "" { |
| 109 |
return fmt.Errorf("must have backup path before compacting") |
| 110 |
} |
| 111 |
var cBackupPath *C.char |
| 112 |
defer C.free(unsafe.Pointer(cBackupPath)) |
| 113 |
return errorWrap(C.notmuch_database_compact_db(db.db, cBackupPath, nil, nil)) |
| 114 |
} |
| 115 |
|
| 116 |
// Return the resolved path to the notmuch database |
| 117 |
func (db *Database) ResolvedPath() string { |
| 118 |
cPath := C.notmuch_database_get_path(db.db) |
| 119 |
return C.GoString(cPath) |
| 120 |
} |
| 121 |
|
| 122 |
// NeedsUpgrade reports if the database must be upgraded before a write |
| 123 |
// operation can be safely performed |
| 124 |
func (db *Database) NeedsUpgrade() bool { |
| 125 |
return C.notmuch_database_needs_upgrade(db.db) == 1 |
| 126 |
} |
| 127 |
|
| 128 |
// Indicate the beginning of an atomic operation |
| 129 |
func (db *Database) BeginAtomic() error { |
| 130 |
return errorWrap(C.notmuch_database_begin_atomic(db.db)) |
| 131 |
} |
| 132 |
|
| 133 |
// Indicate the end of an atomic operation |
| 134 |
func (db *Database) EndAtomic() error { |
| 135 |
return errorWrap(C.notmuch_database_end_atomic(db.db)) |
| 136 |
} |
| 137 |
|
| 138 |
// Returns the UUID and LastMod of the notmuch database |
| 139 |
func (db *Database) Revision() (string, uint64) { |
| 140 |
var uuid *C.char |
| 141 |
defer C.free(unsafe.Pointer(uuid)) |
| 142 |
lastmod := uint64(C.notmuch_database_get_revision(db.db, &uuid)) //nolint:gocritic // see note in notmuch.go |
| 143 |
return C.GoString(uuid), lastmod |
| 144 |
} |
| 145 |
|
| 146 |
// Returns a Directory object relative to the path of the Database |
| 147 |
func (db *Database) Directory(relativePath string) (Directory, error) { |
| 148 |
var result Directory |
| 149 |
|
| 150 |
if relativePath == "" { |
| 151 |
return result, fmt.Errorf("path can't be empty") |
| 152 |
} |
| 153 |
var ( |
| 154 |
dir *C.notmuch_directory_t |
| 155 |
cPath *C.char |
| 156 |
) |
| 157 |
cPath = C.CString(relativePath) |
| 158 |
defer C.free(unsafe.Pointer(cPath)) |
| 159 |
err := errorWrap(C.notmuch_database_get_directory(db.db, cPath, &dir)) //nolint:gocritic // see note in notmuch.go |
| 160 |
if err != nil { |
| 161 |
return result, err |
| 162 |
} |
| 163 |
result.dir = dir |
| 164 |
|
| 165 |
return result, nil |
| 166 |
} |
| 167 |
|
| 168 |
// IndexFile indexes a file with path relative to the database path, or an |
| 169 |
// absolute path which share a common ancestor as the database path |
| 170 |
func (db *Database) IndexFile(path string) (Message, error) { |
| 171 |
var ( |
| 172 |
cPath *C.char |
| 173 |
msg *C.notmuch_message_t |
| 174 |
) |
| 175 |
cPath = C.CString(path) |
| 176 |
defer C.free(unsafe.Pointer(cPath)) |
| 177 |
|
| 178 |
err := errorWrap(C.notmuch_database_index_file(db.db, cPath, nil, &msg)) //nolint:gocritic // see note in notmuch.go |
| 179 |
switch { |
| 180 |
case errors.Is(err, STATUS_DUPLICATE_MESSAGE_ID): |
| 181 |
break |
| 182 |
case err != nil: |
| 183 |
return Message{}, err |
| 184 |
} |
| 185 |
message := Message{ |
| 186 |
message: msg, |
| 187 |
} |
| 188 |
return message, nil |
| 189 |
} |
| 190 |
|
| 191 |
// Remove a file from the database. If this is the last file associated with a |
| 192 |
// message, the message will be removed from the database. |
| 193 |
func (db *Database) RemoveFile(path string) error { |
| 194 |
cPath := C.CString(path) |
| 195 |
defer C.free(unsafe.Pointer(cPath)) |
| 196 |
return errorWrap(C.notmuch_database_remove_message(db.db, cPath)) |
| 197 |
} |
| 198 |
|
| 199 |
// FindMessageByID finds a message by the Message-ID header field value |
| 200 |
func (db *Database) FindMessageByID(id string) (Message, error) { |
| 201 |
var ( |
| 202 |
cID *C.char |
| 203 |
msg *C.notmuch_message_t |
| 204 |
) |
| 205 |
cID = C.CString(id) |
| 206 |
defer C.free(unsafe.Pointer(cID)) |
| 207 |
err := errorWrap(C.notmuch_database_find_message(db.db, cID, &msg)) //nolint:gocritic // see note in notmuch.go |
| 208 |
if err != nil { |
| 209 |
return Message{}, err |
| 210 |
} |
| 211 |
message := Message{ |
| 212 |
message: msg, |
| 213 |
} |
| 214 |
return message, nil |
| 215 |
} |
| 216 |
|
| 217 |
// FindMessageByFilename finds a message by filename |
| 218 |
func (db *Database) FindMessageByFilename(filename string) (Message, error) { |
| 219 |
var ( |
| 220 |
cFilename *C.char |
| 221 |
msg *C.notmuch_message_t |
| 222 |
) |
| 223 |
cFilename = C.CString(filename) |
| 224 |
defer C.free(unsafe.Pointer(cFilename)) |
| 225 |
err := errorWrap(C.notmuch_database_find_message_by_filename(db.db, cFilename, &msg)) //nolint:gocritic // see note in notmuch.go |
| 226 |
if err != nil { |
| 227 |
return Message{}, err |
| 228 |
} |
| 229 |
if msg == nil { |
| 230 |
return Message{}, fmt.Errorf("couldn't find message by filename: %s", filename) |
| 231 |
} |
| 232 |
message := Message{ |
| 233 |
message: msg, |
| 234 |
} |
| 235 |
return message, nil |
| 236 |
} |
| 237 |
|
| 238 |
// Tags returns a slice of all tags in the database |
| 239 |
func (db *Database) Tags() []string { |
| 240 |
cTags := C.notmuch_database_get_all_tags(db.db) |
| 241 |
defer C.notmuch_tags_destroy(cTags) |
| 242 |
|
| 243 |
tags := []string{} |
| 244 |
for C.notmuch_tags_valid(cTags) > 0 { |
| 245 |
tag := C.notmuch_tags_get(cTags) |
| 246 |
tags = append(tags, C.GoString(tag)) |
| 247 |
C.notmuch_tags_move_to_next(cTags) |
| 248 |
} |
| 249 |
return tags |
| 250 |
} |
| 251 |
|
| 252 |
// Create a new Query |
| 253 |
func (db *Database) Query(query string) (Query, error) { |
| 254 |
cQuery := C.CString(query) |
| 255 |
defer C.free(unsafe.Pointer(cQuery)) |
| 256 |
nmQuery := C.notmuch_query_create(db.db, cQuery) |
| 257 |
if nmQuery == nil { |
| 258 |
return Query{}, STATUS_OUT_OF_MEMORY |
| 259 |
} |
| 260 |
q := Query{ |
| 261 |
query: nmQuery, |
| 262 |
} |
| 263 |
return q, nil |
| 264 |
} |