local: refactor default os encoding out from local backend into shared encoder lib

This commit is contained in:
albertony 2021-05-28 13:34:29 +02:00
parent 63708d73be
commit 9a2811f0b2
7 changed files with 28 additions and 32 deletions

View File

@ -1,11 +0,0 @@
//+build darwin
package local
import "github.com/rclone/rclone/lib/encoder"
// This is the encoding used by the local backend for macOS
//
// macOS can't store invalid UTF-8, it converts them into %XX encoding
const defaultEnc = (encoder.Base |
encoder.EncodeInvalidUtf8)

View File

@ -1,8 +0,0 @@
//+build !windows,!darwin
package local
import "github.com/rclone/rclone/lib/encoder"
// This is the encoding used by the local backend for non windows platforms
const defaultEnc = encoder.Base

View File

@ -195,7 +195,7 @@ enabled, rclone will no longer update the modtime after copying a file.`,
Name: config.ConfigEncoding,
Help: config.ConfigEncodingHelp,
Advanced: true,
Default: defaultEnc,
Default: encoder.OS,
}},
}
fs.Register(fsi)

View File

@ -3,6 +3,8 @@ package local
import (
"runtime"
"testing"
"github.com/rclone/rclone/lib/encoder"
)
// Test Windows character replacements
@ -21,7 +23,7 @@ func TestCleanWindows(t *testing.T) {
t.Skipf("windows only")
}
for _, test := range testsWindows {
got := cleanRootPath(test[0], true, defaultEnc)
got := cleanRootPath(test[0], true, encoder.OS)
expect := test[1]
if got != expect {
t.Fatalf("got %q, expected %q", got, expect)

9
lib/encoder/os_darwin.go Normal file
View File

@ -0,0 +1,9 @@
//+build darwin
package encoder
// OS is the encoding used by the local backend for macOS
//
// macOS can't store invalid UTF-8, it converts them into %XX encoding
const OS = (Base |
EncodeInvalidUtf8)

6
lib/encoder/os_other.go Normal file
View File

@ -0,0 +1,6 @@
//+build !windows,!darwin
package encoder
// OS is the encoding used by the local backend for non windows platforms
const OS = Base

View File

@ -1,10 +1,8 @@
//+build windows
package local
package encoder
import "github.com/rclone/rclone/lib/encoder"
// This is the encoding used by the local backend for windows platforms
// OS is the encoding used by the local backend for windows platforms
//
// List of replaced characters:
// < (less than) -> '' // FULLWIDTH LESS-THAN SIGN
@ -24,10 +22,10 @@ import "github.com/rclone/rclone/lib/encoder"
// Also encode invalid UTF-8 bytes as Go can't convert them to UTF-16.
//
// https://docs.microsoft.com/de-de/windows/desktop/FileIO/naming-a-file#naming-conventions
const defaultEnc = (encoder.Base |
encoder.EncodeWin |
encoder.EncodeBackSlash |
encoder.EncodeCtl |
encoder.EncodeRightSpace |
encoder.EncodeRightPeriod |
encoder.EncodeInvalidUtf8)
const OS = (Base |
EncodeWin |
EncodeBackSlash |
EncodeCtl |
EncodeRightSpace |
EncodeRightPeriod |
EncodeInvalidUtf8)