|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "os/exec" |
| 7 | + |
| 8 | + "mypan/pkg/client" |
| 9 | + "mypan/pkg/util" |
| 10 | + |
| 11 | + "github.com/pkg/errors" |
| 12 | +) |
| 13 | + |
| 14 | +type WalkerStdin struct { |
| 15 | + Path string `json:"path"` |
| 16 | + IsDir int `json:"isdir"` |
| 17 | + |
| 18 | + Size uint64 `json:"size"` |
| 19 | + ServerCtime uint64 `json:"server_ctime"` |
| 20 | + ServerMtime uint64 `json:"server_mtime"` |
| 21 | + LocalCtime uint64 `json:"local_ctime"` |
| 22 | + LocalMtime uint64 `json:"local_mtime"` |
| 23 | +} |
| 24 | + |
| 25 | +type Walker struct { |
| 26 | + client client.ClientI |
| 27 | + execArgv []string |
| 28 | + |
| 29 | + parallelDo *util.ParallelDo |
| 30 | +} |
| 31 | + |
| 32 | +func NewWalker( |
| 33 | + client client.ClientI, |
| 34 | + execArgv []string, |
| 35 | +) *Walker { |
| 36 | + w := &Walker{ |
| 37 | + client: client, |
| 38 | + execArgv: execArgv, |
| 39 | + } |
| 40 | + return w |
| 41 | +} |
| 42 | + |
| 43 | +func (w *Walker) Parallel(n int) { |
| 44 | + w.parallelDo = util.NewParallelDo(n) |
| 45 | +} |
| 46 | + |
| 47 | +func (w *Walker) Walk(ctx context.Context, dir string) error { |
| 48 | + if err := w.exec(ctx, &WalkerStdin{ |
| 49 | + Path: dir, |
| 50 | + IsDir: 1, |
| 51 | + }); err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + resp, err := w.client.ListEx(ctx, dir) |
| 55 | + if err != nil { |
| 56 | + return errors.Wrapf(err, "list %s", dir) |
| 57 | + } |
| 58 | + for _, src := range resp.List { |
| 59 | + if src.IsDir != 0 { |
| 60 | + // we check src.Empty by ourselves |
| 61 | + err := w.Walk(ctx, src.Path) |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + } else { |
| 66 | + err := w.exec(ctx, &WalkerStdin{ |
| 67 | + Path: src.Path, |
| 68 | + IsDir: src.IsDir, |
| 69 | + |
| 70 | + Size: src.Size, |
| 71 | + ServerCtime: src.ServerCtime, |
| 72 | + ServerMtime: src.ServerMtime, |
| 73 | + LocalCtime: src.LocalCtime, |
| 74 | + LocalMtime: src.LocalMtime, |
| 75 | + }) |
| 76 | + if err != nil { |
| 77 | + return err |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + return util.TryParallelJoin(ctx, w.parallelDo) |
| 82 | +} |
| 83 | + |
| 84 | +func (w *Walker) exec(ctx context.Context, stdin *WalkerStdin) error { |
| 85 | + return util.TryParallelDo(ctx, w.parallelDo, func(ctx context.Context) error { |
| 86 | + return w.exec_(ctx, stdin) |
| 87 | + }) |
| 88 | +} |
| 89 | + |
| 90 | +func (w *Walker) exec_(ctx context.Context, stdin *WalkerStdin) error { |
| 91 | + stdinData := util.MustMarshalJSON(stdin) |
| 92 | + stdinReader := bytes.NewBuffer(stdinData) |
| 93 | + cmd := exec.CommandContext(ctx, w.execArgv[0], w.execArgv[1:]...) |
| 94 | + cmd.Stdin = stdinReader |
| 95 | + output, err := cmd.CombinedOutput() |
| 96 | + if err != nil { |
| 97 | + return errors.Wrapf(err, "exec for %s: %s", stdin.Path, output) |
| 98 | + } |
| 99 | + return nil |
| 100 | +} |
0 commit comments