Skip to content

Commit

Permalink
fix tracking of removed files/dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
laktak committed Aug 22, 2024
1 parent 9cd60dc commit b9326ba
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 31 deletions.
63 changes: 32 additions & 31 deletions index.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"os"
"path/filepath"
"reflect"
"slices"
)

Expand Down Expand Up @@ -69,10 +68,6 @@ func (i *Index) getIndexFilepath() string {
return filepath.Join(i.path, i.context.IndexFilename)
}

func (i *Index) setMod(value bool) {
i.modified = value
}

func (i *Index) logFilePanic(name string, message string) {
i.context.log(STATUS_PANIC, filepath.Join(i.path, name)+": "+message)
}
Expand Down Expand Up @@ -137,14 +132,14 @@ func (i *Index) checkFix(forceUpdateDmg bool) {
for name, b := range i.new {
if a, ok := i.cur[name]; !ok {
i.logFile(STATUS_NEW, name)
i.setMod(true)
i.modified = true
} else {
amod := int64(a.ModTime)
bmod := int64(b.ModTime)
if a.Hash != nil && b.Hash != nil && *a.Hash == *b.Hash {
i.logFile(STATUS_OK, name)
if amod != bmod {
i.setMod(true)
i.modified = true
}
continue
}
Expand All @@ -155,37 +150,45 @@ func (i *Index) checkFix(forceUpdateDmg bool) {
// keep DMG entry
i.new[name] = a
} else {
i.setMod(true)
i.modified = true
}
} else if amod < bmod {
i.logFile(STATUS_UPDATE, name)
i.setMod(true)
i.modified = true
} else if amod > bmod {
i.logFile(STATUS_UP_WARN_OLD, name)
i.setMod(true)
i.modified = true
}
}
}
if i.context.ShowMissing {
for name := range i.cur {
if _, ok := i.new[name]; !ok {
// track missing
for name := range i.cur {
if _, ok := i.new[name]; !ok {
i.modified = true
if i.context.ShowMissing {
i.logFile(STATUS_MISSING, name)
i.setMod(true)
}
}
// dirs
m := make(map[string]bool)
for _, n := range i.newDirList {
m[n] = true
}
for _, name := range i.curDirList {
if !m[name] {
}

// dirs
m := make(map[string]bool)
for _, n := range i.newDirList {
m[n] = true
}
for _, name := range i.curDirList {
if !m[name] {
i.modified = true
if i.context.ShowMissing {
i.logDir(STATUS_MISSING, name+"/")
i.setMod(true)
}
}

}
if len(i.newDirList) != len(i.curDirList) {
// added
i.modified = true
}

}

func (i *Index) calcFile(name string, a string) (*idxInfo, error) {
Expand Down Expand Up @@ -231,7 +234,7 @@ func (i *Index) save() (bool, error) {
if err != nil {
return false, err
}
i.setMod(false)
i.modified = false
return true, nil
} else {
return false, nil
Expand All @@ -241,13 +244,11 @@ func (i *Index) save() (bool, error) {
func (i *Index) load() error {
if _, err := os.Stat(i.getIndexFilepath()); err != nil {
if os.IsNotExist(err) {
// todo
i.setMod(true)
return nil
}
return err
}
i.setMod(false)
i.modified = false
file, err := os.ReadFile(i.getIndexFilepath())
if err != nil {
return err
Expand All @@ -269,7 +270,7 @@ func (i *Index) load() error {
} else {
}
if data.IdxHash != hashMd5(text) {
i.setMod(true)
i.modified = true
i.logFile(STATUS_ERR_IDX, i.getIndexFilepath())
}
} else {
Expand All @@ -286,12 +287,12 @@ func (i *Index) load() error {
}
}
}

// dirs
if data.Dir != nil {
slices.Sort(data.Dir)
i.curDirList = data.Dir
if i.context.TrackDirectories && !reflect.DeepEqual(i.curDirList, i.newDirList) {
i.setMod(true)
}
}

return nil
}
2 changes: 2 additions & 0 deletions scripts/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ func setupMiscFiles() {

genDir(root)

os.MkdirAll(filepath.Join(root, "day/car/empty"), 0755)

rootPeople := filepath.Join(root, "people")
testPeople := filepath.Join(testDir, "people")

Expand Down

0 comments on commit b9326ba

Please sign in to comment.