ipfs add output not sorted, cmds files sorted

I made the commands lib dir listing sort the contents
so we get the same sequence of files from it repeatably.
This commit is contained in:
Juan Batiz-Benet 2015-01-06 13:40:22 -08:00
parent 87c561b2cf
commit 0395a7af1e
3 changed files with 18 additions and 9 deletions

View File

@ -7,6 +7,7 @@ import (
"os"
fp "path"
"runtime"
"sort"
"strings"
cmds "github.com/jbenet/go-ipfs/commands"
@ -319,8 +320,10 @@ func openPath(file *os.File, path string) (cmds.File, error) {
return nil, err
}
files := make([]cmds.File, 0, len(contents))
// make sure contents are sorted so -- repeatably -- we get the same inputs.
sort.Sort(sortFIByName(contents))
files := make([]cmds.File, 0, len(contents))
for _, child := range contents {
childPath := fp.Join(path, child.Name())
childFile, err := os.Open(childPath)
@ -351,3 +354,9 @@ func isTerminal(stdin *os.File) (bool, error) {
// if stdin is a CharDevice, return true
return ((stat.Mode() & os.ModeCharDevice) != 0), nil
}
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() }

View File

@ -6,7 +6,6 @@ import (
"fmt"
"io"
"path"
"sort"
cmds "github.com/jbenet/go-ipfs/commands"
core "github.com/jbenet/go-ipfs/core"
@ -83,7 +82,8 @@ remains to be implemented.
return nil, u.ErrCast()
}
sort.Stable(val)
// TODO: use this with an option
// sort.Stable(val)
var buf bytes.Buffer
for i, obj := range val.Objects {
@ -204,12 +204,12 @@ func addDagnode(output *AddOutput, name string, dn *dag.Node) error {
// Sort interface implementation to sort add output by name
func (a AddOutput) Len() int {
return len(a.Names)
return len(a.Names)
}
func (a AddOutput) Swap(i, j int) {
a.Names[i], a.Names[j] = a.Names[j], a.Names[i]
a.Objects[i], a.Objects[j] = a.Objects[j], a.Objects[i]
a.Names[i], a.Names[j] = a.Names[j], a.Names[i]
a.Objects[i], a.Objects[j] = a.Objects[j], a.Objects[i]
}
func (a AddOutput) Less(i, j int) bool {
return a.Names[i] < a.Names[j]
return a.Names[i] < a.Names[j]
}

View File

@ -78,9 +78,9 @@ test_expect_success "'ipfs add -r' output looks good" '
PLANETS="QmWSgS32xQEcXMeqd3YPJLrNBLSdsfYCep2U7CFkyrjXwY" &&
MARS="QmPrrHqJzto9m7SyiRzarwkqPcCSsKR2EB1AyqJfe8L8tN" &&
VENUS="QmU5kp3BH3B8tnWUU2Pikdb2maksBNkb92FHRr56hyghh4" &&
echo "added $PLANETS mountdir/planets" >expected &&
echo "added $MARS mountdir/planets/mars.txt" >>expected &&
echo "added $MARS mountdir/planets/mars.txt" >expected &&
echo "added $VENUS mountdir/planets/venus.txt" >>expected &&
echo "added $PLANETS mountdir/planets" >>expected &&
test_cmp expected actual
'