简体   繁体   English

OCaml,Unix.umask的默认值

[英]OCaml, default value for Unix.umask

Consider this code : 考虑以下代码:

#load "unix.cma" ;;
print_int (Unix.umask 0) ;;
print_newline () ;;

When I run it, I get 2 (binary : 000.000.010). 当我运行它时,我得到2(二进制:000.000.010)。 When I run it with 'sudo', I get 18 (binary : 000.010.010) I was expected to get something like 0o640 (binary : 110.010.000), as the standard library says : http://caml.inria.fr/pub/docs/manual-ocaml/libref/Unix.html#TYPEfile_perm My purpose is to make a directory. 当我使用'sudo'运行它时,我得到18(二进制:000.010.010),我希望得到类似0o640(二进制:110.010.000)的内容,因为标准库说: http ://caml.inria.fr /pub/docs/manual-ocaml/libref/Unix.html#TYPEfile_perm我的目的是创建目录。 If I make it with 如果我做到了

(Unix.umask 0) lor (0o640)

it is created but inaccessible. 它已创建但无法访问。 An accurate look at the binary numbers gives me the idea that the default mask could be reverted. 准确查看二进制数可以使我想到可以还原默认掩码。 So, I make a directory using this : 因此,我使用以下命令创建目录:

let revert_mask m =
  let user  = (m land 0b000000111) in
  let group = (m land 0b000111000) lsr 3 in
  let other = (m land 0b111000000) lsr 6 in
  (user lsl 6) lor (group lsl 3) lor other
;;

Then, I create my directory : 然后,创建目录:

let mask = (revert_mask (Unix.umask 0)) lor 0o640 ;;
print_int mask ;;
print_newline () ;;
Unix.mkdir "foo" mask ;;

I get 416 (0o640), which corresponds to my 我得到416(0o640),对应于我的

ls -l | grep foo

:

drw-r----- 2 (me) (me) 4096 june   2 19:23 foo

However, a 但是,

cd foo

won't work. 将无法正常工作。

So, I'm stuck with ubuntu 14.04 and ocaml 4.01.0 toplevel. 因此,我坚持使用ubuntu 14.04和ocaml 4.01.0顶层。

The documentation you linked to says: 您链接到的文档说:

type file_perm = int
The type of file access rights, eg 0o640 is read and write for user, read for group, none for others 文件访问权限的类型,例如0o640是为用户读写的,为组是读取的,其他用户为无

The 0o640 is an example, not the expected default value -- and it's an example file_perm value, not an example umask value. 0o640是一个示例,而不是预期的默认值;它是示例file_perm值,而不是示例umask值。

I'm not familiar with OCaml, but in UNIX in general the umask is an attribute of each process, typically set to a default value but modifiable by calling the umask system call. 我不熟悉OCaml,但是在UNIX中,通常,umask是每个进程的属性,通常设置为默认值,但可以通过调用umask系统调用进行修改。

The bits of the umask are those that are turned off when creating a file or directory. umask的位是在创建文件或目录时关闭的位。 For example, my current umask is 0022 (or, in OCaml syntax, 0o022 ) -- which means that when I create a file or directory, the corresponding bits (write access for group and others) are turned off . 例如,我当前的umask是0022 (或者用OCaml语法是0o022 ),这意味着当我创建文件或目录时,相应的位(对group和others的写访问)被关闭 Directories need execute permission, and files generally don't, so if I create a directory its default permissions will be 755 ( rwxr-xr-x ), and if I create a file its default permissions will be 644 ( rw-r--r-- ). 目录需要执行权限,而文件通常不需要执行权限,因此,如果我创建目录,则其默认权限将为755rwxr-xr-x ),如果我创建文件,则其默认权限将为644rw-r--r-- )。

0o640 is not a sensible umask value. 0o640不是明智的umask值。

To create a directory, just do this: 要创建目录,只需执行以下操作:

Unix.mkdir "/some/dir" 0o777

The operating system will automatically apply the current umask (which is why the documentation refers you to it - you don't actually need to call umask yourself). 操作系统将自动应用当前的umask(这就是文档向您推荐它的原因-您实际上不需要自己调用umask )。

You need execute permission to cd to a directory. 您需要执行权限才能将cd转到目录。 Don't deny yourself execute permission (the octal 100 bit). 不要拒绝自己的执行权限(八进制100位)。

(I have a feeling you're misunderstanding the purpose of umask, but that is a separate question.) (我觉得您误解了umask的目的,但这是一个单独的问题。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM