Skip to content

Commit

Permalink
new --add-only mode
Browse files Browse the repository at this point in the history
  • Loading branch information
laktak committed Aug 22, 2024
1 parent b9326ba commit 8eab0f1
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 23 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,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)
-i, --show-ignored-only show-ignored mode: only show ignored files
-m, --show-missing show missing files/directories
--force force update of damaged items (advanced usage only)
Expand Down
8 changes: 5 additions & 3 deletions cmd/chkbit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +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)"`
ShowIgnoredOnly bool `short:"i" help:"show-ignored mode: only show ignored files"`
ShowMissing bool `short:"m" help:"show missing files/directories"`
Force bool `help:"force update of damaged items (advanced usage only)"`
Expand Down Expand Up @@ -158,9 +159,9 @@ func (m *Main) showStatus() {
func (m *Main) process() bool {
// verify mode
var b01 = map[bool]int8{false: 0, true: 1}
if b01[cli.Check]+b01[cli.Update]+b01[cli.ShowIgnoredOnly] > 1 {
if b01[cli.Check]+b01[cli.Update]+b01[cli.AddOnly]+b01[cli.ShowIgnoredOnly] > 1 {
fmt.Println("Error: can only run one mode at a time!")
return false
os.Exit(1)
}

var err error
Expand All @@ -170,7 +171,8 @@ func (m *Main) process() bool {
return false
}
m.context.ForceUpdateDmg = cli.Force
m.context.UpdateIndex = cli.Update
m.context.UpdateIndex = cli.Update || cli.AddOnly
m.context.AddOnly = cli.AddOnly
m.context.ShowIgnoredOnly = cli.ShowIgnoredOnly
m.context.ShowMissing = cli.ShowMissing
m.context.SkipSymlinks = cli.SkipSymlinks
Expand Down
5 changes: 4 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
type Context struct {
NumWorkers int
UpdateIndex bool
AddOnly bool
ShowIgnoredOnly bool
ShowMissing bool
ForceUpdateDmg bool
Expand Down Expand Up @@ -71,7 +72,9 @@ func (context *Context) log(stat Status, message string) {
context.NumTotal++
context.NumNew++
case STATUS_OK:
context.NumTotal++
if !context.AddOnly {
context.NumTotal++
}
case STATUS_MISSING:
context.NumDel++
//case STATUS_PANIC:
Expand Down
9 changes: 7 additions & 2 deletions index.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (i *Index) calcHashes(ignore *Ignore) {
var info *idxInfo
algo := i.context.HashAlgo
if val, ok := i.cur[name]; ok {
// existing
// existing file
if val.LegacyHash != nil {
// convert from py1 to new format
val = idxInfo{
Expand All @@ -104,8 +104,13 @@ func (i *Index) calcHashes(ignore *Ignore) {
if val.Algo != nil {
algo = *val.Algo
}
info, err = i.calcFile(name, algo)
if i.context.AddOnly {
info = &val
} else {
info, err = i.calcFile(name, algo)
}
} else {
// new file
if i.readonly {
info = &idxInfo{Algo: &algo}
} else {
Expand Down
74 changes: 57 additions & 17 deletions scripts/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ func setDate(filename string, r int) {
os.Chtimes(filename, date, date)
}

func genFile(dir string, a int) {
func genFile(path string, size int) {
os.WriteFile(path, make([]byte, size), 0644)
setDate(path, size*size)
}

func genFiles(dir string, a int) {
os.MkdirAll(dir, 0755)
for i := 1; i <= 5; i++ {
size := a*i*wordIdx*100 + extIdx
Expand All @@ -78,9 +83,7 @@ func genFile(dir string, a int) {
}

file += "." + nextExt()
path := filepath.Join(dir, file)
os.WriteFile(path, make([]byte, size), 0644)
setDate(path, size*size)
genFile(filepath.Join(dir, file), size)
}
}

Expand All @@ -89,11 +92,11 @@ func genDir(root string) {

for i := 1; i <= 5; i++ {
dir := filepath.Join(root, start, nextWord())
genFile(dir, 1)
genFiles(dir, 1)

if wordIdx%3 == 0 {
dir = filepath.Join(dir, nextWord())
genFile(dir, 1)
genFiles(dir, 1)
}
}
}
Expand Down Expand Up @@ -140,7 +143,7 @@ func TestRoot(t *testing.T) {
root := filepath.Join(testDir, "root")

// update index, no recourse
t.Run("Step1", func(t *testing.T) {
t.Run("no-recourse", func(t *testing.T) {
cmd := exec.Command(tool, "-umR", filepath.Join(root, "day/office"))
out, err := cmd.Output()
if err != nil {
Expand All @@ -155,7 +158,7 @@ func TestRoot(t *testing.T) {
})

// update remaining index from root
t.Run("Step2", func(t *testing.T) {
t.Run("update-remaining", func(t *testing.T) {
cmd := exec.Command(tool, "-um", root)
out, err := cmd.Output()
if err != nil {
Expand All @@ -170,7 +173,7 @@ func TestRoot(t *testing.T) {
})

// delete files, check for missing
t.Run("Step3", func(t *testing.T) {
t.Run("delete", func(t *testing.T) {
os.RemoveAll(filepath.Join(root, "thing/change"))
os.Remove(filepath.Join(root, "time/hour/minute/body-information.csv"))

Expand All @@ -185,7 +188,7 @@ func TestRoot(t *testing.T) {
})

// do not report missing without -m
t.Run("Step4", func(t *testing.T) {
t.Run("no-missing", func(t *testing.T) {
cmd := exec.Command(tool, root)
out, err := cmd.Output()
if err != nil {
Expand All @@ -197,7 +200,7 @@ func TestRoot(t *testing.T) {
})

// check for missing and update
t.Run("Step5", func(t *testing.T) {
t.Run("missing", func(t *testing.T) {
cmd := exec.Command(tool, "-um", root)
out, err := cmd.Output()
if err != nil {
Expand All @@ -209,16 +212,53 @@ func TestRoot(t *testing.T) {
})

// check again
t.Run("Step6", func(t *testing.T) {
t.Run("repeat", func(t *testing.T) {
for i := 0; i < 10; i++ {
cmd := exec.Command(tool, "-u", root)
cmd := exec.Command(tool, "-uv", root)
out, err := cmd.Output()
if err != nil {
t.Fatalf("failed with '%s'\n", err)
}
sout := string(out)
checkOut(t, sout, "Processed 289 files")
checkNotOut(t, sout, "removed")
checkNotOut(t, sout, "updated")
checkNotOut(t, sout, "added")
}
})

// add files only
t.Run("add-only", func(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()
if err != nil {
t.Fatalf("failed with '%s'\n", err)
}
sout := string(out)
checkOut(t, sout, "Processed 6 files")
checkOut(t, sout, "- 3 directories were updated")
checkOut(t, sout, "- 6 file hashes were added")
checkOut(t, sout, "- 0 file hashes were updated")
})

// update remaining
t.Run("update-remaining-add", func(t *testing.T) {
cmd := exec.Command(tool, "-u", root)
out, err := cmd.Output()
if err != nil {
t.Fatalf("failed with '%s'\n", err)
}
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")
})
}

Expand Down Expand Up @@ -246,7 +286,7 @@ func TestDMG(t *testing.T) {
t3, _ := time.Parse(time.RFC3339, "2022-02-01T13:00:00Z")

// create test and set the modified time"
t.Run("Step1", func(t *testing.T) {
t.Run("create", func(t *testing.T) {
os.WriteFile(testFile, []byte("foo1"), 0644)
os.Chtimes(testFile, t2, t2)

Expand All @@ -259,7 +299,7 @@ func TestDMG(t *testing.T) {
})

// update test with different content & old modified (expect 'old')"
t.Run("Step2", func(t *testing.T) {
t.Run("expect-old", func(t *testing.T) {
os.WriteFile(testFile, []byte("foo2"), 0644)
os.Chtimes(testFile, t1, t1)

Expand All @@ -272,7 +312,7 @@ func TestDMG(t *testing.T) {
})

// update test & new modified (expect 'upd')"
t.Run("Step3", func(t *testing.T) {
t.Run("expect-upd", func(t *testing.T) {
os.WriteFile(testFile, []byte("foo3"), 0644)
os.Chtimes(testFile, t3, t3)

Expand All @@ -285,7 +325,7 @@ func TestDMG(t *testing.T) {
})

// Now update test with the same modified to simulate damage (expect DMG)"
t.Run("Step4", func(t *testing.T) {
t.Run("expect-DMG", func(t *testing.T) {
os.WriteFile(testFile, []byte("foo4"), 0644)
os.Chtimes(testFile, t3, t3)

Expand Down

0 comments on commit 8eab0f1

Please sign in to comment.