dtmwnqng38644 2017-01-10 09:18
浏览 380
已采纳

设置os.Mkdir权限

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条回答 默认 最新

  • duandao6414 2017-01-10 09:35
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog