Setting os.Mkdir permissions

女生的网名这么多〃 提交于 2021-01-04 06:24:52

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/41565192/setting-os-mkdir-permissions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!