Peter Brennan
Peter Brennan

Reputation: 1376

A folder created with os.Mkdir() has incorrect permissions

I am creating a folder in go using os.Mkdir(). While it does get created, it does not possess the permissions I expected it to.

Here is the code I used to create the directory:

package main

import (
    "fmt"
    "os"
)

func main() {
    err := os.Mkdir("/var/run/testdir", 0777)
    if err != nil {
        fmt.Println("could not create dir: %s", err.Error())
        err = nil
    }
}

As I have given "0777" as parameter, I am excpecting the created dir to have full permissions for everybody. It however has:

drwxr-xr-x  2 root       root         40 Apr 27 11:43 testdir/

What am I getting wrong here?

Upvotes: 2

Views: 971

Answers (1)

icza
icza

Reputation: 417797

The actual permission that the created folder will get is the result of the permission you specify (0777) and the active umask of your process (the running Go program).

This is most likely why the created folder lacks write permission for group and other access.

You can read more about umask on Wikipedia.

Upvotes: 4

Related Questions