package Filesystem import ( "os" "path/filepath" "github.com/vbauerster/mpb" bolt "go.etcd.io/bbolt" "PackageManager/Client/Database" "PackageManager/Client/ProgressBar" "PackageManager/Variables" ) func pickFilesSingle(filePath string) error { var ( indexBucket *bolt.Bucket picksBucket *bolt.Bucket err error ) err = Database.FsDB.Batch(func(tx *bolt.Tx) error { indexBucket = tx.Bucket(Variables.FsHashIndexBucket) picksBucket = tx.Bucket(Variables.FsHashPicksBucket) err = AddFileToBucket(picksBucket, filePath) if err != nil { return err } return RemoveFileFromBucket(indexBucket, filePath) }) return err } func pickFilesRecursive(rootPath string) error { var ( fsStatus FilesystemStatus indexBucket *bolt.Bucket picksBucket *bolt.Bucket bar *mpb.Bar totalLen int f string err error ) fsStatus, err = GetFilesystemDiff(rootPath) if err != nil { return err } totalLen = len(fsStatus.NewFiles) + len(fsStatus.ModifiedFiles) + len(fsStatus.MissingFiles) if totalLen == 0 { return nil } bar = ProgressBar.InitBar("Adding...", totalLen) err = Database.FsDB.Batch(func(tx *bolt.Tx) error { indexBucket = tx.Bucket(Variables.FsHashIndexBucket) picksBucket = tx.Bucket(Variables.FsHashPicksBucket) if len(fsStatus.NewFiles) > 0 { for _, f = range fsStatus.NewFiles { bar.Increment() err = AddFileToBucket(picksBucket, f) if err != nil { return err } } } if len(fsStatus.ModifiedFiles) > 0 { for _, f = range fsStatus.ModifiedFiles { bar.Increment() err = AddFileToBucket(picksBucket, f) if err != nil { return err } } } if len(fsStatus.MissingFiles) > 0 { for _, f = range fsStatus.MissingFiles { bar.Increment() err = RemoveFileFromBucket(indexBucket, f) if err != nil { return err } err = RemoveFileFromBucket(picksBucket, f) if err != nil { return err } } } return nil }) return err } func PickFiles(rootPath string) error { var ( realRootPath string rootStat os.FileInfo err error ) realRootPath = filepath.Join(Variables.RootDir, rootPath) rootStat, err = os.Stat(realRootPath) if err != nil { return err } if !rootStat.IsDir() { return pickFilesSingle(realRootPath) } return pickFilesRecursive(realRootPath) } func ResetAllPickedFiles() error { var err error = Database.FsDB.Batch(func(tx *bolt.Tx) error { return tx.DeleteBucket(Variables.FsHashPicksBucket) }) return err }