# CreateFile Permissions do not match mode parameter

**URL:** <https://ziggit.dev/t/createfile-permissions-do-not-match-mode-parameter/5096>\
**Category:** Help\
**Tags:** compiler, standard-library\
**Created:** [July 11, 2024, 6:43pm UTC](https://ziggit.dev/t/createfile-permissions-do-not-match-mode-parameter/5096 "2024-07-11T18:43:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![SpinTensor](https://ziggit.dev/user_avatar/ziggit.dev/spintensor/32/2942_2.png) [@SpinTensor](https://ziggit.dev/u/SpinTensor)\
**Post date:** [July 11, 2024, 6:43pm UTC](https://ziggit.dev/t/createfile-permissions-do-not-match-mode-parameter/5096/1 "2024-07-11T18:43:43Z")

</div>

Hi,  
I’m new to zig, and I ran into a problem on my first attempt to write a program. I just wanted to know how to create and write to a file and was exploring the documentation of the standard library, when I encountered a problem.Hopefully you can help me.

Consider the following code snippet:

```zig
const std = @import("std");

pub fn main() !void {
    const permissions = 0o666;
    var file = try std.fs.cwd().createFile("sample.txt", .{ .mode = permissions }); 
    defer file.close();
}

```

When running this I get a file “sample.txt” but the permissions are:

```zig
-rw-rw-r-- 1 fuhl fuhl 0 Jul 11 20:32 sample.txt

```

which corresponds to `0o664` not `0o666` like I requested in the source.  
No matter what I try the “world” write permissions are always omitted. If I leave out the parameter the default value should be applied. According to the [stdlib](https://ziglang.org/documentation/master/std/#std.fs.File.default_mode) the default value ist `0o666` which corresponds to “-rw-rw-rw-”, but I get “-rw-rw-r–”

Is this my fault, or is there a problem with the stdlib? Sorry if I missed something obvious.

Best,  
SpinTensor

Btw.: I run the precompiled zig 0.12.0 x86\_64 binary on Ubuntu 22.04.4 LTS

---

<div class="post-metadata">

**Author:** ![gonzo](https://ziggit.dev/user_avatar/ziggit.dev/gonzo/32/54_2.png) [@gonzo](https://ziggit.dev/u/gonzo)\
**Post date:** [July 12, 2024, 8:28am UTC](https://ziggit.dev/t/createfile-permissions-do-not-match-mode-parameter/5096/2 "2024-07-12T08:28:45Z")

</div>

[`umask`](https://askubuntu.com/questions/44542/what-is-umask-and-how-does-it-work)?

---

<div class="post-metadata">

**Author:** ![kudu](https://ziggit.dev/user_avatar/ziggit.dev/kudu/32/2802_2.png) [@kudu](https://ziggit.dev/u/kudu)\
**Post date:** [July 12, 2024, 11:14am UTC](https://ziggit.dev/t/createfile-permissions-do-not-match-mode-parameter/5096/3 "2024-07-12T11:14:49Z")

</div>

You can change permissions after creation

```zig
try file.chmod(permissions);

```

---

<div class="post-metadata">

**Author:** ![SpinTensor](https://ziggit.dev/user_avatar/ziggit.dev/spintensor/32/2942_2.png) [@SpinTensor](https://ziggit.dev/u/SpinTensor)\
**Post date:** [July 12, 2024, 12:05pm UTC](https://ziggit.dev/t/createfile-permissions-do-not-match-mode-parameter/5096/4 "2024-07-12T12:05:28Z")

</div>

Thank you for your answers.  
However, that does not solve the problem. I know how to change permissions. The problem is that the createFile function does not work as specified. There is a bug in the standard library that needs to be fixed.

---

<div class="post-metadata">

**Author:** ![squeek502](https://ziggit.dev/user_avatar/ziggit.dev/squeek502/32/409_2.png) [@squeek502](https://ziggit.dev/u/squeek502)\
**Post date:** [July 12, 2024, 12:17pm UTC](https://ziggit.dev/t/createfile-permissions-do-not-match-mode-parameter/5096/5 "2024-07-12T12:17:25Z")

</div>

I can reproduce the behavior locally, but it is not Zig-caused.

```shellsession
$ zig build-exe createfileperms.zig
$ strace ./createfileperms
openat(AT_FDCWD, "sample.txt", O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0666) = 3

```

So `0666` _is_ being passed to the `openat` syscall by Zig.

`umask` seems to be the answer.

```shellsession
$ umask
0002
$ umask 0
$ zig run createfileperms.zig
$ ls -la sample.txt
-rw-rw-rw- 1 ryan ryan 0 Jul 12 05:15 sample.txt

```

---

<div class="post-metadata">

**Author:** ![SpinTensor](https://ziggit.dev/user_avatar/ziggit.dev/spintensor/32/2942_2.png) [@SpinTensor](https://ziggit.dev/u/SpinTensor)\
**Post date:** [July 12, 2024, 12:17pm UTC](https://ziggit.dev/t/createfile-permissions-do-not-match-mode-parameter/5096/6 "2024-07-12T12:17:38Z")

</div>

I opened an [issue](https://github.com/ziglang/zig/issues/20594) on github

---

<div class="post-metadata">

**Author:** ![kudu](https://ziggit.dev/user_avatar/ziggit.dev/kudu/32/2802_2.png) [@kudu](https://ziggit.dev/u/kudu)\
**Post date:** [July 12, 2024, 12:43pm UTC](https://ziggit.dev/t/createfile-permissions-do-not-match-mode-parameter/5096/7 "2024-07-12T12:43:00Z")

</div>

Maybe it’s not a bug. In the source file `std/fs/File.zig` I see this:

```zig
/// This is the default mode given to POSIX operating systems for creating
/// files. `0o666` is "-rw-rw-rw-" which is counter-intuitive at first,
/// since most people would expect "-rw-r--r--", for example, when using
/// the `touch` command, which would correspond to `0o644`. However, POSIX
/// libc implementations use `0o666` inside `fopen` and then rely on the
/// process-scoped "umask" setting to adjust this number for file creation.
pub const default_mode = switch (builtin.os.tag) {
    .windows => 0,
    .wasi => 0,
    else => 0o666,
};

```

---

<div class="post-metadata">

**Author:** ![endlessly\_amused](https://ziggit.dev/user_avatar/ziggit.dev/endlessly_amused/32/2551_2.png) [@endlessly\_amused](https://ziggit.dev/u/endlessly_amused)\
**Post date:** [July 15, 2024, 2:39am UTC](https://ziggit.dev/t/createfile-permissions-do-not-match-mode-parameter/5096/8 "2024-07-15T02:39:03Z")

</div>

Hi

[squeek502](https://ziggit.dev/u/squeek502) is right

It’s not a bug. It’s your Operating System. Zig asks to create a file and your OS handles the request.

If you run umask in linux you get

```zig
umask
0022

```

Your 666 will become 644. ie 022 will be removed or Umasked.

This is why 777 would give you 755 if you tried it.

to set the Umask to 000 on your operating system try

```zig
umask 000

```

In full

```zig
umask 000

zig run main.zig

$ ls -l
-rw-rw-rw- 1 xxxxx xxxxx 0 Jul 15 12:34 sample.txt

$ umask
0000

```

Hope that make sense.
