Skip to content

Commit

Permalink
handle abort with clean shutdown (ctrl+c)
Browse files Browse the repository at this point in the history
  • Loading branch information
laktak committed Feb 25, 2025
1 parent e26eef8 commit 2741f5c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 17 deletions.
15 changes: 13 additions & 2 deletions cmd/chkbit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"log"
"os"
"os/signal"
"path/filepath"
"strings"
"sync"
Expand Down Expand Up @@ -178,11 +179,17 @@ func (m *Main) logStatus(stat chkbit.Status, message string) bool {
return false
}

func (m *Main) showStatus() {
func (m *Main) handleProgress() {

abortChan := make(chan os.Signal, 1)
signal.Notify(abortChan, os.Interrupt)

last := time.Now().Add(-updateInterval)
stat := ""
for {
select {
case <-abortChan:
m.context.Abort()
case item := <-m.context.LogQueue:
if item == nil {
if m.progress == Fancy {
Expand Down Expand Up @@ -284,7 +291,7 @@ func (m *Main) runCmd(cmd Command, cli CLI) int {
wg.Add(1)
go func() {
defer wg.Done()
m.showStatus()
m.handleProgress()
}()
m.context.Process(pathList)
wg.Wait()
Expand All @@ -301,6 +308,10 @@ func (m *Main) runCmd(cmd Command, cli CLI) int {
numNew = 0
}

if m.context.DidAbort() {
m.logInfo(termAlertFG, "Aborted")
}

if m.progress != Quiet {
mode := ""
if !m.context.UpdateIndex {
Expand Down
16 changes: 15 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,22 @@ type Context struct {
store *indexStore

mutex sync.Mutex
doAbort bool
NumTotal int
NumIdxUpd int
NumNew int
NumUpd int
NumDel int
}

func (context *Context) Abort() {
context.doAbort = true
}

func (context *Context) DidAbort() bool {
return context.doAbort
}

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")
Expand Down Expand Up @@ -150,13 +159,18 @@ func (context *Context) Process(pathList []string) {
}()
wg.Wait()

if _, err := context.store.Finish(); err != nil {
if _, err := context.store.Finish(context.doAbort); err != nil {
context.logErr("indexstore", err)
}
context.LogQueue <- nil
}

func (context *Context) scanDir(root string, parentIgnore *Ignore, depth int) {

if context.doAbort {
return
}

files, err := os.ReadDir(root)
if err != nil {
context.logErr(root+"/", err)
Expand Down
2 changes: 1 addition & 1 deletion fuse.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func FuseIndexStore(path, indexName string, skipSymlinks, verbose bool, log Fuse

fuse.fuseScanDir(path, "")

if _, err := store.Finish(); err != nil {
if _, err := store.Finish(false); err != nil {
return err
}

Expand Down
24 changes: 13 additions & 11 deletions indexstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (s *indexStore) Open(readOnly bool, dbQueueSize int) error {
return err
}

func (s *indexStore) Finish() (updated bool, err error) {
func (s *indexStore) Finish(abort bool) (updated bool, err error) {

if !s.atom {
return
Expand Down Expand Up @@ -148,17 +148,19 @@ func (s *indexStore) Finish() (updated bool, err error) {
cacheFile = s.cacheFileW
}

var newFile string
if newFile, err = s.exportCache(cacheFile, newSuffix); err != nil {
return
}
if !abort {
var newFile string
if newFile, err = s.exportCache(cacheFile, newSuffix); err != nil {
return
}

atomFile := getAtomFile(s.atomPath, s.indexName, "")
if err = os.Rename(atomFile, getAtomFile(s.atomPath, s.indexName, bakSuffix)); err != nil {
return
}
if err = os.Rename(newFile, atomFile); err != nil {
return
atomFile := getAtomFile(s.atomPath, s.indexName, "")
if err = os.Rename(atomFile, getAtomFile(s.atomPath, s.indexName, bakSuffix)); err != nil {
return
}
if err = os.Rename(newFile, atomFile); err != nil {
return
}
}

if s.cacheFileR != "" {
Expand Down
4 changes: 2 additions & 2 deletions worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ type WorkItem struct {
ignore *Ignore
}

func (context *Context) runWorker(id int) {
func (context *Context) runWorker(_ int) {
for {
item := <-context.WorkQueue
if item == nil {
if item == nil || context.doAbort {
break
}

Expand Down

0 comments on commit 2741f5c

Please sign in to comment.