From 4bbd7b421ec3ec2df46349732a2cf22711ed97ec Mon Sep 17 00:00:00 2001 From: Christian Zangl Date: Mon, 19 Aug 2024 22:00:38 +0200 Subject: [PATCH] refactor --- cmd/chkbit/main.go | 12 ++++++++---- context.go | 46 +++++++++++++++++++++------------------------- index.go | 9 +++++---- worker.go | 6 +++--- 4 files changed, 37 insertions(+), 36 deletions(-) diff --git a/cmd/chkbit/main.go b/cmd/chkbit/main.go index 931d1e0..12be65f 100644 --- a/cmd/chkbit/main.go +++ b/cmd/chkbit/main.go @@ -143,7 +143,7 @@ func (m *Main) showStatus(context *chkbit.Context) { statF := fmt.Sprintf("%d files/s", m.fps.Last()) statB := fmt.Sprintf("%d MB/s", m.bps.Last()/sizeMB) stat = "RW" - if !context.Update { + if !context.UpdateIndex { stat = "RO" } stat = fmt.Sprintf("[%s:%d] %5d files $ %s %-13s $ %s %-13s", @@ -168,11 +168,15 @@ func (m *Main) process() *chkbit.Context { return nil } - context, err := chkbit.NewContext(cli.Workers, cli.Force, cli.Update, cli.ShowIgnoredOnly, cli.Algo, cli.SkipSymlinks, cli.IndexName, cli.IgnoreName) + context, err := chkbit.NewContext(cli.Workers, cli.Algo, cli.IndexName, cli.IgnoreName) if err != nil { fmt.Println(err) return nil } + context.ForceUpdateDmg = cli.Force + context.UpdateIndex = cli.Update + context.ShowIgnoredOnly = cli.ShowIgnoredOnly + context.SkipSymlinks = cli.SkipSymlinks var wg sync.WaitGroup wg.Add(1) @@ -209,7 +213,7 @@ func (m *Main) printResult(context *chkbit.Context) { if m.progress != Quiet { mode := "" - if !context.Update { + if !context.UpdateIndex { mode = " in readonly mode" } status := fmt.Sprintf("Processed %s%s.", util.LangNum1MutateSuffix(m.total, "file"), mode) @@ -224,7 +228,7 @@ func (m *Main) printResult(context *chkbit.Context) { fmt.Printf("- %.2f MB/second\n", (float64(m.bps.Total)+float64(m.bps.Current))/float64(sizeMB)/elapsedS) } - if context.Update { + if context.UpdateIndex { if m.numIdxUpd > 0 { cprint(termOKFG, fmt.Sprintf("- %s updated\n- %s added\n- %s updated", util.LangNum1Choice(m.numIdxUpd, "directory was", "directories were"), diff --git a/context.go b/context.go index 203485c..3f104e0 100644 --- a/context.go +++ b/context.go @@ -9,8 +9,8 @@ import ( type Context struct { NumWorkers int - Force bool - Update bool + ForceUpdateDmg bool + UpdateIndex bool ShowIgnoredOnly bool HashAlgo string SkipSymlinks bool @@ -22,7 +22,7 @@ type Context struct { wg sync.WaitGroup } -func NewContext(numWorkers int, force bool, update bool, showIgnoredOnly bool, hashAlgo string, skipSymlinks bool, indexFilename string, ignoreFilename string) (*Context, error) { +func NewContext(numWorkers int, hashAlgo string, indexFilename string, ignoreFilename string) (*Context, error) { if indexFilename[0] != '.' { return nil, errors.New("The index filename must start with a dot!") } @@ -33,17 +33,13 @@ func NewContext(numWorkers int, force bool, update bool, showIgnoredOnly bool, h return nil, errors.New(hashAlgo + " is unknown.") } return &Context{ - NumWorkers: numWorkers, - Force: force, - Update: update, - ShowIgnoredOnly: showIgnoredOnly, - HashAlgo: hashAlgo, - SkipSymlinks: skipSymlinks, - IndexFilename: indexFilename, - IgnoreFilename: ignoreFilename, - WorkQueue: make(chan *WorkItem, numWorkers*10), - LogQueue: make(chan *LogEvent, numWorkers*100), - PerfQueue: make(chan *PerfEvent, numWorkers*10), + NumWorkers: numWorkers, + HashAlgo: hashAlgo, + IndexFilename: indexFilename, + IgnoreFilename: ignoreFilename, + WorkQueue: make(chan *WorkItem, numWorkers*10), + LogQueue: make(chan *LogEvent, numWorkers*100), + PerfQueue: make(chan *PerfEvent, numWorkers*10), }, nil } @@ -121,6 +117,11 @@ func (context *Context) scanDir(root string, parentIgnore *Ignore) { var dirList []string var filesToIndex []string + ignore, err := GetIgnore(context, root, parentIgnore) + if err != nil { + context.logErr(root+"/", err) + } + for _, file := range files { path := filepath.Join(root, file.Name()) if file.Name()[0] == '.' { @@ -130,24 +131,19 @@ func (context *Context) scanDir(root string, parentIgnore *Ignore) { continue } if isDir(file, path) { - dirList = append(dirList, file.Name()) + if !ignore.shouldIgnore(file.Name()) { + dirList = append(dirList, file.Name()) + } else { + context.log(STATUS_IGNORE, file.Name()+"/") + } } else if file.Type().IsRegular() { filesToIndex = append(filesToIndex, file.Name()) } } - ignore, err := GetIgnore(context, root, parentIgnore) - if err != nil { - context.logErr(root+"/", err) - } - context.addWork(root, filesToIndex, ignore) for _, name := range dirList { - if !ignore.shouldIgnore(name) { - context.scanDir(filepath.Join(root, name), ignore) - } else { - context.log(STATUS_IGNORE, name+"/") - } + context.scanDir(filepath.Join(root, name), ignore) } } diff --git a/index.go b/index.go index cb4a24d..4c6e2ad 100644 --- a/index.go +++ b/index.go @@ -20,7 +20,8 @@ type IdxInfo struct { } type IndexFile struct { - V int `json:"v"` + V int `json:"v"` + // IdxRaw -> map[string]IdxInfo IdxRaw json.RawMessage `json:"idx"` IdxHash string `json:"idx_hash"` } @@ -40,7 +41,6 @@ type Index struct { files []string cur map[string]IdxInfo new map[string]IdxInfo - updates []string modified bool readonly bool } @@ -120,7 +120,7 @@ func (i *Index) showIgnoredOnly(ignore *Ignore) { } } -func (i *Index) checkFix(force bool) { +func (i *Index) checkFix(forceUpdateDmg bool) { for name, b := range i.new { if a, ok := i.cur[name]; !ok { i.logFile(STATUS_NEW, name) @@ -138,7 +138,8 @@ func (i *Index) checkFix(force bool) { if amod == bmod { i.logFile(STATUS_ERR_DMG, name) - if !force { + if !forceUpdateDmg { + // keep DMG entry i.new[name] = a } else { i.setMod(true) diff --git a/worker.go b/worker.go index d00da7c..c37fb87 100644 --- a/worker.go +++ b/worker.go @@ -13,7 +13,7 @@ func (context *Context) RunWorker(id int) { break } - index := NewIndex(context, item.path, item.filesToIndex, !context.Update) + index := NewIndex(context, item.path, item.filesToIndex, !context.UpdateIndex) err := index.load() if err != nil { context.log(STATUS_PANIC, index.getIndexFilepath()+": "+err.Error()) @@ -23,9 +23,9 @@ func (context *Context) RunWorker(id int) { index.showIgnoredOnly(item.ignore) } else { index.calcHashes(item.ignore) - index.checkFix(context.Force) + index.checkFix(context.ForceUpdateDmg) - if context.Update { + if context.UpdateIndex { if changed, err := index.save(); err != nil { context.logErr(item.path, err) } else if changed {