Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

fs: fix bandwidth limits below 1 KiB being multiplied by 1024 - fixes #9958

Roland60bdc6d4548f
diff --git a/fs/bwtimetable.go b/fs/bwtimetable.go
index bd737c8a6..cbb0a2f9d 100644
--- a/fs/bwtimetable.go
+++ b/fs/bwtimetable.go
@@ -18,10 +18,10 @@ type BwPair struct {
// String returns a printable representation of a BwPair
func (bp *BwPair) String() string {
var out strings.Builder
- out.WriteString(bp.Tx.String())
+ out.WriteString(bp.Tx.stringRoundTrip())
if bp.Rx != bp.Tx {
out.WriteRune(':')
- out.WriteString(bp.Rx.String())
+ out.WriteString(bp.Rx.stringRoundTrip())
}
return out.String()
}
diff --git a/fs/bwtimetable_test.go b/fs/bwtimetable_test.go
index 1c94c01b7..218bb6ccb 100644
--- a/fs/bwtimetable_test.go
+++ b/fs/bwtimetable_test.go
@@ -339,6 +339,29 @@ func TestBwTimetableSetReplaces(t *testing.T) {
}
}
+func TestBwTimetableStringRoundTrip(t *testing.T) {
+ for _, in := range []string{
+ "1B",
+ "500B",
+ "1023B",
+ "1B:2B",
+ "Mon-10:00,500B",
+ "08:00,512B 12:00,10M 23:00,off",
+ "1M",
+ "1M:100k",
+ "off",
+ "0",
+ } {
+ var tt BwTimetable
+ require.NoError(t, tt.Set(in), in)
+ printed := tt.String()
+
+ var got BwTimetable
+ require.NoError(t, got.Set(printed), "%q printed as %q", in, printed)
+ assert.Equal(t, tt, got, "%q printed as %q", in, printed)
+ }
+}
+
func TestBwTimetableSetErrorKeepsPrevious(t *testing.T) {
for _, in := range []string{
"Mon-11:00,333 bad",
diff --git a/fs/registry.go b/fs/registry.go
index 4a8aa9147..eea554ed0 100644
--- a/fs/registry.go
+++ b/fs/registry.go
@@ -305,16 +305,7 @@ func (o *Option) String() string {
// The default Go encoding can't be decoded uniquely
return CommaSepList(x).String()
case SizeSuffix:
- str := x.String()
- // Suffix bare numbers with "B" unless they are 0
- //
- // This makes sure that fs.SizeSuffix roundtrips through string
- if len(str) > 0 && str != "0" {
- if lastDigit := str[len(str)-1]; lastDigit >= '0' && lastDigit <= '9' {
- str += "B"
- }
- }
- return str
+ return x.stringRoundTrip()
}
return fmt.Sprint(v)
}
diff --git a/fs/sizesuffix.go b/fs/sizesuffix.go
index f725138db..123f923a2 100644
--- a/fs/sizesuffix.go
+++ b/fs/sizesuffix.go
@@ -76,6 +76,19 @@ func (x SizeSuffix) String() string {
return val + suffix
}
+// stringRoundTrip turns SizeSuffix into a string which Set parses back to the
+// same value. A bare number is read by Set as KiB, so values below 1 KiB get a
+// "B" suffix to stop them being scaled up.
+func (x SizeSuffix) stringRoundTrip() string {
+ str := x.String()
+ if len(str) > 0 && str != "0" {
+ if lastDigit := str[len(str)-1]; lastDigit >= '0' && lastDigit <= '9' {
+ str += "B"
+ }
+ }
+ return str
+}
+
// Unit turns SizeSuffix into a string with a unit
func (x SizeSuffix) unit(unit string) string {
val, suffix := x.string()