Skip to content

Commit 20086b8

Browse files
committed
update to fix issue with extraneous files in flatfs during migration
1 parent 9eedf93 commit 20086b8

File tree

3 files changed

+51
-13
lines changed

3 files changed

+51
-13
lines changed

ipfs-4-to-5/go-ds-flatfs/convert.go

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
package flatfs
55

66
import (
7+
"errors"
78
"fmt"
89
"io"
10+
"log"
911
"os"
1012
"path/filepath"
13+
"strings"
1114

1215
"github.com/ipfs/fs-repo-migrations/ipfs-2-to-3/Godeps/_workspace/src/github.com/jbenet/go-os-rename"
1316
"github.com/ipfs/fs-repo-migrations/ipfs-4-to-5/go-datastore"
@@ -110,15 +113,45 @@ func Move(oldPath string, newPath string, out io.Writer) error {
110113
if err != nil {
111114
return err
112115
}
113-
if inf.IsDir() || fn == SHARDING_FN || fn == README_FN {
114-
// we are an empty directory or generated file so just remove it
116+
if inf.IsDir() {
117+
indir, err := os.Open(oldPath)
118+
if err != nil {
119+
return err
120+
}
121+
defer indir.Close()
122+
123+
names, err := indir.Readdirnames(-1)
124+
if err != nil {
125+
return err
126+
}
127+
128+
for _, n := range names {
129+
p := filepath.Join(oldPath, n)
130+
// part of unfinished write transaction
131+
// remove it
132+
if strings.HasPrefix(n, "put-") {
133+
err := os.Remove(p)
134+
if err != nil {
135+
return err
136+
}
137+
} else {
138+
return errors.New("unknown file in flatfs: " + p)
139+
}
140+
}
141+
142+
err = os.Remove(oldPath)
143+
if err != nil {
144+
return err
145+
}
146+
} else if fn == SHARDING_FN || fn == README_FN {
147+
// generated file so just remove it
115148
err := os.Remove(oldPath)
116149
if err != nil {
117150
return err
118151
}
119152
} else {
120153
// else we found something unexpected, so to be safe just move it
121-
fmt.Printf("found unexpected file in datastore directory: \"%s\", moving anyway", fn)
154+
log.Printf("found unexpected file in datastore directory: \"%s\", moving anyway\n", fn)
122155
newPath := filepath.Join(newDS.path, fn)
123156
err := osrename.Rename(oldPath, newPath)
124157
if err != nil {

ipfs-4-to-5/go-ds-flatfs/flatfs.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,17 @@ import (
77
"errors"
88
"fmt"
99
"io/ioutil"
10+
"log"
1011
"os"
11-
"path"
1212
"path/filepath"
1313
"strings"
1414
"time"
1515

16+
"github.com/ipfs/fs-repo-migrations/ipfs-2-to-3/Godeps/_workspace/src/github.com/jbenet/go-os-rename"
1617
"github.com/ipfs/fs-repo-migrations/ipfs-4-to-5/go-datastore"
1718
"github.com/ipfs/fs-repo-migrations/ipfs-4-to-5/go-datastore/query"
18-
"github.com/ipfs/fs-repo-migrations/ipfs-2-to-3/Godeps/_workspace/src/github.com/jbenet/go-os-rename"
1919
)
2020

21-
2221
const (
2322
extension = ".data"
2423
)
@@ -45,7 +44,7 @@ var (
4544

4645
func Create(path string, fun *ShardIdV1) error {
4746

48-
err := os.Mkdir(path, 0777)
47+
err := os.Mkdir(path, 0755)
4948
if err != nil && !os.IsExist(err) {
5049
return err
5150
}
@@ -115,13 +114,13 @@ func (fs *Datastore) ShardStr() string {
115114

116115
func (fs *Datastore) encode(key datastore.Key) (dir, file string) {
117116
noslash := key.String()[1:]
118-
dir = path.Join(fs.path, fs.getDir(noslash))
119-
file = path.Join(dir, noslash+extension)
117+
dir = filepath.Join(fs.path, fs.getDir(noslash))
118+
file = filepath.Join(dir, noslash+extension)
120119
return dir, file
121120
}
122121

123122
func (fs *Datastore) decode(file string) (key datastore.Key, ok bool) {
124-
if path.Ext(file) != extension {
123+
if filepath.Ext(file) != extension {
125124
return datastore.Key{}, false
126125
}
127126
name := file[:len(file)-len(extension)]
@@ -146,7 +145,7 @@ func (fs *Datastore) makeDir(dir string) error {
146145
}
147146

148147
func (fs *Datastore) makeDirNoSync(dir string) error {
149-
if err := os.Mkdir(dir, 0777); err != nil {
148+
if err := os.Mkdir(dir, 0755); err != nil {
150149
// EEXIST is safe to ignore here, that just means the prefix
151150
// directory already existed.
152151
if !os.IsExist(err) {
@@ -175,7 +174,7 @@ func (fs *Datastore) Put(key datastore.Key, value interface{}) error {
175174
break
176175
}
177176

178-
fmt.Printf("too many open files, retrying in %dms\n", 100*i)
177+
log.Printf("too many open files, retrying in %dms\n", 100*i)
179178
time.Sleep(time.Millisecond * 100 * time.Duration(i))
180179
}
181180
return err
@@ -430,7 +429,7 @@ func (fs *Datastore) walk(path string, reschan chan query.Result) error {
430429

431430
key, ok := fs.decode(fn)
432431
if !ok {
433-
fmt.Println("failed to decode entry in flatfs")
432+
log.Println("failed to decode entry in flatfs: ", fn)
434433
continue
435434
}
436435

sharness/t0090-migration-4-to-5.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ test_expect_success "get pin lists" '
8787
ipfs pin ls --type=indirect | sort > start_ind_pins
8888
'
8989

90+
test_expect_success "put some unexpected files in the flatfs dir" '
91+
echo "foo" > "$IPFS_PATH/blocks/CIQBE/put-123451" &&
92+
echo "bar" > "$IPFS_PATH/blocks/CIQKA/put-123451" &&
93+
echo "nonsense" > "$IPFS_PATH/blocks/badbad"
94+
'
95+
9096
test_kill_ipfs_daemon
9197

9298
test_install_ipfs_nd "v0.4.5-pre2"

0 commit comments

Comments
 (0)