This repository has been archived by the owner on Oct 14, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.go
208 lines (179 loc) · 5.1 KB
/
fs.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package gormfs
import (
"io/fs"
"os"
"path/filepath"
"strings"
"time"
"github.com/pkg/errors"
"github.com/spf13/afero"
"gorm.io/gorm"
)
// FIXME: set File.Mode, File.ATime, File.User and File.Group correctly
// FIXME: handle flag correctly
type GormFs struct {
db *gorm.DB
}
func NewGormFs(db *gorm.DB) (*GormFs, error) {
if err := db.AutoMigrate(allModels...); err != nil {
return nil, errors.Wrap(err, "migrate db")
}
return &GormFs{db: db}, nil
}
var _ afero.Fs = (*GormFs)(nil)
func (f *GormFs) Chmod(name string, mode fs.FileMode) error {
file, err := getFile(f.db, name)
if err != nil {
return err
}
isDir := file.Mode&fs.ModeDir != 0
file.Mode = mode
if isDir {
file.Mode |= fs.ModeDir
}
file.MTime = time.Now()
return f.db.Save(file).Error
}
func (f *GormFs) Chown(name string, uid, gid int) error {
file, err := getFile(f.db, name)
if err != nil {
return err
}
file.User = uid
file.Group = gid
file.MTime = time.Now()
return f.db.Save(file).Error
}
func (f *GormFs) Chtimes(name string, atime time.Time, mtime time.Time) error {
file, err := getFile(f.db, name)
if err != nil {
return err
}
file.ATime = atime
file.MTime = mtime
return f.db.Save(file).Error
}
func (f *GormFs) Create(name string) (afero.File, error) {
if !f.hasParent(name) {
return nil, &fs.PathError{Op: "mkdir", Path: name, Err: fs.ErrNotExist}
}
now := time.Now()
if err := f.db.Create(&File{Name: filepath.Clean(name), ATime: now, MTime: now}).Error; err != nil {
return nil, errors.Wrap(err, "create db file")
}
return f.OpenFile(name, os.O_RDWR, os.ModePerm)
}
func (f *GormFs) Mkdir(name string, perm fs.FileMode) error {
name = filepath.Clean(name)
if f.exists(name) {
return &fs.PathError{Op: "mkdir", Path: name, Err: fs.ErrExist}
}
if !f.hasParent(name) {
return &fs.PathError{Op: "mkdir", Path: name, Err: fs.ErrNotExist}
}
if err := f.db.Create(&File{Name: name, IsDir: true, Mode: perm | fs.ModeDir}).Error; err != nil {
return err
}
return nil
}
func (f *GormFs) MkdirAll(path string, perm fs.FileMode) error {
path = filepath.Clean(path)
paths := strings.Split(path, "/") // FIXME: breaks on non-unix
if len(paths) > 0 && paths[0] == "" {
paths[0] = "/"
}
for i, elem := range paths {
name := filepath.Join(append(paths[:i], elem)...)
if f.exists(name) {
continue
}
if err := f.Mkdir(name, perm); err != nil && !os.IsExist(err) {
return err
}
}
return nil
}
func (f *GormFs) Name() string {
return "GormFs"
}
func (f *GormFs) Open(name string) (afero.File, error) {
return f.OpenFile(name, os.O_RDONLY, 0)
}
func (f *GormFs) OpenFile(name string, flag int, perm fs.FileMode) (afero.File, error) {
name = filepath.Clean(name)
if f.exists(name) {
if flag&os.O_CREATE != 0 && flag&os.O_EXCL != 0 {
return nil, &fs.PathError{Op: "openf", Path: name, Err: fs.ErrExist}
}
} else {
if flag&os.O_CREATE == 0 {
return nil, &fs.PathError{Op: "openf", Path: name, Err: fs.ErrNotExist}
}
if err := f.db.Create(&File{Name: filepath.Clean(name), Mode: perm}).Error; err != nil {
return nil, err
}
}
return newAferoFile(f.db, name, flag)
}
func (f *GormFs) Remove(name string) error {
name = filepath.Clean(name)
if !f.exists(name) {
return &fs.PathError{Op: "remove", Path: name, Err: fs.ErrNotExist}
}
return f.db.Delete(&File{Name: name}).Error
}
func (f *GormFs) RemoveAll(path string) error {
path = filepath.Clean(path)
return f.db.
Where("name LIKE ?", filepath.Join(path, "%")).Or("name = ? AND is_dir = true", path). // FIXME: support paths with %
Delete(&File{}).Error
}
func (f *GormFs) Rename(oldname, newname string) error {
oldname = filepath.Clean(oldname)
newname = filepath.Clean(newname)
if !f.exists(oldname) {
return &fs.PathError{Op: "rename", Path: oldname, Err: fs.ErrNotExist} // FIXME: error parity with os
}
oldFiles := []*File{}
if err := f.db.Where("name LIKE ?", filepath.Join(oldname, "%")).Or("name = ?", oldname).Find(&oldFiles).Error; err != nil {
return errors.Wrap(err, "find files")
}
now := time.Now()
newFiles := make([]*File, len(oldFiles))
for i := range oldFiles {
f := *oldFiles[i]
newFiles[i] = &f
fnn := strings.TrimPrefix(oldFiles[i].Name, oldname)
newFiles[i].Name = newname + fnn
newFiles[i].MTime = now
}
if err := f.db.Save(newFiles).Error; err != nil {
return errors.Wrap(err, "save files")
}
if err := f.db.Delete(oldFiles).Error; err != nil {
return errors.Wrap(err, "delete rename remains")
}
return nil
}
func (f *GormFs) Stat(name string) (fs.FileInfo, error) {
file, err := newAferoFile(f.db, name, os.O_RDONLY)
if err != nil {
return nil, err
}
return file.Stat()
}
func (f *GormFs) hasParent(name string) bool {
name = filepath.Clean(name)
parent := filepath.Dir(name)
if parent == "." || parent == "/" { // FIXME: breaks on non-unix
return true
}
return f.db.Where("name = ? AND is_dir = true", parent).Limit(1).Find(&File{}).RowsAffected != 0
}
func (f *GormFs) exists(name string) bool {
name = filepath.Clean(name)
if name == "." || name == "/" { // FIXME: breaks on non-unix
return true
}
return f.db.Where("name = ?", name).Limit(1).Find(&File{}).RowsAffected != 0
}