I'm trying to create directories with certain permissions using os.Mkdir but I cannot make it work, for some reason.
My test program is:
package main
import (
"log"
"os"
)
func main() {
err := os.Mkdir("testdir", 0775)
if err != nil {
log.Print(err)
}
}
However, the created directory has the default 0755 permissions:
drwxr-xr-x 2 user user 4096 Jan 10 10:14 testdir
A chmod from the shell works just fine, so I'm not sure why the Go program is not working.
When creating a file, Unix-like system use a permission mask (umask) to create the default permissions.
With a umask
value of 0022
, new directories will be created with permissions 0755
at most. New files will have permissions 0644
at most.
If you want to create a new directory with permissions 0775
, then you have to set your umask value to 0002
.
An other way of working this around is to modify the permissions after creating the file : Create it with default permissions with os.Mkdir
, then modify those permissions with os.Chmod
.