feat: init commit

This commit is contained in:
2024-08-31 15:12:24 +08:00
parent 131f10d8ba
commit ed021b554e
7 changed files with 276 additions and 0 deletions

128
encfs/file.go Normal file
View File

@@ -0,0 +1,128 @@
package encfs
type EncFile struct {}
func (f *File) Close() error {
if f.closed {
return afero.ErrFileClosed
}
f.closed = true
f.h = nil
f.data = nil
f.fs = nil
return nil
}
func (f *File) Read(p []byte) (n int, err error) {
if f.closed {
return 0, afero.ErrFileClosed
}
if f.h.Typeflag == tar.TypeDir {
return 0, syscall.EISDIR
}
return f.data.Read(p)
}
func (f *File) ReadAt(p []byte, off int64) (n int, err error) {
if f.closed {
return 0, afero.ErrFileClosed
}
if f.h.Typeflag == tar.TypeDir {
return 0, syscall.EISDIR
}
return f.data.ReadAt(p, off)
}
func (f *File) Seek(offset int64, whence int) (int64, error) {
if f.closed {
return 0, afero.ErrFileClosed
}
if f.h.Typeflag == tar.TypeDir {
return 0, syscall.EISDIR
}
return f.data.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}
}
var names []string
for n := range d {
names = append(names, n)
}
sort.Strings(names)
return names, nil
}
func (f *File) 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
}
func (f *File) Readdirnames(n int) ([]string, error) {
fi, err := f.Readdir(n)
if err != nil {
return nil, err
}
var names []string
for _, f := range fi {
names = append(names, f.Name())
}
return names, nil
}
func (f *File) Stat() (os.FileInfo, error) { return f.h.FileInfo(), nil }
func (f *File) Sync() error { return nil }
func (f *File) Truncate(size int64) error { return syscall.EROFS }
func (f *File) WriteString(s string) (ret int, err error) { return 0, syscall.EROFS }

95
encfs/fs.go Normal file
View File

@@ -0,0 +1,95 @@
package encfs
import (
"os"
"time"
)
// copied from afero/os.go
type EncFs struct{}
func NewEncFs() Fs {
return &EncFs{}
}
func (EncFs) Name() string { return "EncFs" }
func (EncFs) Create(name string) (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
}
func (EncFs) Mkdir(name string, perm os.FileMode) error {
return os.Mkdir(name, perm)
}
func (EncFs) MkdirAll(path string, perm os.FileMode) error {
return os.MkdirAll(path, perm)
}
func (EncFs) Open(name string) (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
}
func (EncFs) OpenFile(name string, flag int, perm os.FileMode) (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
// a nil value of type *os.File or nil won't be nil
return nil, e
}
return f, e
}
func (EncFs) Remove(name string) error {
return os.Remove(name)
}
func (EncFs) RemoveAll(path string) error {
return os.RemoveAll(path)
}
func (EncFs) Rename(oldname, newname string) error {
return os.Rename(oldname, newname)
}
func (EncFs) Stat(name string) (os.FileInfo, error) {
return os.Stat(name)
}
func (EncFs) Chmod(name string, mode os.FileMode) error {
return os.Chmod(name, mode)
}
func (EncFs) Chown(name string, uid, gid int) error {
return os.Chown(name, uid, gid)
}
func (EncFs) Chtimes(name string, atime time.Time, mtime time.Time) error {
return os.Chtimes(name, atime, mtime)
}
func (EncFs) LstatIfPossible(name string) (os.FileInfo, bool, error) {
fi, err := os.Lstat(name)
return fi, true, err
}
func (EncFs) SymlinkIfPossible(oldname, newname string) error {
return os.Symlink(oldname, newname)
}
func (EncFs) ReadlinkIfPossible(name string) (string, error) {
return os.Readlink(name)
}