refactor
This commit is contained in:
parent
16f38a9929
commit
4bbd7b421e
@ -143,7 +143,7 @@ func (m *Main) showStatus(context *chkbit.Context) {
|
|||||||
statF := fmt.Sprintf("%d files/s", m.fps.Last())
|
statF := fmt.Sprintf("%d files/s", m.fps.Last())
|
||||||
statB := fmt.Sprintf("%d MB/s", m.bps.Last()/sizeMB)
|
statB := fmt.Sprintf("%d MB/s", m.bps.Last()/sizeMB)
|
||||||
stat = "RW"
|
stat = "RW"
|
||||||
if !context.Update {
|
if !context.UpdateIndex {
|
||||||
stat = "RO"
|
stat = "RO"
|
||||||
}
|
}
|
||||||
stat = fmt.Sprintf("[%s:%d] %5d files $ %s %-13s $ %s %-13s",
|
stat = fmt.Sprintf("[%s:%d] %5d files $ %s %-13s $ %s %-13s",
|
||||||
@ -168,11 +168,15 @@ func (m *Main) process() *chkbit.Context {
|
|||||||
return nil
|
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 {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
context.ForceUpdateDmg = cli.Force
|
||||||
|
context.UpdateIndex = cli.Update
|
||||||
|
context.ShowIgnoredOnly = cli.ShowIgnoredOnly
|
||||||
|
context.SkipSymlinks = cli.SkipSymlinks
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
@ -209,7 +213,7 @@ func (m *Main) printResult(context *chkbit.Context) {
|
|||||||
|
|
||||||
if m.progress != Quiet {
|
if m.progress != Quiet {
|
||||||
mode := ""
|
mode := ""
|
||||||
if !context.Update {
|
if !context.UpdateIndex {
|
||||||
mode = " in readonly mode"
|
mode = " in readonly mode"
|
||||||
}
|
}
|
||||||
status := fmt.Sprintf("Processed %s%s.", util.LangNum1MutateSuffix(m.total, "file"), 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)
|
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 {
|
if m.numIdxUpd > 0 {
|
||||||
cprint(termOKFG, fmt.Sprintf("- %s updated\n- %s added\n- %s updated",
|
cprint(termOKFG, fmt.Sprintf("- %s updated\n- %s added\n- %s updated",
|
||||||
util.LangNum1Choice(m.numIdxUpd, "directory was", "directories were"),
|
util.LangNum1Choice(m.numIdxUpd, "directory was", "directories were"),
|
||||||
|
46
context.go
46
context.go
@ -9,8 +9,8 @@ import (
|
|||||||
|
|
||||||
type Context struct {
|
type Context struct {
|
||||||
NumWorkers int
|
NumWorkers int
|
||||||
Force bool
|
ForceUpdateDmg bool
|
||||||
Update bool
|
UpdateIndex bool
|
||||||
ShowIgnoredOnly bool
|
ShowIgnoredOnly bool
|
||||||
HashAlgo string
|
HashAlgo string
|
||||||
SkipSymlinks bool
|
SkipSymlinks bool
|
||||||
@ -22,7 +22,7 @@ type Context struct {
|
|||||||
wg sync.WaitGroup
|
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] != '.' {
|
if indexFilename[0] != '.' {
|
||||||
return nil, errors.New("The index filename must start with a dot!")
|
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 nil, errors.New(hashAlgo + " is unknown.")
|
||||||
}
|
}
|
||||||
return &Context{
|
return &Context{
|
||||||
NumWorkers: numWorkers,
|
NumWorkers: numWorkers,
|
||||||
Force: force,
|
HashAlgo: hashAlgo,
|
||||||
Update: update,
|
IndexFilename: indexFilename,
|
||||||
ShowIgnoredOnly: showIgnoredOnly,
|
IgnoreFilename: ignoreFilename,
|
||||||
HashAlgo: hashAlgo,
|
WorkQueue: make(chan *WorkItem, numWorkers*10),
|
||||||
SkipSymlinks: skipSymlinks,
|
LogQueue: make(chan *LogEvent, numWorkers*100),
|
||||||
IndexFilename: indexFilename,
|
PerfQueue: make(chan *PerfEvent, numWorkers*10),
|
||||||
IgnoreFilename: ignoreFilename,
|
|
||||||
WorkQueue: make(chan *WorkItem, numWorkers*10),
|
|
||||||
LogQueue: make(chan *LogEvent, numWorkers*100),
|
|
||||||
PerfQueue: make(chan *PerfEvent, numWorkers*10),
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,6 +117,11 @@ func (context *Context) scanDir(root string, parentIgnore *Ignore) {
|
|||||||
var dirList []string
|
var dirList []string
|
||||||
var filesToIndex []string
|
var filesToIndex []string
|
||||||
|
|
||||||
|
ignore, err := GetIgnore(context, root, parentIgnore)
|
||||||
|
if err != nil {
|
||||||
|
context.logErr(root+"/", err)
|
||||||
|
}
|
||||||
|
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
path := filepath.Join(root, file.Name())
|
path := filepath.Join(root, file.Name())
|
||||||
if file.Name()[0] == '.' {
|
if file.Name()[0] == '.' {
|
||||||
@ -130,24 +131,19 @@ func (context *Context) scanDir(root string, parentIgnore *Ignore) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if isDir(file, path) {
|
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() {
|
} else if file.Type().IsRegular() {
|
||||||
filesToIndex = append(filesToIndex, file.Name())
|
filesToIndex = append(filesToIndex, file.Name())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ignore, err := GetIgnore(context, root, parentIgnore)
|
|
||||||
if err != nil {
|
|
||||||
context.logErr(root+"/", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
context.addWork(root, filesToIndex, ignore)
|
context.addWork(root, filesToIndex, ignore)
|
||||||
|
|
||||||
for _, name := range dirList {
|
for _, name := range dirList {
|
||||||
if !ignore.shouldIgnore(name) {
|
context.scanDir(filepath.Join(root, name), ignore)
|
||||||
context.scanDir(filepath.Join(root, name), ignore)
|
|
||||||
} else {
|
|
||||||
context.log(STATUS_IGNORE, name+"/")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
9
index.go
9
index.go
@ -20,7 +20,8 @@ type IdxInfo struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type IndexFile struct {
|
type IndexFile struct {
|
||||||
V int `json:"v"`
|
V int `json:"v"`
|
||||||
|
// IdxRaw -> map[string]IdxInfo
|
||||||
IdxRaw json.RawMessage `json:"idx"`
|
IdxRaw json.RawMessage `json:"idx"`
|
||||||
IdxHash string `json:"idx_hash"`
|
IdxHash string `json:"idx_hash"`
|
||||||
}
|
}
|
||||||
@ -40,7 +41,6 @@ type Index struct {
|
|||||||
files []string
|
files []string
|
||||||
cur map[string]IdxInfo
|
cur map[string]IdxInfo
|
||||||
new map[string]IdxInfo
|
new map[string]IdxInfo
|
||||||
updates []string
|
|
||||||
modified bool
|
modified bool
|
||||||
readonly 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 {
|
for name, b := range i.new {
|
||||||
if a, ok := i.cur[name]; !ok {
|
if a, ok := i.cur[name]; !ok {
|
||||||
i.logFile(STATUS_NEW, name)
|
i.logFile(STATUS_NEW, name)
|
||||||
@ -138,7 +138,8 @@ func (i *Index) checkFix(force bool) {
|
|||||||
|
|
||||||
if amod == bmod {
|
if amod == bmod {
|
||||||
i.logFile(STATUS_ERR_DMG, name)
|
i.logFile(STATUS_ERR_DMG, name)
|
||||||
if !force {
|
if !forceUpdateDmg {
|
||||||
|
// keep DMG entry
|
||||||
i.new[name] = a
|
i.new[name] = a
|
||||||
} else {
|
} else {
|
||||||
i.setMod(true)
|
i.setMod(true)
|
||||||
|
@ -13,7 +13,7 @@ func (context *Context) RunWorker(id int) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
index := NewIndex(context, item.path, item.filesToIndex, !context.Update)
|
index := NewIndex(context, item.path, item.filesToIndex, !context.UpdateIndex)
|
||||||
err := index.load()
|
err := index.load()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
context.log(STATUS_PANIC, index.getIndexFilepath()+": "+err.Error())
|
context.log(STATUS_PANIC, index.getIndexFilepath()+": "+err.Error())
|
||||||
@ -23,9 +23,9 @@ func (context *Context) RunWorker(id int) {
|
|||||||
index.showIgnoredOnly(item.ignore)
|
index.showIgnoredOnly(item.ignore)
|
||||||
} else {
|
} else {
|
||||||
index.calcHashes(item.ignore)
|
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 {
|
if changed, err := index.save(); err != nil {
|
||||||
context.logErr(item.path, err)
|
context.logErr(item.path, err)
|
||||||
} else if changed {
|
} else if changed {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user