doucongqian6644
doucongqian6644
2013-01-10 01:33
浏览 91
已采纳

os.MkDir和os.MkDirAll权限值?

I'm trying to create a log file at the start of my program.

I need to check if a "/log" directory exists if it doesn't create the directory then move on to creating the log file.

Well I tried to use os.Mkdir (as well as os.MkDirAll), but no matter what value I put into the second parameter I get a locked out folder with no permissions. What value should this be in order to get a read / write for user folder? I thought it would be 0x700 but it doesn't seem to work.

Thanks!

图片转代码服务由CSDN问答提供 功能建议

我正在尝试在程序开始时创建日志文件。

如果没有创建目录,则需要检查是否存在“ / log”目录,然后继续创建日志文件。

我尝试使用 os.Mkdir (以及 os.MkDirAll ),但是无论我在第二个参数中输入什么值,都会得到一个没有权限的锁定文件夹。 为了对用户文件夹进行读/写,这应该是什么值? 我以为会是0x700,但似乎不起作用。

谢谢!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doucuo4413
    doucuo4413 2015-07-01 02:11
    已采纳

    You can use octal notation directly:

    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 Commonly used on web servers. The owner can read, write, execute. Everyone else can read and execute but not modify the file.

    0777 Everyone can read write and execute. On a web server, it is not advisable to use ‘777’ permission for your files and folders, as it allows anyone to add malicious code to your server.

    0644 Only the owner can read and write. Everyone else can only read. No one can execute the file.

    0655 Only the owner can read and write, but not execute the file. Everyone else can read and execute, but cannot modify the file.

    www.maketecheasier.com/file-permissions-what-does-chmod-777-means/


    Directory Permissions on Linux

    When applying permissions to directories on Linux, the permission bits have different meanings than on regular files. (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

    A handy permissions calculator.

    点赞 评论
  • dongyu9263
    dongyu9263 2013-02-17 13:51

    @Daniel's statement in his answer is not really correct, and also it talks about a decimal number and then uses an octal one, as @SashaCrofter correctly pointed out in his comment.

    In reality, it doesn't matter what form your permission value is in as long as it represents sensible Unix permissions.

    Since permission bits on POSIX file systems come in triples of bits — three bits for owner, group and others access, plus three bits of modifiers (such as sticky bits), — it's customary to use octal numbers to represent permissions as each digit in an octal number represents a three-bit value.

    Hence, when you use 0700 in Go code, the leading 0 is stripped and is only there to tell the parser it sees an octal number literal, and the following three letters stand for the owner, group and others permissions, in this order. Should you, say, want to also set the group sticky bit as well as making the file system object group-readable and executable, you'd specify 02750 and so on.

    Note that the actual permissions the file system object acquires is further modulated by the active umask of the process which creates the object.

    To get more grip on these topics, it's best to read the chmod manual pages and general literature on Unix-like operating systems.

    点赞 评论

相关推荐