mirror of
https://github.com/ipfs/kubo.git
synced 2025-09-11 15:15:58 +08:00

This changes the pin behavior. It uses the filenames given through the api, and allows files to be streamed faltly (not a hierarchy), which is easier for other things (like vinyl in node-ipfs-api land). Files can also be entirely out of order, and the garbage intermediate directories will not be pinned (gc-ed later). The changes also mean the output of add has changed slightly-- it no longer shows the local path added, but rather the dag path relative to the added roots. This is a small difference, but changes tests. The dagutils.Editor creates a lot of chaff (intermediate objects) along the way. Wonder how we might minimize the writes to the datastore... This commit also removes the "NilRepo()" part of the --only-hash mode. We need to store at least in an in-mem repo/datastore because otherwise the dagutils.Editor breaks. License: MIT Signed-off-by: Juan Batiz-Benet <juan@benet.ai>
77 lines
1.3 KiB
Go
77 lines
1.3 KiB
Go
package files
|
|
|
|
import (
|
|
"errors"
|
|
"io"
|
|
)
|
|
|
|
// SliceFile implements File, and provides simple directory handling.
|
|
// It contains children files, and is created from a `[]File`.
|
|
// SliceFiles are always directories, and can't be read from or closed.
|
|
type SliceFile struct {
|
|
filename string
|
|
path string
|
|
files []File
|
|
n int
|
|
}
|
|
|
|
func NewSliceFile(filename, path string, files []File) *SliceFile {
|
|
return &SliceFile{filename, path, files, 0}
|
|
}
|
|
|
|
func (f *SliceFile) IsDirectory() bool {
|
|
return true
|
|
}
|
|
|
|
func (f *SliceFile) NextFile() (File, error) {
|
|
if f.n >= len(f.files) {
|
|
return nil, io.EOF
|
|
}
|
|
file := f.files[f.n]
|
|
f.n++
|
|
return file, nil
|
|
}
|
|
|
|
func (f *SliceFile) FileName() string {
|
|
return f.filename
|
|
}
|
|
|
|
func (f *SliceFile) FullPath() string {
|
|
return f.path
|
|
}
|
|
|
|
func (f *SliceFile) Read(p []byte) (int, error) {
|
|
return 0, ErrNotReader
|
|
}
|
|
|
|
func (f *SliceFile) Close() error {
|
|
return ErrNotReader
|
|
}
|
|
|
|
func (f *SliceFile) Peek(n int) File {
|
|
return f.files[n]
|
|
}
|
|
|
|
func (f *SliceFile) Length() int {
|
|
return len(f.files)
|
|
}
|
|
|
|
func (f *SliceFile) Size() (int64, error) {
|
|
var size int64
|
|
|
|
for _, file := range f.files {
|
|
sizeFile, ok := file.(SizeFile)
|
|
if !ok {
|
|
return 0, errors.New("Could not get size of child file")
|
|
}
|
|
|
|
s, err := sizeFile.Size()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
size += s
|
|
}
|
|
|
|
return size, nil
|
|
}
|