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"
|
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-16 23:04:44 +00:00
|
|
|
type scanStatsChannels struct {
|
|
|
|
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
|
|
|
|
suffix = "iB"
|
|
|
|
prefixes = "KMGTPE"
|
|
|
|
} else {
|
|
|
|
unit = 1000
|
|
|
|
suffix = "B"
|
|
|
|
prefixes = "kMGTPE"
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2023-10-04 19:09:49 +00:00
|
|
|
func newFile(list []string, sortOrder string, regexes *regexes, 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" {
|
2023-10-04 20:47:43 +00:00
|
|
|
splitPath, 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":
|
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
|
|
|
}
|
|
|
|
|
2023-10-04 19:09:49 +00:00
|
|
|
func nextFile(filePath, sortOrder string, regexes *regexes, formats types.Types) (string, error) {
|
2023-10-04 20:47:43 +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-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) {
|
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-01 17:36:19 +00:00
|
|
|
func walkPath(path string, fileChannel chan<- string, stats *scanStatsChannels, limit chan struct{}, formats types.Types) error {
|
|
|
|
limit <- struct{}{}
|
2023-12-17 12:35:03 +00:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
<-limit
|
|
|
|
}()
|
|
|
|
|
2023-10-03 13:26:55 +00:00
|
|
|
errorChannel := make(chan error)
|
|
|
|
done := make(chan bool, 1)
|
2023-08-02 18:29:37 +00:00
|
|
|
|
|
|
|
nodes, err := os.ReadDir(path)
|
|
|
|
if err != nil {
|
2023-10-03 13:26:55 +00:00
|
|
|
return err
|
2023-08-02 18:29:37 +00:00
|
|
|
}
|
|
|
|
|
2023-10-03 20:00:32 +00:00
|
|
|
var directories, files = 0, 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 {
|
|
|
|
if node.IsDir() {
|
|
|
|
directories++
|
|
|
|
} else {
|
2023-12-31 21:50:13 +00:00
|
|
|
if Ignore && node.Name() == IgnoreFile {
|
2023-12-07 16:34:19 +00:00
|
|
|
skipDir = true
|
|
|
|
}
|
|
|
|
|
2023-08-02 18:29:37 +00:00
|
|
|
files++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-03 13:26:55 +00:00
|
|
|
var skipFiles = false
|
2023-08-02 18:29:37 +00:00
|
|
|
|
2023-12-07 16:34:19 +00:00
|
|
|
if files <= MaxFileCount && files >= MinFileCount && !skipDir {
|
2023-10-03 14:28:00 +00:00
|
|
|
stats.directoriesMatched <- 1
|
|
|
|
} else {
|
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
|
|
|
|
}
|
|
|
|
|
2023-10-03 20:00:32 +00:00
|
|
|
var wg sync.WaitGroup
|
2022-09-08 20:30:51 +00:00
|
|
|
|
2023-10-03 20:00:32 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
for _, node := range nodes {
|
2022-10-20 19:22:01 +00:00
|
|
|
wg.Add(1)
|
|
|
|
|
2023-10-03 20:00:32 +00:00
|
|
|
go func(node fs.DirEntry) {
|
|
|
|
defer wg.Done()
|
2022-10-20 19:22:01 +00:00
|
|
|
|
2023-10-03 20:00:32 +00:00
|
|
|
fullPath := filepath.Join(path, node.Name())
|
2023-10-03 13:26:55 +00:00
|
|
|
|
2023-10-03 20:00:32 +00:00
|
|
|
switch {
|
|
|
|
case node.IsDir() && Recursive:
|
2023-12-17 12:35:03 +00:00
|
|
|
err := walkPath(fullPath, fileChannel, stats, limit, formats)
|
2023-10-03 20:00:32 +00:00
|
|
|
if err != nil {
|
|
|
|
errorChannel <- err
|
2023-09-16 23:04:44 +00:00
|
|
|
|
2023-10-03 20:00:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
case !node.IsDir() && !skipFiles:
|
|
|
|
path, err := normalizePath(fullPath)
|
|
|
|
if err != nil {
|
|
|
|
errorChannel <- err
|
2023-04-11 12:19:06 +00:00
|
|
|
|
2023-10-03 20:00:32 +00:00
|
|
|
return
|
|
|
|
}
|
2023-10-03 14:44:50 +00:00
|
|
|
|
2023-10-03 20:00:32 +00:00
|
|
|
if formats.Validate(path) || Fallback {
|
|
|
|
fileChannel <- path
|
2023-09-15 06:06:52 +00:00
|
|
|
|
2023-10-03 20:00:32 +00:00
|
|
|
stats.filesMatched <- 1
|
2023-09-15 06:06:52 +00:00
|
|
|
|
2023-10-03 20:00:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
stats.filesSkipped <- 1
|
|
|
|
}
|
|
|
|
}(node)
|
2022-09-08 17:12:58 +00:00
|
|
|
}
|
2023-10-03 20:00:32 +00:00
|
|
|
}()
|
2022-10-20 19:22:01 +00:00
|
|
|
|
2023-09-16 23:04:44 +00:00
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
2023-10-03 16:07:58 +00:00
|
|
|
|
2023-09-16 23:04:44 +00:00
|
|
|
done <- true
|
|
|
|
}()
|
2023-09-15 18:51:04 +00:00
|
|
|
|
2023-09-16 23:04:44 +00:00
|
|
|
Poll:
|
|
|
|
for {
|
|
|
|
select {
|
2023-10-03 13:26:55 +00:00
|
|
|
case err := <-errorChannel:
|
|
|
|
return err
|
2023-09-16 23:04:44 +00:00
|
|
|
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-10-04 19:09:49 +00:00
|
|
|
func scanPaths(paths []string, sort string, index *fileIndex, formats types.Types) ([]string, error) {
|
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)
|
|
|
|
errorChannel := make(chan error)
|
2023-10-05 21:33:23 +00:00
|
|
|
done := make(chan bool)
|
2022-09-25 01:28:34 +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-12-17 11:16:46 +00:00
|
|
|
var list []string
|
|
|
|
|
2023-10-05 02:54:06 +00:00
|
|
|
go func() {
|
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
|
|
|
|
2023-10-05 02:54:06 +00:00
|
|
|
go func() {
|
2023-10-05 21:33:23 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case stat := <-statsChannels.filesMatched:
|
|
|
|
filesMatched += stat
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
2023-10-05 02:54:06 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
2023-10-05 21:33:23 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case stat := <-statsChannels.filesSkipped:
|
|
|
|
filesSkipped += stat
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
2023-10-05 02:54:06 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
2023-10-05 21:33:23 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case stat := <-statsChannels.directoriesMatched:
|
|
|
|
directoriesMatched += stat
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
2023-10-05 02:54:06 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
2023-10-05 21:33:23 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case stat := <-statsChannels.directoriesSkipped:
|
|
|
|
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
|
|
|
|
2023-12-17 11:16:46 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2023-01-25 01:06:15 +00:00
|
|
|
for i := 0; i < len(paths); i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
go func(i int) {
|
|
|
|
defer func() {
|
|
|
|
wg.Done()
|
|
|
|
}()
|
2023-12-17 12:35:03 +00:00
|
|
|
|
|
|
|
err := walkPath(paths[i], fileChannel, statsChannels, limit, formats)
|
2023-01-25 01:06:15 +00:00
|
|
|
|
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()
|
2023-10-03 16:07:58 +00:00
|
|
|
|
2023-10-05 21:33:23 +00:00
|
|
|
close(done)
|
2023-09-15 18:51:04 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
Poll:
|
|
|
|
for {
|
|
|
|
select {
|
2023-09-28 07:47:43 +00:00
|
|
|
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-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,
|
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-10-04 19:09:49 +00:00
|
|
|
func fileList(paths []string, filters *filters, sort string, index *fileIndex, formats types.Types) ([]string, error) {
|
2023-09-15 06:06:52 +00:00
|
|
|
switch {
|
2023-09-28 15:09:45 +00:00
|
|
|
case Index && !index.isEmpty() && filters.isEmpty():
|
|
|
|
return index.List(), nil
|
|
|
|
case Index && !index.isEmpty() && !filters.isEmpty():
|
|
|
|
return filters.apply(index.List()), nil
|
|
|
|
case Index && index.isEmpty() && !filters.isEmpty():
|
|
|
|
list, err := scanPaths(paths, sort, index, formats)
|
2023-09-15 18:51:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return []string{}, err
|
|
|
|
}
|
2023-09-28 15:09:45 +00:00
|
|
|
index.set(list)
|
2023-09-15 18:51:04 +00:00
|
|
|
|
2023-09-28 15:09:45 +00:00
|
|
|
return filters.apply(index.List()), nil
|
|
|
|
case Index && index.isEmpty() && filters.isEmpty():
|
|
|
|
list, err := scanPaths(paths, sort, index, formats)
|
2023-09-15 18:51:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return []string{}, err
|
|
|
|
}
|
2023-09-28 15:09:45 +00:00
|
|
|
index.set(list)
|
2023-09-15 18:51:04 +00:00
|
|
|
|
2023-09-28 15:09:45 +00:00
|
|
|
return index.List(), nil
|
|
|
|
case !Index && !filters.isEmpty():
|
|
|
|
list, err := scanPaths(paths, sort, index, formats)
|
2023-09-15 18:51:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return []string{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return filters.apply(list), nil
|
2023-09-15 06:06:52 +00:00
|
|
|
default:
|
2023-09-28 15:09:45 +00:00
|
|
|
list, err := scanPaths(paths, sort, index, formats)
|
2023-09-15 18:51:04 +00:00
|
|
|
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-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
|
|
|
}
|