Add `b` suffix so we can specify bytes in --bwlimit, --min-size etc

Fixes #449
This commit is contained in:
Nick Craig-Wood 2016-06-03 21:16:48 +01:00
parent 1d6698a754
commit 5723d788a4
4 changed files with 16 additions and 8 deletions

View File

@ -322,13 +322,14 @@ possibly signed sequence of decimal numbers, each with optional
fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m". Valid fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m". Valid
time units are "ns", "us" (or "µs"), "ms", "s", "m", "h". time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
Options which use SIZE use kByte by default. However a suffix of `k` Options which use SIZE use kByte by default. However a suffix of `b`
for kBytes, `M` for MBytes and `G` for GBytes may be used. These are for bytes, `k` for kBytes, `M` for MBytes and `G` for GBytes may be
the binary units, eg 2\*\*10, 2\*\*20, 2\*\*30 respectively. used. These are the binary units, eg 1, 2\*\*10, 2\*\*20, 2\*\*30
respectively.
### --bwlimit=SIZE ### ### --bwlimit=SIZE ###
Bandwidth limit in kBytes/s, or use suffix k|M|G. The default is `0` Bandwidth limit in kBytes/s, or use suffix b|k|M|G. The default is `0`
which means to not limit bandwidth. which means to not limit bandwidth.
For example to limit bandwidth usage to 10 MBytes/s use `--bwlimit 10M` For example to limit bandwidth usage to 10 MBytes/s use `--bwlimit 10M`

View File

@ -94,7 +94,7 @@ var (
) )
func init() { func init() {
pflag.VarP(&bwLimit, "bwlimit", "", "Bandwidth limit in kBytes/s, or use suffix k|M|G") pflag.VarP(&bwLimit, "bwlimit", "", "Bandwidth limit in kBytes/s, or use suffix b|k|M|G")
} }
// Turn SizeSuffix into a string // Turn SizeSuffix into a string
@ -104,6 +104,9 @@ func (x SizeSuffix) String() string {
switch { switch {
case x == 0: case x == 0:
return "0" return "0"
case x < 1024:
scaled = float64(x)
suffix = "b"
case x < 1024*1024: case x < 1024*1024:
scaled = float64(x) / 1024 scaled = float64(x) / 1024
suffix = "k" suffix = "k"
@ -132,6 +135,8 @@ func (x *SizeSuffix) Set(s string) error {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.': case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.':
suffixLen = 0 suffixLen = 0
multiplier = 1 << 10 multiplier = 1 << 10
case 'b', 'B':
multiplier = 1
case 'k', 'K': case 'k', 'K':
multiplier = 1 << 10 multiplier = 1 << 10
case 'm', 'M': case 'm', 'M':

View File

@ -12,7 +12,7 @@ func TestSizeSuffixString(t *testing.T) {
want string want string
}{ }{
{0, "0"}, {0, "0"},
{102, "0.100k"}, {102, "102b"},
{1024, "1k"}, {1024, "1k"},
{1024 * 1024, "1M"}, {1024 * 1024, "1M"},
{1024 * 1024 * 1024, "1G"}, {1024 * 1024 * 1024, "1G"},
@ -34,6 +34,8 @@ func TestSizeSuffixSet(t *testing.T) {
err bool err bool
}{ }{
{"0", 0, false}, {"0", 0, false},
{"1b", 1, false},
{"102B", 102, false},
{"0.1k", 102, false}, {"0.1k", 102, false},
{"0.1", 102, false}, {"0.1", 102, false},
{"1K", 1024, false}, {"1K", 1024, false},

View File

@ -35,8 +35,8 @@ var (
) )
func init() { func init() {
pflag.VarP(&minSize, "min-size", "", "Don't transfer any file smaller than this in k or suffix k|M|G") pflag.VarP(&minSize, "min-size", "", "Don't transfer any file smaller than this in k or suffix b|k|M|G")
pflag.VarP(&maxSize, "max-size", "", "Don't transfer any file larger than this in k or suffix k|M|G") pflag.VarP(&maxSize, "max-size", "", "Don't transfer any file larger than this in k or suffix b|k|M|G")
} }
// rule is one filter rule // rule is one filter rule