github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/commands/files/slicefile.go (about)

     1  package files
     2  
     3  import (
     4  	"errors"
     5  	"io"
     6  )
     7  
     8  // SliceFile implements File, and provides simple directory handling.
     9  // It contains children files, and is created from a `[]File`.
    10  // SliceFiles are always directories, and can't be read from or closed.
    11  type SliceFile struct {
    12  	filename string
    13  	path     string
    14  	files    []File
    15  	n        int
    16  }
    17  
    18  func NewSliceFile(filename, path string, files []File) *SliceFile {
    19  	return &SliceFile{filename, path, files, 0}
    20  }
    21  
    22  func (f *SliceFile) IsDirectory() bool {
    23  	return true
    24  }
    25  
    26  func (f *SliceFile) NextFile() (File, error) {
    27  	if f.n >= len(f.files) {
    28  		return nil, io.EOF
    29  	}
    30  	file := f.files[f.n]
    31  	f.n++
    32  	return file, nil
    33  }
    34  
    35  func (f *SliceFile) FileName() string {
    36  	return f.filename
    37  }
    38  
    39  func (f *SliceFile) FullPath() string {
    40  	return f.path
    41  }
    42  
    43  func (f *SliceFile) Read(p []byte) (int, error) {
    44  	return 0, ErrNotReader
    45  }
    46  
    47  func (f *SliceFile) Close() error {
    48  	return ErrNotReader
    49  }
    50  
    51  func (f *SliceFile) Peek(n int) File {
    52  	return f.files[n]
    53  }
    54  
    55  func (f *SliceFile) Length() int {
    56  	return len(f.files)
    57  }
    58  
    59  func (f *SliceFile) Size() (int64, error) {
    60  	var size int64
    61  
    62  	for _, file := range f.files {
    63  		sizeFile, ok := file.(SizeFile)
    64  		if !ok {
    65  			return 0, errors.New("Could not get size of child file")
    66  		}
    67  
    68  		s, err := sizeFile.Size()
    69  		if err != nil {
    70  			return 0, err
    71  		}
    72  		size += s
    73  	}
    74  
    75  	return size, nil
    76  }