Edson Medina
Edson Medina

Reputation: 10269

ioutils.WriteFile() not respecting permissions

I'm trying to use ioutils.WriteFile() but for some reason it's ignoring the 0777 permissions I'm giving it.

package main

import (
    "io/ioutil"
    "os"
)

func main() {

    // normal permissions
    if err := ioutil.WriteFile("cant-touch-this-0644", []byte{}, 0644); err != nil {
            panic(err)
    }


    // full permissions
    if err := ioutil.WriteFile("cant-touch-this-0777", []byte{}, 0777); err != nil {
            panic(err)
    }


    // normal permissions + chmod to full
    if err := ioutil.WriteFile("cant-touch-this-mixed", []byte{}, 0755); err != nil {
            panic(err)
    }

    if err := os.Chmod("cant-touch-this-mixed", 0777); err != nil {
            panic(err)
    }
}

The output I get from this is:

$ ls -l
-rw-r--r--  1 edson edson    0 May  9 17:19  cant-touch-this-0644
-rwxr-xr-x  1 edson edson    0 May  9 17:19  cant-touch-this-0777
-rwxrwxrwx  1 edson edson    0 May  9 17:19  cant-touch-this-mixed

Which means:

What am I doing wrong?

Upvotes: 6

Views: 10997

Answers (1)

Toshinori Sugita
Toshinori Sugita

Reputation: 1974

As the comment on this question says, this is because umask worked. unmask controls how file permissions are set for newly created files. When umask is 022, a file you want to create as 666 will be 644 (removes a write permission from group and other permissions). You can check your directory's umask with umask command.

Upvotes: 3

Related Questions