From d3dd408c02c6e578289bb43a3fafc77023d4dbd1 Mon Sep 17 00:00:00 2001 From: lazywhite <346816483@qq.com> Date: Tue, 6 Jun 2023 10:06:16 +0800 Subject: [PATCH] feat: add 'mkdir -p' support --- ftp.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/ftp.go b/ftp.go index 9892882..1f81ba0 100644 --- a/ftp.go +++ b/ftp.go @@ -1054,6 +1054,41 @@ func (c *ServerConn) MakeDir(path string) error { return err } +// MakeDirRecur create a folder recursively using +// MakeDir and ChangeDir +func (c *ServerConn) MakeDirRecur(folder string) error { + // save current dir path + currentDir, err := c.CurrentDir() + if err != nil { + return err + } + dirs := strings.Split(folder, "/") + for _, dir := range dirs { + if dir == "" { + continue + } + // check if dir exists + err := c.ChangeDir(dir) + if err == nil { + continue + } + err = c.MakeDir(dir) + if err != nil { + return err + } + err = c.ChangeDir(dir) + if err != nil { + return err + } + } + // move to origin dir + err = c.ChangeDir(currentDir) + if err != nil { + return err + } + return nil +} + // RemoveDir issues a RMD FTP command to remove the specified directory from // the remote FTP server. func (c *ServerConn) RemoveDir(path string) error {