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

os.Mkdir and os.MkdirAll set wrong permission bits #15210

Closed
snamber opened this issue Apr 9, 2016 · 6 comments
Closed

os.Mkdir and os.MkdirAll set wrong permission bits #15210

snamber opened this issue Apr 9, 2016 · 6 comments

Comments

@snamber
Copy link

snamber commented Apr 9, 2016

Please answer these questions before submitting your issue. Thanks!

  1. What version of Go are you using (go version)?
    go version go1.6 linux/amd64
  2. What operating system and processor architecture are you using (go env)?
    GOARCH="amd64"
    GOBIN=""
    GOEXE=""
    GOHOSTARCH="amd64"
    GOHOSTOS="linux"
    GOOS="linux"
    GOPATH="/home/user/golang"
    GORACE=""
    GOROOT="/usr/local/go"
    GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
    GO15VENDOREXPERIMENT="1"
    CC="gcc"
    GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
    CXX="g++"
    CGO_ENABLED="1"
  3. What did you do?

I created a directory using os.Mkdir("foo", 0007) and got a folder with permissions ------r-x instead of ------rwx.

This behavior could not be repeated on play.golang.org.
However this program can be used to recreate similar behavior:

package main

import (
    "fmt"
    "os"
)

func main() {
    fmt.Println(os.Mkdir("foo", 0007))
    fmt.Println(os.Mkdir("bar", 0007))
    os.Chmod("bar", 0007)

    infoFoo, _ := os.Stat("foo")
    infoBar, _ := os.Stat("bar")

    fmt.Println(infoFoo.Mode())
    fmt.Println(infoBar.Mode())
}
  1. What did you expect to see?

I expected the folders foo and bar to both have permissions ------rwx

  1. What did you see instead?

Folder foo was created with permissions ------r-x.
os.Chmod was setting the permissions correctly, so bar ends up with permissions ------rwx

This is the output of the program above:

<nil>
<nil>
d------r-x
d------rwx
@cmarcelo
Copy link
Contributor

cmarcelo commented Apr 9, 2016

@sn-amber it seems umask is being applied when creating the file. It filter the permissions when creating a file.

You can check the umask value by running umask in a terminal and also change it before running your program to verify, e.g. calling umask 0 before running your test should make Mkdir work as you expect.

@cmarcelo
Copy link
Contributor

cmarcelo commented Apr 9, 2016

You can also set the umask for the process using syscall.Umask.

@ianlancetaylor
Copy link
Contributor

Closing, because umask is a standard Unix feature that affects programs in all languages.

@arvenil
Copy link

arvenil commented Feb 17, 2017

What would you think about adding this information to documentation? That permissions might be affected by Umask and that you can set it with syscall.Umask?

@ianlancetaylor
Copy link
Contributor

I don't think the Go documentation is the place to discuss details of how the Unix operating system works. If we go down that path we'll wind up documenting things like how the setgid bit of the directory affects file ownership, and the relationship between the effective user ID and file ownership, and I just don't think we should go there. There are many text books and web pages that explain how Unix works.

That said, I could imagine documenting some of these characteristics in a Unix-specific section of the package docs, if somebody wants to work on that. I don't know what others would think of that, though.

@ianlancetaylor
Copy link
Contributor

I should add that this is a discussion to have on the golang-dev mailing list, not on this closed issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants