-1

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.

Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
  • When using `Unix.umask` for the first time, you should get the same value as the one you get from the command line (on bash) by running the shell builtin `umask`. It should be the two-complement of the access rights set for any created file on your behalf. – didierc Jun 02 '14 at 20:20
  • Besides, to access a directory, the execute bit must be set on the directory rights. – didierc Jun 02 '14 at 20:23

3 Answers3

2

The documentation you linked to says:

type file_perm = int
The type of file access rights, e.g. 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.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • The documentation reads : "mkdir ... file_perm...see Unix.umask". I thought that it would be the same for files and directories. However, the permissions for mkdir are those whose bits are turned on (I checked). So umask has no reason to be quoted in mkdir, has it ? – sarpifeste Jun 02 '14 at 18:03
  • @sarpifeste: Assuming `Unix.mkdir` is intended to act like the UNIX `mkdir` system call, yes, `umask` is relevant. The `mode` or `file_perm` argument to `mkdir` is modified by the current `umask`. It may be that the OCaml documentation assumes familiarity with the corresponding UNIX system and library calls. http://man7.org/linux/man-pages/man2/mkdir.2.html – Keith Thompson Jun 02 '14 at 18:06
  • So there is no need in changing umask if I want to make a directory, and I don't have to mix file perm with umask. – sarpifeste Jun 02 '14 at 18:14
  • @sarpifeste: Not in general, no. Your umask is generally either set in a shell startup script (like `$HOME/.bashrc`) or left with whatever default your system provides. You get a different umask when running under `sudo` because `root` uses a different umask (`002` vs. `022`). – Keith Thompson Jun 02 '14 at 18:40
2

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

Thomas Leonard
  • 7,068
  • 2
  • 36
  • 40
1

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

Jeffrey Scofield
  • 65,646
  • 2
  • 72
  • 108