Skip to content

Commit 76b6285

Browse files
committed
refactor: use atomicfile to create new config
1 parent 990c948 commit 76b6285

File tree

2 files changed

+10
-40
lines changed

2 files changed

+10
-40
lines changed

ipfs-9-to-10/migration/config_conv.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"regexp"
99
"strings"
1010

11+
"github.com/ipfs/fs-repo-migrations/ipfs-6-to-7/gx/ipfs/QmdYwCmx8pZRkzdcd8MhmLJqYVoVTC1aGsy5Q4reMGLNLg/atomicfile"
1112
log "github.com/ipfs/fs-repo-migrations/stump"
1213
)
1314

@@ -21,24 +22,19 @@ var (
2122
type convArray func([]string) []string
2223

2324
// convertFile converts a config file from one version to another
24-
func convertFile(orig string, new string, enableQuic bool, convBootstrap convArray, convSwarm convArray) error {
25-
in, err := os.Open(orig)
25+
func convertFile(path string, enableQuic bool, convBootstrap convArray, convSwarm convArray) error {
26+
in, err := os.Open(path)
2627
if err != nil {
2728
return err
2829
}
2930
defer in.Close()
3031

31-
out, err := os.Create(new)
32+
out, err := atomicfile.New(path, 0660)
3233
if err != nil {
3334
return err
3435
}
3536
defer out.Close()
3637

37-
// Make sure file has 600 permissions
38-
if err := out.Chmod(0600); err != nil {
39-
return err
40-
}
41-
4238
return convert(in, out, enableQuic, convBootstrap, convSwarm)
4339
}
4440

ipfs-9-to-10/migration/migration.go

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -41,31 +41,10 @@ func (m Migration) Apply(opts migrate.Options) error {
4141
return err
4242
}
4343

44-
basepath := filepath.Join(opts.Path, "config")
45-
v9path := filepath.Join(opts.Path, "config-v9")
46-
if err := os.Rename(basepath, v9path); err != nil {
47-
if os.IsNotExist(err) {
48-
_, err2 := os.Stat(v9path)
49-
if err2 == nil {
50-
log.Log("... config already renamed to config-v9, continuing")
51-
err = nil
52-
}
53-
}
54-
if err != nil {
55-
return err
56-
}
57-
}
58-
5944
log.Log("> Upgrading config to new format")
6045

61-
if err := convertFile(v9path, basepath, true, ver9to10Bootstrap, ver9to10Swarm); err != nil {
62-
if opts.NoRevert {
63-
return err
64-
}
65-
err := os.Rename(v9path, basepath)
66-
if err != nil {
67-
log.Error(err)
68-
}
46+
path := filepath.Join(opts.Path, "config")
47+
if err := convertFile(path, true, ver9to10Bootstrap, ver9to10Swarm); err != nil {
6948
return err
7049
}
7150

@@ -110,25 +89,21 @@ func (m Migration) Revert(opts migrate.Options) error {
11089
}
11190

11291
phasefile := filepath.Join(opts.Path, "revert-phase")
113-
basepath := filepath.Join(opts.Path, "config")
114-
v10path := filepath.Join(opts.Path, "config-v10")
92+
path := filepath.Join(opts.Path, "config")
11593

11694
phase, err := readPhase(phasefile)
11795
if err != nil {
11896
return fmt.Errorf("reading revert phase: %s", err)
11997
}
12098

121-
for ; phase < 3; phase++ {
99+
defer os.Remove(phasefile)
100+
for ; phase < 2; phase++ {
122101
switch phase {
123102
case 0:
124-
if err := os.Rename(basepath, v10path); err != nil {
103+
if err := convertFile(path, false, ver10to9Bootstrap, ver10to9Swarm); err != nil {
125104
return err
126105
}
127106
case 1:
128-
if err := convertFile(v10path, basepath, false, ver10to9Bootstrap, ver10to9Swarm); err != nil {
129-
return err
130-
}
131-
case 2:
132107
if err := repo.WriteVersion("9"); err != nil {
133108
return err
134109
}
@@ -140,7 +115,6 @@ func (m Migration) Revert(opts migrate.Options) error {
140115
return err
141116
}
142117
}
143-
os.Remove(phasefile)
144118

145119
return nil
146120
}

0 commit comments

Comments
 (0)