mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-21 10:27:46 +08:00
serialfile: localize os.Open into NewSerialFile
License: MIT Signed-off-by: rht <rhtbot@gmail.com>
This commit is contained in:
parent
9c40bf1e97
commit
00a56a14ec
@ -356,21 +356,6 @@ func appendFile(args []files.File, inputs []string, argDef *cmds.Argument, recur
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
if stat.Mode()&os.ModeSymlink != 0 {
|
||||
target, err := os.Readlink(fpath)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
arg := files.NewLinkFile("", fpath, target, stat)
|
||||
return append(args, arg), inputs[1:], nil
|
||||
}
|
||||
|
||||
file, err := os.Open(fpath)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
if stat.IsDir() {
|
||||
if !argDef.Recursive {
|
||||
err = fmt.Errorf("Invalid path '%s', argument '%s' does not support directories",
|
||||
@ -384,7 +369,7 @@ func appendFile(args []files.File, inputs []string, argDef *cmds.Argument, recur
|
||||
}
|
||||
}
|
||||
|
||||
arg, err := files.NewSerialFile(path.Base(fpath), fpath, file)
|
||||
arg, err := files.NewSerialFile(path.Base(fpath), fpath, stat)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
@ -25,8 +25,17 @@ type serialFile struct {
|
||||
current *os.File
|
||||
}
|
||||
|
||||
func NewSerialFile(name, path string, file *os.File) (File, error) {
|
||||
stat, err := file.Stat()
|
||||
func NewSerialFile(name, path string, stat os.FileInfo) (File, error) {
|
||||
if stat.Mode()&os.ModeSymlink != 0 {
|
||||
target, err := os.Readlink(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return NewLinkFile("", path, target, stat), nil
|
||||
}
|
||||
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -49,8 +58,7 @@ func newSerialFile(name, path string, file *os.File, stat os.FileInfo) (File, er
|
||||
|
||||
// we no longer need our root directory file (we already statted the contents),
|
||||
// so close it
|
||||
err = file.Close()
|
||||
if err != nil {
|
||||
if err := file.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
@ -43,18 +43,18 @@ func Add(n *core.IpfsNode, r io.Reader) (string, error) {
|
||||
|
||||
// AddR recursively adds files in |path|.
|
||||
func AddR(n *core.IpfsNode, root string) (key string, err error) {
|
||||
f, err := os.Open(root)
|
||||
stat, err := os.Lstat(root)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
f, err := files.NewSerialFile(root, root, stat)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
ff, err := files.NewSerialFile(root, root, f)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
dagnode, err := addFile(n, ff)
|
||||
dagnode, err := addFile(n, f)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user