Skip to content

Commit

Permalink
also update modified files with -a #19
Browse files Browse the repository at this point in the history
  • Loading branch information
laktak committed Nov 21, 2024
1 parent e34e0f2 commit cbe1849
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Flags:
-H, --tips Show tips.
-c, --check check mode: chkbit will verify files in readonly mode (default mode)
-u, --update update mode: add and update indices
-a, --add-only add mode: only add new files, do not check existing (quicker)
-a, --add-only add mode: only add new and modified files, do not check existing (quicker)
-i, --show-ignored-only show-ignored mode: only show ignored files
-m, --show-missing show missing files/directories
-d, --include-dot include dot files
Expand Down
2 changes: 1 addition & 1 deletion cmd/chkbit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var cli struct {
Tips bool `short:"H" help:"Show tips."`
Check bool `short:"c" help:"check mode: chkbit will verify files in readonly mode (default mode)"`
Update bool `short:"u" help:"update mode: add and update indices"`
AddOnly bool `short:"a" help:"add mode: only add new files, do not check existing (quicker)"`
AddOnly bool `short:"a" help:"add mode: only add new and modified files, do not check existing (quicker)"`
ShowIgnoredOnly bool `short:"i" help:"show-ignored mode: only show ignored files"`
ShowMissing bool `short:"m" help:"show missing files/directories"`
IncludeDot bool `short:"d" help:"include dot files"`
Expand Down
14 changes: 11 additions & 3 deletions index.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ func newIndex(context *Context, path string, files []string, dirList []string, r
}
}

func getMtime(path string) int64 {
info, _ := os.Stat(path)
return int64(info.ModTime().UnixNano() / 1e6)
}

func (i *Index) getIndexFilepath() string {
return filepath.Join(i.path, i.context.IndexFilename)
}
Expand Down Expand Up @@ -106,7 +111,7 @@ func (i *Index) calcHashes(ignore *Ignore) {
if val.Algo != nil {
algo = *val.Algo
}
if i.context.AddOnly {
if i.context.AddOnly && !i.mtimeChanged(name, val) {
info = &val
} else {
info, err = i.calcFile(name, algo)
Expand Down Expand Up @@ -195,13 +200,16 @@ func (i *Index) checkFix(forceUpdateDmg bool) {
// added
i.modified = true
}
}

func (i *Index) mtimeChanged(name string, ii idxInfo) bool {
mtime := getMtime(filepath.Join(i.path, name))
return ii.ModTime != mtime
}

func (i *Index) calcFile(name string, a string) (*idxInfo, error) {
path := filepath.Join(i.path, name)
info, _ := os.Stat(path)
mtime := int64(info.ModTime().UnixNano() / 1e6)
mtime := getMtime(path)
h, err := Hashfile(path, a, i.context.perfMonBytes)
if err != nil {
return nil, err
Expand Down
24 changes: 19 additions & 5 deletions scripts/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,6 @@ func TestRoot(t *testing.T) {

genFiles(filepath.Join(root, "way/add"), 99)
genFile(filepath.Join(root, "time/add-file.txt"), 500)
// modify existing, will not be reported:
genFile(filepath.Join(root, "way/job/word-business.mp3"), 500)

cmd := exec.Command(tool, "-a", root)
out, err := cmd.Output()
Expand All @@ -247,6 +245,25 @@ func TestRoot(t *testing.T) {
checkOut(t, sout, "- 0 file hashes were updated")
})

// add modified files only
t.Run("add-only-mod", func(t *testing.T) {

// modify existing
genFile(filepath.Join(root, "way/job/word-business.mp3"), 500)

cmd := exec.Command(tool, "-a", root)
out, err := cmd.Output()
if err != nil {
t.Fatalf("failed with '%s'\n", err)
}
sout := string(out)
checkOut(t, sout, "old /tmp/chkbit/root/way/job/word-business.mp3")
checkOut(t, sout, "Processed 1 file")
checkOut(t, sout, "- 1 directory was updated")
checkOut(t, sout, "- 0 file hashes were added")
checkOut(t, sout, "- 1 file hash was updated")
})

// update remaining
t.Run("update-remaining-add", func(t *testing.T) {
cmd := exec.Command(tool, "-u", root)
Expand All @@ -256,9 +273,6 @@ func TestRoot(t *testing.T) {
}
sout := string(out)
checkOut(t, sout, "Processed 295 files")
checkOut(t, sout, "- 1 directory was updated")
checkOut(t, sout, "- 0 file hashes were added")
checkOut(t, sout, "- 1 file hash was updated")
})

// ignore dot
Expand Down

0 comments on commit cbe1849

Please sign in to comment.