From bee321c4e854cc4e10214ccb2f5b471d81d50fd6 Mon Sep 17 00:00:00 2001 From: yuxh Date: Wed, 9 Oct 2024 17:05:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=8E=A8=E9=80=81=E8=BF=9C?= =?UTF-8?q?=E7=AB=AF=E6=97=B6=E4=BC=9A=E5=9C=A8=E6=9C=AC=E5=9C=B0=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E6=96=87=E4=BB=B6=E5=A4=B9=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + rsync.go | 21 +++++++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 173454b..f9dda37 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ *.iml out gen +*_test.go \ No newline at end of file diff --git a/rsync.go b/rsync.go index 825f00c..8e2ebef 100644 --- a/rsync.go +++ b/rsync.go @@ -21,17 +21,17 @@ type Rsync struct { type RsyncOptions struct { // Verbose increase verbosity Verbose bool - // Quet suppress non-error messages + // Quiet suppress non-error messages Quiet bool // Checksum skip based on checksum, not mod-time & size Checksum bool - // Archve is archive mode; equals -rlptgoD (no -H,-A,-X) + // Archive is archive mode; equals -rlptgoD (no -H,-A,-X) Archive bool // Recurse into directories Recursive bool // Relative option to use relative path names Relative bool - // NoImliedDirs don't send implied dirs with --relative + // NoImpliedDirs don't send implied dirs with --relative NoImpliedDirs bool // Update skip files that are newer on the receiver Update bool @@ -196,7 +196,8 @@ func (r Rsync) StderrPipe() (io.ReadCloser, error) { // Run start rsync task func (r Rsync) Run() error { - if !isExist(r.Destination) { + // 目标地址为 远程地址 则不进行文件夹创建 + if !isRsyncPath(r.Destination) && !isExist(r.Destination) { if err := createDir(r.Destination); err != nil { return err } @@ -552,3 +553,15 @@ func isExist(p string) bool { stat, err := os.Stat(p) return os.IsExist(err) && stat.IsDir() } + +// isRsyncPath 判断一个给定的字符串或路径是否格式是否符合rsync协议的要求 +func isRsyncPath(path string) bool { + if strings.HasPrefix(path, "rsync://") { + return true + } + parts := strings.SplitN(path, ":", 2) + if len(parts) == 2 && strings.Contains(parts[0], "@") { + return true + } + return false +}