Skip to content

MkdirAll() returns unhelpful error message if the parent directory is / #1637

@msolo

Description

@msolo
Before filing a bug, please check whether it has been fixed since
the latest release: run "hg pull -u" and retry what you did to
reproduce the problem.  Thanks.

What steps will reproduce the problem?
1. os.MkdirAll("/nonexistant", 0755)
2.
3.

What is the expected output?
[mike]voltron:~> ./mkdir_test 
mkdir /zk: permission denied

What do you see instead?
[mike]voltron:~> ./mkdir_test 
mkdir : no such file or directory

Which operating system are you using?
Linux.


Here's the fix:


diff -r 9add7892ac1d src/pkg/os/path.go
--- a/src/pkg/os/path.go    Tue Mar 22 11:52:41 2011 -0700
+++ b/src/pkg/os/path.go    Wed Mar 23 14:34:09 2011 -0700
@@ -33,7 +33,7 @@
        j--
    }
 
-   if j > 0 {
+   if j > 1 {
        // Create parent
        err = MkdirAll(path[0:j-1], perm)
        if err != nil {
diff -r 9add7892ac1d src/pkg/os/path_test.go
--- a/src/pkg/os/path_test.go   Tue Mar 22 11:52:41 2011 -0700
+++ b/src/pkg/os/path_test.go   Wed Mar 23 14:34:09 2011 -0700
@@ -179,3 +179,22 @@
        t.Errorf("MkdirAll %q: %s", path, err)
    }
 }
+
+func TestMkdirAllAtSlash(t *testing.T) {
+   if runtime.GOOS == "windows" {
+       t.Log("Skipping test: does windows have /?")
+       return
+   }
+
+   err := MkdirAll("/_test/dir", 0755)
+   if err != nil {
+       pathErr, ok := err.(*PathError)
+       // common for users not to be able to write to /
+       // previously reported erroneously as ENOENT
+       if ok && pathErr.Error == EACCES {
+           return
+       }
+       t.Fatalf(`MkdirAll "/_test/dir": %v`, err)
+   }
+   defer RemoveAll("/_test/dir")
+}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions