thread number spec

master
Avril 5 years ago
parent 07fc573d72
commit ca5e6c7fdc
Signed by: flanchan
GPG Key ID: 284488987C31F630

2
.gitignore vendored

@ -0,0 +1,2 @@
test/
lazy-rebuild

@ -10,6 +10,8 @@ import (
"strings"
//"log"
"os"
"sync"
"strconv"
)
var keepLongExt bool = false
@ -64,13 +66,21 @@ func dofile(i int, y string) {
fmt.Printf("(%d) %s -> %s\n", i, y, newname)
}
}
var maxThreads int = 0
func main() {
ar := os.Args[1:];
var wg sync.WaitGroup
if (len(ar) < 1) {
fmt.Printf("Usage: %s [--long] [--fake] [--recurse] [-quiet] <file ...>\n\t--long\tKeep long file extensions\n\t--fake\tDo not rename files.\n\t--recurse\tWalk path recursively.\n\t--quiet\tDo not show (some) errors.\n", os.Args[0])
fmt.Printf("Usage: %s [--long] [--fake] [--recurse] [-quiet] [--threads <number>] <file ...>\n\t--long\tKeep long file extensions\n\t--fake\tDo not rename files.\n\t--recurse\tWalk path recursively.\n\t--quiet\tDo not show (some) errors.\n", os.Args[0])
return;
}
var ignore = false
for i, x := range ar {
if ignore {
ignore=false
continue
}
if( strings.HasPrefix(x, "--")) {
switch x[2:] {
case "long":
@ -81,6 +91,17 @@ func main() {
recurse = !recurse
case "quiet":
noErr = !noErr
case "threads":
if i<len(ar)-1 {
if th, err := strconv.Atoi(ar[i+1]); err ==nil {
maxThreads = th
ignore = true
} else {
fmt.Printf("Bad threads flag");
}
} else {
fmt.Printf("No threads flag")
}
default:
fmt.Printf("Unknown flag \"%s\"\n", x)
}
@ -88,23 +109,50 @@ func main() {
if fi, err:= os.Stat(x); os.IsNotExist(err) {
fmt.Printf("(%d) %s does not exist.\n", i, x)
} else {
var sem chan bool
if maxThreads >0 {
sem = make(chan bool, maxThreads)
}else {
sem = make(chan bool)
}
var _dofile = func(i int, s string) {
if maxThreads == 1 {
dofile(i,s)
return
}
wg.Add(1)
if(maxThreads>0) {
sem <- true
}
go func() {
defer wg.Done()
dofile(i,s)
if(maxThreads>0) {
<-sem
}
}()
}
if(fi.Mode().IsDir()) {
if(recurse) {
filepath.Walk(x, func(path string, f os.FileInfo, err error) error {
if(!f.Mode().IsDir()) {
dofile(i,path)
_dofile(i,path)
}
return nil
})
} else {
files, err := ioutil.ReadDir(x)
if err != nil {
fmt.Printf("E: Error enumerating files: %s\n", err)
fmt.Printf("E: Error enumerating files in %s: %s\n", x, err)
} else {
for _, f := range files {
if(!f.Mode().IsDir()) {
fl := fmt.Sprintf("%s/%s", x, f.Name())
dofile(i,fl)
_dofile(i,fl)
} else {
fmt.Printf("!: Ignoring %s (is directory)\n", f.Name())
}
@ -113,11 +161,14 @@ func main() {
}
} else {
dofile(i, x)
_dofile(i, x)
}
close(sem)
}
}
}
wg.Wait()
return;
}

Loading…
Cancel
Save