fix: 文件复制时判断目录是否存在

This commit is contained in:
TsMask
2024-04-16 19:42:14 +08:00
parent 438ba4a06b
commit b050643714

View File

@@ -422,6 +422,10 @@ func (s *SSHClientSFTP) CopyFileRemoteToLocal(remotePath, localPath string) erro
}
defer remoteFile.Close()
if err := os.MkdirAll(filepath.Dir(localPath), 0750); err != nil {
return err
}
// 如果目标文件已经存在,先将目标文件重命名
if info, err := os.Stat(localPath); err == nil && !info.IsDir() {
ext := filepath.Ext(localPath)
@@ -460,6 +464,12 @@ func (s *SSHClientSFTP) CopyFileLocalToRemote(localPath, remotePath string) erro
}
defer localFile.Close()
// 创建远程目录
if err := s.Client.MkdirAll(filepath.Dir(remotePath)); err != nil {
logger.Errorf("CopyFileLocalToRemote failed to creating remote directory %s: => %s", remotePath, err.Error())
return err
}
// 创建远程文件
remoteFile, err := s.Client.Create(remotePath)
if err != nil {