Skip to content

Commit

Permalink
refactoring: move Session.concatenateParts to the bottom
Browse files Browse the repository at this point in the history
  • Loading branch information
vbauerster committed Mar 15, 2024
1 parent c7e92b4 commit c3e87bf
Showing 1 changed file with 68 additions and 68 deletions.
136 changes: 68 additions & 68 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,74 +80,6 @@ func (s *Session) dropSkipped() {
}
}

func (s Session) concatenateParts(progress *mpb.Progress, logger *log.Logger) (err error) {
if len(s.Parts) <= 1 {
return nil
}
totalWritten := s.totalWritten()
if totalWritten != s.ContentLength {
return errors.Errorf("Size mismatch: expected %d got %d", s.ContentLength, totalWritten)
}

bar, err := progress.Add(int64(len(s.Parts)-1),
mpb.BarStyle().Lbound(" ").Rbound(" ").Build(),
mpb.BarFillerTrim(),
mpb.BarPriority(len(s.Parts)+1),
mpb.PrependDecorators(
decor.Name("Concatenating", decor.WCSyncWidthR),
decor.NewPercentage("%d", decor.WCSyncSpace),
),
mpb.AppendDecorators(
decor.OnComplete(decor.AverageETA(decor.ET_STYLE_MMSS, decor.WCSyncWidth), ":"),
decor.Name("", decor.WCSyncSpace),
decor.Name("", decor.WCSyncSpace),
decor.Name("", decor.WCSyncSpace),
),
)
if err != nil {
return err
}
fpart0, err := os.OpenFile(s.Parts[0].FileName, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer func() {
if e := fpart0.Close(); err == nil {
err = e
}
bar.Abort(false) // if bar is completed bar.Abort is nop
}()

for i := 1; i < len(s.Parts); i++ {
if !s.Parts[i].Skip {
fparti, err := os.Open(s.Parts[i].FileName)
if err != nil {
return err
}
logger.Printf("concatenating: %q into %q", fparti.Name(), fpart0.Name())
_, err = io.Copy(fpart0, fparti)
err = eitherError(err, fparti.Close())
if err != nil {
return err
}
err = os.Remove(fparti.Name())
if err != nil {
return err
}
}
bar.Increment()
}

stat, err := fpart0.Stat()
if err != nil {
return err
}
if totalWritten != stat.Size() {
panic("totalWritten != stat.Size()")
}
return nil
}

func (s *Session) loadState(name string) error {
f, err := os.Open(name)
if err != nil {
Expand Down Expand Up @@ -316,3 +248,71 @@ func (s Session) makeTotalBar(
}
}
}

func (s Session) concatenateParts(progress *mpb.Progress, logger *log.Logger) (err error) {
if len(s.Parts) <= 1 {
return nil
}
totalWritten := s.totalWritten()
if totalWritten != s.ContentLength {
return errors.Errorf("Size mismatch: expected %d got %d", s.ContentLength, totalWritten)
}

bar, err := progress.Add(int64(len(s.Parts)-1),
mpb.BarStyle().Lbound(" ").Rbound(" ").Build(),
mpb.BarFillerTrim(),
mpb.BarPriority(len(s.Parts)+1),
mpb.PrependDecorators(
decor.Name("Concatenating", decor.WCSyncWidthR),
decor.NewPercentage("%d", decor.WCSyncSpace),
),
mpb.AppendDecorators(
decor.OnComplete(decor.AverageETA(decor.ET_STYLE_MMSS, decor.WCSyncWidth), ":"),
decor.Name("", decor.WCSyncSpace),
decor.Name("", decor.WCSyncSpace),
decor.Name("", decor.WCSyncSpace),
),
)
if err != nil {
return err
}
fpart0, err := os.OpenFile(s.Parts[0].FileName, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer func() {
if e := fpart0.Close(); err == nil {
err = e
}
bar.Abort(false) // if bar is completed bar.Abort is nop
}()

for i := 1; i < len(s.Parts); i++ {
if !s.Parts[i].Skip {
fparti, err := os.Open(s.Parts[i].FileName)
if err != nil {
return err
}
logger.Printf("concatenating: %q into %q", fparti.Name(), fpart0.Name())
_, err = io.Copy(fpart0, fparti)
err = eitherError(err, fparti.Close())
if err != nil {
return err
}
err = os.Remove(fparti.Name())
if err != nil {
return err
}
}
bar.Increment()
}

stat, err := fpart0.Stat()
if err != nil {
return err
}
if totalWritten != stat.Size() {
panic("totalWritten != stat.Size()")
}
return nil
}

0 comments on commit c3e87bf

Please sign in to comment.