Serialize everything

main
Mari 3 years ago
parent c7a91fa9ae
commit 72929d5636
  1. 81
      drive-demuxer.go

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

Loading…
Cancel
Save