Skip to content

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

Closed

Description

@ctfrancia

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

Activity

  1. puellanivis commented on Dec 20, 2024

    @puellanivis
    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
    
  2. added a commit that references this issue on Dec 30, 2024
  3. puellanivis commented on Dec 30, 2024

    @puellanivis
    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.

  4. added a commit that references this issue on Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions