Commit 28df97e5 authored by Juan Benet's avatar Juan Benet

Merge pull request #1629 from rht/localize-os-open

serialfile: localize os.Open into NewSerialFile
parents 9c40bf1e 0c95ed6b
......@@ -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
}
defer f.Close()
ff, err := files.NewSerialFile(root, root, f)
f, err := files.NewSerialFile(root, root, stat)
if err != nil {
return "", err
}
defer f.Close()
dagnode, err := addFile(n, ff)
dagnode, err := addFile(n, f)
if err != nil {
return "", err
}
......
......@@ -6,6 +6,7 @@ import (
"fmt"
"os"
"github.com/ipfs/go-ipfs/commands/files"
bal "github.com/ipfs/go-ipfs/importer/balanced"
"github.com/ipfs/go-ipfs/importer/chunk"
h "github.com/ipfs/go-ipfs/importer/helpers"
......@@ -20,7 +21,7 @@ var log = u.Logger("importer")
// Builds a DAG from the given file, writing created blocks to disk as they are
// created
func BuildDagFromFile(fpath string, ds dag.DAGService, mp pin.ManualPinner) (*dag.Node, error) {
stat, err := os.Stat(fpath)
stat, err := os.Lstat(fpath)
if err != nil {
return nil, err
}
......@@ -29,7 +30,7 @@ func BuildDagFromFile(fpath string, ds dag.DAGService, mp pin.ManualPinner) (*da
return nil, fmt.Errorf("`%s` is a directory", fpath)
}
f, err := os.Open(fpath)
f, err := files.NewSerialFile(fpath, fpath, stat)
if err != nil {
return nil, 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