diff options
author | Nick White <git@njw.name> | 2019-01-19 15:33:58 +0000 |
---|---|---|
committer | Nick White <git@njw.name> | 2019-01-19 15:33:58 +0000 |
commit | 93a02d75156148ee69e223b6440e07ca581b643c (patch) | |
tree | dda0185e6765305187415078dee9d6003f5eceb2 /line-conf-buckets | |
parent | cbbe18136e7474b9a697d67a99e56eb680f65e23 (diff) |
Ensure files are closed as soon as they're finished with
Diffstat (limited to 'line-conf-buckets')
-rw-r--r-- | line-conf-buckets/line-conf-buckets.go | 31 |
1 files changed, 29 insertions, 2 deletions
diff --git a/line-conf-buckets/line-conf-buckets.go b/line-conf-buckets/line-conf-buckets.go index 9a125d2..c2df074 100644 --- a/line-conf-buckets/line-conf-buckets.go +++ b/line-conf-buckets/line-conf-buckets.go @@ -87,6 +87,7 @@ func main() { for _, f := range flag.Args() { file, err := os.Open(f) if err != nil { + fmt.Fprintf(os.Stderr, "Error opening %s\n", f) log.Fatal(err) } defer file.Close() @@ -114,6 +115,13 @@ func main() { } avg := totalconf / float64(num) + // Explicitly close file immediately after use, rather than relying on defer, + // as too many files could be opened before any of the files are closed, leading + // to a 'too many open files' error + // TODO: rewrite this loop so it uses a function or two, so we can rely + // on defer sensibly again. + file.Close() + if num == 0 || avg == 0 { continue } @@ -124,11 +132,26 @@ func main() { linedetail.Filebase = strings.Replace(f, ".prob", "", 1) linedetail.Basename = filepath.Base(linedetail.Filebase) linedetail.Dirname = filepath.Dir(linedetail.Filebase) - ft, ferr := ioutil.ReadFile(linedetail.Filebase + ".txt") + + txtfile, ferr := os.Open(linedetail.Filebase + ".txt") if ferr != nil { - log.Fatal(err) + fmt.Fprintf(os.Stderr, "Error opening %s\n", linedetail.Filebase + ".txt") + log.Fatal(ferr) + } + defer txtfile.Close() + ft, ferr := ioutil.ReadAll(txtfile) + if ferr != nil { + fmt.Fprintf(os.Stderr, "Error reading %s\n", linedetail.Filebase + ".txt") + log.Fatal(ferr) } linedetail.Fulltext = string(ft) + // Explicitly close file immediately after use, rather than relying on defer, + // as too many files could be opened before any of the files are closed, leading + // to a 'too many open files' error + // TODO: rewrite this loop so it uses a function or two, so we can rely + // on defer sensibly again. + txtfile.Close() + lines = append(lines, linedetail) } @@ -164,6 +187,10 @@ func main() { total := worstnum + mediumnum + bestnum + if total == 0 { + log.Fatal("No lines copied") + } + fmt.Printf("Copied lines to %s\n", outdir) fmt.Printf("---------------------------------\n") fmt.Printf("Lines 98%%+ quality: %d%%\n", 100 * bestnum / total) |