forked from secsy/goftp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallel_walk_test.go
84 lines (69 loc) · 1.76 KB
/
parallel_walk_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright 2015 Muir Manders. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package goftp_test
import (
"fmt"
"os"
"path"
"path/filepath"
"sync/atomic"
"github.com/secsy/goftp"
)
// Just for fun, walk an ftp server in parallel. I make no claim that this is
// correct or a good idea.
func ExampleClient_ReadDir_parallelWalk() {
client, err := goftp.Dial("ftp.hq.nasa.gov")
if err != nil {
panic(err)
}
Walk(client, "", func(fullPath string, info os.FileInfo, err error) error {
if err != nil {
// no permissions is okay, keep walking
if err.(goftp.Error).Code() == 550 {
return nil
}
return err
}
fmt.Println(fullPath)
return nil
})
}
// Walk a FTP file tree in parallel with prunability and error handling.
// See http://golang.org/pkg/path/filepath/#Walk for interface details.
func Walk(client *goftp.Client, root string, walkFn filepath.WalkFunc) (ret error) {
dirsToCheck := make(chan string, 100)
var workCount int32 = 1
dirsToCheck <- root
for dir := range dirsToCheck {
go func(dir string) {
files, err := client.ReadDir(dir)
if err != nil {
if err = walkFn(dir, nil, err); err != nil && err != filepath.SkipDir {
ret = err
close(dirsToCheck)
return
}
}
for _, file := range files {
if err = walkFn(path.Join(dir, file.Name()), file, nil); err != nil {
if file.IsDir() && err == filepath.SkipDir {
continue
}
ret = err
close(dirsToCheck)
return
}
if file.IsDir() {
atomic.AddInt32(&workCount, 1)
dirsToCheck <- path.Join(dir, file.Name())
}
}
atomic.AddInt32(&workCount, -1)
if workCount == 0 {
close(dirsToCheck)
}
}(dir)
}
return ret
}