Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cmd/bbolt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ var (

// ErrNotEnoughArgs is returned with a cmd is being executed with fewer arguments.
ErrNotEnoughArgs = errors.New("not enough arguments")

// ErrCompactSourceIsDestination is returned when running the compact command, while source
// and destination are the same database file.
ErrCompactSourceIsDestination = errors.New("source and destination are the same database file")
)

func main() {
Expand Down Expand Up @@ -1639,6 +1643,9 @@ func (cmd *compactCommand) Run(args ...string) (err error) {
} else if err != nil {
return err
}
if dstInfo, err := os.Stat(cmd.DstPath); err == nil && os.SameFile(fi, dstInfo) {
return ErrCompactSourceIsDestination
}
initialSize := fi.Size()

// Open source database.
Expand Down
66 changes: 66 additions & 0 deletions cmd/bbolt/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"math/rand"
"os"
"path/filepath"
"strconv"
"testing"

Expand Down Expand Up @@ -390,6 +391,71 @@ func TestCompactCommand_Run(t *testing.T) {
}
}

func TestCompactCommand_SourceAsDestination(t *testing.T) {
for _, tc := range []struct {
name string
// dst returns the destination path for the given source path.
dst func(t *testing.T, src string) string
}{
{
name: "same path",
dst: func(_ *testing.T, src string) string { return src },
},
{
name: "relative path",
dst: func(t *testing.T, src string) string {
t.Chdir(filepath.Dir(src))
return filepath.Base(src)
},
},
{
name: "hard link",
dst: func(t *testing.T, src string) string {
dst := filepath.Join(t.TempDir(), "db-link")
if err := os.Link(src, dst); err != nil {
t.Skipf("hard links unavailable: %v", err)
}
return dst
},
},
{
name: "symbolic link",
dst: func(t *testing.T, src string) string {
dst := filepath.Join(t.TempDir(), "db-symlink")
if err := os.Symlink(src, dst); err != nil {
t.Skipf("symbolic links unavailable: %v", err)
}
return dst
},
},
} {
t.Run(tc.name, func(t *testing.T) {
db := btesting.MustCreateDB(t)
err := db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucket([]byte("widgets"))
if err != nil {
return err
}
return b.Put([]byte("foo"), []byte("bar"))
})
require.NoError(t, err)
require.NoError(t, db.Close())

before, err := os.ReadFile(db.Path())
require.NoError(t, err)
dstPath := tc.dst(t, db.Path())

m := NewMain()
err = m.Run("compact", "-o", dstPath, db.Path())
require.ErrorIs(t, err, main.ErrCompactSourceIsDestination)

after, err := os.ReadFile(db.Path())
require.NoError(t, err)
require.True(t, bytes.Equal(before, after), "compact changed the source database")
})
}
}

func TestCommands_Run_NoArgs(t *testing.T) {
testCases := []struct {
name string
Expand Down
Loading