Fix various I/O related deployment bugs

This commit is contained in:
kayomn 2022-12-23 15:49:29 +00:00
parent 62318db717
commit 4613ce055c
2 changed files with 19 additions and 19 deletions

View File

@ -166,12 +166,12 @@ var commands = []Command{
IsVarargs: true, IsVarargs: true,
Action: func(requiredArguments []string, providedArguments []string) (string, error) { Action: func(requiredArguments []string, providedArguments []string) (string, error) {
if len(providedArguments) != len(requiredArguments) { if len(providedArguments) < len(requiredArguments) {
return "", fmt.Errorf("expected %s followed by one or more %ss", return "", fmt.Errorf("expected %s followed by one or more %ss",
requiredArguments[0], requiredArguments[1]) requiredArguments[0], requiredArguments[1])
} }
if game, openGameError := LoadGame(providedArguments[0]); openGameError != nil { if game, openGameError := LoadGame(providedArguments[0]); openGameError == nil {
if deployError := game.Deploy(providedArguments[1:]); deployError != nil { if deployError := game.Deploy(providedArguments[1:]); deployError != nil {
return "", deployError return "", deployError
} }

View File

@ -42,6 +42,10 @@ func (game *Game) Clean() error {
} }
func (game *Game) Deploy(names []string) error { func (game *Game) Deploy(names []string) error {
if cleanError := game.Clean(); cleanError != nil {
return cleanError
}
for _, name := range names { for _, name := range names {
var mod, exists = game.Mods[name] var mod, exists = game.Mods[name]
@ -83,32 +87,20 @@ func (game *Game) Deploy(names []string) error {
// Backup up any pre-existing file before it is overwritten by Zip entry // Backup up any pre-existing file before it is overwritten by Zip entry
// extraction. // extraction.
if fileInfo, statError := os.Stat(deployPath); statError == nil { if _, statError := os.Stat(deployPath); statError == nil {
var backupPath = filepath.Join( var backupPath = filepath.Join(
cachePath(), game.Name, "overwritten", zipFile.Name) cachePath(), game.Name, "overwritten", zipFile.Name)
if dirError := os.MkdirAll(filepath.Dir(backupPath), fileInfo.Mode()); dirError != nil { if dirError := os.MkdirAll(filepath.Dir(backupPath), os.ModePerm); dirError != nil {
if closeError := zipReadCloser.Close(); closeError != nil {
return closeError
}
return dirError return dirError
} }
if renameError := os.Rename(deployPath, backupPath); renameError != nil { if renameError := os.Rename(deployPath, backupPath); renameError != nil {
if closeError := zipReadCloser.Close(); closeError != nil {
return closeError
}
return renameError return renameError
} }
game.OverwrittenFilePaths = append(game.OverwrittenFilePaths, zipFile.Name) game.OverwrittenFilePaths = append(game.OverwrittenFilePaths, zipFile.Name)
} else if !(os.IsNotExist(statError)) { } else if !(os.IsNotExist(statError)) {
if closeError := zipReadCloser.Close(); closeError != nil {
return closeError
}
return statError return statError
} }
@ -337,7 +329,7 @@ func LoadGame(name string) (Game, error) {
OverwrittenFilePaths: make([]string, 0, 512), OverwrittenFilePaths: make([]string, 0, 512),
DeployedFilePaths: make([]string, 0, 512), DeployedFilePaths: make([]string, 0, 512),
Mods: make(map[string]Mod), Mods: make(map[string]Mod),
Path: entry.Key, Path: entry.Value,
} }
if loadError := game.Load(); loadError != nil { if loadError := game.Load(); loadError != nil {
@ -487,10 +479,18 @@ func fallbackPath(getFalliblePath func() (string, error), fallbackSubdir string)
} }
func (game *Game) saveDeployment() error { func (game *Game) saveDeployment() error {
var listPath = filepath.Join(cachePath(), "deployed.txt") var listPath = filepath.Join(cachePath(), game.Name, "deployed.txt")
if len(game.DeployedFilePaths) == 0 { if len(game.DeployedFilePaths) == 0 {
return os.Truncate(listPath, 0) var truncateError = os.Truncate(listPath, 0)
if !(os.IsNotExist(truncateError)) {
return truncateError
}
}
if dirError := os.MkdirAll(filepath.Dir(listPath), os.ModePerm); dirError != nil {
return dirError
} }
if file, updateError := os.Create(listPath); updateError == nil { if file, updateError := os.Create(listPath); updateError == nil {