server(filesystem): fix panic with archive Progress

This commit is contained in:
Matthew Penner 2022-11-04 11:24:19 -06:00
parent 5a760a0dcc
commit f577f5521f
No known key found for this signature in database
2 changed files with 75 additions and 11 deletions

View File

@ -69,20 +69,36 @@ func (p *Progress) Write(v []byte) (int, error) {
// Progress returns a formatted progress string for the current progress. // Progress returns a formatted progress string for the current progress.
func (p *Progress) Progress(width int) string { func (p *Progress) Progress(width int) string {
// current = 100 (Progress, dynamic)
// total = 1000 (Content-Length, dynamic)
// width = 25 (Number of ticks to display, static)
// widthPercentage = 100 / width (What percentage does each tick represent, static)
//
// percentageDecimal = current / total = 0.1
// percentage = percentageDecimal * 100 = 10%
// ticks = percentage / widthPercentage = 2.5
//
// ticks is a float64, so we cast it to an int which rounds it down to 2.
// Values are cast to floats to prevent integer division.
current := p.Written() current := p.Written()
total := p.Total() total := p.Total()
// width := is passed as a parameter
widthPercentage := float64(100) / float64(width)
percentageDecimal := float64(current) / float64(total)
percentage := percentageDecimal * 100
ticks := int(percentage / widthPercentage)
// v = 100 (Progress) // Ensure that we never get a negative number of ticks, this will prevent strings#Repeat
// size = 1000 (Content-Length) // from panicking. A negative number of ticks is likely to happen when the total size is
// p / size = 0.1 // inaccurate, such as when we are going off of rough disk usage calculation.
// * 100 = 10% (Multiply by 100 to get a percentage of the download) if ticks < 0 {
// 10% / tickPercentage = (10% / (100 / 25)) (Divide by tick percentage to get the number of ticks) ticks = 0
// 2.5 (Number of ticks as a float64) } else if ticks > width {
// 2 (convert to an integer) ticks = width
}
// We have to cast these numbers to float in order to get a float result from the division. bar := strings.Repeat("=", ticks) + strings.Repeat(" ", width-ticks)
ticks := ((float64(current) / float64(total)) * 100) / (float64(100) / float64(width))
bar := strings.Repeat("=", int(ticks)) + strings.Repeat(" ", width-int(ticks))
return "[" + bar + "] " + system.FormatBytes(current) + " / " + system.FormatBytes(total) return "[" + bar + "] " + system.FormatBytes(current) + " / " + system.FormatBytes(total)
} }
@ -255,7 +271,7 @@ func (a *Archive) addToArchive(p string, rp string, w *tar.Writer) error {
// it doesn't work. // it doesn't work.
target, err = os.Readlink(s.Name()) target, err = os.Readlink(s.Name())
if err != nil { if err != nil {
// Ignore the not exist errors specifically, since theres nothing important about that. // Ignore the not exist errors specifically, since there is nothing important about that.
if !os.IsNotExist(err) { if !os.IsNotExist(err) {
log.WithField("path", rp).WithField("readlink_err", err.Error()).Warn("failed reading symlink for target path; skipping...") log.WithField("path", rp).WithField("readlink_err", err.Error()).Warn("failed reading symlink for target path; skipping...")
} }

View File

@ -0,0 +1,48 @@
package filesystem
import (
"bytes"
"testing"
. "github.com/franela/goblin"
)
func TestProgress(t *testing.T) {
g := Goblin(t)
g.Describe("Progress", func() {
g.It("properly initializes", func() {
total := int64(1000)
p := NewProgress(total)
g.Assert(p).IsNotNil()
g.Assert(p.Total()).Equal(total)
g.Assert(p.Written()).Equal(int64(0))
})
g.It("increments written when Write is called", func() {
v := []byte("hello")
p := NewProgress(1000)
_, err := p.Write(v)
g.Assert(err).IsNil()
g.Assert(p.Written()).Equal(int64(len(v)))
})
g.It("renders a progress bar", func() {
v := bytes.Repeat([]byte{' '}, 100)
p := NewProgress(1000)
_, err := p.Write(v)
g.Assert(err).IsNil()
g.Assert(p.Written()).Equal(int64(len(v)))
g.Assert(p.Progress(25)).Equal("[== ] 100 B / 1000 B")
})
g.It("renders a progress bar when written exceeds total", func() {
v := bytes.Repeat([]byte{' '}, 1001)
p := NewProgress(1000)
_, err := p.Write(v)
g.Assert(err).IsNil()
g.Assert(p.Written()).Equal(int64(len(v)))
g.Assert(p.Progress(25)).Equal("[=========================] 1001 B / 1000 B")
})
})
}