feat: Start implementation of testing FTP server

This commit is contained in:
Felicitas Pojtinger
2021-12-19 01:12:57 +01:00
parent 592a90d1d4
commit 2f3d2dcc57
6 changed files with 374 additions and 25 deletions
+43 -2
View File
@@ -2,6 +2,7 @@ package fs
import (
"bytes"
"errors"
"io"
"io/fs"
"log"
@@ -16,6 +17,10 @@ import (
"github.com/spf13/afero"
)
var (
ErrIsDirectory = errors.New("is a directory")
)
type File struct {
afero.File
@@ -43,7 +48,7 @@ func NewFile(
path string,
name string,
stat os.FileInfo,
info os.FileInfo,
onHeader func(hdr *models.Header),
) *File {
@@ -55,7 +60,7 @@ func NewFile(
path: path,
name: name,
info: stat,
info: info,
onHeader: onHeader,
}
@@ -115,24 +120,40 @@ func (f *File) Readdirnames(n int) ([]string, error) {
func (f *File) Sync() error {
log.Println("File.Sync", f.name)
if f.info.IsDir() {
return ErrIsDirectory
}
return ErrNotImplemented
}
func (f *File) Truncate(size int64) error {
log.Println("File.Truncate", f.name, size)
if f.info.IsDir() {
return ErrIsDirectory
}
return ErrNotImplemented
}
func (f *File) WriteString(s string) (ret int, err error) {
log.Println("File.WriteString", f.name, s)
if f.info.IsDir() {
return -1, ErrIsDirectory
}
return -1, ErrNotImplemented
}
func (f *File) closeWithoutLocking() error {
log.Println("File.closeWithoutLocking", f.name)
if f.info.IsDir() {
return ErrIsDirectory
}
if f.reader != nil {
if err := f.reader.Close(); err != nil {
return err
@@ -163,6 +184,10 @@ func (f *File) Close() error {
func (f *File) Read(p []byte) (n int, err error) {
log.Println("File.Read", f.name, len(p))
if f.info.IsDir() {
return -1, ErrIsDirectory
}
f.ioLock.Lock()
defer f.ioLock.Unlock()
@@ -211,6 +236,10 @@ func (f *File) Read(p []byte) (n int, err error) {
func (f *File) ReadAt(p []byte, off int64) (n int, err error) {
log.Println("File.ReadAt", f.name, p, off)
if f.info.IsDir() {
return -1, ErrIsDirectory
}
if _, err := f.Seek(off, io.SeekStart); err != nil {
return -1, err
}
@@ -221,6 +250,10 @@ func (f *File) ReadAt(p []byte, off int64) (n int, err error) {
func (f *File) Seek(offset int64, whence int) (int64, error) {
log.Println("File.Seek", f.name, offset, whence)
if f.info.IsDir() {
return -1, ErrIsDirectory
}
f.ioLock.Lock()
defer f.ioLock.Unlock()
@@ -287,11 +320,19 @@ func (f *File) Seek(offset int64, whence int) (int64, error) {
func (f *File) Write(p []byte) (n int, err error) {
log.Println("File.Write", f.name, p)
if f.info.IsDir() {
return -1, ErrIsDirectory
}
return -1, ErrNotImplemented
}
func (f *File) WriteAt(p []byte, off int64) (n int, err error) {
log.Println("File.WriteAt", f.name, p, off)
if f.info.IsDir() {
return -1, ErrIsDirectory
}
return -1, ErrNotImplemented
}
+39 -17
View File
@@ -43,25 +43,25 @@ func NewFileSystem(
}
}
func (S *FileSystem) Name() string {
func (f *FileSystem) Name() string {
log.Println("FileSystem.Name")
return "STFS"
}
func (S *FileSystem) Create(name string) (afero.File, error) {
func (f *FileSystem) Create(name string) (afero.File, error) {
log.Println("FileSystem.Name", name)
panic(ErrNotImplemented)
}
func (S *FileSystem) Mkdir(name string, perm os.FileMode) error {
func (f *FileSystem) Mkdir(name string, perm os.FileMode) error {
log.Println("FileSystem.Mkdir", name, perm)
panic(ErrNotImplemented)
}
func (S *FileSystem) MkdirAll(path string, perm os.FileMode) error {
func (f *FileSystem) MkdirAll(path string, perm os.FileMode) error {
log.Println("FileSystem.MkdirAll", path, perm)
panic(ErrNotImplemented)
@@ -99,67 +99,89 @@ func (f *FileSystem) Open(name string) (afero.File, error) {
), nil
}
func (S *FileSystem) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error) {
func (f *FileSystem) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error) {
log.Println("FileSystem.OpenFile", name, flag, perm)
panic(ErrNotImplemented)
if flag != 0 {
// TODO: Implement update and write
panic(ErrNotImplemented)
}
// TODO: Implement `perm` support
return f.Open(name)
}
func (S *FileSystem) Remove(name string) error {
func (f *FileSystem) Remove(name string) error {
log.Println("FileSystem.Remove", name)
panic(ErrNotImplemented)
}
func (S *FileSystem) RemoveAll(path string) error {
func (f *FileSystem) RemoveAll(path string) error {
log.Println("FileSystem.RemoveAll", path)
panic(ErrNotImplemented)
}
func (S *FileSystem) Rename(oldname, newname string) error {
func (f *FileSystem) Rename(oldname, newname string) error {
log.Println("FileSystem.Rename", oldname, newname)
panic(ErrNotImplemented)
}
func (S *FileSystem) Stat(name string) (os.FileInfo, error) {
func (f *FileSystem) Stat(name string) (os.FileInfo, error) {
log.Println("FileSystem.Stat", name)
panic(ErrNotImplemented)
hdr, err := inventory.Stat(
f.metadata,
name,
f.onHeader,
)
if err != nil {
if err == sql.ErrNoRows {
return nil, os.ErrNotExist
}
panic(err)
}
return NewFileInfo(hdr), nil
}
func (S *FileSystem) Chmod(name string, mode os.FileMode) error {
func (f *FileSystem) Chmod(name string, mode os.FileMode) error {
log.Println("FileSystem.Chmod", name, mode)
panic(ErrNotImplemented)
}
func (S *FileSystem) Chown(name string, uid, gid int) error {
func (f *FileSystem) Chown(name string, uid, gid int) error {
log.Println("FileSystem.Chown", name, uid, gid)
panic(ErrNotImplemented)
}
func (S *FileSystem) Chtimes(name string, atime time.Time, mtime time.Time) error {
func (f *FileSystem) Chtimes(name string, atime time.Time, mtime time.Time) error {
log.Println("FileSystem.Chtimes", name, atime, mtime)
panic(ErrNotImplemented)
}
func (S *FileSystem) LstatIfPossible(name string) (os.FileInfo, bool, error) {
func (f *FileSystem) LstatIfPossible(name string) (os.FileInfo, bool, error) {
log.Println("FileSystem.LstatIfPossible", name)
panic(ErrNotImplemented)
}
func (S *FileSystem) SymlinkIfPossible(oldname, newname string) error {
func (f *FileSystem) SymlinkIfPossible(oldname, newname string) error {
log.Println("FileSystem.SymlinkIfPossible", oldname, newname)
panic(ErrNotImplemented)
}
func (S *FileSystem) ReadlinkIfPossible(name string) (string, error) {
func (f *FileSystem) ReadlinkIfPossible(name string) (string, error) {
log.Println("FileSystem.ReadlinkIfPossible", name)
panic(ErrNotImplemented)