Skip to content

Commit 708a9d2

Browse files
committed
fix: instead of creating a new file, replace all file contents if ModList.txt exists
1 parent bfd8c67 commit 708a9d2

File tree

1 file changed

+10
-21
lines changed

1 file changed

+10
-21
lines changed

internal/workspace/modlist.go

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -101,31 +101,20 @@ func (m ModList) isModEnabled(mod string) (bool, int) {
101101
func (m *ModList) Save() error {
102102
log.WithField("file", "ModList.txt").Debugf("saving ModList.txt")
103103

104-
tmpFilePath := filepath.Join(m.dir, "ModList.txt.new")
105-
106-
// anonymous func to make sure file is always closed no matter what before we rename it
107-
err := func() error {
108-
file, err := os.Create(tmpFilePath)
109-
if err != nil {
110-
return err
111-
}
112-
defer file.Close()
104+
file, err := os.OpenFile(filepath.Join(m.dir, "ModList.txt"), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0x644)
105+
if err != nil {
106+
return err
107+
}
108+
defer file.Close()
113109

114-
writer := bufio.NewWriter(file)
110+
writer := bufio.NewWriter(file)
115111

116-
for _, line := range m.lines {
117-
if _, err = writer.WriteString(fmt.Sprintf("%s\n", line)); err != nil {
118-
return err
119-
}
120-
}
121-
if err = writer.Flush(); err != nil {
112+
for _, line := range m.lines {
113+
if _, err = writer.WriteString(fmt.Sprintf("%s\n", line)); err != nil {
122114
return err
123115
}
124-
125-
return nil
126-
}()
127-
128-
if err = os.Rename(tmpFilePath, filepath.Join(m.dir, "ModList.txt")); err != nil {
116+
}
117+
if err = writer.Flush(); err != nil {
129118
return err
130119
}
131120

0 commit comments

Comments
 (0)