Commit 1fe28bc6 authored by Łukasz Magiera's avatar Łukasz Magiera

files2.0: split adder.addFile into subtyped functions

License: MIT
Signed-off-by: default avatarŁukasz Magiera <magik6k@gmail.com>
parent a92a1745
...@@ -322,7 +322,7 @@ func AddR(n *core.IpfsNode, root string) (key string, err error) { ...@@ -322,7 +322,7 @@ func AddR(n *core.IpfsNode, root string) (key string, err error) {
return "", err return "", err
} }
err = fileAdder.addFile(filepath.Base(root), f) err = fileAdder.addFileNode(filepath.Base(root), f)
if err != nil { if err != nil {
return "", err return "", err
} }
...@@ -348,7 +348,7 @@ func AddWrapped(n *core.IpfsNode, r io.Reader, filename string) (string, ipld.No ...@@ -348,7 +348,7 @@ func AddWrapped(n *core.IpfsNode, r io.Reader, filename string) (string, ipld.No
defer n.Blockstore.PinLock().Unlock() defer n.Blockstore.PinLock().Unlock()
err = fileAdder.addFile(filename, files.NewReaderFile(r)) err = fileAdder.addFileNode(filename, files.NewReaderFile(r))
if err != nil { if err != nil {
return "", nil, err return "", nil, err
} }
...@@ -416,7 +416,7 @@ func (adder *Adder) AddAllAndPin(file files.Node) (ipld.Node, error) { ...@@ -416,7 +416,7 @@ func (adder *Adder) AddAllAndPin(file files.Node) (ipld.Node, error) {
// semantics. // semantics.
it := tf.Entries() it := tf.Entries()
for it.Next() { for it.Next() {
if err := adder.addFile(it.Name(), it.Node()); err != nil { if err := adder.addFileNode(it.Name(), it.Node()); err != nil {
return nil, err return nil, err
} }
} }
...@@ -425,7 +425,7 @@ func (adder *Adder) AddAllAndPin(file files.Node) (ipld.Node, error) { ...@@ -425,7 +425,7 @@ func (adder *Adder) AddAllAndPin(file files.Node) (ipld.Node, error) {
} }
break break
default: default:
if err := adder.addFile("", file); err != nil { if err := adder.addFileNode("", file); err != nil {
return nil, err return nil, err
} }
break break
...@@ -443,7 +443,7 @@ func (adder *Adder) AddAllAndPin(file files.Node) (ipld.Node, error) { ...@@ -443,7 +443,7 @@ func (adder *Adder) AddAllAndPin(file files.Node) (ipld.Node, error) {
return nd, adder.PinRoot() return nd, adder.PinRoot()
} }
func (adder *Adder) addFile(path string, file files.Node) error { func (adder *Adder) addFileNode(path string, file files.Node) error {
defer file.Close() defer file.Close()
err := adder.maybePauseForGC() err := adder.maybePauseForGC()
if err != nil { if err != nil {
...@@ -464,34 +464,38 @@ func (adder *Adder) addFile(path string, file files.Node) error { ...@@ -464,34 +464,38 @@ func (adder *Adder) addFile(path string, file files.Node) error {
} }
adder.liveNodes++ adder.liveNodes++
if dir, ok := file.(files.Directory); ok { switch f := file.(type) {
return adder.addDir(path, dir) case files.Directory:
return adder.addDir(path, f)
case *files.Symlink:
return adder.addSymlink(path, f)
case files.File:
return adder.addFile(path, f)
default:
return errors.New("unknown file type")
} }
}
// case for symlink func (adder *Adder) addSymlink(path string, l *files.Symlink) error {
if s, ok := file.(*files.Symlink); ok { sdata, err := unixfs.SymlinkData(l.Target)
sdata, err := unixfs.SymlinkData(s.Target) if err != nil {
if err != nil { return err
return err }
}
dagnode := dag.NodeWithData(sdata)
dagnode.SetCidBuilder(adder.CidBuilder)
err = adder.dagService.Add(adder.ctx, dagnode)
if err != nil {
return err
}
return adder.addNode(dagnode, path) dagnode := dag.NodeWithData(sdata)
dagnode.SetCidBuilder(adder.CidBuilder)
err = adder.dagService.Add(adder.ctx, dagnode)
if err != nil {
return err
} }
// case for regular file return adder.addNode(dagnode, path)
}
func (adder *Adder) addFile(path string, file files.File) error {
// if the progress flag was specified, wrap the file so that we can send // if the progress flag was specified, wrap the file so that we can send
// progress updates to the client (over the output channel) // progress updates to the client (over the output channel)
reader, ok := file.(io.Reader) var reader io.Reader = file
if !ok {
return errors.New("file doesn't support reading")
}
if adder.Progress { if adder.Progress {
rdr := &progressReader{file: reader, path: path, out: adder.Out} rdr := &progressReader{file: reader, path: path, out: adder.Out}
if fi, ok := file.(files.FileInfo); ok { if fi, ok := file.(files.FileInfo); ok {
...@@ -542,7 +546,7 @@ func (adder *Adder) addDir(path string, dir files.Directory) error { ...@@ -542,7 +546,7 @@ func (adder *Adder) addDir(path string, dir files.Directory) error {
log.Infof("%s is hidden, skipping", fpath) log.Infof("%s is hidden, skipping", fpath)
continue continue
} }
err = adder.addFile(fpath, it.Node()) err = adder.addFileNode(fpath, it.Node())
if err != nil { if err != nil {
return err return err
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment