1

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
}
DMin
  • 10,049
  • 10
  • 45
  • 65
  • 1
    Setting a `nf.Chmod(0777)` after the `os.OpenFile` will work correctly and set the permission as 777. – DMin Feb 08 '21 at 07:20

1 Answers1

4

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)

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250