2022-09-08 15:57:59 +00:00
|
|
|
/*
|
2023-01-18 17:19:29 +00:00
|
|
|
Copyright © 2023 Seednode <seednode@seedno.de>
|
2022-09-08 15:57:59 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-09-09 22:21:03 +00:00
|
|
|
"errors"
|
2022-09-17 16:07:37 +00:00
|
|
|
"fmt"
|
2023-09-10 17:16:50 +00:00
|
|
|
"math/big"
|
2023-09-12 02:22:26 +00:00
|
|
|
"regexp"
|
2022-10-28 23:32:03 +00:00
|
|
|
|
2023-09-10 17:16:50 +00:00
|
|
|
"crypto/rand"
|
2022-09-08 15:57:59 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2022-09-17 16:07:37 +00:00
|
|
|
"strconv"
|
2022-09-24 22:17:56 +00:00
|
|
|
"strings"
|
2022-10-20 19:22:01 +00:00
|
|
|
"sync"
|
2022-09-08 15:57:59 +00:00
|
|
|
"time"
|
2022-09-10 00:00:02 +00:00
|
|
|
|
2023-09-12 21:32:19 +00:00
|
|
|
"seedno.de/seednode/roulette/types"
|
2022-09-08 15:57:59 +00:00
|
|
|
)
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
type regexes struct {
|
2023-09-12 02:22:26 +00:00
|
|
|
alphanumeric *regexp.Regexp
|
|
|
|
filename *regexp.Regexp
|
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
type scanStats struct {
|
2023-09-15 18:51:04 +00:00
|
|
|
filesMatched int
|
|
|
|
filesSkipped int
|
|
|
|
directoriesMatched int
|
|
|
|
directoriesSkipped int
|
2023-08-02 18:29:37 +00:00
|
|
|
}
|
|
|
|
|
2023-09-16 23:04:44 +00:00
|
|
|
type scanStatsChannels struct {
|
|
|
|
filesMatched chan int
|
|
|
|
filesSkipped chan int
|
|
|
|
directoriesMatched chan int
|
|
|
|
directoriesSkipped chan int
|
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
type splitPath struct {
|
2023-01-27 16:06:10 +00:00
|
|
|
base string
|
2023-09-28 08:36:26 +00:00
|
|
|
number string
|
2023-01-27 16:06:10 +00:00
|
|
|
extension string
|
2023-01-21 04:42:44 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
func (splitPath *splitPath) increment() {
|
2023-09-28 08:36:26 +00:00
|
|
|
length := len(splitPath.number)
|
|
|
|
|
|
|
|
asInt, err := strconv.Atoi(splitPath.number)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
splitPath.number = fmt.Sprintf("%0*d", length, asInt+1)
|
2023-01-21 04:42:44 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
func (splitPath *splitPath) decrement() {
|
2023-09-28 08:36:26 +00:00
|
|
|
length := len(splitPath.number)
|
|
|
|
|
|
|
|
asInt, err := strconv.Atoi(splitPath.number)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
splitPath.number = fmt.Sprintf("%0*d", length, asInt-1)
|
2023-01-25 01:06:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func humanReadableSize(bytes int) string {
|
|
|
|
const unit = 1000
|
|
|
|
|
|
|
|
if bytes < unit {
|
|
|
|
return fmt.Sprintf("%d B", bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
div, exp := int64(unit), 0
|
|
|
|
|
|
|
|
for n := bytes / unit; n >= unit; n /= unit {
|
|
|
|
div *= unit
|
|
|
|
exp++
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%.1f %cB",
|
|
|
|
float64(bytes)/float64(div), "KMGTPE"[exp])
|
|
|
|
}
|
|
|
|
|
2023-09-15 20:13:45 +00:00
|
|
|
func kill(path string, cache *fileCache) error {
|
|
|
|
err := os.Remove(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if Cache {
|
|
|
|
cache.remove(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-28 08:36:26 +00:00
|
|
|
func split(path string, regexes *regexes) (*splitPath, int, error) {
|
2023-09-17 01:15:39 +00:00
|
|
|
p := splitPath{}
|
|
|
|
|
|
|
|
split := regexes.filename.FindAllStringSubmatch(path, -1)
|
|
|
|
|
|
|
|
if len(split) < 1 || len(split[0]) < 3 {
|
2023-09-28 08:36:26 +00:00
|
|
|
return &splitPath{}, 0, nil
|
2023-09-17 01:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p.base = split[0][1]
|
|
|
|
|
2023-09-28 08:36:26 +00:00
|
|
|
p.number = split[0][2]
|
2023-01-25 01:06:15 +00:00
|
|
|
|
2023-09-17 01:15:39 +00:00
|
|
|
p.extension = split[0][3]
|
|
|
|
|
2023-09-28 08:36:26 +00:00
|
|
|
return &p, len(p.number), nil
|
2023-01-19 18:07:15 +00:00
|
|
|
}
|
|
|
|
|
2023-09-17 01:15:39 +00:00
|
|
|
func newFile(list []string, sortOrder string, regexes *regexes, formats *types.Types) (string, error) {
|
|
|
|
path, err := pickFile(list)
|
2022-11-05 16:00:42 +00:00
|
|
|
if err != nil {
|
2023-09-15 18:51:04 +00:00
|
|
|
return "", err
|
2022-11-05 16:00:42 +00:00
|
|
|
}
|
2022-09-18 18:07:46 +00:00
|
|
|
|
2023-09-28 08:36:26 +00:00
|
|
|
if sortOrder == "asc" || sortOrder == "desc" {
|
|
|
|
splitPath, length, err := split(path, regexes)
|
2022-10-18 18:42:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-09-28 08:36:26 +00:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case sortOrder == "asc":
|
|
|
|
splitPath.number = fmt.Sprintf("%0*d", length, 1)
|
2022-10-18 18:42:32 +00:00
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
path, err = tryExtensions(splitPath, formats)
|
2022-10-18 21:46:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-09-28 08:36:26 +00:00
|
|
|
case sortOrder == "desc":
|
|
|
|
for {
|
|
|
|
splitPath.increment()
|
2022-09-18 18:07:46 +00:00
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
path, err = tryExtensions(splitPath, formats)
|
2022-11-05 16:00:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-09-17 17:42:25 +00:00
|
|
|
|
2023-09-28 08:36:26 +00:00
|
|
|
if path == "" {
|
|
|
|
splitPath.decrement()
|
|
|
|
|
|
|
|
path, err = tryExtensions(splitPath, formats)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
2022-11-05 16:00:42 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-17 16:07:37 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
return path, nil
|
2022-10-18 21:40:13 +00:00
|
|
|
}
|
|
|
|
|
2023-09-28 07:47:43 +00:00
|
|
|
func nextFile(filePath, sortOrder string, regexes *regexes, formats *types.Types) (string, error) {
|
2023-09-28 08:36:26 +00:00
|
|
|
splitPath, _, err := split(filePath, regexes)
|
2022-11-05 16:17:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-11-05 16:00:42 +00:00
|
|
|
switch {
|
|
|
|
case sortOrder == "asc":
|
2023-09-13 14:26:15 +00:00
|
|
|
splitPath.increment()
|
2022-11-05 16:00:42 +00:00
|
|
|
case sortOrder == "desc":
|
2023-09-13 14:26:15 +00:00
|
|
|
splitPath.decrement()
|
2022-11-05 16:00:42 +00:00
|
|
|
default:
|
|
|
|
return "", nil
|
|
|
|
}
|
2022-10-18 21:40:13 +00:00
|
|
|
|
2023-09-28 07:47:43 +00:00
|
|
|
path, err := tryExtensions(splitPath, formats)
|
2022-09-17 16:07:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-09-28 07:47:43 +00:00
|
|
|
return path, err
|
2022-10-18 18:42:32 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
func tryExtensions(splitPath *splitPath, formats *types.Types) (string, error) {
|
2023-09-28 07:47:43 +00:00
|
|
|
var path string
|
2023-01-25 01:06:15 +00:00
|
|
|
|
2023-09-13 00:46:27 +00:00
|
|
|
for extension := range formats.Extensions {
|
2023-09-28 08:36:26 +00:00
|
|
|
path = fmt.Sprintf("%s%s%s", splitPath.base, splitPath.number, extension)
|
2023-01-25 01:06:15 +00:00
|
|
|
|
2023-09-28 07:47:43 +00:00
|
|
|
exists, err := fileExists(path)
|
2023-01-24 18:03:26 +00:00
|
|
|
if err != nil {
|
2023-01-25 01:06:15 +00:00
|
|
|
return "", err
|
2023-01-24 18:03:26 +00:00
|
|
|
}
|
2022-10-18 21:46:55 +00:00
|
|
|
|
2023-01-25 01:06:15 +00:00
|
|
|
if exists {
|
2023-09-28 07:47:43 +00:00
|
|
|
return path, nil
|
2023-01-24 18:03:26 +00:00
|
|
|
}
|
2022-09-26 17:31:45 +00:00
|
|
|
}
|
|
|
|
|
2023-01-25 01:06:15 +00:00
|
|
|
return "", nil
|
|
|
|
}
|
2022-09-26 17:31:45 +00:00
|
|
|
|
2023-01-25 01:06:15 +00:00
|
|
|
func fileExists(path string) (bool, error) {
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
switch {
|
|
|
|
case err == nil:
|
|
|
|
return true, nil
|
|
|
|
case errors.Is(err, os.ErrNotExist):
|
|
|
|
return false, nil
|
|
|
|
default:
|
|
|
|
return false, err
|
|
|
|
}
|
2022-09-17 16:07:37 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
func pathIsValid(path string, paths []string) bool {
|
2022-10-18 18:42:32 +00:00
|
|
|
var matchesPrefix = false
|
2022-10-20 22:12:29 +00:00
|
|
|
|
2022-10-18 18:42:32 +00:00
|
|
|
for i := 0; i < len(paths); i++ {
|
2023-09-13 14:26:15 +00:00
|
|
|
if strings.HasPrefix(path, paths[i]) {
|
2022-10-18 18:42:32 +00:00
|
|
|
matchesPrefix = true
|
|
|
|
}
|
|
|
|
}
|
2022-10-20 22:55:25 +00:00
|
|
|
|
2022-10-25 05:06:57 +00:00
|
|
|
switch {
|
2023-09-12 18:06:45 +00:00
|
|
|
case Verbose && !matchesPrefix:
|
2023-09-26 10:29:55 +00:00
|
|
|
fmt.Printf("%s | ERROR: File outside specified path(s): %s\n",
|
2023-09-13 14:26:15 +00:00
|
|
|
time.Now().Format(logDate),
|
|
|
|
path,
|
2022-10-20 22:55:25 +00:00
|
|
|
)
|
2022-10-18 18:42:32 +00:00
|
|
|
|
|
|
|
return false
|
2022-10-25 05:06:57 +00:00
|
|
|
case !matchesPrefix:
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
return true
|
2022-10-18 18:42:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-28 07:47:43 +00:00
|
|
|
func hasSupportedFiles(path string, formats *types.Types) (bool, error) {
|
2023-09-11 17:09:08 +00:00
|
|
|
hasRegisteredFiles := make(chan bool, 1)
|
2023-04-11 09:44:18 +00:00
|
|
|
|
|
|
|
err := filepath.WalkDir(path, func(p string, info os.DirEntry, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
2023-09-12 18:06:45 +00:00
|
|
|
case !Recursive && info.IsDir() && p != path:
|
2023-04-11 09:44:18 +00:00
|
|
|
return filepath.SkipDir
|
2023-09-15 00:10:55 +00:00
|
|
|
case !info.IsDir() && formats.Validate(p):
|
|
|
|
hasRegisteredFiles <- true
|
2023-09-14 22:37:22 +00:00
|
|
|
|
2023-09-15 00:10:55 +00:00
|
|
|
return filepath.SkipAll
|
2023-04-11 09:44:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2023-09-11 17:09:08 +00:00
|
|
|
case <-hasRegisteredFiles:
|
2023-04-11 09:44:18 +00:00
|
|
|
return true, nil
|
|
|
|
default:
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-15 18:51:04 +00:00
|
|
|
func pathCount(path string) (int, int, error) {
|
|
|
|
var directories = 0
|
|
|
|
var files = 0
|
2023-08-02 18:29:37 +00:00
|
|
|
|
|
|
|
nodes, err := os.ReadDir(path)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, node := range nodes {
|
|
|
|
if node.IsDir() {
|
|
|
|
directories++
|
|
|
|
} else {
|
|
|
|
files++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return files, directories, nil
|
|
|
|
}
|
|
|
|
|
2023-09-17 01:15:39 +00:00
|
|
|
func walkPath(path string, fileChannel chan<- string, fileScans chan int, stats *scanStatsChannels, formats *types.Types) error {
|
2022-10-20 19:22:01 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2023-09-16 23:04:44 +00:00
|
|
|
errorChannel := make(chan error)
|
|
|
|
done := make(chan bool, 1)
|
2022-09-08 20:30:51 +00:00
|
|
|
|
2023-09-16 23:04:44 +00:00
|
|
|
filepath.WalkDir(path, func(p string, info os.DirEntry, err error) error {
|
2022-09-24 22:17:56 +00:00
|
|
|
switch {
|
2023-09-12 18:06:45 +00:00
|
|
|
case !Recursive && info.IsDir() && p != path:
|
2022-09-24 23:59:10 +00:00
|
|
|
return filepath.SkipDir
|
2022-09-25 01:54:51 +00:00
|
|
|
case !info.IsDir():
|
2022-10-20 19:22:01 +00:00
|
|
|
wg.Add(1)
|
2023-09-16 23:04:44 +00:00
|
|
|
fileScans <- 1
|
2022-10-20 19:22:01 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
wg.Done()
|
2023-09-16 23:04:44 +00:00
|
|
|
<-fileScans
|
2022-10-20 19:22:01 +00:00
|
|
|
}()
|
|
|
|
|
2023-04-11 12:19:06 +00:00
|
|
|
path, err := normalizePath(p)
|
|
|
|
if err != nil {
|
2023-09-15 18:51:04 +00:00
|
|
|
errorChannel <- err
|
2023-09-16 23:04:44 +00:00
|
|
|
|
|
|
|
return
|
2023-04-11 12:19:06 +00:00
|
|
|
}
|
|
|
|
|
2023-09-15 06:06:52 +00:00
|
|
|
if !formats.Validate(path) {
|
2023-09-16 23:04:44 +00:00
|
|
|
stats.filesSkipped <- 1
|
2023-09-15 06:06:52 +00:00
|
|
|
|
|
|
|
return
|
2022-10-20 19:22:01 +00:00
|
|
|
}
|
2023-09-15 06:06:52 +00:00
|
|
|
|
2023-09-15 18:51:04 +00:00
|
|
|
fileChannel <- path
|
2023-09-15 06:06:52 +00:00
|
|
|
|
2023-09-16 23:04:44 +00:00
|
|
|
stats.filesMatched <- 1
|
2022-10-20 19:22:01 +00:00
|
|
|
}()
|
2022-10-20 16:17:40 +00:00
|
|
|
case info.IsDir():
|
2023-08-02 18:29:37 +00:00
|
|
|
files, directories, err := pathCount(p)
|
|
|
|
if err != nil {
|
2023-09-15 18:51:04 +00:00
|
|
|
errorChannel <- err
|
2023-08-02 18:29:37 +00:00
|
|
|
}
|
|
|
|
|
2023-09-26 08:49:20 +00:00
|
|
|
if files > 0 && (files < MinFileCount) || (files > MaxFileCount) {
|
2023-08-02 18:29:37 +00:00
|
|
|
// This count will not otherwise include the parent directory itself, so increment by one
|
2023-09-16 23:04:44 +00:00
|
|
|
stats.directoriesSkipped <- directories + 1
|
|
|
|
stats.filesSkipped <- files
|
2023-08-02 18:29:37 +00:00
|
|
|
|
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
|
|
|
|
2023-09-16 23:04:44 +00:00
|
|
|
stats.directoriesMatched <- 1
|
2022-09-08 17:12:58 +00:00
|
|
|
}
|
2022-09-24 22:17:56 +00:00
|
|
|
|
2023-09-16 23:04:44 +00:00
|
|
|
return nil
|
2022-09-08 17:12:58 +00:00
|
|
|
})
|
2022-10-20 19:22:01 +00:00
|
|
|
|
2023-09-16 23:04:44 +00:00
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
done <- true
|
|
|
|
}()
|
2023-09-15 18:51:04 +00:00
|
|
|
|
2023-09-16 23:04:44 +00:00
|
|
|
Poll:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case e := <-errorChannel:
|
|
|
|
return e
|
|
|
|
case <-done:
|
|
|
|
break Poll
|
|
|
|
}
|
2022-09-08 18:12:50 +00:00
|
|
|
}
|
2023-09-16 23:04:44 +00:00
|
|
|
|
|
|
|
return nil
|
2022-09-08 17:12:58 +00:00
|
|
|
}
|
|
|
|
|
2023-09-15 18:51:04 +00:00
|
|
|
func scanPaths(paths []string, sort string, cache *fileCache, formats *types.Types) ([]string, error) {
|
|
|
|
var list []string
|
|
|
|
|
|
|
|
fileChannel := make(chan string)
|
|
|
|
errorChannel := make(chan error)
|
2023-09-26 08:34:50 +00:00
|
|
|
directoryScans := make(chan int, MaxDirScans)
|
|
|
|
fileScans := make(chan int, MaxFileScans)
|
2023-09-15 18:51:04 +00:00
|
|
|
done := make(chan bool, 1)
|
2022-09-25 01:28:34 +00:00
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
stats := &scanStats{
|
2023-09-15 18:51:04 +00:00
|
|
|
filesMatched: 0,
|
|
|
|
filesSkipped: 0,
|
|
|
|
directoriesMatched: 0,
|
|
|
|
directoriesSkipped: 0,
|
2023-01-25 01:06:15 +00:00
|
|
|
}
|
2022-09-08 15:57:59 +00:00
|
|
|
|
2023-09-16 23:04:44 +00:00
|
|
|
statsChannels := &scanStatsChannels{
|
|
|
|
filesMatched: make(chan int),
|
|
|
|
filesSkipped: make(chan int),
|
|
|
|
directoriesMatched: make(chan int),
|
|
|
|
directoriesSkipped: make(chan int),
|
2023-01-25 01:06:15 +00:00
|
|
|
}
|
2022-10-20 16:17:40 +00:00
|
|
|
|
2023-01-25 01:06:15 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
startTime := time.Now()
|
|
|
|
|
|
|
|
for i := 0; i < len(paths); i++ {
|
|
|
|
wg.Add(1)
|
2023-09-16 23:04:44 +00:00
|
|
|
directoryScans <- 1
|
2023-01-25 01:06:15 +00:00
|
|
|
|
|
|
|
go func(i int) {
|
|
|
|
defer func() {
|
|
|
|
wg.Done()
|
2023-09-16 23:04:44 +00:00
|
|
|
<-directoryScans
|
2023-01-25 01:06:15 +00:00
|
|
|
}()
|
|
|
|
|
2023-09-17 01:15:39 +00:00
|
|
|
err := walkPath(paths[i], fileChannel, fileScans, statsChannels, formats)
|
2023-09-16 23:04:44 +00:00
|
|
|
if err != nil {
|
|
|
|
errorChannel <- err
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2023-01-25 01:06:15 +00:00
|
|
|
}(i)
|
2022-11-05 16:00:42 +00:00
|
|
|
}
|
2022-09-09 22:21:03 +00:00
|
|
|
|
2023-09-15 18:51:04 +00:00
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
done <- true
|
|
|
|
}()
|
|
|
|
|
|
|
|
Poll:
|
|
|
|
for {
|
|
|
|
select {
|
2023-09-28 07:47:43 +00:00
|
|
|
case path := <-fileChannel:
|
|
|
|
list = append(list, path)
|
|
|
|
case stat := <-statsChannels.filesMatched:
|
|
|
|
stats.filesMatched = stats.filesMatched + stat
|
|
|
|
case stat := <-statsChannels.filesSkipped:
|
|
|
|
stats.filesSkipped = stats.filesSkipped + stat
|
|
|
|
case stat := <-statsChannels.directoriesMatched:
|
|
|
|
stats.directoriesMatched = stats.directoriesMatched + stat
|
|
|
|
case stat := <-statsChannels.directoriesSkipped:
|
|
|
|
stats.directoriesSkipped = stats.directoriesSkipped + stat
|
|
|
|
case err := <-errorChannel:
|
|
|
|
return []string{}, err
|
2023-09-15 18:51:04 +00:00
|
|
|
case <-done:
|
|
|
|
break Poll
|
|
|
|
}
|
|
|
|
}
|
2022-09-08 17:12:58 +00:00
|
|
|
|
2023-09-15 18:51:04 +00:00
|
|
|
if stats.filesMatched < 1 {
|
2023-09-15 06:06:52 +00:00
|
|
|
fmt.Println("No files matched")
|
2023-09-15 18:51:04 +00:00
|
|
|
return []string{}, nil
|
2023-09-10 01:57:50 +00:00
|
|
|
}
|
|
|
|
|
2023-09-12 18:06:45 +00:00
|
|
|
if Verbose {
|
2023-09-26 10:29:55 +00:00
|
|
|
fmt.Printf("%s | INDEX: %d/%d files across %d/%d directories in %s\n",
|
2023-09-13 14:26:15 +00:00
|
|
|
time.Now().Format(logDate),
|
2023-09-15 18:51:04 +00:00
|
|
|
stats.filesMatched,
|
|
|
|
stats.filesMatched+stats.filesSkipped,
|
|
|
|
stats.directoriesMatched,
|
|
|
|
stats.directoriesMatched+stats.directoriesSkipped,
|
2023-01-25 01:06:15 +00:00
|
|
|
time.Since(startTime),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-09-15 18:51:04 +00:00
|
|
|
return list, nil
|
2022-09-08 20:30:51 +00:00
|
|
|
}
|
|
|
|
|
2023-09-15 18:51:04 +00:00
|
|
|
func fileList(paths []string, filters *filters, sort string, cache *fileCache, formats *types.Types) ([]string, error) {
|
2023-09-15 06:06:52 +00:00
|
|
|
switch {
|
|
|
|
case Cache && !cache.isEmpty() && filters.isEmpty():
|
2023-09-15 18:51:04 +00:00
|
|
|
return cache.List(), nil
|
2023-09-15 06:06:52 +00:00
|
|
|
case Cache && !cache.isEmpty() && !filters.isEmpty():
|
2023-09-15 18:51:04 +00:00
|
|
|
return filters.apply(cache.List()), nil
|
2023-09-15 06:06:52 +00:00
|
|
|
case Cache && cache.isEmpty() && !filters.isEmpty():
|
2023-09-15 18:51:04 +00:00
|
|
|
list, err := scanPaths(paths, sort, cache, formats)
|
|
|
|
if err != nil {
|
|
|
|
return []string{}, err
|
|
|
|
}
|
|
|
|
cache.set(list)
|
|
|
|
|
|
|
|
return filters.apply(cache.List()), nil
|
2023-09-15 06:06:52 +00:00
|
|
|
case Cache && cache.isEmpty() && filters.isEmpty():
|
2023-09-15 18:51:04 +00:00
|
|
|
list, err := scanPaths(paths, sort, cache, formats)
|
|
|
|
if err != nil {
|
|
|
|
return []string{}, err
|
|
|
|
}
|
|
|
|
cache.set(list)
|
|
|
|
|
|
|
|
return cache.List(), nil
|
2023-09-15 06:06:52 +00:00
|
|
|
case !Cache && !filters.isEmpty():
|
2023-09-15 18:51:04 +00:00
|
|
|
list, err := scanPaths(paths, sort, cache, formats)
|
|
|
|
if err != nil {
|
|
|
|
return []string{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return filters.apply(list), nil
|
2023-09-15 06:06:52 +00:00
|
|
|
default:
|
2023-09-15 18:51:04 +00:00
|
|
|
list, err := scanPaths(paths, sort, cache, formats)
|
|
|
|
if err != nil {
|
|
|
|
return []string{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return list, nil
|
2023-01-25 01:06:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-17 01:15:39 +00:00
|
|
|
func pickFile(list []string) (string, error) {
|
2023-09-15 06:06:52 +00:00
|
|
|
fileCount := len(list)
|
2023-09-14 03:08:01 +00:00
|
|
|
|
2023-04-10 20:53:01 +00:00
|
|
|
if fileCount < 1 {
|
2023-09-11 01:29:11 +00:00
|
|
|
return "", ErrNoMediaFound
|
2023-01-25 01:06:15 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 03:08:01 +00:00
|
|
|
r, err := rand.Int(rand.Reader, big.NewInt(int64(fileCount)))
|
2023-09-10 17:16:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
val, err := strconv.Atoi(strconv.FormatInt(r.Int64(), 10))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-01-25 01:06:15 +00:00
|
|
|
|
2023-09-15 06:06:52 +00:00
|
|
|
return list[val], nil
|
2023-01-25 01:06:15 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 12:19:06 +00:00
|
|
|
func normalizePath(path string) (string, error) {
|
2023-09-13 17:46:14 +00:00
|
|
|
homeDir, err := os.UserHomeDir()
|
2023-09-13 17:30:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if path == "~" {
|
2023-09-13 17:46:14 +00:00
|
|
|
path = homeDir
|
2023-09-13 17:30:24 +00:00
|
|
|
} else if strings.HasPrefix(path, "~/") {
|
2023-09-13 17:46:14 +00:00
|
|
|
path = filepath.Join(homeDir, path[2:])
|
2023-09-13 17:30:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
path, err = filepath.EvalSymlinks(path)
|
2023-04-11 12:19:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
absolutePath, err := filepath.Abs(path)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return absolutePath, nil
|
|
|
|
}
|
|
|
|
|
2023-09-14 01:45:28 +00:00
|
|
|
func validatePaths(args []string, formats *types.Types) ([]string, error) {
|
2023-05-31 18:01:24 +00:00
|
|
|
var paths []string
|
2023-01-25 01:06:15 +00:00
|
|
|
|
2023-05-12 04:22:31 +00:00
|
|
|
var pathList strings.Builder
|
|
|
|
pathList.WriteString("Paths:\n")
|
2023-01-25 01:06:15 +00:00
|
|
|
|
|
|
|
for i := 0; i < len(args); i++ {
|
2023-04-11 12:19:06 +00:00
|
|
|
path, err := normalizePath(args[i])
|
2022-10-25 05:06:57 +00:00
|
|
|
if err != nil {
|
2023-01-25 01:06:15 +00:00
|
|
|
return nil, err
|
2022-10-25 05:06:57 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 12:19:06 +00:00
|
|
|
pathMatches := (args[i] == path)
|
2023-01-25 01:06:15 +00:00
|
|
|
|
2023-09-28 07:47:43 +00:00
|
|
|
hasSupportedFiles, err := hasSupportedFiles(path, formats)
|
2023-04-11 09:44:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-09-28 07:47:43 +00:00
|
|
|
var addPath = false
|
2023-04-11 09:44:18 +00:00
|
|
|
|
|
|
|
switch {
|
2023-04-11 12:19:06 +00:00
|
|
|
case pathMatches && hasSupportedFiles:
|
2023-05-12 04:22:31 +00:00
|
|
|
pathList.WriteString(fmt.Sprintf("%s\n", args[i]))
|
2023-04-11 09:44:18 +00:00
|
|
|
addPath = true
|
2023-04-11 12:19:06 +00:00
|
|
|
case !pathMatches && hasSupportedFiles:
|
2023-05-12 04:22:31 +00:00
|
|
|
pathList.WriteString(fmt.Sprintf("%s (resolved to %s)\n", args[i], path))
|
2023-04-11 09:44:18 +00:00
|
|
|
addPath = true
|
2023-04-11 12:19:06 +00:00
|
|
|
case pathMatches && !hasSupportedFiles:
|
2023-05-12 04:22:31 +00:00
|
|
|
pathList.WriteString(fmt.Sprintf("%s [No supported files found]\n", args[i]))
|
2023-04-11 12:19:06 +00:00
|
|
|
case !pathMatches && !hasSupportedFiles:
|
2023-05-12 04:22:31 +00:00
|
|
|
pathList.WriteString(fmt.Sprintf("%s (resolved to %s) [No supported files found]\n", args[i], path))
|
2022-10-25 05:06:57 +00:00
|
|
|
}
|
2023-01-25 01:06:15 +00:00
|
|
|
|
2023-04-11 09:44:18 +00:00
|
|
|
if addPath {
|
2023-05-31 18:01:24 +00:00
|
|
|
paths = append(paths, path)
|
2023-04-11 09:44:18 +00:00
|
|
|
}
|
2022-09-08 20:30:51 +00:00
|
|
|
}
|
|
|
|
|
2023-05-12 04:22:31 +00:00
|
|
|
if len(paths) > 0 {
|
|
|
|
fmt.Println(pathList.String())
|
|
|
|
}
|
2023-01-25 01:06:15 +00:00
|
|
|
|
|
|
|
return paths, nil
|
2022-09-08 15:57:59 +00:00
|
|
|
}
|