-
-
Notifications
You must be signed in to change notification settings - Fork 480
/
store.go
190 lines (167 loc) · 4.61 KB
/
store.go
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
// Package fs implement a password-store compatible on disk storage layout
// with unencrypted paths.
package fs
import (
"context"
"fmt"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
"github.com/gopasspw/gopass/pkg/debug"
"github.com/gopasspw/gopass/pkg/fsutil"
"github.com/blang/semver/v4"
)
// Store is a fs based store
type Store struct {
path string
}
// New creates a new store
func New(dir string) *Store {
if d, err := filepath.EvalSymlinks(dir); err == nil {
dir = d
}
return &Store{
path: dir,
}
}
// Get retrieves the named content
func (s *Store) Get(ctx context.Context, name string) ([]byte, error) {
if runtime.GOOS == "windows" {
name = filepath.FromSlash(name)
}
path := filepath.Join(s.path, filepath.Clean(name))
debug.Log("Reading %s from %s", name, path)
return os.ReadFile(path)
}
// Set writes the given content
func (s *Store) Set(ctx context.Context, name string, value []byte) error {
if runtime.GOOS == "windows" {
name = filepath.FromSlash(name)
}
filename := filepath.Join(s.path, filepath.Clean(name))
filedir := filepath.Dir(filename)
if !fsutil.IsDir(filedir) {
if err := os.MkdirAll(filedir, 0700); err != nil {
return err
}
}
debug.Log("Writing %s to %s", name, filepath.Join(s.path, name))
return os.WriteFile(filepath.Join(s.path, name), value, 0644)
}
// Delete removes the named entity
func (s *Store) Delete(ctx context.Context, name string) error {
if runtime.GOOS == "windows" {
name = filepath.FromSlash(name)
}
path := filepath.Join(s.path, filepath.Clean(name))
debug.Log("Deleting %s from %s", name, path)
if err := os.Remove(path); err != nil {
return err
}
return s.removeEmptyParentDirectories(path)
}
// Deletes all empty parent directories up to the store root
func (s *Store) removeEmptyParentDirectories(path string) error {
if runtime.GOOS == "windows" {
path = filepath.FromSlash(path)
}
parent := filepath.Dir(path)
if relpath, err := filepath.Rel(s.path, parent); err != nil {
return err
} else if strings.HasPrefix(relpath, ".") {
return nil
}
debug.Log("removing empty parent dir: %q", parent)
err := os.Remove(parent)
switch {
case err == nil:
return s.removeEmptyParentDirectories(parent)
case notEmptyErr(err):
// ignore when directory is non-empty
return nil
default:
return err
}
}
// Exists checks if the named entity exists
func (s *Store) Exists(ctx context.Context, name string) bool {
if runtime.GOOS == "windows" {
name = filepath.FromSlash(name)
}
path := filepath.Join(s.path, filepath.Clean(name))
found := fsutil.IsFile(path)
debug.Log("Checking if %s exists at %s: %t", name, path, found)
return found
}
// List returns a list of all entities
// e.g. foo, far/bar baz/.bang
// directory separator are normalized using `/`
func (s *Store) List(ctx context.Context, prefix string) ([]string, error) {
prefix = strings.TrimPrefix(prefix, "/")
debug.Log("Listing %s", prefix)
files := make([]string, 0, 100)
if err := filepath.Walk(s.path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() && strings.HasPrefix(info.Name(), ".") && path != s.path {
return filepath.SkipDir
}
if info.IsDir() {
return nil
}
if path == s.path {
return nil
}
name := strings.TrimPrefix(path, s.path+string(filepath.Separator))
if runtime.GOOS == "windows" {
name = filepath.ToSlash(name)
}
if !strings.HasPrefix(name, prefix) {
return nil
}
files = append(files, name)
return nil
}); err != nil {
return nil, err
}
sort.Strings(files)
return files, nil
}
// IsDir returns true if the named entity is a directory
func (s *Store) IsDir(ctx context.Context, name string) bool {
if runtime.GOOS == "windows" {
name = filepath.FromSlash(name)
}
path := filepath.Join(s.path, filepath.Clean(name))
isDir := fsutil.IsDir(path)
debug.Log("%s at %s is a directory? %t", name, path, isDir)
return isDir
}
// Prune removes a named directory
func (s *Store) Prune(ctx context.Context, prefix string) error {
path := filepath.Join(s.path, filepath.Clean(prefix))
debug.Log("Purning %s from %s", prefix, path)
if err := os.RemoveAll(path); err != nil {
return err
}
return s.removeEmptyParentDirectories(path)
}
// Name returns the name of this backend
func (s *Store) Name() string {
return "fs"
}
// Version returns the version of this backend
func (s *Store) Version(context.Context) semver.Version {
return semver.Version{Minor: 1}
}
// String implements fmt.Stringer
func (s *Store) String() string {
return fmt.Sprintf("fs(v0.1.0,path:%s)", s.path)
}
// Path returns the path to this storage
func (s *Store) Path() string {
return s.path
}