Skip to content

Commit

Permalink
Sync files to disk. Handle more errors (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
kivutar authored Jun 22, 2019
1 parent a07f977 commit 474d640
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 14 deletions.
7 changes: 7 additions & 0 deletions ludos/connman.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,18 @@ IPv4.method=dhcp
return err
}
defer fd.Close()

_, err = fd.WriteString(config)
if err != nil {
return err
}

// We want the sync to happen before connmanctl connect, don't defer
err = fd.Sync()
if err != nil {
return err
}

err = exec.Command("/usr/bin/connmanctl", "connect", network.Path).Run()
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,5 @@ func main() {
runLoop(vid, m)

// Unload and deinit in the core.
core.UnloadGame()
core.Unload()
}
6 changes: 5 additions & 1 deletion menu/scene_core_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strings"

"github.com/libretro/ludo/core"
ntf "github.com/libretro/ludo/notifications"
"github.com/libretro/ludo/state"
"github.com/libretro/ludo/video"
)
Expand Down Expand Up @@ -34,7 +35,10 @@ func buildCoreOptions() Scene {
core.Options.Choices[i] = 0
}
core.Options.Updated = true
core.Options.Save()
err := core.Options.Save()
if err != nil {
ntf.DisplayAndLog(ntf.Error, "Core", "Error saving core options: %v", err.Error())
}
},
})
}
Expand Down
18 changes: 13 additions & 5 deletions options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,18 @@ func (o *Options) Save() error {
}

name := utils.FileName(state.Global.CorePath)
f, err := os.Create(filepath.Join(usr.HomeDir, ".ludo", name+".json"))
fd, err := os.Create(filepath.Join(usr.HomeDir, ".ludo", name+".json"))
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(f, bytes.NewReader(b))
return err
defer fd.Close()

_, err = io.Copy(fd, bytes.NewReader(b))
if err != nil {
return err
}

return fd.Sync()
}

// Load core options from a file
Expand All @@ -89,6 +94,9 @@ func (o *Options) load() error {

var opts map[string]string
err = json.Unmarshal(b, &opts)
if err != nil {
return err
}

for optk, optv := range opts {
for i, variable := range o.Vars {
Expand All @@ -102,5 +110,5 @@ func (o *Options) load() error {
}
}

return err
return nil
}
15 changes: 12 additions & 3 deletions savefiles/savefiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,19 @@ func SaveSRAM() error {
if err != nil {
return err
}
defer fd.Close()
fd.Write(bytes)

return nil
_, err = fd.Write(bytes)
if err != nil {
fd.Close()
return err
}

err = fd.Close()
if err != nil {
return err
}

return fd.Sync()
}

// LoadSRAM saves the game SRAM to the filesystem
Expand Down
12 changes: 8 additions & 4 deletions settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,18 @@ func Save() error {
return err
}

f, err := os.Create(filepath.Join(usr.HomeDir, ".ludo", "settings.json"))
fd, err := os.Create(filepath.Join(usr.HomeDir, ".ludo", "settings.json"))
if err != nil {
return err
}
defer f.Close()
defer fd.Close()

_, err = io.Copy(f, bytes.NewReader(b))
return err
_, err = io.Copy(fd, bytes.NewReader(b))
if err != nil {
return err
}

return fd.Sync()
}

// CoreForPlaylist returns the absolute path of the default libretro core for
Expand Down

0 comments on commit 474d640

Please sign in to comment.