fs: Extend SizeSuffix to include TB and PB for rclone about

This commit is contained in:
Nick Craig-Wood 2018-04-17 21:47:20 +01:00
parent 1ac6dacf0f
commit ef3bcec76c
3 changed files with 30 additions and 15 deletions

View File

@ -227,9 +227,9 @@ fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m". Valid
time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
Options which use SIZE use kByte by default. However, a suffix of `b`
for bytes, `k` for kBytes, `M` for MBytes and `G` for GBytes may be
used. These are the binary units, eg 1, 2\*\*10, 2\*\*20, 2\*\*30
respectively.
for bytes, `k` for kBytes, `M` for MBytes, `G` for GBytes, `T` for
TBytes and `P` for PBytes may be used. These are the binary units, eg
1, 2\*\*10, 2\*\*20, 2\*\*30 respectively.
### --backup-dir=DIR ###

View File

@ -22,18 +22,24 @@ func (x SizeSuffix) string() (string, string) {
return "off", ""
case x == 0:
return "0", ""
case x < 1024:
case x < 1<<10:
scaled = float64(x)
suffix = ""
case x < 1024*1024:
scaled = float64(x) / 1024
case x < 1<<20:
scaled = float64(x) / (1 << 10)
suffix = "k"
case x < 1024*1024*1024:
scaled = float64(x) / 1024 / 1024
case x < 1<<30:
scaled = float64(x) / (1 << 20)
suffix = "M"
default:
scaled = float64(x) / 1024 / 1024 / 1024
case x < 1<<40:
scaled = float64(x) / (1 << 30)
suffix = "G"
case x < 1<<50:
scaled = float64(x) / (1 << 40)
suffix = "T"
default:
scaled = float64(x) / (1 << 50)
suffix = "P"
}
if math.Floor(scaled) == scaled {
return fmt.Sprintf("%.0f", scaled), suffix
@ -80,6 +86,10 @@ func (x *SizeSuffix) Set(s string) error {
multiplier = 1 << 20
case 'g', 'G':
multiplier = 1 << 30
case 't', 'T':
multiplier = 1 << 40
case 'p', 'P':
multiplier = 1 << 50
default:
return errors.Errorf("bad suffix %q", suffix)
}

View File

@ -44,6 +44,9 @@ func TestSizeSuffixUnit(t *testing.T) {
{1024 * 1024 * 1024, "1 GBytes"},
{10 * 1024 * 1024 * 1024, "10 GBytes"},
{10.1 * 1024 * 1024 * 1024, "10.100 GBytes"},
{10 * 1024 * 1024 * 1024 * 1024, "10 TBytes"},
{10 * 1024 * 1024 * 1024 * 1024 * 1024, "10 PBytes"},
{1 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024, "1024 PBytes"},
{-1, "off"},
{-100, "off"},
} {
@ -70,20 +73,22 @@ func TestSizeSuffixSet(t *testing.T) {
{"1M", 1024 * 1024, false},
{"1.g", 1024 * 1024 * 1024, false},
{"10G", 10 * 1024 * 1024 * 1024, false},
{"10T", 10 * 1024 * 1024 * 1024 * 1024, false},
{"10P", 10 * 1024 * 1024 * 1024 * 1024 * 1024, false},
{"off", -1, false},
{"OFF", -1, false},
{"", 0, true},
{"1p", 0, true},
{"1.p", 0, true},
{"1p", 0, true},
{"1q", 0, true},
{"1.q", 0, true},
{"1q", 0, true},
{"-1K", 0, true},
} {
ss := SizeSuffix(0)
err := ss.Set(test.in)
if test.err {
require.Error(t, err)
require.Error(t, err, test.in)
} else {
require.NoError(t, err)
require.NoError(t, err, test.in)
}
assert.Equal(t, test.want, int64(ss))
}