Skip to content

Commit

Permalink
fixed delete old updates
Browse files Browse the repository at this point in the history
  • Loading branch information
giwty authored and giwty committed Sep 13, 2020
1 parent ff03f0a commit 3623dd2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 26 deletions.
9 changes: 6 additions & 3 deletions db/localSwitchFilesDB.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ func scanFolder(folder string, recursive bool, files *[]ExtendedFileInfo, progre
if path == folder {
return nil
}
if info.IsDir() {
if err != nil {
zap.S().Error("Error while scanning folders", err)
return nil
}

if err != nil {
zap.S().Error("Error while scanning folders", err)
if info.IsDir() {
return nil
}

Expand Down Expand Up @@ -184,6 +184,9 @@ func (ldb *LocalSwitchDBManager) processLocalFiles(files []ExtendedFileInfo,
}
switchTitle.Updates[metadata.Version] = SwitchFileInfo{ExtendedInfo: file, Metadata: metadata}
if metadata.Version > switchTitle.LatestUpdate {
if switchTitle.LatestUpdate != 0 {
skipped[switchTitle.Updates[switchTitle.LatestUpdate].ExtendedInfo] = "old update file, newer update exist locally"
}
switchTitle.LatestUpdate = metadata.Version
} else {
skipped[file] = "old update file, newer update exist locally"
Expand Down
4 changes: 3 additions & 1 deletion gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,9 @@ func (g *GUI) buildLocalDB(localDbManager *db.LocalSwitchDBManager) (*db.LocalSw
func (g *GUI) organizeLibrary() {
folderToScan := settings.ReadSettings(g.baseFolder).Folder
process.OrganizeByFolders(folderToScan, g.state.localDB, g.state.switchDB, g)

if settings.ReadSettings(g.baseFolder).OrganizeOptions.DeleteOldUpdateFiles {
process.DeleteOldUpdates(g.state.localDB, g)
}
}

func (g *GUI) UpdateProgress(curr int, total int, message string) {
Expand Down
38 changes: 16 additions & 22 deletions process/organizefolderStructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"path/filepath"
"regexp"
"robpike.io/nihongo"
"sort"
"strconv"
"strings"
)
Expand All @@ -22,33 +21,28 @@ var (

func DeleteOldUpdates(localDB *db.LocalSwitchFilesDB, updateProgress db.ProgressUpdater) {
i := 0
for _, v := range localDB.TitlesMap {
for k, v := range localDB.TitlesMap {
if !v.BaseExist {
continue
}
i++
if updateProgress != nil {
updateProgress.UpdateProgress(i, len(localDB.TitlesMap), v.File.ExtendedInfo.Info.Name())
updateProgress.UpdateProgress(i, len(localDB.TitlesMap), v.File.ExtendedInfo.Info.Name()+k)
}
if len(v.Updates) > 1 {
//sort the available local versions
localVersions := make([]int, len(v.Updates))
i := 0
for k := range v.Updates {
localVersions[i] = k
i++
}
sort.Ints(localVersions)

for i := 0; i < len(localVersions)-1; i++ {
if localVersions[i] == 0 {
//should not happen, but make sure we do not delete base
continue
}
fileToRemove := filepath.Join(v.Updates[localVersions[i]].ExtendedInfo.BaseFolder, v.Updates[localVersions[i]].ExtendedInfo.Info.Name())
zap.S().Infof("--> [Delete] Old update file: %v [latest update:%v]\n", fileToRemove, localVersions[len(localVersions)-1])
err := os.Remove(fileToRemove)
if err != nil {
zap.S().Errorf("Failed to delete file %v [%v]\n", fileToRemove, err)
for version, update := range v.Updates {
if version < v.LatestUpdate && version != 0 {
fileToRemove := filepath.Join(update.ExtendedInfo.BaseFolder, update.ExtendedInfo.Info.Name())
zap.S().Infof("--> [Delete] Old update file: %v [latest update:%v]\n", fileToRemove, v.LatestUpdate)
/*err := os.Remove(fileToRemove)
if err != nil {
zap.S().Errorf("Failed to delete file %v [%v]\n", fileToRemove, err)
}*/
}

}
v.Updates = map[int]db.SwitchFileInfo{localVersions[len(localVersions)-1]: v.Updates[localVersions[len(localVersions)-1]]}
//v.Updates = map[int]db.SwitchFileInfo{localVersions[len(localVersions)-1]: v.Updates[localVersions[len(localVersions)-1]]}
}

}
Expand Down

0 comments on commit 3623dd2

Please sign in to comment.