From 00d6bc3770d15fab9ed6b42fce4f30368943f590 Mon Sep 17 00:00:00 2001 From: Steve Yeom Date: Mon, 2 Mar 2020 14:26:33 -0800 Subject: [PATCH] BTFS-1586-2 --- file.go | 5 ++-- multipartfile.go | 63 +++++++++++++++++------------------------------ readerfile.go | 4 +++ serialfile.go | 20 +++++++++++---- slicedirectory.go | 21 ++++++++++++---- 5 files changed, 60 insertions(+), 53 deletions(-) diff --git a/file.go b/file.go index 52c779f..6c64226 100644 --- a/file.go +++ b/file.go @@ -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() } @@ -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 diff --git a/multipartfile.go b/multipartfile.go index 16a216f..a08d6f8 100644 --- a/multipartfile.go +++ b/multipartfile.go @@ -1,8 +1,6 @@ package files import ( - "errors" - "fmt" "io" "io/ioutil" "mime" @@ -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 { @@ -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 { @@ -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. @@ -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 } @@ -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{} diff --git a/readerfile.go b/readerfile.go index f98fec4..4f92554 100644 --- a/readerfile.go +++ b/readerfile.go @@ -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{} diff --git a/serialfile.go b/serialfile.go index 4ca7aaa..e59e5f1 100644 --- a/serialfile.go +++ b/serialfile.go @@ -18,6 +18,7 @@ type serialFile struct { files []os.FileInfo stat os.FileInfo handleHiddenFiles bool + size int64 } type serialIterator struct { @@ -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 { @@ -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, @@ -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 { @@ -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{} diff --git a/slicedirectory.go b/slicedirectory.go index 6682d32..d82e88e 100644 --- a/slicedirectory.go +++ b/slicedirectory.go @@ -1,6 +1,9 @@ package files -import "sort" +import ( + "errors" + "sort" +) type fileEntry struct { name string @@ -44,10 +47,6 @@ func (it *sliceIterator) Err() error { return nil } -func (it *sliceIterator) AbsRootPath() (string, error) { - return "", nil -} - func (it *sliceIterator) SetReedSolomon() { } @@ -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{}