简体   繁体   中英

Does Unix.mkdir set umask correctly?

I called Unix.mkdir "test" 0o000 and expected directory with rwxrwxrwx permissions but had -------w- . After call Unix.mkdir "test" (Unix.umask 0o000) I have the same result.

I can't understand why.

How to create directory with rwx permissions for all with OCaml Unix module?

The value you specify to Unix.mkdir is the permissions you want the directory to have, as modified by your current umask . If you specify 0o000 you should expect to create a directory with no permissions allowed to anybody. Since the umask can only deny some extra permissions, your reported result is impossible, at least in Unix.

Note that the second parameter to Unix.mkdir is not a umask value, it's a permissions value. The reason the OCaml documentation says to look at umask is so that you realize the specified value will be modified by your umask . It works like this: the directory will be created with the permissions you specify, except that any bit that is set in your umask will be clear in the resulting permissions. In other words, the umask specifies the accesses you wish to be denied by default.

If you really want to create a directory with all permissions allowed to everybody, you'll need to make sure your umask is 0. Here's what happens with a reasonable umask value of 0o022 :

$ umask
0022
$ ocaml
        OCaml version 4.02.1

# #load "unix.cma";;
# Unix.mkdir "testing1" 0o777;;
- : unit = ()
# ^D
$ ls -ld testing1
drwxr-xr-x  2 jeffsco  staff  68 Jul 30 13:43 testing1

The resulting directory has all permissions allowed, except the 0o022 permissions of the umask. (No write permission for group or other.)

Here's what happens if you set your umask to 0 before creating the directory:

$ ocaml
        OCaml version 4.02.1

# #load "unix.cma";;
# Unix.umask 0o000;;
- : int = 18
# Unix.mkdir "testing2" 0o777;;
- : unit = ()
# ^D
$ ls -ld testing2
drwxrwxrwx  2 jeffsco  staff  68 Jul 30 13:45 testing2

When the umask is set to 0, the permissions of the created directory will be exactly those specified in the call to Unix.mkdir .

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