Skip to content

Commit

Permalink
Merge pull request #8 from TRON-US/BTFS-1586-2
Browse files Browse the repository at this point in the history
BTFS-1586-2
  • Loading branch information
taiyangc authored Mar 4, 2020
2 parents c59d5af + 00d6bc3 commit a7e7169
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 53 deletions.
5 changes: 2 additions & 3 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ type DirIterator interface {
// return nil
Err() error

// AbsRootPath returns the absolute path of the root directory.
AbsRootPath() (string, error)

// SetReedSolomon sets the flag to indicate this Directory is used for Reed-solomon
SetReedSolomon()
}
Expand All @@ -86,6 +83,8 @@ type Directory interface {
// Note that you can't store the result of it.Node() and use it after
// advancing the iterator
Entries() DirIterator

SetSize(int64) error
}

// FileInfo exposes information on files in local filesystem
Expand Down
63 changes: 23 additions & 40 deletions multipartfile.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package files

import (
"errors"
"fmt"
"io"
"io/ioutil"
"mime"
Expand Down Expand Up @@ -31,6 +29,7 @@ type multipartDirectory struct {

// part is the part describing the directory. It's nil when implicit.
part *multipart.Part
size int64
}

type multipartWalker struct {
Expand Down Expand Up @@ -75,6 +74,14 @@ func NewFileFromPartReader(reader *multipart.Reader, mediatype string) (Director
}, nil
}

func IsMultiPartDirectory(d Directory) bool {
if _, ok := d.(*multipartDirectory); ok {
return true
} else {
return false
}
}

func (w *multipartWalker) nextFile() (Node, error) {
part, err := w.getPart()
if err != nil {
Expand Down Expand Up @@ -220,27 +227,10 @@ func (it *multipartIterator) Next() bool {
// Finally, advance to the next file.
it.curFile, it.err = it.f.walker.nextFile()

if it.forReedSolomon {
if it.absRootPath == "" && it.f.walker.currAbsPath != "" && it.f.path != "/" {
var err error
if it.absRootPath, err = getAbsRootPath(it.f.walker.currAbsPath, it.f.path); err != nil {
it.err = err
}
}
}

return it.err == nil
}
}

func getAbsRootPath(partPath string, dirPath string) (string, error) {
strs := strings.Split(partPath, dirPath)
if len(strs) <= 1 {
return "", fmt.Errorf("can not find dir path [%s] from part path [%s] ", partPath, dirPath)
}
return strs[0] + dirPath, nil
}

func (it *multipartIterator) Err() error {
// We use EOF to signal that this iterator is done. That way, we don't
// need to check every time `Next` is called.
Expand All @@ -250,26 +240,6 @@ func (it *multipartIterator) Err() error {
return it.err
}

func (it *multipartIterator) AbsRootPath() (string, error) {
if !it.forReedSolomon {
return "", errors.New("Not supported for non-Reed-Solomon directory")
}
first := true
for {
more := it.Next()
if !more {
if first {
return "", nil
}
return "", errors.New("could not find any absolue root path. Possibly no file inside the directory")
}
first = false
if it.absRootPath != "" {
return it.absRootPath, nil
}
}
}

func (it *multipartIterator) SetReedSolomon() {
it.forReedSolomon = true
}
Expand All @@ -286,7 +256,20 @@ func (f *multipartDirectory) Close() error {
}

func (f *multipartDirectory) Size() (int64, error) {
return 0, ErrNotSupported
return f.size, nil
}

func (f *multipartDirectory) SetSize(size int64) error {
f.size = size
return nil
}

func MultiPartReader(d Directory) *multipart.Reader {
md, ok := d.(*multipartDirectory)
if !ok {
return nil
}
return md.walker.reader
}

var _ Directory = &multipartDirectory{}
4 changes: 4 additions & 0 deletions readerfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,9 @@ func (f *ReaderFile) Seek(offset int64, whence int) (int64, error) {
return 0, ErrNotSupported
}

func (f *ReaderFile) Reader() io.Reader {
return f.reader
}

var _ File = &ReaderFile{}
var _ FileInfo = &ReaderFile{}
20 changes: 15 additions & 5 deletions serialfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type serialFile struct {
files []os.FileInfo
stat os.FileInfo
handleHiddenFiles bool
size int64
}

type serialIterator struct {
Expand Down Expand Up @@ -47,7 +48,7 @@ func NewSerialFile(path string, hidden bool, stat os.FileInfo) (Node, error) {
if err != nil {
return nil, err
}
return &serialFile{path, contents, stat, hidden}, nil
return &serialFile{path, contents, stat, hidden, 0}, nil
case mode&os.ModeSymlink != 0:
target, err := os.Readlink(path)
if err != nil {
Expand Down Expand Up @@ -108,10 +109,6 @@ func (it *serialIterator) Err() error {
func (it *serialIterator) SetReedSolomon() {
}

func (it *serialIterator) AbsRootPath() (string, error) {
return "", nil
}

func (f *serialFile) Entries() DirIterator {
return &serialIterator{
path: f.path,
Expand All @@ -120,6 +117,14 @@ func (f *serialFile) Entries() DirIterator {
}
}

func IsSerialFileDirectory(d Directory) bool {
if _, ok := d.(*serialFile); ok {
return true
} else {
return false
}
}

func (f *serialFile) NextFile() (string, Node, error) {
// if there aren't any files left in the root directory, we're done
if len(f.files) == 0 {
Expand Down Expand Up @@ -181,5 +186,10 @@ func (f *serialFile) Size() (int64, error) {
return du, err
}

func (f *serialFile) SetSize(size int64) error {
f.size = size
return nil
}

var _ Directory = &serialFile{}
var _ DirIterator = &serialIterator{}
21 changes: 16 additions & 5 deletions slicedirectory.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package files

import "sort"
import (
"errors"
"sort"
)

type fileEntry struct {
name string
Expand Down Expand Up @@ -44,10 +47,6 @@ func (it *sliceIterator) Err() error {
return nil
}

func (it *sliceIterator) AbsRootPath() (string, error) {
return "", nil
}

func (it *sliceIterator) SetReedSolomon() {
}

Expand Down Expand Up @@ -100,5 +99,17 @@ func (f *SliceFile) Size() (int64, error) {
return size, nil
}

func (f *SliceFile) SetSize(size int64) error {
return errors.New("not supported")
}

func IsMapDirectory(d Directory) bool {
if _, ok := d.(*SliceFile); ok {
return true
} else {
return false
}
}

var _ Directory = &SliceFile{}
var _ DirEntry = fileEntry{}

0 comments on commit a7e7169

Please sign in to comment.