This commit is contained in:
Christian Zangl 2024-08-19 22:00:38 +02:00
parent 16f38a9929
commit 4bbd7b421e
No known key found for this signature in database
GPG Key ID: 6D468AC36E2A4B3D
4 changed files with 37 additions and 36 deletions

View File

@ -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"),

View File

@ -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!")
}
@ -34,11 +34,7 @@ func NewContext(numWorkers int, force bool, update bool, showIgnoredOnly bool, h
}
return &Context{
NumWorkers: numWorkers,
Force: force,
Update: update,
ShowIgnoredOnly: showIgnoredOnly,
HashAlgo: hashAlgo,
SkipSymlinks: skipSymlinks,
IndexFilename: indexFilename,
IgnoreFilename: ignoreFilename,
WorkQueue: make(chan *WorkItem, numWorkers*10),
@ -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) {
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+"/")
}
}
}

View File

@ -21,6 +21,7 @@ type IdxInfo struct {
type IndexFile struct {
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)

View File

@ -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 {