aboutsummaryrefslogtreecommitdiffstats
path: root/lib/notmuch/database.go
blob: 046d5d187fd4b767005f602be8e77fdafad84c9f (plain) (blame)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//go:build notmuch
// +build notmuch

package notmuch

/*
#cgo LDFLAGS: -lnotmuch

#include <stdlib.h>
#include <notmuch.h>

*/
import "C"

import (
	"errors"
	"fmt"
	"unsafe"
)

type Mode int

const (
	MODE_READ_ONLY  Mode = C.NOTMUCH_DATABASE_MODE_READ_ONLY
	MODE_READ_WRITE Mode = C.NOTMUCH_DATABASE_MODE_READ_WRITE
)

type Database struct {
	// The path to the notmuch database. If Path is the empty string, the
	// location will be found in the following order:
	//
	// 1. The value of the environment variable NOTMUCH_DATABASE
	// 2. From the config file specified by Config
	// 3. From the Profile specified by profile, given by
	//    $XDG_DATA_HOME/notmuch/$PROFILE
	Path string

	// The path to the notmuch configuration file to use.
	Config string

	// If FindConfig is true, libnotmuch will attempt to locate a suitable
	// configuration file in the following order:
	//
	// 1. The value of the environment variable NOTMUCH_CONFIG
	// 2. $XDG_CONFIG_HOME/notmuch/
	// 3. $HOME/.notmuch-config
	//
	// If not configuration file is found, a STATUS_NO_CONFIG error will be
	// returned
	FindConfig bool

	// The profile to use. If Profile is non-empty, the value will be
	// appended to the paths determined for Config and Path. If Profile is
	// the empty string, the profile will be determined in the following
	// order:
	//
	// 1. The value of the environment variable NOTMUCH_PROFILE
	// 2. "default" if Config and/or Path are a directory, "" if they are a
	//    filepath
	Profile string

	db   *C.notmuch_database_t
	open bool
}

// Create creates a notmuch database at the Path
func (db *Database) Create() error {
	var cdb *C.notmuch_database_t
	var cPath *C.char
	defer C.free(unsafe.Pointer(cPath))
	if db.Path != "" {
		cPath = C.CString(db.Path)
	}
	err := errorWrap(C.notmuch_database_create(cPath, &cdb)) //nolint:gocritic // see note in notmuch.go
	if err != nil {
		return err
	}
	db.db = cdb
	return nil
}

// Open opens the database with the given mode. Caller must call Close when done
// to commit changes and free resources
func (db *Database) Open(mode Mode) error {
	var (
		cPath    *C.char
		cConfig  *C.char
		cProfile *C.char
		cErr     *C.char
	)
	defer C.free(unsafe.Pointer(cPath))
	defer C.free(unsafe.Pointer(cConfig))
	defer C.free(unsafe.Pointer(cProfile))
	defer C.free(unsafe.Pointer(cErr))

	if db.Path != "" {
		cPath = C.CString(db.Path)
	}

	if !db.FindConfig {
		cConfig = C.CString(db.Config)
	}

	if db.Profile != "" {
		cProfile = C.CString(db.Profile)
	}
	cmode := C.notmuch_database_mode_t(mode)

	var cdb *C.notmuch_database_t

	// gocritic:dupSubExpr throws an issue here no matter how we call this
	// function
	err := errorWrap(
		C.notmuch_database_open_with_config(
			cPath, cmode, cConfig, cProfile, &cdb, &cErr, //nolint:gocritic // see above
		),
	)
	if err != nil {
		return err
	}
	db.db = cdb
	db.open = true
	return nil
}

// Reopen an open notmuch database, usually with a different mode
func (db *Database) Reopen(mode Mode) error {
	cmode := C.notmuch_database_mode_t(mode)
	return errorWrap(C.notmuch_database_reopen(db.db, cmode))
}

// Close commits changes and closes the database, freeing any resources
// associated with it
func (db *Database) Close() error {
	if !db.open {
		return nil
	}
	err := errorWrap(C.notmuch_database_close(db.db))
	if err != nil {
		return err
	}
	err = errorWrap(C.notmuch_database_destroy(db.db))
	if err != nil {
		return err
	}
	db.open = false
	return nil
}

// LastStatus returns the last status string for the database
func (db *Database) LastStatus() string {
	cStatus := C.notmuch_database_status_string(db.db)
	defer C.free(unsafe.Pointer(cStatus))
	return C.GoString(cStatus)
}

func (db *Database) Compact(backupPath string) error {
	if backupPath == "" {
		return fmt.Errorf("must have backup path before compacting")
	}
	var cBackupPath *C.char
	defer C.free(unsafe.Pointer(cBackupPath))
	return errorWrap(C.notmuch_database_compact_db(db.db, cBackupPath, nil, nil))
}

