Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions pkg/runner/ftp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -186,24 +192,40 @@ 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
}

modTime := info.ModTime()
child := CommandResult{
Name: entry.Name(),
Path: fullPath,
Code: returnCodes[List].Success,
Size: info.Size(),
ModTime: &modTime,
}
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
18 changes: 17 additions & 1 deletion pkg/runner/ftp_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"`
Expand All @@ -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,
Expand All @@ -88,20 +98,23 @@ var returnCodes = map[FtpCommand]returnCode{
Success: 250,
Error: map[string]int{
ErrPermissionDenied: 450,
ErrOperationNotPermitted: 450,
ErrNoSuchFileOrDirectory: 550,
},
},
Pwd: {
Success: 250,
Error: map[string]int{
ErrPermissionDenied: 450,
ErrOperationNotPermitted: 450,
ErrNoSuchFileOrDirectory: 550,
},
},
Dele: {
Success: 250,
Error: map[string]int{
ErrPermissionDenied: 450,
ErrOperationNotPermitted: 450,
ErrInvalidArgument: 452,
ErrNoSuchFileOrDirectory: 550,
},
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down