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

sftp: fix sync deleting files with control characters in their names

nielash0c9826d3440d
diff --git a/backend/sftp/sftp.go b/backend/sftp/sftp.go
index 5cc4dd1a9..bb1fd3b15 100644
--- a/backend/sftp/sftp.go
+++ b/backend/sftp/sftp.go
@@ -250,7 +250,7 @@ E.g. the second example above should be rewritten as:
Name: config.ConfigEncoding,
Help: config.ConfigEncodingHelp,
Advanced: true,
- Default: encoder.Display,
+ Default: encoder.Base,
}, {
Name: "set_modtime",
Default: true,
@@ -1883,8 +1883,7 @@ func (f *Fs) PutStream(ctx context.Context, in io.Reader, src fs.ObjectInfo, opt
// mkParentDir makes the parent of remote if necessary and any
// directories above that
func (f *Fs) mkParentDir(ctx context.Context, remote string) error {
- parent := path.Dir(remote)
- return f.mkdir(ctx, f.remotePath(parent))
+ return f.mkdir(ctx, path.Dir(f.remotePath(remote)))
}
// mkdir makes the directory and parents using native paths
diff --git a/backend/sftp/sftp_internal_test.go b/backend/sftp/sftp_internal_test.go
index e4d11735a..53453c1f4 100644
--- a/backend/sftp/sftp_internal_test.go
+++ b/backend/sftp/sftp_internal_test.go
@@ -18,6 +18,7 @@ import (
"testing"
"github.com/rclone/rclone/fs"
+ "github.com/rclone/rclone/fs/config"
"github.com/rclone/rclone/fs/config/configmap"
"github.com/rclone/rclone/fs/config/obscure"
"github.com/rclone/rclone/fstest/fstests"
@@ -163,6 +164,14 @@ func TestRemoteShellPathRelativePathOverrideLeadingDash(t *testing.T) {
assert.Equal(t, "./-file", f.remoteShellPath("-file"))
}
+func TestDefaultEncodingStandardizesControlCharacters(t *testing.T) {
+ ri, err := fs.Find("sftp")
+ require.NoError(t, err)
+ enc := ri.Options.Get(config.ConfigEncoding).Default.(encoder.MultiEncoder)
+ assert.Equal(t, "a␊b", enc.ToStandardName("a\nb"))
+ assert.Equal(t, "a\nb", enc.FromStandardName("a␊b"))
+}
+
func TestParseHash(t *testing.T) {
for i, test := range []struct {
sshOutput, checksum string
diff --git a/docs/content/sftp.md b/docs/content/sftp.md
index 9c79b6ad0..3e7e229ce 100644
--- a/docs/content/sftp.md
+++ b/docs/content/sftp.md
@@ -522,6 +522,24 @@ upload (for example, certain configurations of ProFTPd with mod_sftp). If you
are using one of these servers, you can set the option `set_modtime = false` in
your RClone backend configuration to disable this behaviour.
+### Restricted filename characters
+
+SFTP servers on Unix-like systems can store any character in a file name
+except NUL and `/`, so by default only these are replaced:
+
+| Character | Value | Replacement |
+| --------- |:-----:|:-----------:|
+| NUL | 0x00 | ␀ |
+| / | 0x2F | / |
+
+If the server runs on Windows, which can't store many more characters,
+add the Windows restricted characters with `--sftp-encoding`, as in the
+[FTP example](/overview/#encoding-example-ftp):
+
+```text
+--sftp-encoding "Slash,LtGt,DoubleQuote,Colon,Question,Asterisk,Pipe,BackSlash,Ctl,RightSpace,RightPeriod,InvalidUtf8,Dot"
+```
+
### About command
The `about` command returns the total space, free space, and used