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

client.MkdirAll not returning an error when docs state it should #609

Closed
ctfrancia opened this issue Dec 20, 2024 · 2 comments · Fixed by #611
Closed

client.MkdirAll not returning an error when docs state it should #609

ctfrancia opened this issue Dec 20, 2024 · 2 comments · Fixed by #611

Comments

@ctfrancia
Copy link

ctfrancia commented Dec 20, 2024

Hi, I am just making a project learning about sftp and transferring data, anyways, I am making a directory, creating a file within it, and then extending that file directory by one. According to the docs of MkdirAll it says:

MkdirAll creates a directory named path, along with any necessary parents, and returns nil, or else returns an error. If path is already a directory, MkdirAll does nothing and returns nil. If path contains a regular file, an error is returned

however nil is still received back in the code below

	// connect to ssh server
	conn, err := ssh.Dial("tcp", sshAddr, config)
	if err != nil {
		log.Fatal("Failed to dial: ", err)
	}

	defer conn.Close()

	// open an SFTP session over an existing ssh connection.
	client, err := sftp.NewClient(conn)
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	var user = "USER"
	var sftpBasePath = fmt.Sprintf("home/%s", user)
	err = client.MkdirAll(sftpBasePath)
	if err != nil {
		log.Fatal(err)
	}

	// leave your mark
	f, err := client.Create(fmt.Sprintf("%s/hello.txt", sftpBasePath))
	if err != nil {
		log.Fatal(err)
	}

	if _, err := f.Write([]byte("Hello world!")); err != nil {
		log.Fatal(err)
	}

	// error should be returned below as there is a ".txt"
        // file in the path before creating the `dir` directory
	err = client.MkdirAll(sftpBasePath + "/dir")
	if err != nil {
		log.Fatal(err)
	}

	f.Close()

I should also mention that the ssh server when I inspect the documents the file is created at that path and the dir directory

@puellanivis
Copy link
Collaborator

The order of commands that you give should not yield any errors. It simply creates a dir next to hello.txt:

localhost tmp$ rm -Rf home
localhost tmp$ mkdir -p home/puellanivis # MkdirAll(sftpBasePath)
localhost tmp$ echo "Hello world!" > home/puellanivis/hello.txt # Create(sftpBasePath + "/hello.txt") ; Write(Hello World)
localhost tmp$ mkdir -p home/puellanivis/dir # MkdirAll(sftpBasePath + "/dir")
localhost tmp$ tree home
home
└── puellanivis
    ├── dir
    └── hello.txt

2 directories, 1 file

@puellanivis
Copy link
Collaborator

I figured out what was meant in the raising of this issue, and looking at it, the language does indeed appear to be ambiguous. If you could check the PR #611 and confirm or not whether it is more clear, then we can resolve this issue.

puellanivis added a commit that referenced this issue Jan 3, 2025
…dition

Issue #609 - clarify what 'path contains a regular file' actually means
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants