Skip to content

Commit

Permalink
serialfile: localize os.Open into NewSerialFile
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: rht <[email protected]>
  • Loading branch information
rht committed Aug 31, 2015
1 parent 8c65290 commit 0d4d18d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 31 deletions.
9 changes: 2 additions & 7 deletions commands/cli/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,12 +352,7 @@ func appendFile(args []files.File, inputs []string, argDef *cmds.Argument, recur
fpath = cwd
}

file, err := os.Open(fpath)
if err != nil {
return nil, nil, err
}

stat, err := file.Stat()
stat, err := os.Stat(fpath)
if err != nil {
return nil, nil, err
}
Expand All @@ -375,7 +370,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
}
Expand Down
24 changes: 4 additions & 20 deletions commands/files/serialfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@ package files

import (
"io"
"io/ioutil"
"os"
fp "path/filepath"
"sort"
"syscall"
)

type sortFIByName []os.FileInfo

func (es sortFIByName) Len() int { return len(es) }
func (es sortFIByName) Swap(i, j int) { es[i], es[j] = es[j], es[i] }
func (es sortFIByName) Less(i, j int) bool { return es[i].Name() < es[j].Name() }

// serialFile implements File, and reads from a path on the OS filesystem.
// No more than one file will be opened at a time (directories will advance
// to the next file when NextFile() is called).
Expand All @@ -25,8 +19,8 @@ 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) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
Expand All @@ -42,21 +36,11 @@ func newSerialFile(name, path string, file *os.File, stat os.FileInfo) (File, er

// for directories, stat all of the contents first, so we know what files to
// open when NextFile() is called
contents, err := file.Readdir(0)
contents, err := ioutil.ReadDir(path)
if err != nil {
return nil, err
}

// we no longer need our root directory file (we already statted the contents),
// so close it
err = file.Close()
if err != nil {
return nil, err
}

// make sure contents are sorted so -- repeatably -- we get the same inputs.
sort.Sort(sortFIByName(contents))

return &serialFile{name, path, contents, stat, nil}, nil
}

Expand Down
8 changes: 4 additions & 4 deletions core/coreunix/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.Stat(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
}
Expand Down

0 comments on commit 0d4d18d

Please sign in to comment.