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

@@ -1,111 +1,103 @@
package encfs
type EncFile struct {}
import (
"os"
"syscall"
func (f *File) Close() error {
"github.com/spf13/afero"
)
type EncFile struct {
isDir bool
closed bool
file *os.File
}
func NewEncFile(file *os.File) (*EncFile, error) {
fileInfo, err := file.Stat()
if err != nil {
return nil, err
}
isDir := fileInfo.IsDir()
return &EncFile{
isDir: isDir,
closed: false,
file: file,
}, nil
}
func (f *EncFile) Close() error {
if f.closed {
return afero.ErrFileClosed
}
f.closed = true
f.h = nil
f.data = nil
f.fs = nil
return nil
return f.file.Close()
}
func (f *File) Read(p []byte) (n int, err error) {
if f.closed {
return 0, afero.ErrFileClosed
func (f *EncFile) Read(p []byte) (n int, err error) {
checkIsFileErr := f.checkIsFile()
if checkIsFileErr != nil {
return 0, checkIsFileErr
}
if f.h.Typeflag == tar.TypeDir {
return 0, syscall.EISDIR
}
return f.data.Read(p)
// TODO decrypt
return f.file.Read(p)
}
func (f *File) ReadAt(p []byte, off int64) (n int, err error) {
if f.closed {
return 0, afero.ErrFileClosed
func (f *EncFile) ReadAt(p []byte, off int64) (n int, err error) {
checkIsFileErr := f.checkIsFile()
if checkIsFileErr != nil {
return 0, checkIsFileErr
}
if f.h.Typeflag == tar.TypeDir {
return 0, syscall.EISDIR
}
return f.data.ReadAt(p, off)
// TODO decrypt
return f.file.ReadAt(p, off)
}
func (f *File) Seek(offset int64, whence int) (int64, error) {
if f.closed {
return 0, afero.ErrFileClosed
func (f *EncFile) Seek(offset int64, whence int) (int64, error) {
checkIsFileErr := f.checkIsFile()
if checkIsFileErr != nil {
return 0, checkIsFileErr
}
if f.h.Typeflag == tar.TypeDir {
return 0, syscall.EISDIR
}
return f.data.Seek(offset, whence)
// TODO decrypt
return f.file.Seek(offset, whence)
}
func (f *File) Write(p []byte) (n int, err error) { return 0, syscall.EROFS }
func (f *File) WriteAt(p []byte, off int64) (n int, err error) { return 0, syscall.EROFS }
func (f *File) Name() string {
return filepath.Join(splitpath(f.h.Name))
}
func (f *File) getDirectoryNames() ([]string, error) {
d, ok := f.fs.files[f.Name()]
if !ok {
return nil, &os.PathError{Op: "readdir", Path: f.Name(), Err: syscall.ENOENT}
func (f *EncFile) Write(p []byte) (n int, err error) {
checkIsFileErr := f.checkIsFile()
if checkIsFileErr != nil {
return 0, checkIsFileErr
}
var names []string
for n := range d {
names = append(names, n)
}
sort.Strings(names)
return names, nil
// TODO encrypt
return f.file.Write(p)
}
func (f *File) Readdir(count int) ([]os.FileInfo, error) {
func (f *EncFile) WriteAt(p []byte, off int64) (n int, err error) {
checkIsFileErr := f.checkIsFile()
if checkIsFileErr != nil {
return 0, checkIsFileErr
}
// TODO encrypt
return f.file.WriteAt(p, off)
}
func (f *EncFile) Name() string {
return f.file.Name()
}
func (f *EncFile) Readdir(count int) ([]os.FileInfo, error) {
if f.closed {
return nil, afero.ErrFileClosed
}
if !f.h.FileInfo().IsDir() {
return nil, syscall.ENOTDIR
}
names, err := f.getDirectoryNames()
if err != nil {
return nil, err
}
d := f.fs.files[f.Name()]
var fi []os.FileInfo
for _, n := range names {
if n == "" {
continue
}
f := d[n]
fi = append(fi, f.h.FileInfo())
if count > 0 && len(fi) >= count {
break
}
}
return fi, nil
return f.file.Readdir(count)
}
func (f *File) Readdirnames(n int) ([]string, error) {
func (f *EncFile) Readdirnames(n int) ([]string, error) {
fi, err := f.Readdir(n)
if err != nil {
return nil, err
@@ -119,10 +111,29 @@ func (f *File) Readdirnames(n int) ([]string, error) {
return names, nil
}
func (f *File) Stat() (os.FileInfo, error) { return f.h.FileInfo(), nil }
func (f *EncFile) Stat() (os.FileInfo, error) {
return f.file.Stat()
}
func (f *File) Sync() error { return nil }
func (f *EncFile) Sync() error {
return f.file.Sync()
}
func (f *File) Truncate(size int64) error { return syscall.EROFS }
func (f *EncFile) Truncate(size int64) error {
return f.file.Truncate(size)
}
func (f *File) WriteString(s string) (ret int, err error) { return 0, syscall.EROFS }
func (f *EncFile) WriteString(s string) (ret int, err error) {
// TODO encrypt
return f.file.WriteString(s)
}
func (f *EncFile) checkIsFile() error {
if f.closed {
return afero.ErrFileClosed
}
if f.isDir {
return syscall.EISDIR
}
return nil
}

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
}