From 51faab0a3555d3ee53c2b64e83db12e283c2de5e Mon Sep 17 00:00:00 2001 From: Jackson Owens Date: Tue, 12 Mar 2024 12:46:52 -0400 Subject: [PATCH] tool: add DirectoryLock option Add a new DirectoryLock option so that users of the tool package's programmatic interface may pass an already-acquired directory lock to Pebble. In CockroachDB, we acquire the directory lock before opening Open in order to prevent concurrent modification of the encryption-at-rest state. --- tool/tool.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tool/tool.go b/tool/tool.go index 4895b763d2..df046d62ea 100644 --- a/tool/tool.go +++ b/tool/tool.go @@ -82,6 +82,18 @@ func Filters(filters ...FilterPolicy) Option { } } +// DirectoryLock may be passed to New to pass an existing file lock within a +// data directory. Some tool options open the database and will acquire the +// directory's file lock if not already held. If the caller has already acquired +// the file lock, they may pass it here. When the tool is embedded within the +// CockroachDB CLI, we do this to ensure the lock provides mutual exclusion over +// the persistent encryption-at-rest state. +func DirectoryLock(lock *pebble.Lock) Option { + return func(t *T) { + t.opts.Lock = lock + } +} + // FS sets the filesystem implementation to use by the introspection tools. func FS(fs vfs.FS) Option { return func(t *T) {