ludenus
ludenus

Reputation: 1341

Why do I need to set permissions when calling OpenFile?

I understand what permissions mean for files and dirs stored in filesystem. But why do I need to set permissions when calling os.OpenFile? Does it update file permission on filesystem if opened successfully? If not - what difference does it make to open same file with 0000 or 0777?

https://golang.org/src/os/file.go?s=8454:8520#L272

func OpenFile(name string, flag int, perm FileMode) (*File, error)
...
f, err := os.OpenFile("access.log", os.O_APPEND, 0644)

Upvotes: 5

Views: 4224

Answers (1)

Jonathan Hall
Jonathan Hall

Reputation: 79576

As documented (emphasis added):

OpenFile is the generalized open call; most users will use Open or Create instead. It opens the named file with specified flag (O_RDONLY etc.) and perm (before umask), if applicable. If successful, methods on the returned File can be used for I/O. If there is an error, it will be of type *PathError.

So, the perm value is only used when the file is created--when opening an existing file, it is not applicable, so it is ignored.

Upvotes: 11

Related Questions