使用os.Mkdir()创建的文件夹权限不正确。

huangapple go评论88阅读模式
英文:

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

问题

我正在使用os.Mkdir()在Go语言中创建一个文件夹。虽然它确实被创建了,但它没有我期望的权限。

以下是我用来创建目录的代码:

package main

import (
	"fmt"
	"os"
)

func main() {
	err := os.Mkdir("/var/run/testdir", 0777)
	if err != nil {
		fmt.Println("无法创建目录:%s", err.Error())
		err = nil
	}
}

由于我将"0777"作为参数,我期望创建的目录对所有人都具有完全权限。然而,它实际上具有以下权限:

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

我在这里做错了什么?

英文:

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?

答案1

得分: 4

创建的文件夹实际上获得的权限是由您指定的权限(0777)和您的进程(运行中的Go程序)的活动umask的结果。

这很可能是为什么创建的文件夹缺少组和其他访问的写权限。

您可以在Wikipedia上阅读有关umask的更多信息。

英文:

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.

huangapple
  • 本文由 发表于 2015年4月27日 19:50:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/29894924.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定