feat: still working

This commit is contained in:
2024-09-01 14:20:27 +08:00
parent 02584eee70
commit 1971f37a9d
5 changed files with 166 additions and 101 deletions

View File

@@ -3,26 +3,28 @@ package encfs
import (
"os"
"time"
"github.com/spf13/afero"
)
// copied from afero/os.go
type EncFs struct{}
func NewEncFs() Fs {
func NewEncFs() afero.Fs {
return &EncFs{}
}
func (EncFs) Name() string { return "EncFs" }
func (EncFs) Create(name string) (File, error) {
func (EncFs) Create(name string) (afero.File, error) {
f, e := os.Create(name)
if f == nil {
// while this looks strange, we need to return a bare nil (of type nil) not
// a nil value of type *os.File or nil won't be nil
return nil, e
}
return f, e
return convertOsFileToEncFile(f, e)
}
func (EncFs) Mkdir(name string, perm os.FileMode) error {
@@ -33,17 +35,17 @@ func (EncFs) MkdirAll(path string, perm os.FileMode) error {
return os.MkdirAll(path, perm)
}
func (EncFs) Open(name string) (File, error) {
func (EncFs) Open(name string) (afero.File, error) {
f, e := os.Open(name)
if f == nil {
// while this looks strange, we need to return a bare nil (of type nil) not
// a nil value of type *os.File or nil won't be nil
return nil, e
}
return f, e
return convertOsFileToEncFile(f, e)
}
func (EncFs) OpenFile(name string, flag int, perm os.FileMode) (File, error) {
func (EncFs) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error) {
f, e := os.OpenFile(name, flag, perm)
if f == nil {
// while this looks strange, we need to return a bare nil (of type nil) not
@@ -54,14 +56,17 @@ func (EncFs) OpenFile(name string, flag int, perm os.FileMode) (File, error) {
}
func (EncFs) Remove(name string) error {
// TODO remove enc file meta
return os.Remove(name)
}
func (EncFs) RemoveAll(path string) error {
// TODO remove enc file meta
return os.RemoveAll(path)
}
func (EncFs) Rename(oldname, newname string) error {
// TODO rename enc file meta
return os.Rename(oldname, newname)
}
@@ -93,3 +98,15 @@ func (EncFs) SymlinkIfPossible(oldname, newname string) error {
func (EncFs) ReadlinkIfPossible(name string) (string, error) {
return os.Readlink(name)
}
func convertOsFileToEncFile(file *os.File, e error) (afero.File, error) {
if e != nil {
return nil, e
}
// TODO add password, and IV etc ...
encFile, err := NewEncFile(file)
if err != nil {
return nil, err
}
return encFile, nil
}