From c6ac8394184f2ed16a3ec978baa5344cb41e2355 Mon Sep 17 00:00:00 2001 From: Tovi Jaeschke-Rogers Date: Sun, 18 Jul 2021 22:00:01 +0930 Subject: [PATCH] Add progress bar and increase ignored files --- Client/Filesystem/CommitFiles.go | 18 +++--- Client/Filesystem/FilesystemDiff.go | 86 +++++++++++++++++------------ Client/Filesystem/PickFiles.go | 42 +++++++------- Client/Filesystem/Print.go | 24 ++++++++ Client/ProgressBar/Bar.go | 41 ++++++++++++++ Variables/Variables.go | 19 +++++++ 6 files changed, 164 insertions(+), 66 deletions(-) create mode 100644 Client/Filesystem/Print.go create mode 100644 Client/ProgressBar/Bar.go diff --git a/Client/Filesystem/CommitFiles.go b/Client/Filesystem/CommitFiles.go index 5886fce..0b47f19 100644 --- a/Client/Filesystem/CommitFiles.go +++ b/Client/Filesystem/CommitFiles.go @@ -2,9 +2,10 @@ package Filesystem import ( "PackageManager/Client/Database" + "PackageManager/Client/ProgressBar" "PackageManager/Variables" - "fmt" + "github.com/vbauerster/mpb" bolt "go.etcd.io/bbolt" ) @@ -12,6 +13,7 @@ func CommitFiles() error { var ( fsStatus FilesystemStatus indexBucket *bolt.Bucket + bar *mpb.Bar f string e error ) @@ -25,20 +27,20 @@ func CommitFiles() error { indexBucket = tx.Bucket(Variables.FsHashIndexBucket) if len(fsStatus.PickedFiles) > 0 { + bar = ProgressBar.InitBar("Commiting...", len(fsStatus.PickedFiles)) for _, f = range fsStatus.PickedFiles { - fmt.Println(f) e = AddFileToBucket(indexBucket, f) + bar.Increment() if e != nil { return e } } - /* - e = tx.DeleteBucket(Variables.FsHashPicksBucket) - if e != nil { - return e - } - */ + e = tx.DeleteBucket(Variables.FsHashPicksBucket) + if e != nil { + return e + } + ProgressBar.CloseBar(bar) } return nil diff --git a/Client/Filesystem/FilesystemDiff.go b/Client/Filesystem/FilesystemDiff.go index 9f49260..5d023b1 100644 --- a/Client/Filesystem/FilesystemDiff.go +++ b/Client/Filesystem/FilesystemDiff.go @@ -2,6 +2,7 @@ package Filesystem import ( "PackageManager/Client/Database" + "PackageManager/Color" "PackageManager/Variables" "fmt" "log" @@ -30,47 +31,60 @@ func ShowFilesystemDiff(root string) error { return e } - if len(fsStatus.NewFiles) > 0 { - fmt.Printf("New files: %d\n", len(fsStatus.NewFiles)) - /* - fmt.Println("New files:") - for _, f = range fsStatus.NewFiles { - fmt.Printf("\t%s\n", Color.Green(f)) - } - */ - } + fmt.Println("New files:") + PrintFilesOrLength(fsStatus.NewFiles, Color.Green) - if len(fsStatus.PickedFiles) > 0 { - fmt.Printf("Picked files: %d\n", len(fsStatus.PickedFiles)) - /* - fmt.Println("Added files:") - for _, f = range fsStatus.PickedFiles { - fmt.Printf("\t%s\n", Color.Green(f)) - } - */ - } + fmt.Println("Added files:") + PrintFilesOrLength(fsStatus.PickedFiles, Color.Green) - if len(fsStatus.ModifiedFiles) > 0 { - fmt.Printf("Modified files: %d\n", len(fsStatus.ModifiedFiles)) - /* - fmt.Println("Modified files:") - for _, f = range fsStatus.ModifiedFiles { - fmt.Printf("\t%s\n", Color.Green(f)) - } - */ + fmt.Println("Modified files:") + PrintFilesOrLength(fsStatus.ModifiedFiles, Color.Warning) + + fmt.Println("Deleted files:") + PrintFilesOrLength(fsStatus.MissingFiles, Color.Fatal) + + return nil +} + +func GetFilesystemLength(root string) (int, error) { + var ( + rootStat os.FileInfo + fsCount int = 0 + e error + ) + + rootStat, e = os.Stat(root) + if e != nil { + return fsCount, e } - if len(fsStatus.MissingFiles) > 0 { - fmt.Printf("Modified files: %d\n", len(fsStatus.MissingFiles)) - /* - fmt.Println("Deleted files:") - for _, f = range fsStatus.MissingFiles { - fmt.Printf("\t%s\n", Color.Green(f)) - } - */ + if rootStat.IsDir() && root[len(root)-1:] != "/" { + root = root + "/" } - return nil + filepath.Walk(root, func(p string, i os.FileInfo, _ error) error { + + // Ignore path in Variables.PruneRegexPaths + if i.IsDir() && matchAny(p, PruneRegex) { + log.Println("Prune", p) + return filepath.SkipDir + } + + // Ignore path in Variables.IgnoreRegexPaths + if matchAny(p, IgnoreRegex) { + return nil + } + + if !i.Mode().IsRegular() && (i.Mode()&os.ModeSymlink == 0) { + return nil + } + + fsCount++ + + return nil + }) + + return fsCount, e } func GetFilesystemDiff(root string) (FilesystemStatus, error) { @@ -102,7 +116,7 @@ func GetFilesystemDiff(root string) (FilesystemStatus, error) { e = Database.FsDB.View(func(tx *bolt.Tx) error { picksBucket = tx.Bucket(Variables.FsHashPicksBucket) - indexBucket = tx.Bucket(Variables.FsHashPicksBucket) + indexBucket = tx.Bucket(Variables.FsHashIndexBucket) filepath.Walk(root, func(p string, i os.FileInfo, _ error) error { diff --git a/Client/Filesystem/PickFiles.go b/Client/Filesystem/PickFiles.go index 1c31779..fd908a0 100644 --- a/Client/Filesystem/PickFiles.go +++ b/Client/Filesystem/PickFiles.go @@ -2,8 +2,10 @@ package Filesystem import ( "PackageManager/Client/Database" + "PackageManager/Client/ProgressBar" "PackageManager/Variables" + "github.com/vbauerster/mpb" bolt "go.etcd.io/bbolt" ) @@ -11,6 +13,8 @@ func PickFiles(rootPath string) error { var ( fsStatus FilesystemStatus picksBucket *bolt.Bucket + totalLen int + bar *mpb.Bar f string e error ) @@ -20,12 +24,21 @@ func PickFiles(rootPath string) error { return e } + totalLen = len(fsStatus.NewFiles) + len(fsStatus.ModifiedFiles) + len(fsStatus.MissingFiles) + + if totalLen == 0 { + return nil + } + + bar = ProgressBar.InitBar("Adding...", totalLen) + e = Database.FsDB.Batch(func(tx *bolt.Tx) error { picksBucket = tx.Bucket(Variables.FsHashPicksBucket) if len(fsStatus.NewFiles) > 0 { for _, f = range fsStatus.NewFiles { e = AddFileToBucket(picksBucket, f) + bar.Increment() if e != nil { return e } @@ -33,8 +46,9 @@ func PickFiles(rootPath string) error { } if len(fsStatus.ModifiedFiles) > 0 { - for _, f = range fsStatus.NewFiles { + for _, f = range fsStatus.ModifiedFiles { e = AddFileToBucket(picksBucket, f) + bar.Increment() if e != nil { return e } @@ -44,6 +58,7 @@ func PickFiles(rootPath string) error { if len(fsStatus.MissingFiles) > 0 { for _, f = range fsStatus.NewFiles { e = RemoveFileFromBucket(picksBucket, f) + bar.Increment() if e != nil { return e } @@ -53,35 +68,18 @@ func PickFiles(rootPath string) error { return nil }) + ProgressBar.CloseBar(bar) + return e } func ResetAllPickedFiles() error { var ( - fsStatus FilesystemStatus - picksBucket *bolt.Bucket - f string - e error + e error ) - fsStatus, e = GetFilesystemDiff(Variables.RootDir) - if e != nil { - return e - } - e = Database.FsDB.Batch(func(tx *bolt.Tx) error { - picksBucket = tx.Bucket(Variables.FsHashPicksBucket) - - if len(fsStatus.PickedFiles) > 0 { - for _, f = range fsStatus.PickedFiles { - e = RemoveFileFromBucket(picksBucket, f) - if e != nil { - return e - } - } - } - - return nil + return tx.DeleteBucket(Variables.FsHashPicksBucket) }) return e diff --git a/Client/Filesystem/Print.go b/Client/Filesystem/Print.go new file mode 100644 index 0000000..cfeaef0 --- /dev/null +++ b/Client/Filesystem/Print.go @@ -0,0 +1,24 @@ +package Filesystem + +import ( + "fmt" +) + +func PrintFiles(files []string, color func(...interface{}) string) { + var f string + for _, f = range files { + fmt.Printf("\t%s\n", color(f)) + } +} + +func PrintFilesLength(files []string) { + fmt.Printf("\t%d files found\n", len(files)) +} + +func PrintFilesOrLength(files []string, color func(...interface{}) string) { + if len(files) < 25 && len(files) > 0 { + PrintFiles(files, color) + return + } + PrintFilesLength(files) +} diff --git a/Client/ProgressBar/Bar.go b/Client/ProgressBar/Bar.go new file mode 100644 index 0000000..2e0ade7 --- /dev/null +++ b/Client/ProgressBar/Bar.go @@ -0,0 +1,41 @@ +package ProgressBar + +import ( + "sync" + + "github.com/vbauerster/mpb" + "github.com/vbauerster/mpb/decor" +) + +var ( + BarWG sync.WaitGroup + P = mpb.New(mpb.WithWaitGroup(&BarWG)) +) + +func InitBar(name string, total int) *mpb.Bar { + var ( + bar *mpb.Bar + ) + + BarWG.Add(1) + + bar = P.AddBar(int64(total), + mpb.PrependDecorators( + decor.Name(name), + decor.Percentage(decor.WCSyncSpace), + ), + mpb.AppendDecorators( + // replace ETA decorator with "done" message, OnComplete event + decor.OnComplete( + decor.AverageETA(decor.ET_STYLE_GO, decor.WC{W: 4}), "done", + ), + ), + ) + + return bar +} + +func CloseBar(bar *mpb.Bar) { + bar.Abort(false) + BarWG.Done() +} diff --git a/Variables/Variables.go b/Variables/Variables.go index fd12a60..a52aa55 100644 --- a/Variables/Variables.go +++ b/Variables/Variables.go @@ -27,6 +27,21 @@ var ( "^/home$", "^/build$", "^/tools$", + "^/run/user$", + "^/usr/share/zoneinfo-leaps$", + "^/usr/share/zoneinfo$", + "^/usr/share/zsh$", + "^/tmp$", + "^/var/db$", + "^/var/cache$", + "^/var/log$", + "^/var/spool$", + "^/var/lib/texmf$", + "^/var/lib/postgres$", + "^/var/lib/pacman$", + "^/var/lib/NetworkManager$", + "^/var/lib/systemd$", + "^/var/lib/xkb/README.compiled$", "/lost\\+found$", } @@ -35,6 +50,10 @@ var ( "^/etc/passwd-$", "^/etc/group$", "^/etc/group-$", + "^/var/.updated$", + "^/var/lib/mlocate/mlocate.db$", + "^/var/lib/krb5kdc/kdc.conf$", + "^/var/lib/alsa/asound.state$", } )