-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cli: support encrypted fs with pebble debug tool
Release note (cli change): The `cockroach debug pebble` tool can now be used with encrypted stores.
- Loading branch information
Andy Yang
committed
May 17, 2021
1 parent
dd1bf20
commit ef4c259
Showing
8 changed files
with
271 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
// Copyright 2021 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package cli | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/cockroachdb/pebble/vfs" | ||
) | ||
|
||
// absoluteFS is a wrapper vfs.fs that converts filepath names to absolute paths | ||
// before calling the underlying interface implementation for some functions. | ||
// | ||
// This is needed when using encryptedFS, since the PebbleFileRegistry used in | ||
// that context attempts to convert function input paths to relative paths using | ||
// the DBDir. Both the DBDir and function input paths in a CockroachDB node are | ||
// absolute paths, but when using the Pebble tool, the function input paths are | ||
// based on what the cli user passed to the pebble command. We do not wish for a | ||
// user using the cli to remember to pass an absolute path to the various pebble | ||
// tool commands that accept paths. Note that the pebble tool commands taking a | ||
// path parameter are quite varied: ranging from "pebble db" to "pebble lsm", | ||
// so it is simplest to intercept the function input paths here. | ||
// | ||
// Note that absoluteFS assumes the wrapped vfs.FS corresponds to the underlying | ||
// OS filesystem and will not work for the general case of a vfs.FS. | ||
// This limitation is acceptable for this tool's use cases. | ||
type absoluteFS struct { | ||
fs vfs.FS | ||
} | ||
|
||
var _ vfs.FS = &absoluteFS{} | ||
|
||
func (fs *absoluteFS) Create(name string) (vfs.File, error) { | ||
return wrapWithAbsolute1(fs.fs.Create, name) | ||
} | ||
|
||
func (fs *absoluteFS) Link(oldname, newname string) error { | ||
return wrapWithAbsolute3(fs.fs.Link, oldname, newname) | ||
} | ||
|
||
func (fs *absoluteFS) Open(name string, opts ...vfs.OpenOption) (vfs.File, error) { | ||
return wrapWithAbsolute2(fs.fs.Open, name, opts...) | ||
} | ||
|
||
func (fs *absoluteFS) OpenDir(name string) (vfs.File, error) { | ||
return fs.fs.OpenDir(name) | ||
} | ||
|
||
func (fs *absoluteFS) Remove(name string) error { | ||
return wrapWithAbsolute0(fs.fs.Remove, name) | ||
} | ||
|
||
func (fs *absoluteFS) RemoveAll(name string) error { | ||
return fs.fs.RemoveAll(name) | ||
} | ||
|
||
func (fs *absoluteFS) Rename(oldname, newname string) error { | ||
return wrapWithAbsolute3(fs.fs.Rename, oldname, newname) | ||
} | ||
|
||
func (fs *absoluteFS) ReuseForWrite(oldname, newname string) (vfs.File, error) { | ||
return wrapWithAbsolute4(fs.fs.ReuseForWrite, oldname, newname) | ||
} | ||
|
||
func (fs *absoluteFS) MkdirAll(dir string, perm os.FileMode) error { | ||
return fs.fs.MkdirAll(dir, perm) | ||
} | ||
|
||
func (fs *absoluteFS) Lock(name string) (io.Closer, error) { | ||
return fs.fs.Lock(name) | ||
} | ||
|
||
func (fs *absoluteFS) List(dir string) ([]string, error) { | ||
return fs.fs.List(dir) | ||
} | ||
|
||
func (fs *absoluteFS) Stat(name string) (os.FileInfo, error) { | ||
return fs.fs.Stat(name) | ||
} | ||
|
||
func (fs *absoluteFS) PathBase(path string) string { | ||
return fs.fs.PathBase(path) | ||
} | ||
|
||
func (fs *absoluteFS) PathJoin(elem ...string) string { | ||
return fs.fs.PathJoin(elem...) | ||
} | ||
|
||
func (fs *absoluteFS) PathDir(path string) string { | ||
return fs.fs.PathDir(path) | ||
} | ||
|
||
func (fs *absoluteFS) GetFreeSpace(path string) (uint64, error) { | ||
return fs.fs.GetFreeSpace(path) | ||
} | ||
|
||
func wrapWithAbsolute0(fn func(string) error, name string) error { | ||
name, err := filepath.Abs(name) | ||
if err != nil { | ||
return err | ||
} | ||
return fn(name) | ||
} | ||
|
||
func wrapWithAbsolute1(fn func(string) (vfs.File, error), name string) (vfs.File, error) { | ||
name, err := filepath.Abs(name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return fn(name) | ||
} | ||
|
||
func wrapWithAbsolute2( | ||
fn func(string, ...vfs.OpenOption) (vfs.File, error), name string, opts ...vfs.OpenOption, | ||
) (vfs.File, error) { | ||
name, err := filepath.Abs(name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return fn(name, opts...) | ||
} | ||
|
||
func wrapWithAbsolute3(fn func(string, string) error, oldname, newname string) error { | ||
oldname, err := filepath.Abs(oldname) | ||
if err != nil { | ||
return err | ||
} | ||
newname, err = filepath.Abs(newname) | ||
if err != nil { | ||
return err | ||
} | ||
return fn(oldname, newname) | ||
} | ||
|
||
func wrapWithAbsolute4( | ||
fn func(string, string) (vfs.File, error), oldname, newname string, | ||
) (vfs.File, error) { | ||
oldname, err := filepath.Abs(oldname) | ||
if err != nil { | ||
return nil, err | ||
} | ||
newname, err = filepath.Abs(newname) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return fn(oldname, newname) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2021 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package cli | ||
|
||
import "github.com/cockroachdb/pebble/vfs" | ||
|
||
// swappableFS is a vfs.FS that can be swapped out at a future time. | ||
type swappableFS struct { | ||
vfs.FS | ||
} | ||
|
||
// set replaces the FS in a swappableFS. | ||
func (s *swappableFS) set(fs vfs.FS) { | ||
s.FS = fs | ||
} |