Skip to content

Commit

Permalink
Sync counters on read/write error (incl io.EOF) (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored Dec 24, 2023
1 parent 9a21465 commit 2f57786
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release-assets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jobs:
uses: actions/checkout@v3
- name: Build artifacts
run: |
git fetch -t
make release-assets
- name: Upload linux_amd64.tar.gz
if: hashFiles('linux_amd64.tar.gz') != ''
Expand Down
5 changes: 2 additions & 3 deletions cmd/catp/catp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,6 @@ func (r *runner) cat(filename string) (err error) {
r.readFile(rd, out)
}

cr.Close()

if r.parallel <= 1 {
r.pr.Stop()
}
Expand Down Expand Up @@ -391,7 +389,8 @@ func Main() error { //nolint:funlen,cyclop,gocognit,gocyclo
ver := flag.Bool("version", false, "print version and exit")

flag.Usage = func() {
fmt.Println("catp", version.Info().Version+",", version.Info().GoVersion, strings.Join(versionExtra, " "))
fmt.Println("catp", version.Module("github.com/bool64/progress").Version+",",
version.Info().GoVersion, strings.Join(versionExtra, " "))
fmt.Println()
fmt.Println("catp prints contents of files to STDOUT or dir/file output, \n" +
"while printing current progress status to STDERR. \n" +
Expand Down
18 changes: 9 additions & 9 deletions progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,10 @@ func (cr *sharedCounters) SetBytes(bytes *int64) {
cr.bytes = bytes
}

func (cr *sharedCounters) count(n int, p []byte) {
func (cr *sharedCounters) count(n int, p []byte, err error) {
cr.localBytes += int64(n)

if cr.localBytes > 100000 && cr.bytes != nil {
if (err != nil || cr.localBytes > 100000) && cr.bytes != nil {
atomic.AddInt64(cr.bytes, cr.localBytes)
cr.localBytes = 0
}
Expand All @@ -292,19 +292,19 @@ func (cr *sharedCounters) count(n int, p []byte) {
for i := 0; i < n; i++ {
if p[i] == '\n' {
cr.localLines++

if cr.localLines > 1000 {
atomic.AddInt64(cr.lines, cr.localLines)
cr.localLines = 0
}
}
}

if err != nil || cr.localLines > 1000 {
atomic.AddInt64(cr.lines, cr.localLines)
cr.localLines = 0
}
}

// Read reads and counts bytes.
func (cr *CountingReader) Read(p []byte) (n int, err error) {
n, err = cr.Reader.Read(p)
cr.count(n, p)
cr.count(n, p, err)

return n, err
}
Expand Down Expand Up @@ -349,7 +349,7 @@ type CountingWriter struct {
// Write writes and counts bytes.
func (cr *CountingWriter) Write(p []byte) (n int, err error) {
n, err = cr.Writer.Write(p)
cr.count(n, p)
cr.count(n, p, err)

return n, err
}
Expand Down

0 comments on commit 2f57786

Please sign in to comment.