Search code examples
linuxgofile-permissions

Why will os.OpenFile not create a 777 file


I am trying to make a 0777 file, but os.OpenFile will only create a 775 file for me. Can you tell me why the permissions are not getting set correctly?

package main

import (
    "fmt"
    "os"
)

func main(){

    nf, _ := os.OpenFile("hello", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0777)

    fileStat, _ := nf.Stat()
    fmt.Printf("File created with permission: %v \n", fileStat.Mode().String())
    // Output is:
    // File created with permission: -rwxr-xr-x
    // Why is this is 0775 not 0777
}

Solution

  • As mentioned in "os.MkDir and os.MkDirAll permission value?":

    these permissions are 'filtered' by whatever umask has been set.

    So check first if your umask is set (for instance 002): that would explain why the created file appears as 775 instead of 777 (umask 000)