diff --git a/pkg/runner/ftp.go b/pkg/runner/ftp.go index 2dfc9beb..4e8ffabe 100644 --- a/pkg/runner/ftp.go +++ b/pkg/runner/ftp.go @@ -175,6 +175,12 @@ func (fc *FtpClient) list(rootDir string, depth int) (CommandResult, error) { } func (fc *FtpClient) listRecursive(path string, depth, current int) (CommandResult, error) { + if depth > 3 { + return CommandResult{ + Message: ErrTooLargeDepth, + }, fmt.Errorf("%s", ErrTooLargeDepth) + } + result := CommandResult{ Name: filepath.Base(path), Type: "folder", @@ -186,17 +192,32 @@ func (fc *FtpClient) listRecursive(path string, depth, current int) (CommandResu entries, err := os.ReadDir(path) if err != nil { - return CommandResult{ + errResult := CommandResult{ Name: filepath.Base(path), Path: path, Message: err.Error(), - }, nil + } + _, errResult.Code = GetFtpErrorCode(List, errResult) + + return errResult, nil } for _, entry := range entries { fullPath := filepath.Join(path, entry.Name()) - info, err := entry.Info() + info, err := os.Lstat(fullPath) if err != nil { + errChild := CommandResult{ + Name: entry.Name(), + Path: fullPath, + Message: err.Error(), + } + _, errChild.Code = GetFtpErrorCode(List, errChild) + result.Children = append(result.Children, errChild) + + continue + } + + if info.Mode()&os.ModeSymlink != 0 { continue } @@ -204,6 +225,7 @@ func (fc *FtpClient) listRecursive(path string, depth, current int) (CommandResu child := CommandResult{ Name: entry.Name(), Path: fullPath, + Code: returnCodes[List].Success, Size: info.Size(), ModTime: &modTime, } @@ -213,13 +235,14 @@ func (fc *FtpClient) listRecursive(path string, depth, current int) (CommandResu if current < depth-1 { childResult, err := fc.listRecursive(fullPath, depth, current+1) if err != nil { + result.Children = append(result.Children, childResult) continue } - child.Children = childResult.Children - child.Size = childResult.Size + child = childResult } } else { child.Type = "file" + child.Code = returnCodes[List].Success } result.Children = append(result.Children, child) @@ -229,9 +252,11 @@ func (fc *FtpClient) listRecursive(path string, depth, current int) (CommandResu dirInfo, err := os.Stat(path) if err != nil { result.Message = err.Error() + _, result.Code = GetFtpErrorCode(List, result) } else { modTime := dirInfo.ModTime() result.ModTime = &modTime + result.Code = returnCodes[List].Success } return result, nil @@ -336,6 +361,7 @@ func (fc *FtpClient) mv(src, dst string) (CommandResult, error) { } return CommandResult{ + Dst: dst, Message: fmt.Sprintf("Move %s to %s", src, dst), }, nil } @@ -366,6 +392,7 @@ func (fc *FtpClient) cpDir(src, dst string) (CommandResult, error) { } return CommandResult{ + Dst: dst, Message: fmt.Sprintf("Copy %s to %s", src, dst), }, nil } @@ -379,6 +406,7 @@ func (fc *FtpClient) cpFile(src, dst string) (CommandResult, error) { } return CommandResult{ + Dst: dst, Message: fmt.Sprintf("Copy %s to %s", src, dst), }, nil } diff --git a/pkg/runner/ftp_types.go b/pkg/runner/ftp_types.go index ad2986f6..503c9e09 100644 --- a/pkg/runner/ftp_types.go +++ b/pkg/runner/ftp_types.go @@ -22,6 +22,8 @@ const ( const ( ErrPermissionDenied = "permission denied" + ErrOperationNotPermitted = "operation not permitted" + ErrTooLargeDepth = "depth has reached its limit. please try a lower depth" ErrInvalidArgument = "invalid argument" ErrNoSuchFileOrDirectory = "no such file or directory" ErrFileExists = "file exists" @@ -59,6 +61,8 @@ type CommandResult struct { Name string `json:"name,omitempty"` Type string `json:"type,omitempty"` Path string `json:"path,omitempty"` + Dst string `json:"dst,omitempty"` + Code int `json:"code,omitempty"` Size int64 `json:"size,omitempty"` Children []CommandResult `json:"children,omitempty"` ModTime *time.Time `json:"mod_time,omitempty"` @@ -73,12 +77,18 @@ type returnCode struct { var returnCodes = map[FtpCommand]returnCode{ List: { Success: 250, - Error: map[string]int{}, + Error: map[string]int{ + ErrPermissionDenied: 450, + ErrOperationNotPermitted: 450, + ErrTooLargeDepth: 452, + ErrNoSuchFileOrDirectory: 550, + }, }, Mkd: { Success: 250, Error: map[string]int{ ErrPermissionDenied: 450, + ErrOperationNotPermitted: 450, ErrInvalidArgument: 452, ErrNoSuchFileOrDirectory: 550, ErrFileExists: 552, @@ -88,6 +98,7 @@ var returnCodes = map[FtpCommand]returnCode{ Success: 250, Error: map[string]int{ ErrPermissionDenied: 450, + ErrOperationNotPermitted: 450, ErrNoSuchFileOrDirectory: 550, }, }, @@ -95,6 +106,7 @@ var returnCodes = map[FtpCommand]returnCode{ Success: 250, Error: map[string]int{ ErrPermissionDenied: 450, + ErrOperationNotPermitted: 450, ErrNoSuchFileOrDirectory: 550, }, }, @@ -102,6 +114,7 @@ var returnCodes = map[FtpCommand]returnCode{ Success: 250, Error: map[string]int{ ErrPermissionDenied: 450, + ErrOperationNotPermitted: 450, ErrInvalidArgument: 452, ErrNoSuchFileOrDirectory: 550, }, @@ -110,6 +123,7 @@ var returnCodes = map[FtpCommand]returnCode{ Success: 250, Error: map[string]int{ ErrPermissionDenied: 450, + ErrOperationNotPermitted: 450, ErrInvalidArgument: 452, ErrNoSuchFileOrDirectory: 550, ErrDirectoryNotEmpty: 552, @@ -119,6 +133,7 @@ var returnCodes = map[FtpCommand]returnCode{ Success: 250, Error: map[string]int{ ErrPermissionDenied: 450, + ErrOperationNotPermitted: 450, ErrInvalidArgument: 452, ErrNoSuchFileOrDirectory: 550, ErrFileExists: 552, @@ -128,6 +143,7 @@ var returnCodes = map[FtpCommand]returnCode{ Success: 250, Error: map[string]int{ ErrPermissionDenied: 450, + ErrOperationNotPermitted: 450, ErrInvalidArgument: 452, ErrNoSuchFileOrDirectory: 550, ErrFileExists: 552,