feat: udpates

This commit is contained in:
2024-09-01 15:13:18 +08:00
parent 1971f37a9d
commit 84c0e2e872
4 changed files with 130 additions and 12 deletions

View File

@@ -2,6 +2,7 @@ package encfs
import (
"os"
"strings"
"time"
"github.com/spf13/afero"
@@ -18,13 +19,16 @@ func NewEncFs() afero.Fs {
func (EncFs) Name() string { return "EncFs" }
func (EncFs) Create(name string) (afero.File, error) {
if err := checkFileExt(name); err != nil {
return nil, err
}
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 convertOsFileToEncFile(f, e)
return convertOsFileToEncFile(name, f, e, true)
}
func (EncFs) Mkdir(name string, perm os.FileMode) error {
@@ -36,13 +40,16 @@ func (EncFs) MkdirAll(path string, perm os.FileMode) error {
}
func (EncFs) Open(name string) (afero.File, error) {
if err := checkFileExt(name); err != nil {
return nil, err
}
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 convertOsFileToEncFile(f, e)
return convertOsFileToEncFile(name, f, e, false)
}
func (EncFs) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error) {
@@ -99,12 +106,19 @@ func (EncFs) ReadlinkIfPossible(name string) (string, error) {
return os.Readlink(name)
}
func convertOsFileToEncFile(file *os.File, e error) (afero.File, error) {
func checkFileExt(name string) error {
if strings.HasSuffix(name, EncFileExt) {
return ErrFileForbiddenFileExt
}
return nil
}
func convertOsFileToEncFile(name string, file *os.File, e error, isCreate bool) (afero.File, error) {
if e != nil {
return nil, e
}
// TODO add password, and IV etc ...
encFile, err := NewEncFile(file)
encFile, err := NewEncFile(name, file, isCreate)
if err != nil {
return nil, err
}