Skip to content

Commit b576b1f

Browse files
committed
[*] workaround for "path/filepath: Walk recurses on directory symlinks on Windows" issue (see golang/go#17540)
[~] command output now shows how much actual overhead is better/worse than rough estimate
1 parent b873887 commit b576b1f

File tree

1 file changed

+43
-21
lines changed

1 file changed

+43
-21
lines changed

src/fsized/fsized.go

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ import (
66
"flag"
77
"fmt"
88
"log"
9-
10-
"github.com/kivisade/tabfmt"
11-
"github.com/dustin/go-humanize"
129
"time"
1310
"regexp"
1411
"strconv"
12+
"runtime"
13+
14+
"github.com/kivisade/tabfmt"
15+
"github.com/dustin/go-humanize"
1516
)
1617

1718
func p2(n uint64) (p uint) {
@@ -107,6 +108,9 @@ func (s *StatCounter) Walk(path string, f os.FileInfo, err error) (abort error)
107108
return
108109
}
109110
if f.IsDir() {
111+
if runtime.GOOS[:] == "windows" && f.Mode() & os.ModeSymlink != 0 { // NB: runtime.GOOS[:] is to mute the annoying "condition is always true" warning
112+
return filepath.SkipDir
113+
}
110114
return
111115
}
112116
s.addFile(uint64(f.Size()))
@@ -176,19 +180,28 @@ func (s *StatCounter) GetTotalCount() uint64 {
176180
return s.totalCount
177181
}
178182

183+
func (s *StatCounter) GetTotalOverhead() uint64 {
184+
return s.totalOverhead
185+
}
186+
179187
func main() {
180188
var (
181-
stats *StatCounter
182-
ready chan error = make(chan error)
183-
running bool = true
184-
root string
185-
block string
186-
blockSz uint64
187-
out string
188-
start time.Time
189-
runtime time.Duration
190-
totalCount uint64
191-
fps float64
189+
stats *StatCounter
190+
ready chan error = make(chan error)
191+
running bool = true
192+
err error
193+
root string
194+
block string
195+
blockSz uint64
196+
out string
197+
start time.Time
198+
runningTime time.Duration
199+
totalCount uint64
200+
fps float64
201+
ohdActual uint64
202+
ohdEstimate uint64
203+
ohdPrc float64
204+
ohdSgn string = "better"
192205
)
193206

194207
flag.StringVar(&out, "out", "formatted", "Output format ('formatted' for pretty-printed table or 'tab' for Excel-friendly tabbed format)")
@@ -227,24 +240,33 @@ func main() {
227240
}
228241
}()
229242

230-
if err := <-ready; err != nil {
231-
running = false
243+
if err, running = <-ready, false; err != nil {
232244
log.Printf("Error while recursively walking %s: %s", root, err)
233245
}
234246

235-
runtime = time.Since(start)
247+
runningTime = time.Since(start)
236248
if totalCount = stats.GetTotalCount(); totalCount > 0 {
237-
fps = float64(totalCount) / runtime.Seconds()
249+
fps = float64(totalCount) / runningTime.Seconds()
238250
}
239251

252+
fmt.Println()
253+
240254
switch out {
241255
case "formatted":
242256
stats.Print()
243257
default:
244258
stats.PrintSimple()
245259
}
246260

247-
fmt.Printf("\nScanned %d files in %s (avg. %.2f files per second).\n", totalCount, runtime, fps)
248-
fmt.Printf("Rough estimate of overhead per %d files using allocation units of %d bytes is %s.\n",
249-
totalCount, blockSz, humanize.Bytes(totalCount*blockSz/2))
261+
ohdActual, ohdEstimate = stats.GetTotalOverhead(), totalCount*blockSz/2
262+
ohdPrc = 1 - float64(ohdActual) / float64(ohdEstimate)
263+
if ohdPrc < 0 {
264+
ohdPrc *= -1
265+
ohdSgn = "worse"
266+
}
267+
268+
fmt.Printf("\nScanned %d files in %s (avg. %.2f files per second).\n\n", totalCount, runningTime, fps)
269+
fmt.Printf("Rough estimate of overhead per %d files using allocation units of %d bytes is %s. " +
270+
"Actual overhead of %s is %.2f%% %s.\n",
271+
totalCount, blockSz, humanize.Bytes(ohdEstimate), humanize.Bytes(ohdActual), 100*ohdPrc, ohdSgn)
250272
}

0 commit comments

Comments
 (0)