> Unix/Linux文件权限通过使操作系统清除从程序的权限中删除的所有用户请求权限起作用.请求被批准.也就是说,在调用创建新文件的文件系统操作时,您将使用代码提供一种模式.在这种情况下,您提供的 0777 实际上是 rwxrwxrwx .但是,与此同时,用户的操作环境 1 包含另一个八进制模式,称为 umask .在这种情况下,它大概是 022 或 ---- w--w-.这些权限已从您提供的权限中删除: 0777&(^ 022)是 0755 ,它是 rwxr-xr-x .因此,即使您的程序要求输入 0777 ,新创建的文件的模式为 rwxr-xr-x 或 0755 >.由于这个原因,大多数Unix程序应该在文件创建操作期间使用两种模式之一: 0777 用于目录和可执行文件,以及 0666 用于不可执行文件.用户的umask会删除不需要的权限:需要隐私的用户可以将其umask设置为 077 ,这将同时删除组和其他用户的 rwx ,仅保留 0700 或 0600 (视情况而定).该规则的一个例外是,任何要确保额外隐私的程序(例如与 ssh 相关的代码,在创建密钥对时)都应提供其密码.模式设置为 0600 ,这样一开始就不会设置组和其他用户权限位.请注意, chmod 调用不会应用用户的umask设置.如果您需要发现用户的umask来为 chmod 调用计算正确的值,请使用新的 golang.org/sys/x/unix Umask 函数,或旧的系统调用 Umask 函数.不幸的是,您必须提供一个 new umask才能发现原始 umask.这将替换每个进程的umask,直到您重新安装原始值为止.因此,您可能希望在init函数中执行此操作,并将结果存储在变量中以进行安全维护,同时还还原原始文件: var Umask intfunc init(){Umask = unix.Umask(0777)unix.Umask(Umask)} 例如,(假设新样式;旧样式 syscall.Umask 用法相同;均不返回任何错误).不过,这使您的程序特定于Unix. 1 表示存储在内核的按进程数据中的权限,而不是存储在shell风格的环境变量中.I am writing file to disk on unix with permission 0777, but the ls -la shows it has -rwxr-xr-x which is not expected. err = ioutil.WriteFile(path, jsonByte, 0777) if err != nil { log.Print(err) }I expect -rwxrwxrwx permission. I do not understand why w permission is missing. 解决方案 First, as Burak Serdar notes in a comment, be sure that this really is a new file, as the OS will generally leave the existing permissions in place when overwriting an existing file (though the details are OS-specific, and in particular some Unix-ish OSes clear setuid/setgid here).Unix/Linux file permissions work by having the OS clear any permissions that the user requests be removed, from the permissions that the program requests be granted.That is, when calling a file system operation that creates a new file, you, in your code, supply a mode. In this case you are supplying 0777 which is indeed rwxrwxrwx.Meanwhile, however, the user's operating environment1 contains another octal mode called the umask. In this case it is presumably 022, or ----w--w-. These permissions are removed from your supplied permissions: 0777 & (^022) is 0755 which is rwxr-xr-x. So the newly created file has mode rwxr-xr-x, or 0755, even though your program asked for 0777.For this reason, most Unix programs should use one of two modes during file creation operation: 0777 for directories and executable files, and 0666 for non-executable files. The user's umask will remove unwanted permissions: users who want privacy can set their umask to 077 which will remove rwx for both group and other, leaving only 0700 or 0600 as appropriate.One exception to this rule is that any program that wants to ensure extra privacy—such as ssh-related code, when it creates key-pairs—should supply its mode as 0600 so that there are no group and other-user permission bits set in the first place.Note that chmod calls do not have the user's umask setting applied. If you need to discover the user's umask to compute the correct value for a chmod call, use either the new golang.org/sys/x/unix Umask function, or the old syscall Umask function. Unfortunately you must supply a new umask to discover the original umask. This displaces the per-process umask until you re-install the original value. Hence, you might want to do this in an init function and store the result in a variable for safekeeping while also restoring the original:var Umask intfunc init() { Umask = unix.Umask(0777) unix.Umask(Umask)}for instance (assuming the new-style; the old style syscall.Umask usage is identical; neither returns any error). This makes your program Unix-specific, though.1This means permissions stored in the kernel's per-process data, not shell-style environment variables. 这篇关于写入权限为0777的文件失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-15 02:53