Skip to content
This repository has been archived by the owner on Oct 14, 2021. It is now read-only.

Commit

Permalink
feat: implement Seek, Read and ReadAt
Browse files Browse the repository at this point in the history
  • Loading branch information
Norman Meier committed Sep 27, 2021
1 parent 7a9cef4 commit f00f8fd
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 14 deletions.
36 changes: 33 additions & 3 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gormfs

import (
"errors"
"io"
"io/fs"
"os"
"path/filepath"
Expand All @@ -14,6 +15,7 @@ type aferoFile struct {
db *gorm.DB
name string
flag int
head int64
}

var _ afero.File = (*aferoFile)(nil)
Expand Down Expand Up @@ -52,7 +54,15 @@ func (af *aferoFile) Stat() (fs.FileInfo, error) {
}

func (af *aferoFile) Seek(offset int64, whence int) (int64, error) {
return -1, errors.New("aferoFile.Seek not implemented")
switch whence {
case io.SeekStart:
af.head = offset
case io.SeekCurrent:
af.head += offset
case io.SeekEnd:
af.head = af.Size() + offset
}
return af.head, nil
}

func (af *aferoFile) Readdirnames(count int) ([]string, error) {
Expand All @@ -64,11 +74,31 @@ func (af *aferoFile) Readdir(count int) ([]fs.FileInfo, error) {
}

func (af *aferoFile) ReadAt(p []byte, off int64) (int, error) {
return -1, errors.New("aferoFile.ReadAt not implemented")
f, err := getFile(af.db, af.name)
if err != nil {
return 0, err
}

if off >= int64(len(f.Data)) {
return 0, io.EOF
}

return copy(p, f.Data[off:]), nil
}

func (af *aferoFile) Read(p []byte) (int, error) {
return -1, errors.New("aferoFile.Read not implemented")
f, err := getFile(af.db, af.name)
if err != nil {
return 0, err
}

if af.head >= int64(len(f.Data)) {
return 0, io.EOF
}

n := copy(p, f.Data[af.head:])
af.head += int64(n)
return n, nil
}

func (af *aferoFile) Name() string {
Expand Down
15 changes: 4 additions & 11 deletions fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
)

// FIXME: set File.Mode, File.ATime, File.User and File.Group correctly
// FIXME: handle flag correctly

type GormFs struct {
db *gorm.DB
Expand All @@ -29,7 +30,7 @@ func NewGormFs(db *gorm.DB) (*GormFs, error) {
var _ afero.Fs = (*GormFs)(nil)

func (fs *GormFs) Chmod(name string, mode os.FileMode) error {
f, err := fs.get(name)
f, err := getFile(fs.db, name)
if err != nil {
return err
}
Expand All @@ -39,7 +40,7 @@ func (fs *GormFs) Chmod(name string, mode os.FileMode) error {
}

func (fs *GormFs) Chown(name string, uid, gid int) error {
f, err := fs.get(name)
f, err := getFile(fs.db, name)
if err != nil {
return err
}
Expand All @@ -50,7 +51,7 @@ func (fs *GormFs) Chown(name string, uid, gid int) error {
}

func (fs *GormFs) Chtimes(name string, atime time.Time, mtime time.Time) error {
f, err := fs.get(name)
f, err := getFile(fs.db, name)
if err != nil {
return err
}
Expand Down Expand Up @@ -179,11 +180,3 @@ func (fs *GormFs) exists(name string) bool {
}
return fs.db.Where("name = ?", name).Limit(1).Find(&File{}).RowsAffected != 0
}

func (fs *GormFs) get(name string) (*File, error) {
var f File
if err := fs.db.Where("name = ?", name).Limit(1).Find(&f).Error; err != nil {
return nil, err
}
return &f, nil
}
1 change: 1 addition & 0 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type File struct {
IsDir bool
User int
Group int
Data []byte
}

var allModels = []interface{}{&File{}}
11 changes: 11 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package gormfs

import "gorm.io/gorm"

func getFile(db *gorm.DB, name string) (*File, error) {
var f File
if err := db.Where("name = ?", name).Limit(1).Find(&f).Error; err != nil {
return nil, err
}
return &f, nil
}

0 comments on commit f00f8fd

Please sign in to comment.