Skip to content

Commit

Permalink
feat: make filevault dir index in reverse mode
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalvas committed Mar 30, 2024
1 parent 7e940f5 commit a6ea840
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pkg/vault/filevault/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func (v *Vault) ListKeys() ([][]byte, error) {
fileList, err := filepath.Glob(filepath.Join(v.storagePath, "*", "*", "*", fmt.Sprintf("*%s", fileExtension)))
fileList, err := filepath.Glob(filepath.Join(v.storagePath, "*", "*", fmt.Sprintf("*%s", fileExtension)))
if err != nil {
return nil, fmt.Errorf("failed to list files: %w", err)
}
Expand Down
9 changes: 8 additions & 1 deletion pkg/vault/filevault/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/hex"
"fmt"
"path/filepath"
"slices"
)

const (
Expand All @@ -13,7 +14,13 @@ const (
func getKeyPath(key []byte) (string, string) {
fileName := hex.EncodeToString(key)

fileDir := filepath.Join(fileName[0:2], fileName[2:4], fileName[4:6])
keyReverse := make([]byte, len(key))
copy(keyReverse, key)
slices.Reverse(keyReverse)

fileNameReverse := hex.EncodeToString(keyReverse)

fileDir := filepath.Join(fileNameReverse[0:2], fileNameReverse[2:4])
filePath := filepath.Join(fileDir, fileName)

return fmt.Sprintf("%s%s", filePath, fileExtension), fileDir
Expand Down
4 changes: 2 additions & 2 deletions pkg/vault/filevault/tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import "testing"

func TestGetKeyPath(t *testing.T) {
key := []byte{0x01, 0x02, 0x03, 0x04}
expectedPath := "01/02/03/01020304.txt"
expectedDir := "01/02/03"
expectedPath := "04/03/01020304.txt"
expectedDir := "04/03"

filePath, fileDir := getKeyPath(key)
if filePath != expectedPath {
Expand Down

0 comments on commit a6ea840

Please sign in to comment.