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-10-03 20:00:32 +00:00
|
|
|
"io/fs"
|
2023-09-10 17:16:50 +00:00
|
|
|
"math/big"
|
2023-09-12 02:22:26 +00:00
|
|
|
"regexp"
|
2024-01-06 00:50:45 +00:00
|
|
|
"runtime"
|
2024-01-04 14:43:52 +00:00
|
|
|
"slices"
|
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
|
|
|
)
|
|
|
|
|
2024-01-10 19:07:24 +00:00
|
|
|
var (
|
|
|
|
filename = regexp.MustCompile(`(.+?)([0-9]*)(\..+)`)
|
|
|
|
)
|
2023-09-12 02:22:26 +00:00
|
|
|
|
2024-01-05 13:29:16 +00:00
|
|
|
type scanStats struct {
|
2023-09-16 23:04:44 +00:00
|
|
|
filesMatched chan int
|
|
|
|
filesSkipped chan int
|
|
|
|
directoriesMatched chan int
|
|
|
|
directoriesSkipped chan int
|
|
|
|
}
|
|
|
|
|
2023-01-25 01:06:15 +00:00
|
|
|
func humanReadableSize(bytes int) string {
|
2024-01-03 18:37:32 +00:00
|
|
|
var unit int
|
|
|
|
var suffix string
|
|
|
|
var prefixes string
|
|
|
|
|
|
|
|
if BinaryPrefix {
|
|
|
|
unit = 1024
|
|
|
|
prefixes = "KMGTPE"
|
2024-01-05 13:29:16 +00:00
|
|
|
suffix = "iB"
|
2024-01-03 18:37:32 +00:00
|
|
|
} else {
|
|
|
|
unit = 1000
|
|
|
|
prefixes = "kMGTPE"
|
2024-01-05 13:29:16 +00:00
|
|
|
suffix = "B"
|
2024-01-03 18:37:32 +00:00
|
|
|
}
|
2023-01-25 01:06:15 +00:00
|
|
|
|
|
|
|
if bytes < unit {
|
|
|
|
return fmt.Sprintf("%d B", bytes)
|
|
|
|
}
|
|
|
|
|
2024-01-03 18:37:32 +00:00
|
|
|
div, exp := unit, 0
|
2023-01-25 01:06:15 +00:00
|
|
|
|
|
|
|
for n := bytes / unit; n >= unit; n /= unit {
|
|
|
|
div *= unit
|
|
|
|
exp++
|
|
|
|
}
|
|
|
|
|
2024-01-03 18:37:32 +00:00
|
|
|
return fmt.Sprintf("%.1f %c%s",
|
|
|
|
float64(bytes)/float64(div), prefixes[exp], suffix)
|
2023-01-25 01:06:15 +00:00
|
|
|
}
|
|
|
|
|
2023-09-28 15:09:45 +00:00
|
|
|
func kill(path string, index *fileIndex) error {
|
2023-09-15 20:13:45 +00:00
|
|
|
err := os.Remove(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-28 15:09:45 +00:00
|
|
|
if Index {
|
|
|
|
index.remove(path)
|
2023-09-15 20:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-10 19:07:24 +00:00
|
|
|
func newFile(list []string, sortOrder string, formats types.Types) (string, error) {
|
2023-09-17 01:15:39 +00:00
|
|
|
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" {
|
2024-01-10 19:07:24 +00:00
|
|
|
splitPath, err := split(path)
|
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":
|
2023-10-04 20:47:43 +00:00
|
|
|
splitPath.number = fmt.Sprintf("%0*d", len(splitPath.number), 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 {
|
2023-10-20 22:18:35 +00:00
|
|
|
splitPath.number = 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 == "" {
|
2023-10-20 22:18:35 +00:00
|
|
|
splitPath.number = splitPath.decrement()
|
2023-09-28 08:36:26 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-01-10 19:07:24 +00:00
|
|
|
func nextFile(filePath, sortOrder string, formats types.Types) (string, error) {
|
|
|
|
splitPath, err := split(filePath)
|
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-10-20 22:18:35 +00:00
|
|
|
splitPath.number = splitPath.increment()
|
2022-11-05 16:00:42 +00:00
|
|
|
case sortOrder == "desc":
|
2023-10-20 22:18:35 +00:00
|
|
|
splitPath.number = 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-10-04 19:09:49 +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-10-04 19:09:49 +00:00
|
|
|
for extension := range formats {
|
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
|
2023-10-03 16:07:58 +00:00
|
|
|
|
|
|
|
break
|
2022-10-18 18:42:32 +00:00
|
|
|
}
|
|
|
|
}
|
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-10-04 19:09:49 +00:00
|
|
|
func hasSupportedFiles(path string, formats types.Types) (bool, error) {
|
2024-01-06 00:50:45 +00:00
|
|
|
if AllowEmpty {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-09 23:36:25 +00:00
|
|
|
func walkPath(path string, fileChannel chan<- string, wg1 *sync.WaitGroup, stats *scanStats, limit chan struct{}, formats types.Types, errorChannel chan<- error) {
|
2024-01-01 17:36:19 +00:00
|
|
|
limit <- struct{}{}
|
2023-12-17 12:35:03 +00:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
<-limit
|
|
|
|
}()
|
|
|
|
|
2023-08-02 18:29:37 +00:00
|
|
|
nodes, err := os.ReadDir(path)
|
|
|
|
if err != nil {
|
2024-01-06 15:41:30 +00:00
|
|
|
stats.directoriesSkipped <- 1
|
|
|
|
|
|
|
|
errorChannel <- err
|
|
|
|
|
|
|
|
return
|
2023-08-02 18:29:37 +00:00
|
|
|
}
|
|
|
|
|
2024-01-06 15:41:30 +00:00
|
|
|
var files = 0
|
2023-10-03 13:26:55 +00:00
|
|
|
|
2023-12-07 16:34:19 +00:00
|
|
|
var skipDir = false
|
|
|
|
|
2023-08-02 18:29:37 +00:00
|
|
|
for _, node := range nodes {
|
2024-01-10 19:07:24 +00:00
|
|
|
if !node.IsDir() {
|
|
|
|
files++
|
2024-01-06 15:41:30 +00:00
|
|
|
|
2024-01-10 19:07:24 +00:00
|
|
|
if Ignore && node.Name() == IgnoreFile {
|
|
|
|
skipDir = true
|
|
|
|
}
|
|
|
|
}
|
2023-08-02 18:29:37 +00:00
|
|
|
}
|
|
|
|
|
2023-10-03 13:26:55 +00:00
|
|
|
var skipFiles = false
|
2023-08-02 18:29:37 +00:00
|
|
|
|
2024-01-06 15:41:30 +00:00
|
|
|
if files > MaxFileCount || files < MinFileCount || skipDir {
|
2023-10-03 13:26:55 +00:00
|
|
|
stats.filesSkipped <- files
|
|
|
|
stats.directoriesSkipped <- 1
|
2022-10-20 19:22:01 +00:00
|
|
|
|
2023-10-03 13:26:55 +00:00
|
|
|
skipFiles = true
|
2024-01-06 15:41:30 +00:00
|
|
|
} else {
|
|
|
|
stats.directoriesMatched <- 1
|
2023-10-03 13:26:55 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 23:36:25 +00:00
|
|
|
var wg2 sync.WaitGroup
|
2022-09-08 20:30:51 +00:00
|
|
|
|
2024-01-09 23:36:25 +00:00
|
|
|
for _, node := range nodes {
|
|
|
|
wg2.Add(1)
|
2022-10-20 19:22:01 +00:00
|
|
|
|
2024-01-09 23:36:25 +00:00
|
|
|
go func(node fs.DirEntry) {
|
|
|
|
defer wg2.Done()
|
2022-10-20 19:22:01 +00:00
|
|
|
|
2024-01-09 23:36:25 +00:00
|
|
|
fullPath := filepath.Join(path, node.Name())
|
2023-10-03 13:26:55 +00:00
|
|
|
|
2024-01-09 23:36:25 +00:00
|
|
|
switch {
|
|
|
|
case node.IsDir() && Recursive:
|
|
|
|
wg1.Add(1)
|
2023-09-16 23:04:44 +00:00
|
|
|
|
2024-01-10 17:58:55 +00:00
|
|
|
go func() {
|
|
|
|
defer wg1.Done()
|
|
|
|
|
|
|
|
walkPath(fullPath, fileChannel, wg1, stats, limit, formats, errorChannel)
|
|
|
|
}()
|
|
|
|
|
2024-01-09 23:36:25 +00:00
|
|
|
case !node.IsDir() && !skipFiles:
|
|
|
|
path, err := normalizePath(fullPath)
|
2023-09-15 06:06:52 +00:00
|
|
|
|
2024-01-10 19:07:24 +00:00
|
|
|
switch {
|
|
|
|
case err != nil:
|
|
|
|
errorChannel <- err
|
|
|
|
case formats.Validate(path) || Fallback:
|
2024-01-09 23:36:25 +00:00
|
|
|
fileChannel <- path
|
2023-09-15 06:06:52 +00:00
|
|
|
|
2024-01-09 23:36:25 +00:00
|
|
|
stats.filesMatched <- 1
|
2023-10-03 20:00:32 +00:00
|
|
|
|
2024-01-09 23:36:25 +00:00
|
|
|
return
|
2023-10-03 20:00:32 +00:00
|
|
|
}
|
2022-10-20 19:22:01 +00:00
|
|
|
|
2024-01-09 23:36:25 +00:00
|
|
|
stats.filesSkipped <- 1
|
|
|
|
}
|
|
|
|
}(node)
|
|
|
|
}
|
2024-01-09 23:17:26 +00:00
|
|
|
|
2024-01-09 23:36:25 +00:00
|
|
|
wg2.Wait()
|
2022-09-08 17:12:58 +00:00
|
|
|
}
|
|
|
|
|
2024-01-06 15:41:30 +00:00
|
|
|
func scanPaths(paths []string, sort string, index *fileIndex, formats types.Types, errorChannel chan<- error) []string {
|
2023-10-05 02:54:06 +00:00
|
|
|
startTime := time.Now()
|
|
|
|
|
2023-12-17 11:16:46 +00:00
|
|
|
var filesMatched, filesSkipped int
|
|
|
|
var directoriesMatched, directoriesSkipped int
|
2023-10-05 02:54:06 +00:00
|
|
|
|
2023-09-15 18:51:04 +00:00
|
|
|
fileChannel := make(chan string)
|
2023-10-05 21:33:23 +00:00
|
|
|
done := make(chan bool)
|
2022-09-25 01:28:34 +00:00
|
|
|
|
2024-01-05 13:29:16 +00:00
|
|
|
stats := &scanStats{
|
2023-09-16 23:04:44 +00:00
|
|
|
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-12-17 11:16:46 +00:00
|
|
|
var list []string
|
|
|
|
|
2024-01-09 23:36:25 +00:00
|
|
|
var wg0 sync.WaitGroup
|
|
|
|
|
|
|
|
wg0.Add(1)
|
2023-10-05 02:54:06 +00:00
|
|
|
go func() {
|
2024-01-09 23:36:25 +00:00
|
|
|
defer wg0.Done()
|
2023-10-05 21:33:23 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case path := <-fileChannel:
|
|
|
|
list = append(list, path)
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
2023-10-05 02:54:06 +00:00
|
|
|
}
|
|
|
|
}()
|
2023-01-25 01:06:15 +00:00
|
|
|
|
2024-01-09 23:36:25 +00:00
|
|
|
wg0.Add(1)
|
2023-10-05 02:54:06 +00:00
|
|
|
go func() {
|
2024-01-09 23:36:25 +00:00
|
|
|
defer wg0.Done()
|
|
|
|
|
2023-10-05 21:33:23 +00:00
|
|
|
for {
|
|
|
|
select {
|
2024-01-05 13:29:16 +00:00
|
|
|
case stat := <-stats.filesMatched:
|
2023-10-05 21:33:23 +00:00
|
|
|
filesMatched += stat
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
2023-10-05 02:54:06 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2024-01-09 23:36:25 +00:00
|
|
|
wg0.Add(1)
|
2023-10-05 02:54:06 +00:00
|
|
|
go func() {
|
2024-01-09 23:36:25 +00:00
|
|
|
defer wg0.Done()
|
|
|
|
|
2023-10-05 21:33:23 +00:00
|
|
|
for {
|
|
|
|
select {
|
2024-01-05 13:29:16 +00:00
|
|
|
case stat := <-stats.filesSkipped:
|
2023-10-05 21:33:23 +00:00
|
|
|
filesSkipped += stat
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
2023-10-05 02:54:06 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2024-01-09 23:36:25 +00:00
|
|
|
wg0.Add(1)
|
2023-10-05 02:54:06 +00:00
|
|
|
go func() {
|
2024-01-09 23:36:25 +00:00
|
|
|
defer wg0.Done()
|
|
|
|
|
2023-10-05 21:33:23 +00:00
|
|
|
for {
|
|
|
|
select {
|
2024-01-05 13:29:16 +00:00
|
|
|
case stat := <-stats.directoriesMatched:
|
2023-10-05 21:33:23 +00:00
|
|
|
directoriesMatched += stat
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
2023-10-05 02:54:06 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2024-01-09 23:36:25 +00:00
|
|
|
wg0.Add(1)
|
2023-10-05 02:54:06 +00:00
|
|
|
go func() {
|
2024-01-09 23:36:25 +00:00
|
|
|
defer wg0.Done()
|
|
|
|
|
2023-10-05 21:33:23 +00:00
|
|
|
for {
|
|
|
|
select {
|
2024-01-05 13:29:16 +00:00
|
|
|
case stat := <-stats.directoriesSkipped:
|
2023-10-05 21:33:23 +00:00
|
|
|
directoriesSkipped += stat
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
2023-10-05 02:54:06 +00:00
|
|
|
}
|
|
|
|
}()
|
2023-01-25 01:06:15 +00:00
|
|
|
|
2024-01-01 17:36:19 +00:00
|
|
|
limit := make(chan struct{}, Concurrency)
|
2023-12-17 12:35:03 +00:00
|
|
|
|
2024-01-09 23:36:25 +00:00
|
|
|
var wg1 sync.WaitGroup
|
2023-12-17 11:16:46 +00:00
|
|
|
|
2023-01-25 01:06:15 +00:00
|
|
|
for i := 0; i < len(paths); i++ {
|
2024-01-09 23:36:25 +00:00
|
|
|
wg1.Add(1)
|
2023-01-25 01:06:15 +00:00
|
|
|
|
|
|
|
go func(i int) {
|
2024-01-10 17:58:55 +00:00
|
|
|
defer wg1.Done()
|
|
|
|
|
2024-01-09 23:36:25 +00:00
|
|
|
walkPath(paths[i], fileChannel, &wg1, stats, limit, formats, errorChannel)
|
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
|
|
|
|
2024-01-09 23:36:25 +00:00
|
|
|
wg1.Wait()
|
2023-09-15 18:51:04 +00:00
|
|
|
|
2024-01-06 15:41:30 +00:00
|
|
|
close(done)
|
2022-09-08 17:12:58 +00:00
|
|
|
|
2024-01-09 23:36:25 +00:00
|
|
|
wg0.Wait()
|
|
|
|
|
2023-09-12 18:06:45 +00:00
|
|
|
if Verbose {
|
2023-10-05 16:18:21 +00:00
|
|
|
fmt.Printf("%s | INDEX: Selected %d/%d files across %d/%d directories in %s\n",
|
2023-09-13 14:26:15 +00:00
|
|
|
time.Now().Format(logDate),
|
2023-10-05 02:54:06 +00:00
|
|
|
filesMatched,
|
|
|
|
filesMatched+filesSkipped,
|
|
|
|
directoriesMatched,
|
|
|
|
directoriesMatched+directoriesSkipped,
|
2024-01-11 09:36:16 +00:00
|
|
|
time.Since(startTime).Round(time.Microsecond),
|
2023-01-25 01:06:15 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-01-04 14:43:52 +00:00
|
|
|
slices.Sort(list)
|
|
|
|
|
2024-01-06 15:41:30 +00:00
|
|
|
return list
|
2022-09-08 20:30:51 +00:00
|
|
|
}
|
|
|
|
|
2024-01-06 15:41:30 +00:00
|
|
|
func fileList(paths []string, filters *filters, sort string, index *fileIndex, formats types.Types, errorChannel chan<- error) []string {
|
2023-09-15 06:06:52 +00:00
|
|
|
switch {
|
2023-09-28 15:09:45 +00:00
|
|
|
case Index && !index.isEmpty() && filters.isEmpty():
|
2024-01-06 15:41:30 +00:00
|
|
|
return index.List()
|
2023-09-28 15:09:45 +00:00
|
|
|
case Index && !index.isEmpty() && !filters.isEmpty():
|
2024-01-06 15:41:30 +00:00
|
|
|
return filters.apply(index.List())
|
2023-09-28 15:09:45 +00:00
|
|
|
case Index && index.isEmpty() && !filters.isEmpty():
|
2024-01-06 15:41:30 +00:00
|
|
|
index.set(scanPaths(paths, sort, index, formats, errorChannel))
|
2023-09-15 18:51:04 +00:00
|
|
|
|
2024-01-06 15:41:30 +00:00
|
|
|
return filters.apply(index.List())
|
2023-09-28 15:09:45 +00:00
|
|
|
case Index && index.isEmpty() && filters.isEmpty():
|
2024-01-06 15:41:30 +00:00
|
|
|
index.set(scanPaths(paths, sort, index, formats, errorChannel))
|
2023-09-15 18:51:04 +00:00
|
|
|
|
2024-01-06 15:41:30 +00:00
|
|
|
return index.List()
|
2023-09-28 15:09:45 +00:00
|
|
|
case !Index && !filters.isEmpty():
|
2024-01-06 15:41:30 +00:00
|
|
|
return filters.apply(scanPaths(paths, sort, index, formats, errorChannel))
|
2023-09-15 06:06:52 +00:00
|
|
|
default:
|
2024-01-06 15:41:30 +00:00
|
|
|
return scanPaths(paths, sort, index, formats, errorChannel)
|
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
|
|
|
|
2024-01-06 00:50:45 +00:00
|
|
|
switch {
|
|
|
|
case fileCount < 1 && AllowEmpty:
|
|
|
|
return "", nil
|
|
|
|
case 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
|
|
|
}
|
|
|
|
|
2024-01-06 00:50:45 +00: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 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-10-04 19:09:49 +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
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
2023-04-11 12:19:06 +00:00
|
|
|
case pathMatches && hasSupportedFiles:
|
2023-09-28 12:29:33 +00:00
|
|
|
fmt.Printf("%s | PATHS: Added %s\n",
|
|
|
|
time.Now().Format(logDate),
|
|
|
|
args[i],
|
|
|
|
)
|
|
|
|
|
|
|
|
paths = append(paths, path)
|
2023-04-11 12:19:06 +00:00
|
|
|
case !pathMatches && hasSupportedFiles:
|
2023-09-28 12:29:33 +00:00
|
|
|
fmt.Printf("%s | PATHS: Added %s [resolved to %s]\n",
|
|
|
|
time.Now().Format(logDate),
|
|
|
|
args[i],
|
|
|
|
path,
|
|
|
|
)
|
2023-01-25 01:06:15 +00:00
|
|
|
|
2023-05-31 18:01:24 +00:00
|
|
|
paths = append(paths, path)
|
2023-09-28 12:29:33 +00: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 09:44:18 +00:00
|
|
|
}
|
2022-09-08 20:30:51 +00:00
|
|
|
}
|
|
|
|
|
2023-01-25 01:06:15 +00:00
|
|
|
return paths, nil
|
2022-09-08 15:57:59 +00:00
|
|
|
}
|