Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug Fix: Fixed error when sending files to a target directory that didn't exist #836

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
25 changes: 24 additions & 1 deletion src/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,13 @@ Or you can go back to the classic croc behavior by enabling classic mode:
}
}
if c.String("out") != "" {
if err = os.Chdir(c.String("out")); err != nil {
// validate the path
destPath, err := validateDestPath(c.String("out"))
if err != nil {
return err
}

if err = os.Chdir(destPath); err != nil {
return err
}
}
Expand Down Expand Up @@ -714,3 +720,20 @@ func relay(c *cli.Context) (err error) {
}
return tcp.Run(debugString, host, ports[0], determinePass(c), tcpPorts)
}

func validateDestPath(path string) (string, error) {
_, err := os.Stat(path)
if err != nil {
// Path doesn't exist, fallback to current directory
currentDir, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("could not get current directory: %w", err)
}
log.Debugf("Path %s does not exist, falling back to current directory: %s", path, currentDir)
fmt.Println("Target directory doesn't exist, receiving in current directory")
// flush the stdout buffer
os.Stdout.Sync()
return currentDir, nil
}
return path, nil
}