Serialize everything

main
Mari 4 years ago
parent c7a91fa9ae
commit 72929d5636
  1. 35
      drive-demuxer.go

@ -3,13 +3,11 @@ package main
import ( import (
"bytes" "bytes"
"flag" "flag"
"github.com/cheggaaa/pb/v3"
"io" "io"
"log" "log"
"os" "os"
"path" "path"
"sync"
"github.com/cheggaaa/pb/v3"
) )
type TopLevel struct { type TopLevel struct {
@ -18,8 +16,6 @@ type TopLevel struct {
LeftOutput string LeftOutput string
CombinedOutput string CombinedOutput string
RightOutput string RightOutput string
OpenHandles chan int
OpenDirectories chan int
DryRun bool DryRun bool
Bar *pb.ProgressBar Bar *pb.ProgressBar
} }
@ -191,10 +187,6 @@ func (s *Step) RemoveInputDirs(child string) {
} }
func (s *Step) ListChildren() []string { func (s *Step) ListChildren() []string {
s.OpenHandles <- 1
defer func() {
<-s.OpenHandles
}()
leftInPath := s.InputPath("", LEFT) leftInPath := s.InputPath("", LEFT)
rightInPath := s.InputPath("", RIGHT) rightInPath := s.InputPath("", RIGHT)
results := make(map[string]bool) results := make(map[string]bool)
@ -220,10 +212,6 @@ func (s *Step) ListChildren() []string {
} }
func (s *Step) AreFilesIdentical(child string) bool { func (s *Step) AreFilesIdentical(child string) bool {
s.OpenHandles <- 1
defer func() {
<-s.OpenHandles
}()
leftInPath := s.InputPath(child, LEFT) leftInPath := s.InputPath(child, LEFT)
rightInPath := s.InputPath(child, RIGHT) rightInPath := s.InputPath(child, RIGHT)
leftInfo, err := os.Stat(leftInPath) leftInfo, err := os.Stat(leftInPath)
@ -290,8 +278,6 @@ func (s *Step) AreFilesIdentical(child string) bool {
} }
func (s *Step) Walk() { func (s *Step) Walk() {
wg := sync.WaitGroup{}
children := s.ListChildren() children := s.ListChildren()
s.Bar.AddTotal(int64(len(children))) s.Bar.AddTotal(int64(len(children)))
for _, child := range children { for _, child := range children {
@ -330,11 +316,6 @@ func (s *Step) Walk() {
switch rightState { switch rightState {
case FILE: case FILE:
if leftState == FILE { if leftState == FILE {
wg.Add(1)
go func(child string) {
defer func() {
wg.Done()
}()
if s.AreFilesIdentical(child) { if s.AreFilesIdentical(child) {
s.Combine(child) s.Combine(child)
s.Bar.Increment() s.Bar.Increment()
@ -342,20 +323,12 @@ func (s *Step) Walk() {
s.Separate(child) s.Separate(child)
s.Bar.Increment() s.Bar.Increment()
} }
}(child)
} else { } else {
s.Separate(child) s.Separate(child)
s.Bar.Increment() s.Bar.Increment()
} }
case DIRECTORY: case DIRECTORY:
if leftState == DIRECTORY { if leftState == DIRECTORY {
s.OpenDirectories <- 1
wg.Add(1)
go func(child string) {
defer func() {
wg.Done()
<-s.OpenDirectories
}()
substep := Step{ substep := Step{
TopLevel: s.TopLevel, TopLevel: s.TopLevel,
Subpath: path.Join(s.Subpath, child), Subpath: path.Join(s.Subpath, child),
@ -364,9 +337,7 @@ func (s *Step) Walk() {
substep.Walk() substep.Walk()
s.RemoveInputDirs(child) s.RemoveInputDirs(child)
s.Bar.Increment() s.Bar.Increment()
}(child)
} else { } else {
wg.Add(1)
s.Separate(child) s.Separate(child)
s.Bar.Increment() s.Bar.Increment()
} }
@ -374,14 +345,10 @@ func (s *Step) Walk() {
panic("Unexpected state") panic("Unexpected state")
} }
} }
wg.Wait()
} }
func main() { func main() {
settings := TopLevel{ settings := TopLevel{
OpenHandles: make(chan int, 450),
OpenDirectories: make(chan int, 1000),
Bar: pb.StartNew(1), Bar: pb.StartNew(1),
} }
flag.StringVar(&settings.LeftInput, flag.StringVar(&settings.LeftInput,

Loading…
Cancel
Save