我试图在程序开始时创建一个日志(log)文件.
我需要判断/log
目录是否存在,如果它没有创建该目录,然后继续创建日志(log)文件.
我try 使用os.Mkdir
(以及os.MkdirAll
),但无论我在第二个参数中输入什么值,我都会得到一个没有权限的锁定文件夹.为了获得用户文件夹的读/写操作,这个值应该是多少?我以为是0x700
,但似乎不起作用.
谢谢
我试图在程序开始时创建一个日志(log)文件.
我需要判断/log
目录是否存在,如果它没有创建该目录,然后继续创建日志(log)文件.
我try 使用os.Mkdir
(以及os.MkdirAll
),但无论我在第二个参数中输入什么值,我都会得到一个没有权限的锁定文件夹.为了获得用户文件夹的读/写操作,这个值应该是多少?我以为是0x700
,但似乎不起作用.
谢谢
您可以直接使用八进制表示法:
os.Mkdir("dirname", 0700)
Permission Bits
+-----+---+--------------------------+
| rwx | 7 | Read, write and execute |
| rw- | 6 | Read, write |
| r-x | 5 | Read, and execute |
| r-- | 4 | Read, |
| -wx | 3 | Write and execute |
| -w- | 2 | Write |
| --x | 1 | Execute |
| --- | 0 | no permissions |
+------------------------------------+
+------------+------+-------+
| Permission | Octal| Field |
+------------+------+-------+
| rwx------ | 0700 | User |
| ---rwx--- | 0070 | Group |
| ------rwx | 0007 | Other |
+------------+------+-------+
A Unix Permission Primer
Common Permission Usages
0755个常用于web服务器.所有者可以读、写、执行.其他人可以读取和执行文件,但不能修改文件.
每个人都能读写和执行.在web服务器上,不建议对文件和文件夹使用"777"权限,因为它允许任何人向服务器添加恶意代码.
0644只有所有者才能读写.其他人只能阅读.没有人能执行这个文件.
0655只有所有者可以读写,但不能执行文件.其他所有人都可以读取和执行,但不能修改文件.
www.maketecheasier.com/file-permissions-what-does-chmod-777-means/
Directory Permissions on Linux
在Linux上对目录应用权限时,权限位的含义与普通文件不同.(source)
Read bit The user can read the file names contained in the directory.
Write bit The user can {add,rename,delete} files names IF the execute bit is set too.
Execute bit The user can enter the directory and access the files inside.
https://unix.stackexchange.com/a/21252
Permissions Calculator
一个方便的permissions calculator.