英文:
Why are the file permissions that are set using Golang different than expected?
问题
func stringToInt(s string) int {
i, err := strconv.Atoi(s)
check(err)
return i
}
os.FileMode(stringToInt("0777"))
当不需要将字符串转换为整数时(前导零不会被移除),可以直接设置权限:
os.FileMode(0777)
文件权限是正确的。
当前结果
777
777
-r----x--x
753
753
--wxrw---x
500
500
-rwxrw-r--
期望结果
777
-rwxrwxrwx
753
-r-xr-x-wx
500
-r-x------
英文:
func stringToInt(s string) int {
i, err := strconv.Atoi(s)
check(err)
return i
}
os.FileMode(stringToInt("0777"))
when no conversion to int is required (leading zero is not removed) by setting the permission directly:
os.FileMode(0777)
the file permissions are correct
Current outcome
777
777
-r----x--x
753
753
--wxrw---x
500
500
-rwxrw-r--
Expected outcome
777
-rwxrwxrwx
753
-r-xr-x-wx
500
-r-x------
答案1
得分: 1
根据@AdamSmith和@JamesHenstridge的评论,以下解决了这个问题:
mode, _ := strconv.ParseInt(fileAttributes["mode"], 8, 32)
然而,当前导的零被移除后,结果完全相同:
777
511
-rwxrwxrwx
753
491
-r-xr-x-wx
500
320
-r-x------
总之,前导的零根本不是必需的。导致问题的是错误的类型。
英文:
Based on the comments of @AdamSmith and @JamesHenstridge the following solves the issue:
mode, _ := strconv.ParseInt(fileAttributes["mode"], 8, 32)
0777
511
-rwxrwxrwx
0753
491
-r-xr-x-wx
0500
320
-r-x------
However when the leading zeros have been removed the outcome is exactly the same:
777
511
-rwxrwxrwx
753
491
-r-xr-x-wx
500
320
-r-x------
In conclusion, the leading zeros are not required at all. It was the incorrect type that was causing the issue.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论