// Return the resolved path to the notmuch database
func (db *Database) ResolvedPath() string {
	cPath := C.notmuch_database_get_path(db.db)
	return C.GoString(cPath)
}

// NeedsUpgrade reports if the database must be upgraded before a write
// operation can be safely performed
func (db *Database) NeedsUpgrade() bool {
	return C.notmuch_database_needs_upgrade(db.db) == 1
}

// Indicate the beginning of an atomic operation
func (db *Database) BeginAtomic() error {
	return errorWrap(C.notmuch_database_begin_atomic(db.db))
}

// Indicate the end of an atomic operation
func (db *Database) EndAtomic() error {
	return errorWrap(C.notmuch_database_end_atomic(db.db))
}

// Returns the UUID and LastMod of the notmuch database
func (db *Database) Revision() (string, uint64) {
	var uuid *C.char
	defer C.free(unsafe.Pointer(uuid))
	lastmod := uint64(C.notmuch_database_get_revision(db.db, &uuid)) //nolint:gocritic // see note in notmuch.go
	return C.GoString(uuid), lastmod
}

// Returns a Directory object relative to the path of the Database
func (db *Database) Directory(relativePath string) (Directory, error) {
	var result Directory

	if relativePath == "" {
		return result, fmt.Errorf("path can't be empty")
	}
	var (
		dir   *C.notmuch_directory_t
		cPath *C.char
	)
	cPath = C.CString(relativePath)
	defer C.free(unsafe.Pointer(cPath))
	err := errorWrap(C.notmuch_database_get_directory(db.db, cPath, &dir)) //nolint:gocritic // see note in notmuch.go
	if err != nil {
		return result, err
	}
	result.dir = dir

	return result, nil
}

// IndexFile indexes a file with path relative to the database path, or an
// absolute path which share a common ancestor as the database path
func (db *Database) IndexFile(path string) (Message, error) {
	var (
		cPath *C.char
		msg   *C.notmuch_message_t
	)
	cPath = C.CString(path)
	defer C.free(unsafe.Pointer(cPath))

	err := errorWrap(C.notmuch_database_index_file(db.db, cPath, nil, &msg)) //nolint:gocritic // see note in notmuch.go
	switch {
	case errors.Is(err, STATUS_DUPLICATE_MESSAGE_ID):
		break
	case err != nil:
		return Message{}, err
	}
	message := Message{
		message: msg,
	}
	return message, nil
}

// Remove a file from the database. If this is the last file associated with a
// message, the message will be removed from the database.
func (db *Database) RemoveFile(path string) error {
	cPath := C.CString(path)
	defer C.free(unsafe.Pointer(cPath))
	return errorWrap(C.notmuch_database_remove_message(db.db, cPath))
}

// FindMessageByID finds a message by the Message-ID header field value
func (db *Database) FindMessageByID(id string) (Message, error) {
	var (
		cID *C.char
		msg *C.notmuch_message_t
	)
	cID = C.CString(id)
	defer C.free(unsafe.Pointer(cID))
	err := errorWrap(C.notmuch_database_find_message(db.db, cID, &msg)) //nolint:gocritic // see note in notmuch.go
	if err != nil {
		return Message{}, err
	}
	message := Message{
		message: msg,
	}
	return message, nil
}

// FindMessageByFilename finds a message by filename
func (db *Database) FindMessageByFilename(filename string) (Message, error) {
	var (
		cFilename *C.char
		msg       *C.notmuch_message_t
	)
	cFilename = C.CString(filename)
	defer C.free(unsafe.Pointer(cFilename))
	err := errorWrap(C.notmuch_database_find_message_by_filename(db.db, cFilename, &msg)) //nolint:gocritic // see note in notmuch.go
	if err != nil {
		return Message{}, err
	}
	if msg == nil {
		return Message{}, fmt.Errorf("couldn't find message by filename: %s", filename)
	}
	message := Message{
		message: msg,
	}
	return message, nil
}

// Tags returns a slice of all tags in the database
func (db *Database) Tags() []string {
	cTags := C.notmuch_database_get_all_tags(db.db)
	defer C.notmuch_tags_destroy(cTags)

	tags := []string{}
	for C.notmuch_tags_valid(cTags) > 0 {
		tag := C.notmuch_tags_get(cTags)
		tags = append(tags, C.GoString(tag))
		C.notmuch_tags_move_to_next(cTags)
	}
	return tags
}

// Create a new Query
func (db *Database) Query(query string) (Query, error) {
	cQuery := C.CString(query)
	defer C.free(unsafe.Pointer(cQuery))
	nmQuery := C.notmuch_query_create(db.db, cQuery)
	if nmQuery == nil {
		return Query{}, STATUS_OUT_OF_MEMORY
	}
	q := Query{
		query: nmQuery,
	}
	return q, nil
}