简体   繁体   中英

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). 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. 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

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.

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

The 0o640 is an example, not the expected default value -- and it's an example file_perm value, not an example umask value.

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.

The bits of the umask are those that are turned off when creating a file or directory. 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 . 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-- ).

0o640 is not a sensible umask value.

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).

You need execute permission to cd to a directory. Don't deny yourself execute permission (the octal 100 bit).

(I have a feeling you're misunderstanding the purpose of umask, but that is a separate question.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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