2022-09-08 10:57:59 -05:00
|
|
|
/*
|
2023-01-18 11:19:29 -06:00
|
|
|
Copyright © 2023 Seednode <seednode@seedno.de>
|
2022-09-08 10:57:59 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-09-09 17:21:03 -05:00
|
|
|
"errors"
|
2022-09-17 11:07:37 -05:00
|
|
|
"fmt"
|
2023-10-03 15:00:32 -05:00
|
|
|
"io/fs"
|
2023-09-10 12:16:50 -05:00
|
|
|
"math/big"
|
2023-09-11 21:22:26 -05:00
|
|
|
"regexp"
|
2024-01-05 18:50:45 -06:00
|
|
|
"runtime"
|
2024-01-04 08:43:52 -06:00
|
|
|
"slices"
|
2022-10-28 18:32:03 -05:00
|
|
|
|
2023-09-10 12:16:50 -05:00
|
|
|
"crypto/rand"
|
2022-09-08 10:57:59 -05:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2022-09-17 11:07:37 -05:00
|
|
|
"strconv"
|
2022-09-24 17:17:56 -05:00
|
|
|
"strings"
|
2022-10-20 14:22:01 -05:00
|
|
|
"sync"
|
2022-09-08 10:57:59 -05:00
|
|
|
"time"
|
2022-09-09 19:00:02 -05:00
|
|
|
|
2023-09-12 16:32:19 -05:00
|
|
|
"seedno.de/seednode/roulette/types"
|
2022-09-08 10:57:59 -05:00
|
|
|
)
|
|
|
|
|
2023-09-13 09:26:15 -05:00
|
|
|
type regexes struct {
|
2023-09-11 21:22:26 -05:00
|
|
|
alphanumeric *regexp.Regexp
|
|
|
|
filename *regexp.Regexp
|
|
|
|
}
|
|
|
|
|
2024-01-05 07:29:16 -06:00
|
|
|
type scanStats struct {
|
2023-09-16 18:04:44 -05:00
|
|
|
filesMatched chan int
|
|
|
|
filesSkipped chan int
|
|
|
|
directoriesMatched chan int
|
|
|
|
directoriesSkipped chan int
|
|
|
|
}
|
|
|
|
|
2023-01-24 19:06:15 -06:00
|
|
|
func humanReadableSize(bytes int) string {
|
2024-01-03 12:37:32 -06:00
|
|
|
var unit int
|
|
|
|
var suffix string
|
|
|
|
var prefixes string
|
|
|
|
|
|
|
|
if BinaryPrefix {
|
|
|
|
unit = 1024
|
|
|
|
prefixes = "KMGTPE"
|
2024-01-05 07:29:16 -06:00
|
|
|
suffix = "iB"
|
2024-01-03 12:37:32 -06:00
|
|
|
} else {
|
|
|
|
unit = 1000
|
|
|
|
prefixes = "kMGTPE"
|
2024-01-05 07:29:16 -06:00
|
|
|
suffix = "B"
|
2024-01-03 12:37:32 -06:00
|
|
|
}
|
2023-01-24 19:06:15 -06:00
|
|
|
|
|
|
|
if bytes < unit {
|
|
|
|
return fmt.Sprintf("%d B", bytes)
|
|
|
|
}
|
|
|
|
|
2024-01-03 12:37:32 -06:00
|
|
|
div, exp := unit, 0
|
2023-01-24 19:06:15 -06:00
|
|
|
|
|
|
|
for n := bytes / unit; n >= unit; n /= unit {
|
|
|
|
div *= unit
|
|
|
|
exp++
|
|
|
|
}
|
|
|
|
|
2024-01-03 12:37:32 -06:00
|
|
|
return fmt.Sprintf("%.1f %c%s",
|
|
|
|
float64(bytes)/float64(div), prefixes[exp], suffix)
|
2023-01-24 19:06:15 -06:00
|
|
|
}
|
|
|
|
|
2023-09-28 10:09:45 -05:00
|
|
|
func kill(path string, index *fileIndex) error {
|
2023-09-15 15:13:45 -05:00
|
|
|
err := os.Remove(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-28 10:09:45 -05:00
|
|
|
if Index {
|
|
|
|
index.remove(path)
|
2023-09-15 15:13:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-04 14:09:49 -05:00
|
|
|
func newFile(list []string, sortOrder string, regexes *regexes, formats types.Types) (string, error) {
|
2023-09-16 20:15:39 -05:00
|
|
|
path, err := pickFile(list)
|
2022-11-05 11:00:42 -05:00
|
|
|
if err != nil {
|
2023-09-15 13:51:04 -05:00
|
|
|
return "", err
|
2022-11-05 11:00:42 -05:00
|
|
|
}
|
2022-09-18 13:07:46 -05:00
|
|
|
|
2023-09-28 03:36:26 -05:00
|
|
|
if sortOrder == "asc" || sortOrder == "desc" {
|
2023-10-04 15:47:43 -05:00
|
|
|
splitPath, err := split(path, regexes)
|
2022-10-18 13:42:32 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-09-28 03:36:26 -05:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case sortOrder == "asc":
|
2023-10-04 15:47:43 -05:00
|
|
|
splitPath.number = fmt.Sprintf("%0*d", len(splitPath.number), 1)
|
2022-10-18 13:42:32 -05:00
|
|
|
|
2023-09-13 09:26:15 -05:00
|
|
|
path, err = tryExtensions(splitPath, formats)
|
2022-10-18 16:46:55 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-09-28 03:36:26 -05:00
|
|
|
case sortOrder == "desc":
|
|
|
|
for {
|
2023-10-20 17:18:35 -05:00
|
|
|
splitPath.number = splitPath.increment()
|
2022-09-18 13:07:46 -05:00
|
|
|
|
2023-09-13 09:26:15 -05:00
|
|
|
path, err = tryExtensions(splitPath, formats)
|
2022-11-05 11:00:42 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-09-17 12:42:25 -05:00
|
|
|
|
2023-09-28 03:36:26 -05:00
|
|
|
if path == "" {
|
2023-10-20 17:18:35 -05:00
|
|
|
splitPath.number = splitPath.decrement()
|
2023-09-28 03:36:26 -05:00
|
|
|
|
|
|
|
path, err = tryExtensions(splitPath, formats)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
2022-11-05 11:00:42 -05:00
|
|
|
}
|
|
|
|
}
|
2022-09-17 11:07:37 -05:00
|
|
|
}
|
|
|
|
|
2023-09-13 09:26:15 -05:00
|
|
|
return path, nil
|
2022-10-18 16:40:13 -05:00
|
|
|
}
|
|
|
|
|
2023-10-04 14:09:49 -05:00
|
|
|
func nextFile(filePath, sortOrder string, regexes *regexes, formats types.Types) (string, error) {
|
2023-10-04 15:47:43 -05:00
|
|
|
splitPath, err := split(filePath, regexes)
|
2022-11-05 11:17:31 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-11-05 11:00:42 -05:00
|
|
|
switch {
|
|
|
|
case sortOrder == "asc":
|
2023-10-20 17:18:35 -05:00
|
|
|
splitPath.number = splitPath.increment()
|
2022-11-05 11:00:42 -05:00
|
|
|
case sortOrder == "desc":
|
2023-10-20 17:18:35 -05:00
|
|
|
splitPath.number = splitPath.decrement()
|
2022-11-05 11:00:42 -05:00
|
|
|
default:
|
|
|
|
return "", nil
|
|
|
|
}
|
2022-10-18 16:40:13 -05:00
|
|
|
|
2023-09-28 02:47:43 -05:00
|
|
|
path, err := tryExtensions(splitPath, formats)
|
2022-09-17 11:07:37 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-09-28 02:47:43 -05:00
|
|
|
return path, err
|
2022-10-18 13:42:32 -05:00
|
|
|
}
|
|
|
|
|
2023-10-04 14:09:49 -05:00
|
|
|
func tryExtensions(splitPath *splitPath, formats types.Types) (string, error) {
|
2023-09-28 02:47:43 -05:00
|
|
|
var path string
|
2023-01-24 19:06:15 -06:00
|
|
|
|
2023-10-04 14:09:49 -05:00
|
|
|
for extension := range formats {
|
2023-09-28 03:36:26 -05:00
|
|
|
path = fmt.Sprintf("%s%s%s", splitPath.base, splitPath.number, extension)
|
2023-01-24 19:06:15 -06:00
|
|
|
|
2023-09-28 02:47:43 -05:00
|
|
|
exists, err := fileExists(path)
|
2023-01-24 12:03:26 -06:00
|
|
|
if err != nil {
|
2023-01-24 19:06:15 -06:00
|
|
|
return "", err
|
2023-01-24 12:03:26 -06:00
|
|
|
}
|
2022-10-18 16:46:55 -05:00
|
|
|
|
2023-01-24 19:06:15 -06:00
|
|
|
if exists {
|
2023-09-28 02:47:43 -05:00
|
|
|
return path, nil
|
2023-01-24 12:03:26 -06:00
|
|
|
}
|
2022-09-26 12:31:45 -05:00
|
|
|
}
|
|
|
|
|
2023-01-24 19:06:15 -06:00
|
|
|
return "", nil
|
|
|
|
}
|
2022-09-26 12:31:45 -05:00
|
|
|
|
2023-01-24 19:06:15 -06: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 11:07:37 -05:00
|
|
|
}
|
|
|
|
|
2023-09-13 09:26:15 -05:00
|
|
|
func pathIsValid(path string, paths []string) bool {
|
2022-10-18 13:42:32 -05:00
|
|
|
var matchesPrefix = false
|
2022-10-20 17:12:29 -05:00
|
|
|
|
2022-10-18 13:42:32 -05:00
|
|
|
for i := 0; i < len(paths); i++ {
|
2023-09-13 09:26:15 -05:00
|
|
|
if strings.HasPrefix(path, paths[i]) {
|
2022-10-18 13:42:32 -05:00
|
|
|
matchesPrefix = true
|
2023-10-03 11:07:58 -05:00
|
|
|
|
|
|
|
break
|
2022-10-18 13:42:32 -05:00
|
|
|
}
|
|
|
|
}
|
2022-10-20 17:55:25 -05:00
|
|
|
|
2022-10-25 00:06:57 -05:00
|
|
|
switch {
|
2023-09-12 13:06:45 -05:00
|
|
|
case Verbose && !matchesPrefix:
|
2023-09-26 05:29:55 -05:00
|
|
|
fmt.Printf("%s | ERROR: File outside specified path(s): %s\n",
|
2023-09-13 09:26:15 -05:00
|
|
|
time.Now().Format(logDate),
|
|
|
|
path,
|
2022-10-20 17:55:25 -05:00
|
|
|
)
|
2022-10-18 13:42:32 -05:00
|
|
|
|
|
|
|
return false
|
2022-10-25 00:06:57 -05:00
|
|
|
case !matchesPrefix:
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
return true
|
2022-10-18 13:42:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-04 14:09:49 -05:00
|
|
|
func hasSupportedFiles(path string, formats types.Types) (bool, error) {
|
2024-01-05 18:50:45 -06:00
|
|
|
if AllowEmpty {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2023-09-11 12:09:08 -05:00
|
|
|
hasRegisteredFiles := make(chan bool, 1)
|
2023-04-11 04:44:18 -05:00
|
|
|
|
|
|
|
err := filepath.WalkDir(path, func(p string, info os.DirEntry, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
2023-09-12 13:06:45 -05:00
|
|
|
case !Recursive && info.IsDir() && p != path:
|
2023-04-11 04:44:18 -05:00
|
|
|
return filepath.SkipDir
|
2023-09-14 19:10:55 -05:00
|
|
|
case !info.IsDir() && formats.Validate(p):
|
|
|
|
hasRegisteredFiles <- true
|
2023-09-14 17:37:22 -05:00
|
|
|
|
2023-09-14 19:10:55 -05:00
|
|
|
return filepath.SkipAll
|
2023-04-11 04:44:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2023-09-11 12:09:08 -05:00
|
|
|
case <-hasRegisteredFiles:
|
2023-04-11 04:44:18 -05:00
|
|
|
return true, nil
|
|
|
|
default:
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-06 09:41:30 -06:00
|
|
|
func walkPath(path string, fileChannel chan<- string, wg0 *sync.WaitGroup, stats *scanStats, limit chan struct{}, formats types.Types, errorChannel chan<- error) {
|
|
|
|
defer func() {
|
|
|
|
wg0.Done()
|
|
|
|
}()
|
|
|
|
|
2024-01-01 11:36:19 -06:00
|
|
|
limit <- struct{}{}
|
2023-12-17 06:35:03 -06:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
<-limit
|
|
|
|
}()
|
|
|
|
|
2023-08-02 13:29:37 -05:00
|
|
|
nodes, err := os.ReadDir(path)
|
|
|
|
if err != nil {
|
2024-01-06 09:41:30 -06:00
|
|
|
stats.directoriesSkipped <- 1
|
|
|
|
|
|
|
|
errorChannel <- err
|
|
|
|
|
|
|
|
return
|
2023-08-02 13:29:37 -05:00
|
|
|
}
|
|
|
|
|
2024-01-06 09:41:30 -06:00
|
|
|
var files = 0
|
2023-10-03 08:26:55 -05:00
|
|
|
|
2023-12-07 10:34:19 -06:00
|
|
|
var skipDir = false
|
|
|
|
|
2023-08-02 13:29:37 -05:00
|
|
|
for _, node := range nodes {
|
2024-01-06 09:41:30 -06:00
|
|
|
if Ignore && !node.IsDir() && node.Name() == IgnoreFile {
|
|
|
|
skipDir = true
|
2023-08-02 13:29:37 -05:00
|
|
|
}
|
2024-01-06 09:41:30 -06:00
|
|
|
|
|
|
|
files++
|
2023-08-02 13:29:37 -05:00
|
|
|
}
|
|
|
|
|
2023-10-03 08:26:55 -05:00
|
|
|
var skipFiles = false
|
2023-08-02 13:29:37 -05:00
|
|
|
|
2024-01-06 09:41:30 -06:00
|
|
|
if files > MaxFileCount || files < MinFileCount || skipDir {
|
2023-10-03 08:26:55 -05:00
|
|
|
stats.filesSkipped <- files
|
|
|
|
stats.directoriesSkipped <- 1
|
2022-10-20 14:22:01 -05:00
|
|
|
|
2023-10-03 08:26:55 -05:00
|
|
|
skipFiles = true
|
2024-01-06 09:41:30 -06:00
|
|
|
} else {
|
|
|
|
stats.directoriesMatched <- 1
|
2023-10-03 08:26:55 -05:00
|
|
|
}
|
|
|
|
|
2024-01-06 09:41:30 -06:00
|
|
|
var wg1 sync.WaitGroup
|
2022-09-08 15:30:51 -05:00
|
|
|
|
2024-01-06 09:41:30 -06:00
|
|
|
wg1.Add(1)
|
2023-10-03 15:00:32 -05:00
|
|
|
go func() {
|
2024-01-06 09:41:30 -06:00
|
|
|
defer wg1.Done()
|
2023-10-03 15:00:32 -05:00
|
|
|
for _, node := range nodes {
|
2024-01-06 09:41:30 -06:00
|
|
|
wg1.Add(1)
|
2022-10-20 14:22:01 -05:00
|
|
|
|
2023-10-03 15:00:32 -05:00
|
|
|
go func(node fs.DirEntry) {
|
2024-01-06 09:41:30 -06:00
|
|
|
defer wg1.Done()
|
2022-10-20 14:22:01 -05:00
|
|
|
|
2023-10-03 15:00:32 -05:00
|
|
|
fullPath := filepath.Join(path, node.Name())
|
2023-10-03 08:26:55 -05:00
|
|
|
|
2023-10-03 15:00:32 -05:00
|
|
|
switch {
|
|
|
|
case node.IsDir() && Recursive:
|
2024-01-06 09:41:30 -06:00
|
|
|
wg0.Add(1)
|
2023-09-16 18:04:44 -05:00
|
|
|
|
2024-01-06 09:41:30 -06:00
|
|
|
walkPath(fullPath, fileChannel, wg0, stats, limit, formats, errorChannel)
|
2023-10-03 15:00:32 -05:00
|
|
|
case !node.IsDir() && !skipFiles:
|
|
|
|
path, err := normalizePath(fullPath)
|
|
|
|
if err != nil {
|
|
|
|
errorChannel <- err
|
2023-04-11 07:19:06 -05:00
|
|
|
|
2024-01-06 09:41:30 -06:00
|
|
|
stats.filesSkipped <- 1
|
|
|
|
|
2023-10-03 15:00:32 -05:00
|
|
|
return
|
|
|
|
}
|
2023-10-03 09:44:50 -05:00
|
|
|
|
2023-10-03 15:00:32 -05:00
|
|
|
if formats.Validate(path) || Fallback {
|
|
|
|
fileChannel <- path
|
2023-09-15 01:06:52 -05:00
|
|
|
|
2023-10-03 15:00:32 -05:00
|
|
|
stats.filesMatched <- 1
|
2023-09-15 01:06:52 -05:00
|
|
|
|
2023-10-03 15:00:32 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
stats.filesSkipped <- 1
|
|
|
|
}
|
|
|
|
}(node)
|
2022-09-08 12:12:58 -05:00
|
|
|
}
|
2023-10-03 15:00:32 -05:00
|
|
|
}()
|
2022-10-20 14:22:01 -05:00
|
|
|
|
2024-01-06 09:41:30 -06:00
|
|
|
wg1.Wait()
|
2022-09-08 12:12:58 -05:00
|
|
|
}
|
|
|
|
|
2024-01-06 09:41:30 -06:00
|
|
|
func scanPaths(paths []string, sort string, index *fileIndex, formats types.Types, errorChannel chan<- error) []string {
|
2023-10-04 21:54:06 -05:00
|
|
|
startTime := time.Now()
|
|
|
|
|
2023-12-17 05:16:46 -06:00
|
|
|
var filesMatched, filesSkipped int
|
|
|
|
var directoriesMatched, directoriesSkipped int
|
2023-10-04 21:54:06 -05:00
|
|
|
|
2023-09-15 13:51:04 -05:00
|
|
|
fileChannel := make(chan string)
|
2023-10-05 16:33:23 -05:00
|
|
|
done := make(chan bool)
|
2022-09-24 20:28:34 -05:00
|
|
|
|
2024-01-05 07:29:16 -06:00
|
|
|
stats := &scanStats{
|
2023-09-16 18:04:44 -05:00
|
|
|
filesMatched: make(chan int),
|
|
|
|
filesSkipped: make(chan int),
|
|
|
|
directoriesMatched: make(chan int),
|
|
|
|
directoriesSkipped: make(chan int),
|
2023-01-24 19:06:15 -06:00
|
|
|
}
|
2022-10-20 11:17:40 -05:00
|
|
|
|
2023-12-17 05:16:46 -06:00
|
|
|
var list []string
|
|
|
|
|
2023-10-04 21:54:06 -05:00
|
|
|
go func() {
|
2023-10-05 16:33:23 -05:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case path := <-fileChannel:
|
|
|
|
list = append(list, path)
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
2023-10-04 21:54:06 -05:00
|
|
|
}
|
|
|
|
}()
|
2023-01-24 19:06:15 -06:00
|
|
|
|
2023-10-04 21:54:06 -05:00
|
|
|
go func() {
|
2023-10-05 16:33:23 -05:00
|
|
|
for {
|
|
|
|
select {
|
2024-01-05 07:29:16 -06:00
|
|
|
case stat := <-stats.filesMatched:
|
2023-10-05 16:33:23 -05:00
|
|
|
filesMatched += stat
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
2023-10-04 21:54:06 -05:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
2023-10-05 16:33:23 -05:00
|
|
|
for {
|
|
|
|
select {
|
2024-01-05 07:29:16 -06:00
|
|
|
case stat := <-stats.filesSkipped:
|
2023-10-05 16:33:23 -05:00
|
|
|
filesSkipped += stat
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
2023-10-04 21:54:06 -05:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
2023-10-05 16:33:23 -05:00
|
|
|
for {
|
|
|
|
select {
|
2024-01-05 07:29:16 -06:00
|
|
|
case stat := <-stats.directoriesMatched:
|
2023-10-05 16:33:23 -05:00
|
|
|
directoriesMatched += stat
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
2023-10-04 21:54:06 -05:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
2023-10-05 16:33:23 -05:00
|
|
|
for {
|
|
|
|
select {
|
2024-01-05 07:29:16 -06:00
|
|
|
case stat := <-stats.directoriesSkipped:
|
2023-10-05 16:33:23 -05:00
|
|
|
directoriesSkipped += stat
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
2023-10-04 21:54:06 -05:00
|
|
|
}
|
|
|
|
}()
|
2023-01-24 19:06:15 -06:00
|
|
|
|
2024-01-01 11:36:19 -06:00
|
|
|
limit := make(chan struct{}, Concurrency)
|
2023-12-17 06:35:03 -06:00
|
|
|
|
2024-01-06 09:41:30 -06:00
|
|
|
var wg0 sync.WaitGroup
|
2023-12-17 05:16:46 -06:00
|
|
|
|
2023-01-24 19:06:15 -06:00
|
|
|
for i := 0; i < len(paths); i++ {
|
2024-01-06 09:41:30 -06:00
|
|
|
wg0.Add(1)
|
2023-01-24 19:06:15 -06:00
|
|
|
|
|
|
|
go func(i int) {
|
2024-01-06 09:41:30 -06:00
|
|
|
walkPath(paths[i], fileChannel, &wg0, stats, limit, formats, errorChannel)
|
2023-01-24 19:06:15 -06:00
|
|
|
}(i)
|
2022-11-05 11:00:42 -05:00
|
|
|
}
|
2022-09-09 17:21:03 -05:00
|
|
|
|
2024-01-06 09:41:30 -06:00
|
|
|
wg0.Wait()
|
2023-09-15 13:51:04 -05:00
|
|
|
|
2024-01-06 09:41:30 -06:00
|
|
|
close(done)
|
2022-09-08 12:12:58 -05:00
|
|
|
|
2023-09-12 13:06:45 -05:00
|
|
|
if Verbose {
|
2023-10-05 11:18:21 -05:00
|
|
|
fmt.Printf("%s | INDEX: Selected %d/%d files across %d/%d directories in %s\n",
|
2023-09-13 09:26:15 -05:00
|
|
|
time.Now().Format(logDate),
|
2023-10-04 21:54:06 -05:00
|
|
|
filesMatched,
|
|
|
|
filesMatched+filesSkipped,
|
|
|
|
directoriesMatched,
|
|
|
|
directoriesMatched+directoriesSkipped,
|
2024-01-05 20:03:49 -06:00
|
|
|
time.Since(startTime),
|
2023-01-24 19:06:15 -06:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-01-04 08:43:52 -06:00
|
|
|
slices.Sort(list)
|
|
|
|
|
2024-01-06 09:41:30 -06:00
|
|
|
return list
|
2022-09-08 15:30:51 -05:00
|
|
|
}
|
|
|
|
|
2024-01-06 09:41:30 -06:00
|
|
|
func fileList(paths []string, filters *filters, sort string, index *fileIndex, formats types.Types, errorChannel chan<- error) []string {
|
2023-09-15 01:06:52 -05:00
|
|
|
switch {
|
2023-09-28 10:09:45 -05:00
|
|
|
case Index && !index.isEmpty() && filters.isEmpty():
|
2024-01-06 09:41:30 -06:00
|
|
|
return index.List()
|
2023-09-28 10:09:45 -05:00
|
|
|
case Index && !index.isEmpty() && !filters.isEmpty():
|
2024-01-06 09:41:30 -06:00
|
|
|
return filters.apply(index.List())
|
2023-09-28 10:09:45 -05:00
|
|
|
case Index && index.isEmpty() && !filters.isEmpty():
|
2024-01-06 09:41:30 -06:00
|
|
|
index.set(scanPaths(paths, sort, index, formats, errorChannel))
|
2023-09-15 13:51:04 -05:00
|
|
|
|
2024-01-06 09:41:30 -06:00
|
|
|
return filters.apply(index.List())
|
2023-09-28 10:09:45 -05:00
|
|
|
case Index && index.isEmpty() && filters.isEmpty():
|
2024-01-06 09:41:30 -06:00
|
|
|
index.set(scanPaths(paths, sort, index, formats, errorChannel))
|
2023-09-15 13:51:04 -05:00
|
|
|
|
2024-01-06 09:41:30 -06:00
|
|
|
return index.List()
|
2023-09-28 10:09:45 -05:00
|
|
|
case !Index && !filters.isEmpty():
|
2024-01-06 09:41:30 -06:00
|
|
|
return filters.apply(scanPaths(paths, sort, index, formats, errorChannel))
|
2023-09-15 01:06:52 -05:00
|
|
|
default:
|
2024-01-06 09:41:30 -06:00
|
|
|
return scanPaths(paths, sort, index, formats, errorChannel)
|
2023-01-24 19:06:15 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-16 20:15:39 -05:00
|
|
|
func pickFile(list []string) (string, error) {
|
2023-09-15 01:06:52 -05:00
|
|
|
fileCount := len(list)
|
2023-09-13 22:08:01 -05:00
|
|
|
|
2024-01-05 18:50:45 -06:00
|
|
|
switch {
|
|
|
|
case fileCount < 1 && AllowEmpty:
|
|
|
|
return "", nil
|
|
|
|
case fileCount < 1:
|
2023-09-10 20:29:11 -05:00
|
|
|
return "", ErrNoMediaFound
|
2023-01-24 19:06:15 -06:00
|
|
|
}
|
|
|
|
|
2023-09-13 22:08:01 -05:00
|
|
|
r, err := rand.Int(rand.Reader, big.NewInt(int64(fileCount)))
|
2023-09-10 12:16:50 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
val, err := strconv.Atoi(strconv.FormatInt(r.Int64(), 10))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-01-24 19:06:15 -06:00
|
|
|
|
2023-09-15 01:06:52 -05:00
|
|
|
return list[val], nil
|
2023-01-24 19:06:15 -06:00
|
|
|
}
|
|
|
|
|
2024-01-05 18:50:45 -06:00
|
|
|
func preparePath(prefix, path string) string {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return fmt.Sprintf("%s/%s", prefix, filepath.ToSlash(path))
|
|
|
|
}
|
|
|
|
|
|
|
|
return prefix + path
|
|
|
|
}
|
|
|
|
|
2023-04-11 07:19:06 -05:00
|
|
|
func normalizePath(path string) (string, error) {
|
2023-09-13 12:46:14 -05:00
|
|
|
homeDir, err := os.UserHomeDir()
|
2023-09-13 12:30:24 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if path == "~" {
|
2023-09-13 12:46:14 -05:00
|
|
|
path = homeDir
|
2023-09-13 12:30:24 -05:00
|
|
|
} else if strings.HasPrefix(path, "~/") {
|
2023-09-13 12:46:14 -05:00
|
|
|
path = filepath.Join(homeDir, path[2:])
|
2023-09-13 12:30:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
path, err = filepath.EvalSymlinks(path)
|
2023-04-11 07:19:06 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
absolutePath, err := filepath.Abs(path)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return absolutePath, nil
|
|
|
|
}
|
|
|
|
|
2023-10-04 14:09:49 -05:00
|
|
|
func validatePaths(args []string, formats types.Types) ([]string, error) {
|
2023-05-31 13:01:24 -05:00
|
|
|
var paths []string
|
2023-01-24 19:06:15 -06:00
|
|
|
|
|
|
|
for i := 0; i < len(args); i++ {
|
2023-04-11 07:19:06 -05:00
|
|
|
path, err := normalizePath(args[i])
|
2022-10-25 00:06:57 -05:00
|
|
|
if err != nil {
|
2023-01-24 19:06:15 -06:00
|
|
|
return nil, err
|
2022-10-25 00:06:57 -05:00
|
|
|
}
|
|
|
|
|
2023-04-11 07:19:06 -05:00
|
|
|
pathMatches := (args[i] == path)
|
2023-01-24 19:06:15 -06:00
|
|
|
|
2023-09-28 02:47:43 -05:00
|
|
|
hasSupportedFiles, err := hasSupportedFiles(path, formats)
|
2023-04-11 04:44:18 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
2023-04-11 07:19:06 -05:00
|
|
|
case pathMatches && hasSupportedFiles:
|
2023-09-28 07:29:33 -05:00
|
|
|
fmt.Printf("%s | PATHS: Added %s\n",
|
|
|
|
time.Now().Format(logDate),
|
|
|
|
args[i],
|
|
|
|
)
|
|
|
|
|
|
|
|
paths = append(paths, path)
|
2023-04-11 07:19:06 -05:00
|
|
|
case !pathMatches && hasSupportedFiles:
|
2023-09-28 07:29:33 -05:00
|
|
|
fmt.Printf("%s | PATHS: Added %s [resolved to %s]\n",
|
|
|
|
time.Now().Format(logDate),
|
|
|
|
args[i],
|
|
|
|
path,
|
|
|
|
)
|
2023-01-24 19:06:15 -06:00
|
|
|
|
2023-05-31 13:01:24 -05:00
|
|
|
paths = append(paths, path)
|
2023-09-28 07:29:33 -05:00
|
|
|
case pathMatches && !hasSupportedFiles:
|
|
|
|
fmt.Printf("%s | PATHS: Skipped %s (No supported files found)\n",
|
|
|
|
time.Now().Format(logDate),
|
|
|
|
args[i],
|
|
|
|
)
|
|
|
|
case !pathMatches && !hasSupportedFiles:
|
|
|
|
fmt.Printf("%s | PATHS: Skipped %s [resolved to %s] (No supported files found)\n",
|
|
|
|
time.Now().Format(logDate),
|
|
|
|
args[i],
|
|
|
|
path,
|
|
|
|
)
|
2023-04-11 04:44:18 -05:00
|
|
|
}
|
2022-09-08 15:30:51 -05:00
|
|
|
}
|
|
|
|
|
2023-01-24 19:06:15 -06:00
|
|
|
return paths, nil
|
2022-09-08 10:57:59 -05:00
|
|
|
}
|